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

✨ [RUM-4052] Sanitize site parameter in configuration #2735

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../tools/experimentalFeatures'
import { TrackingConsent } from '../trackingConsent'
import type { InitConfiguration } from './configuration'
import { serializeConfiguration, validateAndBuildConfiguration } from './configuration'
import { DOC_LINK, serializeConfiguration, validateAndBuildConfiguration } from './configuration'

describe('validateAndBuildConfiguration', () => {
const clientToken = 'some_client_token'
Expand Down Expand Up @@ -210,6 +210,13 @@ describe('validateAndBuildConfiguration', () => {
})
})

describe('site parameter validation', () => {
it('should validate the site parameter', () => {
validateAndBuildConfiguration({ clientToken, site: 'foo.com' })
expect(displaySpy).toHaveBeenCalledOnceWith(`Site should be a valid Datadog site. Learn more here: ${DOC_LINK}.`)
})
})

describe('serializeConfiguration', () => {
it('should serialize the configuration', () => {
// By specifying the type here, we can ensure that serializeConfiguration is returning an
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TrackingConsent } from '../trackingConsent'
import type { TransportConfiguration } from './transportConfiguration'
import { computeTransportConfiguration } from './transportConfiguration'

export const DOC_LINK = 'https://docs.datadoghq.com/getting_started/site/'
export const DefaultPrivacyLevel = {
ALLOW: 'allow',
MASK: 'mask',
Expand Down Expand Up @@ -107,6 +108,9 @@ export interface Configuration extends TransportConfiguration {
batchMessagesLimit: number
messageBytesLimit: number
}
function isDatadogSite(site: string) {
return /(datadog|ddog|datad0g|dd0g)/.test(site)
nulrich marked this conversation as resolved.
Show resolved Hide resolved
}

export function validateAndBuildConfiguration(initConfiguration: InitConfiguration): Configuration | undefined {
if (!initConfiguration || !initConfiguration.clientToken) {
Expand Down Expand Up @@ -148,6 +152,11 @@ export function validateAndBuildConfiguration(initConfiguration: InitConfigurati
return
}

if (initConfiguration.site && !isDatadogSite(initConfiguration.site)) {
display.error(`Site should be a valid Datadog site. Learn more here: ${DOC_LINK}.`)
return
}

// Set the experimental feature flags as early as possible, so we can use them in most places
if (Array.isArray(initConfiguration.enableExperimentalFeatures)) {
addExperimentalFeatures(
Expand Down