Web Development

Chrome extension content script blocked on pages with CSP 'strict-dynamic' — programmatic injection appears successful but inline script execution is refused

Environment

Environment

Chrome 117 on Windows 10, extension using Manifest V3 and chrome.scripting.executeScript to inject code into third-party pages. Target pages send a strict Content-Security-Policy header containing 'script-src' with 'strict-dynamic' and nonces.

Symptoms

Symptoms

chrome.scripting.executeScript calls succeed, but scripts that inject inline script tags or write <script> elements into the page DOM fail silently; console shows 'Refused to execute inline script because of Content-Security-Policy' for the injected code. Extension-hosted resources (chrome-extension://) are not authorized by the page CSP.

Observations

Observations

Content scripts run in an isolated world and can access the DOM, but any attempt to add inline script textContent or to inject a script tag that executes page-context JS is blocked by the page's CSP strict-dynamic policy unless the script carries a valid nonce or hash. Network-fetched scripts from https: with relevant trust can be allowed, but extension URLs and blob URLs are typically not considered trusted by strict-dynamic.

Already attempted

Already attempted

Tried creating script tags with textContent via content script, tried injecting external script URLs, attempted earlier-style chrome.tabs.executeScript approaches. Also attempted messaging from service worker to content script to have content script insert DOM script; CSP still blocked inline page-executable script.

Question

Question

How can my extension run necessary code on pages protected by strict-dynamic CSP? What are safe, supported patterns to interact with page JavaScript without violating CSP?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Trying to inject inline script tags into page DOM; these are blocked by strict-dynamic CSP 90%
Confusion between content script isolated world and page execution context 60%
Browser regression in executeScript handling (unlikely with current Chrome stable) 10%

Summary

Page CSP controls what runs in the page context; content scripts execute in an isolated world and are not subject to the page's script-src restrictions for their own code, but scripts injected into the page's execution context (by inserting script tags) are subject to the page CSP. With 'strict-dynamic', only scripts with a valid nonce/hash or fetched from trusted https: origins run. Extensions cannot add a matching nonce. The recommended pattern: keep your logic in the content script (isolated world) and use a safe bridge to interact with page JS: create a tiny, non-executing DOM element or use window.postMessage to exchange data; if you must run code in page context, use an injected script created by chrome.scripting.executeScript that runs in the isolated world and communicates with page context through custom DOM events or postMessage. Avoid injecting inline scripts into page DOM.

Suggested Fixes

Diagnostic step 1
Refactor so the extension's logic runs inside the content script (isolated world) and avoids adding inline <script> tags to the page.
Diagnostic step 2
If you need to interact with page JS, implement a small messaging bridge: content script injects a minimalist, non-executing marker and uses window.postMessage or CustomEvent to communicate; the page-side code (if present) can respond.
Diagnostic step 3
Use chrome.scripting.executeScript to run code in the isolated world and then exchange data through DOM events rather than injecting executable inline scripts.

Continue troubleshooting

Matched from the title, description and tags