Skip to content

Commit 1dbf6f9

Browse files
committed
use pool for rand, use attr key only for nonce
1 parent 5a14b2e commit 1dbf6f9

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

modules/markup/external/openapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (p *openAPIRenderer) Render(ctx *markup.RenderContext, input io.Reader, out
7272
</head>
7373
<body>
7474
<div id="swagger-ui"><textarea class="swagger-spec-content" data-spec-filename="%s">%s</textarea></div>
75-
<script nonce="not-needed" type="module" src="%s"></script>
75+
<script nonce type="module" src="%s"></script>
7676
</body>
7777
</html>`,
7878
public.AssetURI("css/swagger.css"),

modules/markup/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func RenderWithRenderer(ctx *RenderContext, renderer Renderer, input io.Reader,
248248
extraLinkHref := ctx.RenderOptions.StandalonePageOptions.CurrentWebTheme.PublicAssetURI()
249249
// "<script>" must go before "<link>", to make Golang's http.DetectContentType() can still recognize the content as "text/html"
250250
// DO NOT use "type=module", the script must run as early as possible, to set up the environment in the iframe
251-
extraHeadHTML = htmlutil.HTMLFormat(`<script nonce="not-needed" crossorigin src="%s"></script><link rel="stylesheet" href="%s">`, extraScriptSrc, extraLinkHref)
251+
extraHeadHTML = htmlutil.HTMLFormat(`<script nonce crossorigin src="%s"></script><link rel="stylesheet" href="%s">`, extraScriptSrc, extraLinkHref)
252252
}
253253

254254
ctx.usedByRender = true

modules/util/util.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,24 @@ func CryptoRandomBytes(length int64) ([]byte, error) {
9797
return buf, nil
9898
}
9999

100-
var chaCha8Rand = sync.OnceValue(func() *rand2.ChaCha8 {
101-
var buf [32]byte
102-
_, _ = rand.Read(buf[:])
103-
return rand2.NewChaCha8(buf)
100+
var chaCha8RandPool = sync.OnceValue(func() *sync.Pool {
101+
return &sync.Pool{
102+
New: func() any {
103+
var buf [32]byte
104+
_, _ = rand.Read(buf[:])
105+
return rand2.NewChaCha8(buf)
106+
},
107+
}
104108
})
105109

106110
func FastCryptoRandomBytes(length int) []byte {
107111
// ChaCha8 is about 20x times faster than system's crypto/rand.
108112
// It is suitable for UUIDs, session IDs, etc
113+
pool := chaCha8RandPool()
114+
chaCha8Rand := pool.Get().(*rand2.ChaCha8)
115+
defer pool.Put(chaCha8Rand)
109116
buf := make([]byte, length)
110-
_, _ = chaCha8Rand().Read(buf)
117+
_, _ = chaCha8Rand.Read(buf)
111118
return buf
112119
}
113120

tests/integration/markup_external_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestExternalMarkupRenderer(t *testing.T) {
108108
// default sandbox in sub page response
109109
assert.Equal(t, "frame-src 'self'; sandbox allow-scripts allow-popups", respSub.Header().Get("Content-Security-Policy"))
110110
// FIXME: actually here is a bug (legacy design problem), the "PostProcess" will escape "<script>" tag, but it indeed is the sanitizer's job
111-
assert.Equal(t, `<script nonce="not-needed" crossorigin src="`+public.AssetURI("js/external-render-helper.js")+`"></script><link rel="stylesheet" href="`+public.AssetURI("css/theme-gitea-auto.css")+`"><div><any attr="val">&lt;script&gt;&lt;/script&gt;</any></div>`, respSub.Body.String())
111+
assert.Equal(t, `<script nonce crossorigin src="`+public.AssetURI("js/external-render-helper.js")+`"></script><link rel="stylesheet" href="`+public.AssetURI("css/theme-gitea-auto.css")+`"><div><any attr="val">&lt;script&gt;&lt;/script&gt;</any></div>`, respSub.Body.String())
112112
})
113113
})
114114

@@ -131,7 +131,7 @@ func TestExternalMarkupRenderer(t *testing.T) {
131131
t.Run("HTMLContentWithExternalRenderIframeHelper", func(t *testing.T) {
132132
req := NewRequest(t, "GET", "/user2/repo1/render/branch/master/html.no-sanitizer")
133133
respSub := MakeRequest(t, req, http.StatusOK)
134-
assert.Equal(t, `<script nonce="not-needed" crossorigin src="`+public.AssetURI("js/external-render-helper.js")+`"></script><link rel="stylesheet" href="`+public.AssetURI("css/theme-gitea-auto.css")+`"><script>foo("raw")</script>`, respSub.Body.String())
134+
assert.Equal(t, `<script nonce crossorigin src="`+public.AssetURI("js/external-render-helper.js")+`"></script><link rel="stylesheet" href="`+public.AssetURI("css/theme-gitea-auto.css")+`"><script>foo("raw")</script>`, respSub.Body.String())
135135
assert.Equal(t, "frame-src 'self'", respSub.Header().Get("Content-Security-Policy"))
136136
})
137137
})

0 commit comments

Comments
 (0)