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

✨ [RUMF-1237] The event bridge allowed hosts should also match subdomains #1499

Merged
merged 4 commits into from
Apr 15, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/core/src/transport/eventBridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ describe('canUseEventBridge', () => {
})

it('should detect when the bridge is present and the webView host is allowed', () => {
initEventBridgeStub()
expect(canUseEventBridge()).toBeTrue()
})

it('should not detect when the bridge is absent', () => {
expect(canUseEventBridge()).toBeFalse()
initEventBridgeStub(allowedWebViewHosts)
expect(canUseEventBridge('foo.bar')).toBeTrue()
expect(canUseEventBridge('baz.foo.bar')).toBeTrue()
expect(canUseEventBridge('www.foo.bar')).toBeTrue()
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
expect(canUseEventBridge('www.qux.foo.bar')).toBeTrue()
})

it('should not detect when the bridge is present and the webView host is not allowed', () => {
initEventBridgeStub(allowedWebViewHosts)
expect(canUseEventBridge('foo.com')).toBeFalse()
expect(canUseEventBridge('foo.bar.baz')).toBeFalse()
expect(canUseEventBridge('bazfoo.bar')).toBeFalse()
})

it('should not detect when the bridge on the parent domain if only the subdomain is allowed', () => {
initEventBridgeStub(['baz.foo.bar'])
expect(canUseEventBridge('foo.bar')).toBeFalse()
})

it('should not detect when the bridge is absent', () => {
expect(canUseEventBridge()).toBeFalse()
})
})
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/transport/eventBridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getGlobalObject, includes } from '..'
import { getGlobalObject } from '..'

export interface BrowserWindowWithEventBridge extends Window {
DatadogEventBridge?: DatadogEventBridge
Expand Down Expand Up @@ -26,10 +26,16 @@ export function getEventBridge<T, E>() {
}
}

export function canUseEventBridge(): boolean {
export function canUseEventBridge(hostname = getGlobalObject<Window>().location?.hostname): boolean {
const bridge = getEventBridge()

return !!bridge && includes(bridge.getAllowedWebViewHosts(), window.location.hostname)
return (
!!bridge &&
bridge.getAllowedWebViewHosts().some((host) => {
const escapedHost = host.replace(/\./g, '\\.')
const isDomainOrSubDomain = new RegExp(`^(.+\\.)*${escapedHost}$`)
return !!isDomainOrSubDomain.exec(hostname)
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
})
)
}

function getEventBridgeGlobal() {
Expand Down