Skip to content

Commit

Permalink
fix: Add X-NewRelic-ID header only if defined (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfield authored May 17, 2023
1 parent 806bcc4 commit 36ceedf
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/features/ajax/instrument/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function subscribeToEvents (agentIdentifier, ee, handler, dt) {

function onOpenXhrEnd (args, xhr) {
var loader_config = getLoaderConfig(agentIdentifier)
if ('xpid' in loader_config && this.sameOrigin) {
if (loader_config.xpid && this.sameOrigin) {
xhr.setRequestHeader('X-NewRelic-ID', loader_config.xpid)
}

Expand Down
13 changes: 7 additions & 6 deletions tests/assets/cat-cors.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
<html>
<head>
<meta charset="utf-8">
<title>Example Page</title>
<title>CAT header on CORS XHR</title>
{init}
{config}
{loader}
<script type="text/javascript">
if (!NREUM.loader_config) NREUM.loader_config = {}
NREUM.loader_config.xpid = '12#34'

</script>
{config}
{loader}
<script type="text/javascript">
var xhr = new XMLHttpRequest()
var testId = window.location.search.match(/testId=([^&]*)/)[1]
var testIdMatches = window.location.search.match(/testId=([^&]*)/)
var testId = testIdMatches ? testIdMatches[1] : ''
var url = 'http://' + NREUM.info.beacon + '/cat-cors/' + testId

xhr.open('GET', url)
xhr.send()
</script>
Expand Down
32 changes: 32 additions & 0 deletions tests/assets/cat-same-origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<!--
Copyright 2020 New Relic Corporation.
PDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<meta charset="utf-8">
<title>CAT header on same-origin XHR</title>
{init}
<script type="text/javascript">
if (!NREUM.loader_config) NREUM.loader_config = {}
try {
NREUM.loader_config.xpid = window.location.search.match(/xpid=([^&]*)/)[1]
} catch (e) {
NREUM.loader_config.xpid = '12#36'
}
</script>
{config}
{loader}
<script type="text/javascript">
var xhr = new XMLHttpRequest()
var testIdMatches = window.location.search.match(/testId=([^&]*)/)
var testId = testIdMatches ? testIdMatches[1] : ''
var url = '/same-origin/' + testId
xhr.open('GET', url)
xhr.send()
</script>
</head>
<body>
</body>
</html>
38 changes: 37 additions & 1 deletion tests/functional/xhr/cat-cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const testDriver = require('../../../tools/jil/index')
const { fail } = require('./helpers')

testDriver.test('does not set CAT headers on outbound XHRs to different origin', function (t, browser, router) {
testDriver.test('does not set CAT header on cross-origin XHRs', function (t, browser, router) {
t.plan(1)

const ajaxPromise = router.expect('bamServer', {
Expand All @@ -23,3 +23,39 @@ testDriver.test('does not set CAT headers on outbound XHRs to different origin',
})
.catch(fail(t, 'unexpected error'))
})

testDriver.test('sets CAT header on same origin XHRs if xpid is defined', function (t, browser, router) {
t.plan(1)

const ajaxPromise = router.expect('assetServer', {
test: function (request) {
const url = new URL(request.url, 'resolve://')
return url.pathname === `/same-origin/${router.testId}`
}
})
let loadPromise = browser.get(router.assetURL('cat-same-origin.html', { testId: router.testId }))

Promise.all([ajaxPromise, loadPromise])
.then(([{ request }]) => {
t.ok(request.headers['x-newrelic-id'], 'same-origin XHR should have CAT header if xpid is specified')
})
.catch(fail(t, 'unexpected error'))
})

testDriver.test('does not set CAT header on same origin XHRs if xpid is undefined', function (t, browser, router) {
t.plan(1)

const ajaxPromise = router.expect('assetServer', {
test: function (request) {
const url = new URL(request.url, 'resolve://')
return url.pathname === `/same-origin/${router.testId}`
}
})
let loadPromise = browser.get(router.assetURL('cat-same-origin.html', { testId: router.testId, xpid: '' }))

Promise.all([ajaxPromise, loadPromise])
.then(([{ request }]) => {
t.notok(request.headers['x-newrelic-id'], 'same-origin XHR should not have CAT header if xpid is undefined')
})
.catch(fail(t, 'unexpected error'))
})
20 changes: 10 additions & 10 deletions tools/browsers-lists/browsers-supported.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"chrome": [
{
"browserName": "chrome",
"platformName": "Windows 10",
"platform": "Windows 10",
"version": "103",
"browserVersion": "103"
"platformName": "Windows 11",
"platform": "Windows 11",
"version": "104",
"browserVersion": "104"
},
{
"browserName": "chrome",
"platformName": "Windows 10",
"platform": "Windows 10",
"version": "106",
"browserVersion": "106"
"version": "107",
"browserVersion": "107"
},
{
"browserName": "chrome",
Expand All @@ -34,15 +34,15 @@
"browserName": "MicrosoftEdge",
"platformName": "Windows 10",
"platform": "Windows 10",
"version": "103",
"browserVersion": "103"
"version": "104",
"browserVersion": "104"
},
{
"browserName": "MicrosoftEdge",
"platformName": "Windows 11",
"platform": "Windows 11",
"version": "106",
"browserVersion": "106"
"version": "107",
"browserVersion": "107"
},
{
"browserName": "MicrosoftEdge",
Expand Down

0 comments on commit 36ceedf

Please sign in to comment.