Skip to content

Commit

Permalink
More complex check for authTokenSyncUrl (#8076)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Mar 21, 2024
1 parent 0c51501 commit 9ca1a4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-mugs-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/auth': patch
---

Additional protection against misuse of the authTokenSyncURL experiment
23 changes: 15 additions & 8 deletions packages/auth/src/platform_browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,21 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
});

const authTokenSyncPath = getExperimentalSetting('authTokenSyncURL');
// Don't allow urls (XSS possibility), only paths on the same domain
// (starting with a single '/')
if (authTokenSyncPath && authTokenSyncPath.match(/^\/[^\/].*/)) {
const mintCookie = mintCookieFactory(authTokenSyncPath);
beforeAuthStateChanged(auth, mintCookie, () =>
mintCookie(auth.currentUser)
);
onIdTokenChanged(auth, user => mintCookie(user));
// Only do the Cookie exchange in a secure context
if (
authTokenSyncPath &&
typeof isSecureContext === 'boolean' &&
isSecureContext
) {
// Don't allow urls (XSS possibility), only paths on the same domain
const authTokenSyncUrl = new URL(authTokenSyncPath, location.origin);
if (location.origin === authTokenSyncUrl.origin) {
const mintCookie = mintCookieFactory(authTokenSyncUrl.toString());
beforeAuthStateChanged(auth, mintCookie, () =>
mintCookie(auth.currentUser)
);
onIdTokenChanged(auth, user => mintCookie(user));
}
}

const authEmulatorHost = getDefaultEmulatorHost('auth');
Expand Down

0 comments on commit 9ca1a4e

Please sign in to comment.