diff --git a/.gitea/workflows/mirror.yaml b/.gitea/workflows/mirror.yaml new file mode 100644 index 0000000..e69de29 diff --git a/docs/decisions.md b/docs/decisions.md index 2846972..1783ee6 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -997,3 +997,30 @@ commits look as before. - **Reuse key:** includes the verdicts, so commit pages are rebuilt when a key is added in Gitea and old commits turn verified. - Icons: Octicons `lock` and `unlock` (19.15.0), added to `scripts/icons.py`. + +## Copy a file + +File pages get a copy button joined to Raw (`[Raw | ⧉]`), like GitHub's. + +- **It copies the raw file.** `copy.js` fetches the Raw link's URL and puts + the bytes on the clipboard, so CRLF, mixed line endings and files too + big to show (over 512 KB) all copy exactly. That needs `connect-src + 'self'` in the CSP: the third change to spec rule 6, after `script-src + 'self'`. It only lets this site's own scripts (`theme.js`, `copy.js`) + request this origin. It adds no way out: an injected script could already + send data anywhere through `img-src https:`, and the site holds nothing + private. Under `base_path` the origin is danieldaum.net and the Raw link + is already rebased, so it works there too. +- **Safari** only allows clipboard writes during the click, so the + download is handed to `ClipboardItem` as a promise. Browsers without + `ClipboardItem` get `writeText` after the download. +- **Which files:** text with a raw copy, up to 5 MB (`maxCopy`). Images, + binary files and files over 5 MB keep Raw alone. +- **Trailing blank lines:** chroma drops the empty line after a final + newline, so a file ending in blank lines showed one line too few. + `Highlight` adds it back, and `TestHighlightKeepsText` checks the lines + give back the source. +- **Hidden without JS, for real now.** `.copy` and `.button` set `display`, + which beat the `hidden` attribute, so the clone box's copy button showed + without JS too, contrary to "Copy buttons need JS". `[hidden]` rules for + both fix it. diff --git a/docs/security.md b/docs/security.md index a771ab0..e2cea3e 100644 --- a/docs/security.md +++ b/docs/security.md @@ -46,6 +46,10 @@ enforces it and the test that proves it. Run them all with `just test`. allows exactly that one script tag and nothing else. - Repo SVGs on `raw/` stay `text/plain` even though `/static` serves our own icons as `image/svg+xml` (covered by `TestRoutes`). +- The CSP now has `connect-src 'self'` (2026-11-24), so `copy.js` can fetch + the raw file a file page's copy button copies. Requests go to this origin + only; `img-src https:` was already a wider way out. See decisions, "Copy + a file". ## Second review (2026-11-23) diff --git a/internal/build/repo.go b/internal/build/repo.go index bbdd633..1ed4660 100644 --- a/internal/build/repo.go +++ b/internal/build/repo.go @@ -22,6 +22,7 @@ import ( const ( maxRender = 512 << 10 // larger files get no highlighted view maxRaw = 50 << 20 // larger files get no raw copy + maxCopy = 5 << 20 // larger files get no copy button: too much for a clipboard ) // repo is one allowlisted repo with its API metadata and local mirror. @@ -178,6 +179,7 @@ type BlobPage struct { Lines []template.HTML Image, Binary bool Large bool // too big to render; Raw is "" if too big to keep + Copy bool // text with a raw copy, small enough for the copy button } // loadRepo fetches metadata and updates the mirror. Anonymous access means a @@ -427,6 +429,7 @@ func (b *Builder) renderBlob(ctx context.Context, rd *render.Renderer, dir strin } } p.Large = e.Size > maxRender + p.Copy = p.Raw != "" && !p.Binary && e.Size <= maxCopy // blob pages are stored as .html; the server maps /blob/ to them return rd.Write(dir, r.Name+"/blob/"+e.Name+".html", "blob", render.Page{Title: e.Name + " · " + r.Name, D: &p}) } diff --git a/internal/render/highlight.go b/internal/render/highlight.go index f53905f..5a0af7b 100644 --- a/internal/render/highlight.go +++ b/internal/render/highlight.go @@ -25,7 +25,7 @@ func Highlight(name, lang, src string) []template.HTML { if lx == nil { lx = lexers.Fallback } - src = strings.TrimSuffix(src, "\n") + src = strings.TrimSuffix(strings.TrimSuffix(src, "\n"), "\r") // the last line's ending it, err := chroma.Coalesce(lx).Tokenise(nil, src) if err != nil { it = chroma.Literator(chroma.Token{Type: chroma.Text, Value: src}) @@ -43,6 +43,9 @@ func Highlight(name, lang, src string) []template.HTML { } lines = append(lines, template.HTML(b.String())) } + if strings.HasSuffix(src, "\n") { // chroma drops the empty line after a final newline + lines = append(lines, "") + } return lines } diff --git a/internal/render/highlight_test.go b/internal/render/highlight_test.go new file mode 100644 index 0000000..68ed6eb --- /dev/null +++ b/internal/render/highlight_test.go @@ -0,0 +1,31 @@ +package render + +import ( + "html" + "regexp" + "strings" + "testing" +) + +// TestHighlightKeepsText: the lines' text, joined, gives back the source, +// trailing blank lines included, so line counts are right. +func TestHighlightKeepsText(t *testing.T) { + tags := regexp.MustCompile(`<[^>]*>`) + for _, src := range []string{"a\nb", "a\nb\n", "a\n\n", "a\n\n\n", "\n", "\tif x {\n\t\treturn\n\t}\n", "&\n", "x\r\ny\r\n"} { + var lines []string + for _, l := range Highlight("f.go", "", src) { + lines = append(lines, html.UnescapeString(tags.ReplaceAllString(string(l), ""))) + } + nl := "\n" + if strings.Contains(src, "\r\n") { + nl = "\r\n" // chroma drops the \r + } + got := strings.Join(lines, nl) + if strings.HasSuffix(src, "\n") { + got += nl + } + if got != src { + t.Errorf("%q: %d lines give back %q", src, len(lines), got) + } + } +} diff --git a/internal/server/server.go b/internal/server/server.go index f996c93..7b33d73 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -22,8 +22,9 @@ import ( ) // script-src 'self' is only for /static/theme.js (palette) and copy.js (copy -// buttons); nothing inline. -const csp = "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' https:; font-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" +// buttons); nothing inline. connect-src 'self' lets copy.js fetch the raw +// file it copies, from this origin only. +const csp = "default-src 'none'; connect-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' https:; font-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" type Server struct { Cfg *config.Config diff --git a/testdata/golden/index.html b/testdata/golden/index.html index cecdc04..206d479 100644 --- a/testdata/golden/index.html +++ b/testdata/golden/index.html @@ -21,9 +21,9 @@ - - - + + + diff --git a/testdata/golden/repo-a/blob/README.md.html b/testdata/golden/repo-a/blob/README.md.html index f6037a4..08ff895 100644 --- a/testdata/golden/repo-a/blob/README.md.html +++ b/testdata/golden/repo-a/blob/README.md.html @@ -10,13 +10,13 @@ - - - + + + - +