Skip to content

Commit

Permalink
✨ [RUM-4052] Sanitize site parameter in configuration (#2735)
Browse files Browse the repository at this point in the history
* Check if is datadog site and stop init if not

* Fix error message

* Fix import

* Add documentation link in error message

* Polish wording
  • Loading branch information
cy-moi authored May 6, 2024
1 parent 4cf2687 commit a8923eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/core/src/domain/configuration/configuration.spec.ts
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)
}

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

0 comments on commit a8923eb

Please sign in to comment.