Skip to content

Don't send service API key to other hosts on redirect - #382

Draft
iLLiCiTiT wants to merge 1 commit into
developfrom
bugfix/strip-api-key-on-redirect
Draft

iLLiCiTiT wants to merge 1 commit into
developfrom
bugfix/strip-api-key-on-redirect

Conversation

@iLLiCiTiT

Copy link
Copy Markdown
Member

Changelog Description

Service API key (X-Api-Key) and X-as-user headers are no longer sent to a different host when the server redirects a request, e.g. project file downloads redirected to S3 or a CDN.

Additional info

requests removes only the Authorization header when a redirect leads to a different host (Session.rebuild_auth). Users logged in with a session token were safe, but services using an API key sent it to every host the server redirected to.

Changes:

  • ServerSession in utils.py is a requests.Session that also removes X-Api-Key and X-as-user in rebuild_auth, using the same should_strip_auth check as requests does for Authorization (other host, but not an http → https upgrade on the same host).
  • ServerAPI creates its sessions (authorized and validation) from ServerSession.
  • automated_api.py got Session in its fake requests module.
  • test_token_validation.py replaces ServerSession instead of requests.Session with its fake server.

This is independent of #380, but becomes more relevant with it: users with AYON_CA_FILE couldn't follow redirects to public hosts until now, because TLS verification failed. The two PRs touch the same session creation lines in ServerAPI, so whichever gets merged second needs a small conflict resolution.

Testing notes:

  1. pytest tests/test_redirect_auth_headers.py: a local server redirects to itself through another host name (localhost → 127.0.0.1). The API key and X-as-user aren't sent to the other host for REST requests and download_file, and are still sent on a same-host redirect. On develop the two other-host tests fail.
  2. The same check passes on requests 2.27.1 + urllib3 1.26 and requests 2.34.2 + urllib3 2.7.
  3. Offline tests pass (token validation, download resume, EventHub lifecycle and ping, request retries, max retries kwarg, server version validation, GraphQL pagination).
  4. python automated_api.py makes no changes.

'requests' strips only 'Authorization' header on redirect to a different
host. 'ServerSession' also strips 'X-Api-Key' and 'X-as-user', so
service api key is not sent e.g. to cloud storage the server redirects
file downloads to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iLLiCiTiT iLLiCiTiT added the type: bug Something isn't working label Nov 10, 2026
@iLLiCiTiT
iLLiCiTiT marked this pull request as draft November 10, 2026 13:17
@iLLiCiTiT

Copy link
Copy Markdown
Member Author

Downside of this change is that some redirects ayon.studio → ayon-01.studio might loose the authentication headers. Keeping this opened as draft.

@BigRoy

BigRoy commented Nov 10, 2026

Copy link
Copy Markdown
Member

Downside of this change is that some redirects ayon.studio → ayon-01.studio might loose the authentication headers. Keeping this opened as draft.

For what it's worth, here's some note that Claude gave me that I think made sense to share 1-to-1:

Conflict resolution needed: #380's send_request should also use ServerSession. Otherwise get_user_info_by_token without a session and EventHub's token check send X-Api-Key through a plain session.
On the draft concern (auth lost on a redirect like ayon.studio → ayon-01.studio): requests already strips Authorization on those redirects. Every logged-in user would already fail with that setup, so service keys now just behave the same as user tokens. The backend's own redirect for file payloads is a relative URL on the same host, so it keeps the key.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants