Use Content-Security-Policy: script nonce#37232
Conversation
ad8cd4e to
23956fa
Compare
23956fa to
fd3246c
Compare
There was a problem hiding this comment.
Pull request overview
Adds per-request CSP script nonces and updates templates/JS to apply nonces consistently, aiming to improve compatibility with stricter Content-Security-Policy configurations (Fix #305).
Changes:
- Introduces
ctx.CspScriptNonce,ctx.ScriptImport, andctx.HeadMetaContentSecurityPolicyinTemplateContextand updates templates to use them. - Adds
nonce="{{ctx.CspScriptNonce}}"to various inline/external<script>tags and propagates the nonce when dynamically executing scripts in the PR merge box. - Updates external markup rendering outputs/tests to include a nonce attribute on injected helper scripts.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| web_src/js/features/repo-issue-pull.ts | Propagates CSP nonce to dynamically executed scripts in the merge box reload path. |
| tests/integration/markup_external_test.go | Updates expected HTML to include nonce="not-needed" in injected script tags. |
| templates/user/dashboard/repolist.tmpl | Adds CSP nonce to inline module script. |
| templates/user/auth/captcha.tmpl | Adds CSP nonce to captcha provider scripts. |
| templates/swagger/openapi-viewer.tmpl | Adds CSP meta and switches to ctx.ScriptImport for nonce-aware script tags. |
| templates/status/500.tmpl | Adds CSP meta and nonce to inline script on the error page. |
| templates/shared/combomarkdowneditor.tmpl | Adds nonce to inline script used for editor styling toggle. |
| templates/repo/issue/view_content/pull_merge_box.tmpl | Adds nonce to inline module script in merge UI. |
| templates/repo/diff/box.tmpl | Adds nonces to inline scripts controlling diff file tree UI. |
| templates/base/head_script.tmpl | Adds nonce to the global inline config script and switches to ctx.ScriptImport. |
| templates/base/head.tmpl | Injects CSP meta into the base page head. |
| templates/base/footer.tmpl | Switches to ctx.ScriptImport for the main JS entry. |
| services/context/context_template.go | Adds CSP nonce generation, CSP meta emission, and nonce-aware script import helper. |
| services/context/context.go | Moves TemplateContext type definition out (now in context_template.go). |
| modules/util/util.go | Adds “fast” random helpers and changes CryptoRandomBytes error handling behavior. |
| modules/templates/helper.go | Removes legacy global ScriptImport template func implementation. |
| modules/markup/render.go | Adds nonce="not-needed" to external-render helper script injection. |
| modules/markup/external/openapi.go | Adds nonce="not-needed" to swagger script tag in generated HTML. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Why these |
Dummy attribute. Then you grep the whole code base (or AI greps), find a lot of |
Will browsers ignore such invalid values or could it cause issues? |
When no CSP, the nonce attribute does nothing. |
|
Fine if it's not causing issues, but I'd feel more comfortable if this would be a |
|
One must-fix from a review pass:
Options:
Also worth considering while touching this area:
This comment was written with the help of Claude Opus 4.6. |
Done in 1dbf6f9
Changed it to |
|
Aggregated review pass with two models (Claude Opus 4.6 + GPT-5.4). Findings where they converged or diverged:
|
I have no interest to read. If you think anything is a must, please point out directly |
|
Those are high-quality reviews, you should at minimum comment them. Point 3 is likely a dupe as per discussion above. Why do you even request Copilot review when you don't want to read AI reviews? |
That's just bullshit. AI slop |
I ask AI to help to write code and review, I address my problems. I always hide the unrelated review, never waste reviewers' or author's time. As the document said: you should post your thoughts, but not copy paste AI slop. You are just keeping pasting AI slop, why I don't just ask my AI to review and waste time on answering your AI's bullshit? |
|
Ok with me to dismiss all except the |
If you don't like it, show your plan, show feasible and actionable "TODOs". Don't make nonsense comment with your guess/imagination. |
Plan is to set |
When you write and review other PRs, haven't you seen URLs like You also ever said that "AssetURI" is a good name because it can be a full URL, not related to this origin. I don't see any easy plan to make it completely right for end users. |
It is never the plan. It will also break a lot of users who use custom templates or external renders. I believe you should remember this:
|
7192fcc to
a0c5b94
Compare
I think I have answered everything in #37232 (comment). These reviews are all bullshit. |
|
We should at least give users a opt-in ini setting so they can configure Do you not see the huge security benefit |
Why you assume that I don't see? I have explained everything in a0c5b94 Does this PR make anything worse? If no, I don't think |
silverwind
left a comment
There was a problem hiding this comment.
It's an improvement and it won't break stuff, but we definitely should make * configurable later.
|
Some followup ideas in #37238. |
* main: Replace `dropzone` with `@deltablot/dropzone` (go-gitea#37237) Add `ExternalIDClaim` option for OAuth2 OIDC auth source (go-gitea#37229) Remove error returns from crypto random helpers and callers (go-gitea#37240) Use Content-Security-Policy: script nonce (go-gitea#37232) Remove htmx (go-gitea#37224) Refactor "htmx" to "fetch action" (go-gitea#37208) Fix UI regression (go-gitea#37218) Fix corrupted JSON caused by goccy library (go-gitea#37214) Add test for "fetch redirect", add CSS value validation for external render (go-gitea#37207) Fix incorrect concurrency check (go-gitea#37205) refactor: simplify ParseCatFileTreeLine and catBatchParseTreeEntries (go-gitea#37210) Update go js py dependencies (go-gitea#37204)
|
Regression: #37257 |
asciinema-player uses WebAssembly, which is blocked by the main site CSP introduced in go-gitea#37232 (script-src lacks 'wasm-unsafe-eval'). Moving the renderer into an iframe does not help on its own because srcdoc iframes inherit the parent CSP per CSP3 §4.2.3.6. Two changes: - add 'wasm-unsafe-eval' to script-src so srcdoc iframes can load WASM - convert the asciicast renderer to the existing frontendRenderer iframe pattern (same as 3D viewer and openapi-swagger) for consistency and isolation Fixes go-gitea#37257 Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>


Fix #305
For custom theme users only: if you used
<script>tags in custom templates, you need to add thenonceattribute to them: