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

Allow to pass nonce option to getBrowserTimingHeader. #266

Merged
merged 2 commits into from
Jul 9, 2018
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
10 changes: 8 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MODULE_TYPE = require('./lib/shim/constants').MODULE_TYPE
* CONSTANTS
*
*/
const RUM_STUB = "<script type='text/javascript'>window.NREUM||(NREUM={});" +
const RUM_STUB = "<script type='text/javascript' %s>window.NREUM||(NREUM={});" +
"NREUM.info = %s; %s</script>"

// these messages are used in the _gracefail() method below in getBrowserTimingHeader
Expand Down Expand Up @@ -499,16 +499,19 @@ API.prototype.addIgnoringRule = function addIgnoringRule(pattern) {
*
* Do *not* reuse the headers between users, or even between requests.
*
* @param {{nonce: string}} options Options used to generate `<script>` header.
* @returns {string} The `<script>` header to be injected.
*/
API.prototype.getBrowserTimingHeader = function getBrowserTimingHeader() {
API.prototype.getBrowserTimingHeader = function getBrowserTimingHeader(options) {
var metric = this.agent.metrics.getOrCreateMetric(
NAMES.SUPPORTABILITY.API + '/getBrowserTimingHeader'
)
metric.incrementCallCount()

var config = this.agent.config

options = options || {}

/**
* Gracefully fail.
*
Expand Down Expand Up @@ -616,10 +619,13 @@ API.prototype.getBrowserTimingHeader = function getBrowserTimingHeader() {
var tabs = config.browser_monitoring.debug ? 2 : 0
var json = JSON.stringify(rum_hash, null, tabs)

// set nonce attribute if passed in options
var nonce = options.nonce ? 'nonce="' + options.nonce + '"' : ''

// the complete header to be written to the browser
var out = util.format(
RUM_STUB,
nonce,
json,
js_agent_loader
)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/rum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,13 @@ describe("the RUM API", function() {
})
})

it('should add nonce attribute to script if passed in options', function () {
var nonce = "12345";
helper.runInTransaction(agent, function (t) {
t.finalizeNameFromUri('hello')
api.getBrowserTimingHeader({ nonce: nonce })
.indexOf('nonce="' + nonce + '">').should.not.equal(-1)
})
})

})