enhance: move window.config to JSON, improve CSP format - #39236
Conversation
When a reverse proxy rewrites the CSP meta tag and emits a malformed nonce, browsers drop the invalid token and the policy degrades to `script-src *`, which blocks every inline script. window.config is then undefined and the whole frontend dies. A `<script type="application/json">` block is a data block, not executed, so it is not subject to script-src and survives that degradation. Assisted-by: Claude Code:Opus 5
wxiaoguang
left a comment
There was a problem hiding this comment.
Completely AI Hallucination
No, it is not. It was fully tested, try this: <head>
<meta http-equiv=content-security-policy content="script-src * 'nonce-abc''
'sha256-vPvJQz887eJZSZgwg8NtCuSg7k61DtotXq1MmbJ8jD8='">
<script nonce=abc>window.config={a:1}</script>
<script>document.write(`config: ${JSON.stringify(window.config)}`)</script>In Firefox and Chrome, this prints <head>
<meta http-equiv=content-security-policy content="script-src * 'nonce-abc''
'sha256-SHRdxdXcsATTUja2rx3saPBqCTCfmebidvfWleYWYd0='">
<script nonce=abc type=application/json id=gitea-config>{"a":1}</script>
<script>window.config=JSON.parse(document.querySelector("#gitea-config").textContent);document.write(`config:
${JSON.stringify(window.config)}`)</script>Also, using JSON saves 1ms of browser main thread time on page load. This is such a clear improvement, I don't know why I even need to justify why JSON is safer than JS. |
|
I don't know what you want to tell me. JSON for data exchange is the right format. JS is not for data exchange. |
|
For your example, when you have
Right? |
|
Yes, |
|
Then read this again There are so many places using How does this PR "fix"? Isn't it completely AI hallucination? |
|
Yes, all |
|
So it is just a refactor or chore, it doesn't fix the real problem. But you said that "Fixes: #39225", "fully tested". Also, I don't see why the |
|
The speed gain depends on the size of the data. With 277 KB of I'm testing a solution that completely generates the JSON in go now which will make it more safe and faster to generate while removing some template helpers. |
|
I think the real reason why we break the Cloudflare parser is the way we encode that meta header with those <meta http-equiv="Content-Security-Policy" content="default-src * data: blob:;script-src * 'nonce-9420f50aa4e1d2614de25e05548e01dd';style-src * 'unsafe-inline';">Likely their parser looks for literal |
Are you serious ........ which instance has "277 KB of |
Open commit 792b4db in any gitea instance and it will be 277kB of |
|
For special pages, the
|
|
Yes, and looking at Still checking on this |
I think we can build the string directly without HTMLFormat or HTMLEscape. Then the single quote problem is fixed. |
Assembling the config map in Go lets html/template serialize and escape it in a single action, so the template no longer hand-writes JSON and three funcmap helpers that existed only for it can go. The CSP meta value is built directly rather than through HTMLFormat so its source quoting stays literal, matching the header form. The policy is composed of literals and a hex nonce, and a test locks that invariant because the value is written into the attribute unescaped. Assisted-by: Claude Code:Opus 5
|
All done, |
|
Well, there are more new problems. |
I don't think any of our code does that, could only be third party code which imho has no business of accessing |
|
If you want I can attempt to refactor away |

window.configto go-built json which generates and parses faster'instead of'so Cloudflare parser does not trip on itFixes: #39225