Skip to content

[ENG-12178] Fix Mendeley OAuth: handle callback errors, retry Cloudflare-blocked requests - #344

Merged
brianjgeiger merged 3 commits into
CenterForOpenScience:developfrom
Vlad0n20:fix/ENG-12178
Oct 26, 2026
Merged

brianjgeiger merged 3 commits into
CenterForOpenScience:developfrom
Vlad0n20:fix/ENG-12178

Conversation

@Vlad0n20

@Vlad0n20 Vlad0n20 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Ticket

Purpose

Changes

Side Effects

API Documentation

QE Notes

CE Notes

@adlius adlius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some security related issues. Otherwise looks good.

Comment thread addon_service/oauth2/views.py Outdated
# https://www.rfc-editor.org/rfc/rfc6749.html#section-4.1.2.1
if "error" in request.GET:
return HttpResponse(
f"oauth2 authorization failed: {request.GET['error']}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd either want to escape this or make the HttpResponse not use HTML to avoid XSS attacks. For example, the user can click a malicious link with <gvdomain>/oauth2/callback/?error=<script>(some malicious script here...)</script> and the browser would execute it on GV's domain.

@adlius adlius left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more questions. To summarize:

  1. Is the urllib fallback really necessary? Can we apply the user agent string to aiohttp and see if that passes cloudflare check?
  2. Should we drop Authorization headers on redirects?
  3. Should we limit the scope to only Mendeley if it is the only provider that we currently have issues with?

Comment thread addon_service/common/urllib_fallback.py Outdated
"""fallback http sending for providers whose CDN challenges aiohttp requests

Some providers (e.g. Mendeley, behind Cloudflare) answer aiohttp requests with a
"403 text/html" bot challenge, though the same request sent with stdlib `urllib` gets through.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I have another question for the choice of using urllib for making the request: I can't think of any reason why Cloudflare would choose to reject a request from aiohttp but accept a request from urllib.request. It seems that this files add a user agent string GravyValet/1.0 instead of using whatever that is default for aiohttp. Have you tried using the GravyValet/1.0 for aiohttp?

Comment thread addon_service/common/urllib_fallback.py Outdated
url = f"{url}{_sep}{urllib.parse.urlencode(list(params))}"
_request = urllib.request.Request(url, data=_body, method=method, headers=_headers)
try:
with urllib.request.urlopen(_request, timeout=_TIMEOUT_SECONDS) as _response:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another security issue here where it seems that, if the request is redirected, this would send the Authorization header to the redirected url again, which maybe shouldn't happen. Aiohttp drops Authorization header on redirects.

Comment thread addon_service/common/network.py Outdated
_response.status == HTTPStatus.UNAUTHORIZED
and _private.account.credentials_format == CredentialsFormats.OAUTH2
self._raise_if_expired_token(_response.status)
if not urllib_fallback.is_bot_challenge(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the only provider we have issues is Mendeley. This change would apply to all providers if it runs into a bot challenge. Can we limit the scope to only Mendeley?

@adlius adlius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@brianjgeiger
brianjgeiger merged commit 0fbe33f into CenterForOpenScience:develop Oct 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants