Phase 5 part 2: preview any branch of a site

Sites → Previews builds a branch with Hugo (drafts included) and serves it
under /preview/<site>/<branch>/, links rewritten to stay inside, behind a
sandboxing CSP so a branch's scripts can't act on the app or phone home.
The image now builds Hugo v0.170.1 from source; Alpine's 0.147.2 couldn't
build the site. Hugo's errors show their innermost cause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 14:02:23 -07:00
co-authored by Claude Opus 5.5
parent 1152cd448b
commit 233c6ac630
13 changed files with 722 additions and 5 deletions
+113
View File
@@ -1,13 +1,18 @@
package web
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"supermarket/internal/hugorepo"
"supermarket/internal/hugorepo/hugotest"
"supermarket/internal/profile"
"supermarket/internal/secrets"
@@ -64,3 +69,111 @@ func TestSitesPage(t *testing.T) {
t.Errorf("after a failed read: %s", body)
}
}
func TestSitePreviews(t *testing.T) {
if hugorepo.HugoVersion(context.Background()) == "" {
t.Skip("no hugo")
}
clone := hugotest.Site(t, map[string]string{
"layouts/_default/single.html": `{{ define "main" }}<h1>{{ .Title }}</h1><a href="/pricing/">Prices</a><img src="{{ "logo.png" | relURL }}">{{ .Content }}{{ end }}`,
"static/logo.png": "png",
})
// A second branch, as a pull request would have.
work := t.TempDir()
for _, args := range [][]string{
{"clone", "-q", clone, work},
{"-C", work, "checkout", "-qb", "supermarket/page-1"},
} {
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %s", args, out)
}
}
os.WriteFile(filepath.Join(work, "content", "new.md"), []byte("---\ntitle: Brand new\ndraft: true\n---\nFresh.\n"), 0o644)
for _, args := range [][]string{
{"-C", work, "add", "."},
{"-C", work, "-c", "user.name=T", "-c", "user.email=t@example.com", "commit", "-qm", "Add a page"},
{"-C", work, "push", "-q", "origin", "supermarket/page-1"},
} {
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %s", args, out)
}
}
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/v1/repos/Us/site" {
fmt.Fprintf(w, `{"full_name":"Us/site","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
return
}
http.NotFound(w, r)
}))
defer g.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example/", Repo: "Us/site"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
if body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String(); !strings.Contains(body, "under <a href=\"/sites?site=0\">Sections and pages</a> first") {
t.Errorf("before reading: %s", body)
}
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String()
if !strings.Contains(body, "<strong>main</strong> <span class=\"badge published\">") || !strings.Contains(body, "supermarket/page-1") ||
!strings.Contains(body, "Add a page") || strings.Contains(body, "Open preview") {
t.Errorf("branches: %s", body)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/", nil, nil); rec.Code != http.StatusNotFound {
t.Errorf("before building: %d", rec.Code)
}
rec := postForm(h, "/sites/previews/build", url.Values{"site": {"0"}, "branch": {"supermarket/page-1"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("build: %d %s", rec.Code, rec.Body.String())
}
body = do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String()
if !strings.Contains(body, `href="/preview/0/supermarket-page-1/"`) || strings.Contains(body, "moved on") {
t.Errorf("after building: %s", body)
}
// The draft page is there, its links stay inside the preview, and the
// page is sandboxed.
rec = do(h, http.MethodGet, "/preview/0/supermarket-page-1/new/", nil, nil)
got := rec.Body.String()
if rec.Code != http.StatusOK || !strings.Contains(got, "<h1>Brand new</h1>") || !strings.Contains(got, `href="/preview/0/supermarket-page-1/pricing/"`) ||
!strings.Contains(got, `src="/preview/0/supermarket-page-1/logo.png"`) {
t.Errorf("preview page: %d %s", rec.Code, got)
}
if csp := rec.Header().Get("Content-Security-Policy"); !strings.HasPrefix(csp, "sandbox allow-scripts") || strings.Contains(csp, "allow-same-origin") {
t.Errorf("csp: %s", csp)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/logo.png", nil, nil); rec.Code != http.StatusOK || rec.Body.String() != "png" {
t.Errorf("static file: %d", rec.Code)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1", nil, nil); rec.Code != http.StatusMovedPermanently {
t.Errorf("no slash: %d", rec.Code)
}
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/../../../app.db", nil, nil); rec.Code == http.StatusOK {
t.Errorf("escaped the preview: %d", rec.Code)
}
// The working copy is back on the published branch.
if _, err := os.Stat(filepath.Join(s.siteDir("Us/site"), "content", "new.md")); err == nil {
t.Error("the working copy was left on the branch")
}
// A post from a preview page (its origin is "null") is refused.
if rec := do(h, http.MethodPost, "/sites/previews/delete", strings.NewReader("site=0&branch=supermarket/page-1"),
map[string]string{"Origin": "null", "Content-Type": "application/x-www-form-urlencoded"}); rec.Code != http.StatusForbidden {
t.Errorf("post from a preview: %d", rec.Code)
}
// The branch moves on: the preview says so.
os.WriteFile(filepath.Join(work, "content", "new.md"), []byte("---\ntitle: Newer\n---\n"), 0o644)
exec.Command("git", "-C", work, "-c", "user.name=T", "-c", "user.email=t@example.com", "commit", "-qam", "Again").Run()
exec.Command("git", "-C", work, "push", "-q", "origin", "supermarket/page-1").Run()
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
if body := do(h, http.MethodGet, "/sites/previews", nil, nil).Body.String(); !strings.Contains(body, "moved on") {
t.Errorf("stale: %s", body)
}
postForm(h, "/sites/previews/delete", url.Values{"site": {"0"}, "branch": {"supermarket/page-1"}})
if rec := do(h, http.MethodGet, "/preview/0/supermarket-page-1/new/", nil, nil); rec.Code != http.StatusNotFound {
t.Errorf("after deleting: %d", rec.Code)
}
if _, err := os.Stat(s.previewDir("Us/site", "supermarket-page-1")); err == nil {
t.Error("the files are still there")
}
}