Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Customize the instance from an instance repository

6ce635a84ad5d5d0c862d4deb4d46489dc97c677 · Keenan Nemetz · 1d ago · parent f2fd4024

Customize the instance from an instance repository

The landing page's prose was config-only, so changing it meant editing
vole.toml on the host and restarting, and the packaged service cannot write
that file. Custom CSS, assets, and a favicon had nowhere to live at all.

This adds an instance repository, modelled on GitHub's .github repository:
fixed paths in one private repository, read as data rather than executed.

    profile/README.md           the landing page introduction
    profile/private/README.md   replaces it for signed-in viewers
    profile/custom.css          loaded after the built-in stylesheets
    profile/favicon.svg         favicon override
    assets/                     served at /static/custom/assets/

forge/instancerepo reads one commit into an immutable snapshot on a two
second timer and swaps it atomically; a request never runs git, and a load
failure keeps the previous theme. Markdown goes through the issue sanitiser,
assets are an extension whitelist with size and count caps, and scripts and
HTML are not assets. The repository is private to instance admins and hidden
from the home grid and activity rail, but its files are served to visitors.

The Admin Configuration tab grows an Instance repository panel: status, the
last commit, the last load error, the clone URL, the recognized paths, and a
button that creates the repository with starter files. The home page's pencil
keeps the in-place editor, but a save is now a commit to profile/README.md
with the admin as the author and web.home.intro in the audit log, instead of a
row in users.db.

The rest of the landing page stays in the shape it had: instance_name as the
heading, the introduction card, then the repository grid beside the Latest
rail. Moving a configurable page layout into a template system was tried and
dropped: this is fixed files, not a templating surface.

This replaces the pending introduction-store change, which is closed.

server/web/instance_assets.go +75

@@ -0,0 +1,75 @@
1 package web
2
3 // The instance repository's static files. A theme's CSS, favicon and assets are
4 // served from /static/custom/ before the embedded set is consulted, so the URL
5 // namespace is one: a theme file never has to shadow a built-in path, and a miss
6 // is still the embedded 404.
7 //
8 // The response is keyed by the commit it came from: the URL carries ?v=<commit>,
9 // and the ETag is that commit. A push moves the commit and every custom URL with
10 // it, so a browser never has to revalidate a file that cannot have changed.
11
12 import (
13 "bytes"
14 "net/http"
15 "path"
16 "strings"
17 "time"
18 )
19
20 const instanceAssetPrefix = "/static/custom/"
21
22 // Static serves the instance repository's files first, then the embedded assets.
23 func (w *Web) Static() http.Handler {
24 embedded := Static()
25 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
26 if w.serveInstanceAsset(rw, r) {
27 return
28 }
29 embedded.ServeHTTP(rw, r)
30 })
31 }
32
33 // serveInstanceAsset answers a request under /static/custom/ and reports whether
34 // it did. It never builds a filesystem path: the snapshot map is the whole
35 // lookup, so a traversal is simply a miss.
36 func (w *Web) serveInstanceAsset(rw http.ResponseWriter, r *http.Request) bool {
37 if w.InstanceRepo == nil || !strings.HasPrefix(r.URL.Path, instanceAssetPrefix) {
38 return false
39 }
40 snap := w.InstanceRepo.Current()
41 if snap == nil {
42 return false
43 }
44 rel := strings.TrimPrefix(r.URL.Path, instanceAssetPrefix)
45 var body []byte
46 switch {
47 case rel == "profile/custom.css":
48 body = snap.CSS
49 case rel == "profile/favicon.svg":
50 body = snap.Favicon
51 case strings.HasPrefix(rel, "assets/"):
52 if a, ok := snap.Assets[strings.TrimPrefix(rel, "assets/")]; ok {
53 body = a.Body
54 }
55 }
56 if len(body) == 0 {
57 return false
58 }
59 v := snap.Commit
60 if len(v) > 12 {
61 v = v[:12]
62 }
63 rw.Header().Set("ETag", `"`+v+`"`)
64 rw.Header().Set("Cache-Control", "no-cache")
65 rw.Header().Set("X-Content-Type-Options", "nosniff")
66 if strings.HasSuffix(rel, ".svg") {
67 // An SVG opened directly is a document, and a document can carry script.
68 // The CSP makes it inert; as an <img> it renders normally.
69 rw.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'")
70 }
71 // ServeContent answers If-None-Match from the ETag and picks the content type
72 // from the name; an unlisted file never gets this far.
73 http.ServeContent(rw, r, path.Base(rel), time.Time{}, bytes.NewReader(body))
74 return true
75 }