Skip to content

Commit 81fc65e

Browse files
[bugfix] fix auth service worker to handle cross-origin redirects to GCS (#6265)
## Summary Fixes CORS errors in HTTPS environments where the auth service worker blocked cross-origin redirects to Google Cloud Storage. ## Problem The service worker was using `mode: 'same-origin'` which prevented following redirects when `/api/view` returns a 302 redirect to GCS: ``` Unsafe attempt to load URL https://storage.googleapis.com/... from frame with URL https://testcloud.comfy.org/auth-sw.js. Domains, protocols and ports must match. ``` This only occurred in HTTPS/cloud environments where media is served from GCS. Localhost/HTTP test environments serve files directly without redirects, so the issue wasn't caught there. ## Solution Changed redirect handling from automatic to manual: 1. **Initial request to `/api/view`**: Sends WITH auth headers (validates user access) 2. **Detect redirect response**: Checks for 301/302/opaqueredirect 3. **Follow redirect to GCS**: Fetches WITHOUT auth headers (signed URL has built-in auth) ### Key Changes - Removed `mode: 'same-origin'` (was blocking cross-origin redirects) - Changed `redirect: event.request.redirect` to `redirect: 'manual'` - Added manual redirect handling that follows to GCS without Firebase auth headers ## Why This Works The two requests have different authentication mechanisms: - **`/api/view` request**: Uses Firebase auth header (backend validates user access) - **GCS request**: Uses signed URL with query params (`Signature=...`, `GoogleAccessId=...`, `Expires=...`) The security check still happens on the initial `/api/view` request, but we allow the redirect to GCS to use its own authentication system. ## Testing - Typecheck passed - Should be tested in HTTPS cloud environment with media files stored in GCS ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6265-bugfix-fix-auth-service-worker-to-handle-cross-origin-redirects-to-GCS-2976d73d365081d0b124db4918f8194e) by [Unito](https://www.unito.io)
1 parent 55c9cf7 commit 81fc65e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

public/auth-sw.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,36 @@ self.addEventListener('fetch', (event) => {
5454
headers.set(key, value)
5555
}
5656

57-
return fetch(
57+
// Fetch with manual redirect to handle cross-origin redirects (e.g., GCS signed URLs)
58+
const response = await fetch(
5859
new Request(event.request.url, {
5960
method: event.request.method,
6061
headers: headers,
61-
mode: 'same-origin',
6262
credentials: event.request.credentials,
6363
cache: 'no-store',
64-
redirect: event.request.redirect,
64+
redirect: 'manual',
6565
referrer: event.request.referrer,
6666
integrity: event.request.integrity
6767
})
6868
)
69+
70+
// If redirected to external storage (GCS), follow without auth headers
71+
// The signed URL contains its own authentication in query params
72+
if (
73+
response.type === 'opaqueredirect' ||
74+
response.status === 302 ||
75+
response.status === 301
76+
) {
77+
const location = response.headers.get('location')
78+
if (location) {
79+
return fetch(location, {
80+
method: 'GET',
81+
redirect: 'follow'
82+
})
83+
}
84+
}
85+
86+
return response
6987
} catch (error) {
7088
console.error('[Auth SW] Request failed:', error)
7189
return fetch(event.request)

0 commit comments

Comments
 (0)