Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More complex check for authTokenSyncUrl #8076

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
21 changes: 14 additions & 7 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)
if (authTokenSyncPath) {
// Reduce the chances of an XSS attack by only allowing secure contexts or the same origin.
const isLocalHost = ['localhost', '127.0.0.1', '0.0.0.0'].includes(
jamesdaniels marked this conversation as resolved.
Show resolved Hide resolved
location.hostname
);
onIdTokenChanged(auth, user => mintCookie(user));
if (isSecureContext || isLocalHost) {
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