Customizing Gitea: add "nonce" to script tags (#520)

Reviewed-on: https://gitea.com/gitea/docs/pulls/520
Reviewed-by: silverwind <[email protected]>
This commit is contained in:
wxiaoguang
2026-08-20 07:56:43 +00:00
parent 9a34857df9
commit d0a42af7ff
2 changed files with 20 additions and 16 deletions
+4 -2
View File
@@ -85,6 +85,8 @@ To obtain any embedded file (including templates), the [`gitea embedded` tool](.
Be aware that any statement contained inside `{{` and `}}` are Gitea's template syntax and
shouldn't be touched without fully understanding these components.
Gitea pages are protected by Content-Security-Policy, use `<script nonce="{{ctx.CspScriptNonce}}">...</script>` for script tags.
### Customizing startpage / homepage
Copy [`home.tmpl`](https://github.com/go-gitea/gitea/blob/main/templates/home.tmpl) for your version of Gitea from `templates` to `$GITEA_CUSTOM/templates`.
@@ -139,7 +141,7 @@ copy JavaScript files from https://gitea.com/davidsvantesson/plantuml-code-highl
`$GITEA_CUSTOM/public/assets/` folder. Then add the following to `$GITEA_CUSTOM/templates/custom/footer.tmpl`:
```html
<script>
<script nonce="{{ctx.CspScriptNonce}}">
$(async () => {
if (!$('.language-plantuml').length) return;
await Promise.all([
@@ -185,7 +187,7 @@ nano $GITEA_CUSTOM/templates/custom/footer.tmpl
```
```html
<script>
<script nonce="{{ctx.CspScriptNonce}}">
function onPageChange() {
// Supported 3D file types
const fileTypes = ['3dm', '3ds', '3mf', 'amf', 'bim', 'brep', 'dae', 'fbx', 'fcstd', 'glb', 'gltf', 'ifc', 'igs', 'iges', 'stp', 'step', 'stl', 'obj', 'off', 'ply', 'wrl'];
@@ -85,6 +85,8 @@ To obtain any embedded file (including templates), the [`gitea embedded` tool](.
Be aware that any statement contained inside `{{` and `}}` are Gitea's template syntax and
shouldn't be touched without fully understanding these components.
Gitea pages are protected by Content-Security-Policy, use `<script nonce="{{ctx.CspScriptNonce}}">...</script>` for script tags.
### Customizing startpage / homepage
Copy [`home.tmpl`](https://github.com/go-gitea/gitea/blob/main/templates/home.tmpl) for your version of Gitea from `templates` to `$GITEA_CUSTOM/templates`.
@@ -139,7 +141,7 @@ copy JavaScript files from https://gitea.com/davidsvantesson/plantuml-code-highl
`$GITEA_CUSTOM/public/assets/` folder. Then add the following to `$GITEA_CUSTOM/templates/custom/footer.tmpl`:
```html
<script>
<script nonce="{{ctx.CspScriptNonce}}">
$(async () => {
if (!$('.language-plantuml').length) return;
await Promise.all([
@@ -185,7 +187,7 @@ nano $GITEA_CUSTOM/templates/custom/footer.tmpl
```
```html
<script>
<script nonce="{{ctx.CspScriptNonce}}">
function onPageChange() {
// Supported 3D file types
const fileTypes = ['3dm', '3ds', '3mf', 'amf', 'bim', 'brep', 'dae', 'fbx', 'fcstd', 'glb', 'gltf', 'ifc', 'igs', 'iges', 'stp', 'step', 'stl', 'obj', 'off', 'ply', 'wrl'];