From cd3d1e18c693b1912bb751c05c09e2b7baf23b91 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Thu, 3 Oct 2024 09:33:14 -0400 Subject: [PATCH 1/2] feat: add cspNonce support --- src/options.js | 1 + src/utils.js | 4 ++++ vue-gtag.d.ts | 1 + 3 files changed, 6 insertions(+) diff --git a/src/options.js b/src/options.js index c0a2a91..52a16d9 100644 --- a/src/options.js +++ b/src/options.js @@ -22,6 +22,7 @@ export const getDefaultParams = () => ({ globalDataLayerName: "dataLayer", globalObjectName: "gtag", defaultGroupName: "default", + cspNonce: null, includes: null, config: { id: null, diff --git a/src/utils.js b/src/utils.js index 86784f7..47fb9eb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,6 +20,10 @@ export const load = (url, options = {}) => { head.appendChild(link); } + if (options.cspNonce) { + script.setAttribute("nonce", options.cspNonce); + } + head.appendChild(script); script.onload = resolve; diff --git a/vue-gtag.d.ts b/vue-gtag.d.ts index 4db2a8a..b0c6b89 100644 --- a/vue-gtag.d.ts +++ b/vue-gtag.d.ts @@ -280,6 +280,7 @@ declare module "vue-gtag" { pageTrackerEnabled?: boolean; pageTrackerScreenviewEnabled?: boolean; defaultGroupName?: string; + cspNonce?: string; includes?: DomainConfig[]; config?: DomainConfig; } From 1b9fd21b319469aa3dc8bec7fc1abf3c440ec9f6 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Thu, 3 Oct 2024 09:34:36 -0400 Subject: [PATCH 2/2] test: augment test suite with csp nonce assertion --- test/__snapshots__/options.spec.js.snap | 2 ++ test/options.spec.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/options.spec.js.snap b/test/__snapshots__/options.spec.js.snap index 02e0449..b832630 100644 --- a/test/__snapshots__/options.spec.js.snap +++ b/test/__snapshots__/options.spec.js.snap @@ -10,6 +10,7 @@ Object { "send_page_view": false, }, }, + "cspNonce": null, "customPreconnectOrigin": "https://www.googletagmanager.com", "customResourceURL": "https://www.googletagmanager.com/gtag/js", "defaultGroupName": "default", @@ -43,6 +44,7 @@ Object { "send_page_view": false, }, }, + "cspNonce": "abcdef", "customPreconnectOrigin": "https://www.googletagmanager.com", "customResourceURL": "https://www.googletagmanager.com/gtag/js", "defaultGroupName": "default", diff --git a/test/options.spec.js b/test/options.spec.js index 6e7b4b7..60de65d 100644 --- a/test/options.spec.js +++ b/test/options.spec.js @@ -10,7 +10,7 @@ describe("options", () => { }); test("set and get options", () => { - setOptions({ config: { id: 1 } }); + setOptions({ config: { id: 1 }, cspNonce: "abcdef" }); expect(getOptions()).toMatchSnapshot(); });