Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): substitute the CSP nonce at every occurrence

Yudhi Armyndharis5de119d895ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5eb19f7bc..7232061c7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- The dashboard CSP nonce is substituted at every occurrence in the served document, not only the first. One placeholder exists today, so a second would have been left reading the literal text and its script refused by the browser.
+
## [0.22.0] - 2026-08-19
### Fixed
diff --git a/src/config/dashboard-csp.spec.ts b/src/config/dashboard-csp.spec.ts
index d5203e937..6512cddbf 100644
--- a/src/config/dashboard-csp.spec.ts
+++ b/src/config/dashboard-csp.spec.ts
@@ -7,3 +7,14 @@ describe('injectDashboardCspNonce', () => {
expect(injectDashboardCspNonce(template, 'tab-b')).toContain('content="tab-b"');
});
});
+
+describe('injectDashboardCspNonce: every occurrence', () => {
+ it('substitutes a placeholder that appears more than once', () => {
+ const html = `<meta content="${DASHBOARD_CSP_NONCE_PLACEHOLDER}"><script nonce="${DASHBOARD_CSP_NONCE_PLACEHOLDER}">`;
+ const out = injectDashboardCspNonce(html, 'abc123');
+ // A first-occurrence-only replace leaves the script reading the literal placeholder, which the
+ // browser then refuses under the document's own CSP.
+ expect(out).toBe('<meta content="abc123"><script nonce="abc123">');
+ expect(out).not.toContain(DASHBOARD_CSP_NONCE_PLACEHOLDER);
+ });
+});
diff --git a/src/config/dashboard-csp.ts b/src/config/dashboard-csp.ts
index 5f5937e71..e53ffa9ae 100644
--- a/src/config/dashboard-csp.ts
+++ b/src/config/dashboard-csp.ts
@@ -1,6 +1,13 @@
export const DASHBOARD_CSP_NONCE_PLACEHOLDER = '__OPENWA_CSP_NONCE__';
-/** Inject the response-specific CSP nonce into the bundled dashboard document. */
+/**
+ * Inject the response-specific CSP nonce into the bundled dashboard document.
+ *
+ * Every occurrence, not just the first: the document carries one placeholder today, in the meta
+ * element the plugin config UI reads, but the natural next one is a `nonce=` attribute on a script
+ * tag. A single-occurrence replace would leave that one reading the literal placeholder, and the
+ * browser would refuse the script with nothing failing server-side.
+ */
export function injectDashboardCspNonce(html: string, nonce: string): string {
- return html.replace(DASHBOARD_CSP_NONCE_PLACEHOLDER, nonce);
+ return html.split(DASHBOARD_CSP_NONCE_PLACEHOLDER).join(nonce);
}