Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(toolbar): match authorized URLs by exact origin (#61020)

Tom Piccirellob10ea6b985c5
diff --git a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
index 15e30ba778ff..ec369cfe048d 100644
--- a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
+++ b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
@@ -14,6 +14,7 @@ import {
SuggestedDomain,
appEditorUrl,
authorizedUrlListLogic,
+ checkUrlIsAuthorized,
directToolbarUrl,
filterNotAuthorizedUrls,
validateProposedUrl,
@@ -118,6 +119,38 @@ describe('the authorized urls list logic', () => {
})
})
+ describe('checkUrlIsAuthorized', () => {
+ const testCases: { url: string; authorized: string[]; expected: boolean }[] = [
+ // Legitimate matches
+ { url: 'https://example.com', authorized: ['https://example.com'], expected: true },
+ { url: 'https://example.com/some/path?q=1', authorized: ['https://example.com'], expected: true },
+ { url: 'https://example.com', authorized: ['https://example.com/already/has/a/path'], expected: true },
+ // www-equivalence both directions
+ { url: 'https://example.com', authorized: ['https://www.example.com'], expected: true },
+ { url: 'https://www.example.com', authorized: ['https://example.com'], expected: true },
+ // wildcard subdomains and ports
+ { url: 'https://app.example.com', authorized: ['https://*.example.com'], expected: true },
+ { url: 'https://a.b.example.com', authorized: ['https://*.example.com'], expected: true },
+ { url: 'http://localhost:3000', authorized: ['http://localhost:*'], expected: true },
+ // Suffix bypass: an attacker domain that merely ends with the authorized origin string
+ { url: 'https://example.com.evil.com', authorized: ['https://example.com'], expected: false },
+ { url: 'https://app.example.com.evil.com', authorized: ['https://*.example.com'], expected: false },
+ // Prefix/substring bypass: a different registrable domain that is a substring of the entry
+ { url: 'https://example.co', authorized: ['https://example.com'], expected: false },
+ // Plain unrelated origin
+ { url: 'https://evil.com', authorized: ['https://example.com'], expected: false },
+ { url: 'https://example.org', authorized: ['https://example.com'], expected: false },
+ // Nothing authorized
+ { url: 'https://example.com', authorized: [], expected: false },
+ ]
+
+ testCases.forEach(({ url, authorized, expected }) => {
+ it(`"${url}" against [${authorized.join(', ')}] is ${expected ? 'authorized' : 'not authorized'}`, () => {
+ expect(checkUrlIsAuthorized(url, authorized)).toBe(expected)
+ })
+ })
+ })
+
describe('validating proposed URLs', () => {
const testCases = [
{ proposedUrl: 'https://valid.*.example.com', validityMessage: undefined },
diff --git a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
index 8f04c1effed2..2bbc4ceb6850 100644
--- a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
+++ b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
@@ -198,32 +198,33 @@ export const checkUrlIsAuthorized = (url: string | URL, authorizedUrls: string[]
const urlWithoutPath = parsedUrl.protocol + '//' + parsedUrl.host
const hostNormalized = stripWww(parsedUrl.hostname)
- const exactMatch = authorizedUrls.some((authorizedUrl) => {
- if (authorizedUrl.indexOf(urlWithoutPath) > -1) {
- return true
+ return authorizedUrls.some((authorizedUrl) => {
+ // Wildcard entries (subdomain or port wildcards, e.g. `https://*.example.com`). Anchor the
+ // pattern with ^…$ so a `*` cannot match a suffix of an unrelated origin such as
+ // `https://app.example.com.evil.com`.
+ if (authorizedUrl.includes('*')) {
+ try {
+ const regex = new RegExp('^' + authorizedUrl.replace(/\./g, '\\.').replace(/\*/g, '.*') + '$')
+ return regex.test(urlWithoutPath)
+ } catch {
+ return false
+ }
}
- // www-equivalence: compare hostnames with www. stripped
+
+ // Exact entries: compare by origin (protocol + host) instead of a substring check, so a
+ // different domain cannot be authorized merely by being a prefix/substring of an entry
+ // (e.g. `https://example.co` against `https://example.com`).
try {
- const authorizedHost = sanitizePossibleWildCardedURL(authorizedUrl).hostname
- return stripWww(authorizedHost) === hostNormalized
+ const authorizedUrlParsed = sanitizePossibleWildCardedURL(authorizedUrl)
+ if (authorizedUrlParsed.protocol + '//' + authorizedUrlParsed.host === urlWithoutPath) {
+ return true
+ }
+ // www-equivalence: compare hostnames with www. stripped
+ return stripWww(authorizedUrlParsed.hostname) === hostNormalized
} catch {
return false
}
})
-
- if (exactMatch) {
- return true
- }
-
- const wildcardMatch = !!authorizedUrls.find((authorizedUrl) => {
- // Matches something like `https://*.example.com` against the urlWithoutPath
- const regex = new RegExp(authorizedUrl.replace(/\./g, '\\.').replace(/\*/g, '.*'))
- return urlWithoutPath.match(regex)
- })
-
- if (wildcardMatch) {
- return true
- }
} catch {
// Ignore invalid URLs
}