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

fix: heroku subdomain check #842

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DEFAULT_BLOCKED_UA_STRS,
loadScript,
_isUrlMatchingRegex,
isCrossDomainCookie,
} from '../utils'

function userAgentFor(botString) {
Expand Down Expand Up @@ -272,4 +273,16 @@ describe('loadScript', () => {
expect(_isUrlMatchingRegex('https://example.com/something/test', 'example.com/(.*.)/test')).toEqual(true)
})
})

describe('check for cross domain cookies', () => {
it.each([
[false, 'https://test.herokuapp.com'],
// ensure it isn't matching herokuapp anywhere in the domain
[true, 'https://test.herokuapp.com.impersonator.io'],
[false, undefined],
[true, 'https://bbc.co.uk'],
])('should return %s when hostname is %s', (expectedResult, hostname) => {
expect(isCrossDomainCookie(hostname)).toEqual(expectedResult)
})
})
})
3 changes: 2 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
userAgent,
window,
logger,
isCrossDomainCookie,
} from './utils'
import { autocapture } from './autocapture'
import { PostHogFeatureFlags } from './posthog-featureflags'
Expand Down Expand Up @@ -109,7 +110,7 @@ const defaultConfig = (): PostHogConfig => ({
token: '',
autocapture: true,
rageclick: true,
cross_subdomain_cookie: document?.location?.hostname?.indexOf('herokuapp.com') === -1,
cross_subdomain_cookie: isCrossDomainCookie(document?.location?.hostname),
persistence: 'cookie',
persistence_name: '',
cookie_name: '',
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,14 @@
},
}

export function isCrossDomainCookie(hostname: string | undefined) {
if (!_isString(hostname)) {
return false
}
// split and slice isn't a great way to match arbitrary domains,
// but it's good enough for ensuring we only match herokuapp.com when it is the TLD
// for the hostname
return hostname.split('.').slice(-2).join('.').indexOf('herokuapp.com') === -1

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

'
herokuapp.com
' can be anywhere in the URL, and arbitrary hosts may come before or after it.
}

export { win as window, userAgent, document }
Loading