Repository navigation
fix: list the Chat UI and the page's own scripts in the chat page's script policy - #593
Conversation
…cript policy The chat page's script policy allowed any inline script. It now lists the Chat UI bundle and the page's own inline scripts, which are identified by a value generated for each page. The page is resolved once and set into a new browser every time the chat view is shown, so its text is now produced when it is set and every load gets a new value. Chat startup, theming and messaging with the IDE are unchanged.
ashishrp-aws
left a comment
There was a problem hiding this comment.
🤖 AI-assisted review at 57be77c.
Approved. script-src is now the Chat UI path plus a nonce, and a new nonce is generated each time setContent puts the page into a browser. Both inline scripts carry the nonce, so the WebKit class script still runs on non-Windows platforms. Startup still uses window load, and the client configuration is still resolved once. Calls the plugin makes through browser.execute and BrowserFunction are not inline page scripts, so the policy doesn't affect them. The headless run used stand-ins, so please confirm on a real SWT browser on each platform: Edge/WebView2 on Windows, WebKit on macOS and Linux.
Across the four PRs: every IDE now drops 'unsafe-inline' from script-src, so any inline event-handler attribute or javascript: URL that the Chat UI creates at runtime would now be blocked, not just ones in the page template. The VS Code E2E jobs and the Eclipse headless run with the released bundle are good evidence that nothing breaks. Before release, please also do a quick manual pass in each IDE over the interactive parts: code-block copy/insert, links in answers, the model selector, tab and history actions, and a profile switch.
| <meta | ||
| http-equiv="Content-Security-Policy" | ||
| content="default-src 'none'; script-src %s 'unsafe-inline'; style-src %s 'unsafe-inline'; | ||
| content="default-src 'none'; script-src %s 'nonce-%s'; style-src %s 'unsafe-inline'; |
There was a problem hiding this comment.
🔴 Blocked inline scripts leave no trace: the stricter script-src has no violation reporting
script-src drops 'unsafe-inline' and now admits only this page's nonce, but the policy carries no report-to or report-uri and the page registers no securitypolicyviolation listener — searching the repository for securitypolicyviolation, report-uri and report-to returns nothing. The script the policy must keep tolerating is not shipped with the plugin: resolveJsPath() loads amazonq-ui.js from getClientDirectory(), and LspConstants pins only the floating range [1.0.0, 2.0.0) resolved at runtime against the remote manifest at CW_MANIFEST_URL. 'strict-dynamic' is absent as well, so any inline script that bundle injects at runtime is refused. The PR's headless Chromium and WebKit check covers the one bundle released today and cannot bound the versions that will float in later; that check is also not observable from this review.
content="default-src 'none'; script-src %s 'nonce-%s'; style-src %s 'unsafe-inline';
img-src 'self' data:; object-src 'none'; base-uri 'none'; connect-src swt:;"Why it matters: A tightened script-src with no violation sink (cause) → a 1.x Chat UI bundle that injects an inline script has it refused, and the only record is a WebView console that Eclipse users and support engineers cannot open (mechanism) → the chat view renders blank or half-working for everyone on that bundle, the Eclipse error log stays empty, the breakage is indistinguishable from an LSP download or network fault, and recovery needs a new plugin release because the policy is baked into the plugin rather than the bundle (consequence).
Suggestion: Register a securitypolicyviolation listener inside the nonce'd startup script and forward event.violatedDirective and event.blockedURI through the existing ideCommand BrowserFunction into Activator.getLogger(), so a refused script lands in the Eclipse log instead of a console nobody can read. Then decide explicitly whether the bundle needs 'strict-dynamic' — it would let the trusted bundle create further scripts, at the cost of trusting whatever it creates — and record that decision beside the policy.
[code-review-sage]
There was a problem hiding this comment.
Added in 59b6fc1. The page reports a Chat UI script that does not load, a Chat UI script that loads without defining the Chat UI, and what the page policy does not allow (up to ten per page, values shortened in the page), through ideCommand. The plugin logs them and does not send them to the chat server. 'strict-dynamic' is not needed: with the 1.87.0 bundle, normal use produces no report in Chromium or WebKit.
| /** | ||
| * Creates a value that is unique to one page. | ||
| */ | ||
| private static String createScriptNonce() { |
There was a problem hiding this comment.
🟡 The login webview keeps 'unsafe-inline', so the same injection hole stays open one page over
createScriptNonce() and the per-nonce page builder are private to ChatWebViewAssetProvider. ToolkitLoginWebViewAssetProvider is the other page built on the same WebViewAssetProvider base, with the same browser.setText() delivery, the same WebviewAssetServer origin and the same waitForFunction/ideCommand startup shape, and it still declares script-src %s 'unsafe-inline' (ToolkitLoginWebViewAssetProvider.java:106). Neither the diff nor the description mentions that page or defers it to a follow-up.
private static String createScriptNonce() {
byte[] bytes = new byte[SCRIPT_NONCE_BYTES];
SECURE_RANDOM.nextBytes(bytes);
return Base64.getEncoder().encodeToString(bytes);
}Why it matters: The nonce helper stays private to one provider (cause) → the login page keeps 'unsafe-inline' and the next person must re-derive the same per-page nonce plumbing in a second place (mechanism) → the script-injection class this change closes stays open on the page that runs the authentication flow, and the two structurally identical pages drift into two different script policies (consequence).
Suggestion: Move createScriptNonce() and the per-nonce page construction into WebViewAssetProvider and have both providers build their page through it, so the login page loses 'unsafe-inline' by the same mechanism. If the login page is deliberately out of scope, say so in the description and link the follow-up.
[code-review-sage]
There was a problem hiding this comment.
This change is scoped to the chat page. The sign-in page loads a different bundle, which ships with the plugin, so it is out of scope here.
| <script type="text/javascript"> | ||
| <script type="text/javascript" nonce="%s"> | ||
| // The nonce is only needed to start this script. | ||
| document.currentScript.removeAttribute('nonce'); |
There was a problem hiding this comment.
🟡 Stripping the nonce attribute protects nothing while the policy meta tag still carries it
Both inline scripts strip their own nonce attribute as their first statement. The page is delivered with browser.setText(), so the policy can only travel in a tag, which means the same nonce stays in the DOM verbatim for the life of the page and anything that can read the document can read it there. The engines this plugin uses — WebView2 on Windows, WebKit on macOS and Linux, per AmazonQBrowserProvider.getBrowserStyle() — already blank the nonce content attribute once the element is parsed. The removal therefore buys nothing, yet it is the one statement that must succeed before waitForFunction, amazonQChat.createChat and getInputFunctions() are even defined, and the new inlineScriptsRemoveTheirNonceAttributeFirst test pins that ordering as a contract.
<script type="text/javascript" nonce="%s">
// The nonce is only needed to start this script.
document.currentScript.removeAttribute('nonce');Why it matters: An unachievable hardening step placed first on the startup path (cause) → on any engine or embedding where document.currentScript is null the first statement throws, so the remaining statements in that script never execute and window.addEventListener('load', init) is never registered (mechanism) → chat never starts at all, and the nonce the statement was trying to hide is still sitting in the meta tag anyway (consequence).
Suggestion: Drop both removeAttribute('nonce') statements and the test that pins them, since meta-tag delivery makes nonce hiding unachievable on this page. If they stay, make them non-fatal and last rather than first — document.currentScript?.removeAttribute('nonce') at the end of the script — so an optional hardening step cannot stop chat from starting.
[code-review-sage]
There was a problem hiding this comment.
With a <meta> policy, WebKit and Chromium do not blank the attribute: getAttribute('nonce') returns the value and attribute selectors match it until the script removes it. Only a policy delivered in a response header blanks it. document.currentScript is the script element while a classic inline script runs, so the statement does not throw. Kept as is.
| http-equiv="Content-Security-Policy" | ||
| content="default-src 'none'; script-src %s 'unsafe-inline'; style-src %s 'unsafe-inline'; | ||
| content="default-src 'none'; script-src %s 'nonce-%s'; style-src %s 'unsafe-inline'; | ||
| img-src 'self' data:; object-src 'none'; base-uri 'none'; connect-src swt:;" |
There was a problem hiding this comment.
🟡 The description never says 'unsafe-inline' was removed from script-src
The title and body say the policy "now lists the Chat UI bundle and the page's own inline scripts", but the bundle was already listed before this change (script-src %s); the actual edit is the removal of 'unsafe-inline' from script-src. The body never uses the words 'unsafe-inline', never names the injection risk the removal closes, and never says what the page may no longer do — inline event-handler attributes, and any inline script injected at runtime without the nonce. "Styles and all other policy directives are unchanged" points the reader at what did not change rather than at the one directive that became stricter.
- content="default-src 'none'; script-src %s 'unsafe-inline'; style-src %s 'unsafe-inline';
+ content="default-src 'none'; script-src %s 'nonce-%s'; style-src %s 'unsafe-inline';Why it matters: A restrictive security change written in additive wording with no stated why (cause) → reviewers and the release owner read it as adding entries to a list rather than withdrawing a standing permission (mechanism) → the bundle-compatibility and CSP-violation testing a tightened policy needs gets skipped, and whoever later debugs a blocked script finds no trail in the description saying the policy was narrowed (consequence).
Suggestion: State plainly that script-src no longer allows 'unsafe-inline', name the injection path the removal closes (scripts arriving through rendered model output and tool results), and list what the page may no longer do: inline event-handler attributes and un-nonced inline scripts injected at runtime.
[code-review-sage]
There was a problem hiding this comment.
Updated. The description now shows script-src before and after, and what the page runs under it.
…cy does not allow The chat page now tells the plugin when the Chat UI script does not load, when it loads without defining the Chat UI, and what the page policy does not allow (up to ten times for each page). Reported values leave the page shortened and without a query string or fragment. A report raised before the plugin has defined ideCommand is held and sent once it exists. The plugin writes the reports to its log: script reports as errors, with the Chat UI script location, and policy reports as warnings. Reports are not sent to the chat server. The tests run the page's startup script against stand-ins for the browser objects it uses.
What changed
Script policy
script-srcchanges from<Chat UI location> 'unsafe-inline'to<Chat UI location> 'nonce-<value>'. The page runs the Chat UI bundle and its own inline scripts, which carry a value generated for each page (32 random bytes, base64). Inline event handler attributes and inline scripts without the page's value do not run.ChatWebViewAssetProviderproduces the page text when it is set into a browser, so every load of the chat view gets a new value. The Chat UI location, theme variables and client configuration are still resolved once.Load reports
ideCommandis held and sent once it exists.Amazon Q chat page: the Chat UI script did not load (Chat UI script and allowed script source: …)Amazon Q chat page: the Chat UI script loaded without defining the Chat UI (…)Amazon Q chat page: the page policy did not allow "<address>" (<directive>)Chat startup (page
load, wait forideCommand,amazonQChat.createChat), theming and messaging with the IDE are unchanged.Testing
mvn -B clean package: 586 tests pass, 0 Checkstyle violations.ChatWebViewAssetProviderTest(Windows, macOS and Linux page variants): policy text, the same value on every inline script and in the policy, a new value for each page, value size, no event-handler attributes in the page, Chat UI script loaded by source, startup wiring. New tests run the page's startup script and check each report, the limit of ten, shortening of reported values, reports held untilideCommandexists, and that chat still starts when a report cannot be sent.ChatPageLoadReportTestandAmazonQChatViewActionHandlerTest: the log text and level for each report, values that are not text or are not printable, unknown reports, and that a report is not sent to the chat server.ideCommanddefined 2.5 s after load, held reports are sent in order once it exists.