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-1544] Fix badly polyfilled URL #2141

Merged
merged 7 commits into from
Apr 6, 2023
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ module.exports = {
rules: {
'local-rules/disallow-side-effects': 'error',
'local-rules/disallow-zone-js-patched-values': 'error',
'local-rules/disallow-url-constructor-patched-values': 'error',
'no-restricted-syntax': [
'error',
{
Expand Down
29 changes: 29 additions & 0 deletions eslint-local-rules/disallowUrlConstructorPatchValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
meta: {
docs: {
description: 'Disallow problematic URL constructor patched values.',
recommended: false,
},
schema: [],
},

create(context) {
return {
'Program:exit'(node) {
const globalScope = context.getScope(node)
const variable = globalScope.set.get('URL')

if (variable && variable.defs.length === 0) {
variable.references.forEach((ref) => {
const idNode = ref.identifier
const parent = idNode.parent

if (parent && parent.type === 'NewExpression' && parent.callee === idNode) {
context.report(idNode, 'This value might be patched. Use `buildUrl` from @datadog/browser-core instead')
}
})
}
},
}
},
}
1 change: 1 addition & 0 deletions eslint-local-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'disallow-protected-directory-import': require('./disallowProtectedDirectoryImport'),
'disallow-test-import-export-from-src': require('./disallowTestImportExportFromSrc'),
'disallow-zone-js-patched-values': require('./disallowZoneJsPatchedValues'),
'disallow-url-constructor-patched-values': require('./disallowUrlConstructorPatchValues.js'),
'disallow-generic-utils': require('./disallowGenericUtils'),
'secure-command-execution': require('./secureCommandExecution'),
}
6 changes: 6 additions & 0 deletions packages/core/src/tools/utils/urlPolyfill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('isValidUrl', () => {
expect(isValidUrl('/plop')).toBe(false)
expect(isValidUrl('')).toBe(false)
})

it('should return the same result if the URL has been wrongfully overridden between calls', () => {
expect(isValidUrl('http://www.datadoghq.com')).toBe(true)
spyOn(window, 'URL').and.throwError('wrong URL override')
expect(isValidUrl('http://www.datadoghq.com')).toBe(true)
})
})

describe('getOrigin', () => {
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/tools/utils/urlPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ export function getHash(url: string) {
}

export function buildUrl(url: string, base?: string) {
if (checkURLSupported()) {
const supportedURL = getSupportedUrl()
if (supportedURL) {
try {
return base !== undefined ? new URL(url, base) : new URL(url)
return base !== undefined ? new supportedURL(url, base) : new supportedURL(url)
} catch (error) {
throw new Error(`Failed to construct URL. ${jsonStringify({ url, base })!}`)
throw new Error(`Failed to construct URL: ${String(error)} ${jsonStringify({ url, base })!}`)
}
}
if (base === undefined && !/:/.test(url)) {
Expand All @@ -57,19 +58,18 @@ export function buildUrl(url: string, base?: string) {
return anchorElement
}

const originalURL = URL
let isURLSupported: boolean | undefined
function checkURLSupported() {
if (isURLSupported !== undefined) {
return isURLSupported
}
try {
const url = new URL('http://test/path')
isURLSupported = url.href === 'http://test/path'
return isURLSupported
} catch {
isURLSupported = false
function getSupportedUrl(): typeof URL | undefined {
if (isURLSupported === undefined) {
try {
const url = new originalURL('http://test/path')
isURLSupported = url.href === 'http://test/path'
} catch {
isURLSupported = false
}
}
return isURLSupported
return isURLSupported ? originalURL : undefined
}

export function getLocationOrigin() {
Expand Down
5 changes: 4 additions & 1 deletion packages/rum/src/boot/recorderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ function isBrowserSupported() {
// Array.from is a bit less supported by browsers than CSSSupportsRule, but has higher chances
// to be polyfilled. Test for both to be more confident. We could add more things if we find out
// this test is not sufficient.
typeof Array.from === 'function' && typeof CSSSupportsRule === 'function' && 'forEach' in NodeList.prototype
typeof Array.from === 'function' &&
typeof CSSSupportsRule === 'function' &&
typeof URL.createObjectURL === 'function' &&
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
'forEach' in NodeList.prototype
)
}