1
0
mirror of synced 2025-12-19 09:57:42 -05:00

fix: handle invalid Referer header in article lookup middleware (#58793)

This commit is contained in:
Kevin Heis
2025-12-10 09:46:47 -08:00
committed by GitHub
parent ac1d99c016
commit abf0033fe8
2 changed files with 22 additions and 5 deletions

View File

@@ -172,11 +172,19 @@ function incrementArticleLookup(
// logs the source of the request, if it's for hovercards it'll have the header X-Request-Source.
// see src/links/components/LinkPreviewPopover.tsx
const source =
req.get('X-Request-Source') ||
(req.get('Referer')
? `external-${new URL(req.get('Referer') || '').hostname || 'unknown'}`
: 'external')
let source = req.get('X-Request-Source')
if (!source) {
const referer = req.get('Referer')
if (referer) {
try {
source = `external-${new URL(referer).hostname || 'unknown'}`
} catch {
source = 'external'
}
} else {
source = 'external'
}
}
const tags = [
// According to https://docs.datadoghq.com/getting_started/tagging/#define-tags

View File

@@ -66,4 +66,13 @@ describe('article body api', () => {
const { error } = JSON.parse(res.body)
expect(error).toContain("isn't yet available in markdown")
})
test('invalid Referer header does not crash', async () => {
const res = await get(makeURL('/en/get-started/start-your-journey/hello-world'), {
headers: {
Referer: 'invalid-url',
},
})
expect(res.statusCode).toBe(200)
})
})