Skip to content

Fix bookmarklet cross-origin requests when auth is enabled#88

Open
vdaluz wants to merge 1 commit intosam1am:mainfrom
vdaluz:fix/bookmarklet-cross-origin
Open

Fix bookmarklet cross-origin requests when auth is enabled#88
vdaluz wants to merge 1 commit intosam1am:mainfrom
vdaluz:fix/bookmarklet-cross-origin

Conversation

@vdaluz
Copy link
Copy Markdown

@vdaluz vdaluz commented Apr 16, 2026

Problem

With ENABLE_AUTH=true, the GOG and Ubisoft bookmarklets fail to POST game data to Backlogia. The bookmarklets run on external domains (gog.com, ubisoft.com) and can't reliably send the Backlogia session cookie cross-origin.

Two separate failure modes:

1. CORS middleware order
Starlette's add_middleware is last-in/outermost. CORS was added first (became innermost) and auth second (became outermost), so auth returned 401 responses before CORS could attach Access-Control-Allow-Origin headers. The browser rejected the response as a CORS failure and fetch() threw — showing "Failed to connect to Backlogia" in the overlay.

2. Authentication required
Even after fixing the order, credentials: 'include' is required for the browser to send cookies on cross-origin requests, and third-party cookie restrictions in modern browsers make this unreliable regardless.

Fix

Three changes:

  1. CORS middleware order: add auth first (innermost), CORS last (outermost) so CORS headers wrap all responses including auth rejections.

  2. credentials: 'include' on the GOG and Ubisoft fetch() calls in bookmarklet.js — best-effort, works where third-party cookies are allowed.

  3. Exempt /api/import/* from auth in the middleware. These endpoints are write-only and accept game data the user is deliberately sending from their own authenticated browser session on the external site. Exempting them is the only reliably cross-browser solution.

When ENABLE_AUTH=true, the GOG and Ubisoft bookmarklets fail to POST
game data to Backlogia because they run on external domains without
the Backlogia session cookie.

Three related fixes:

1. CORS middleware order: Starlette add_middleware is last-in/outermost.
   CORS was added first (innermost) and auth second (outermost), so auth
   returned 401 responses before CORS could add Access-Control-Allow-Origin
   headers. Browsers saw a CORS error and fetch() threw 'Failed to
   connect to Backlogia'. Fixed by adding auth first, CORS last.

2. bookmarklet.js: Add credentials: 'include' to the GOG and Ubisoft
   import fetch() calls so the session cookie is sent cross-origin.

3. Exempt /api/import/* from auth. Cross-origin cookie sending is
   unreliable (third-party cookie restrictions vary by browser). These
   write-only endpoints accept game data the user is deliberately sending
   from their own browser — acceptable to leave unauthenticated.
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.

1 participant