Skip to content

Commit

Permalink
fix: heroku subdomain check (#842)
Browse files Browse the repository at this point in the history
* fix: heroku subdomain check

* satisfy codeql like this?

* more tests
  • Loading branch information
pauldambra authored Oct 23, 2023
1 parent 8ca6d94 commit 719b3a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
17 changes: 17 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,20 @@ 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'],
[false, 'test.herokuapp.com'],
[false, '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'],
[true, 'bbc.co.uk'],
[true, 'www.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),
persistence: 'cookie',
persistence_name: '',
cookie_name: '',
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,16 @@ export const _info = {
},
}

export function isCrossDomainCookie(documentLocation: Location | undefined) {
const hostname = documentLocation?.hostname

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
}

export { win as window, userAgent, document }

0 comments on commit 719b3a8

Please sign in to comment.