aboutsummaryrefslogtreecommitdiffstats
path: root/server/server_test.go
diff options
context:
space:
mode:
authorElvis Claros Castro <elvis@claros.ar>2026-10-03 19:03:27 -0300
committerElvis Claros Castro <elvis@claros.ar>2026-10-03 19:03:27 -0300
commit60c586001cf4d21ef1351be8c8ad7a269a6f005a (patch)
treec3fd1685968c4b3835b8755e73412eeef286dfab /server/server_test.go
parent2f8bc985b79e036ca6d34bb680a8858154c17a3b (diff)
downloadclaude-logbook-60c586001cf4d21ef1351be8c8ad7a269a6f005a.tar.gz
claude-logbook-60c586001cf4d21ef1351be8c8ad7a269a6f005a.zip
Share one session through a public link
claude-logbook -s REF --share uploads a session to a share server and prints its link; --shares lists them, --unshare deletes them, --expire picks how long they live. Before uploading it shows what goes up and warns about anything that looks like a credential. server/ is that share server: Go, standard library only, a FROM scratch image. It serves /share/<id> as a page, .txt and .json, accepts only the JSON payload (never HTML), pins the template's scripts with a CSP, keeps secrets hashed and rate-limits per client IP.
Diffstat (limited to 'server/server_test.go')
-rw-r--r--server/server_test.go269
1 files changed, 269 insertions, 0 deletions
diff --git a/server/server_test.go b/server/server_test.go
new file mode 100644
index 0000000..72f5b9c
--- /dev/null
+++ b/server/server_test.go
@@ -0,0 +1,269 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+)
+
+const session = `{"id":"aaaaaaaa-0000-0000-0000-000000000001","p":"/home/u/proj","b":"main",
+"t":"Fix the build","ai":true,"n":false,"e":false,"i":false,"f":"2025-08-14T10:00:00.000Z",
+"l":"2025-08-14T10:05:00.000Z","d":5,"u":1,"a":1,"k":1.5,"v":"1.0.0",
+"c":[{"r":"u","x":"why does the build fail?"},{"r":"t","x":"Bash: make"},
+{"r":"a","x":"A missing </script><script>alert(1)</script> tag."}]}`
+
+func payload(sessions ...string) string {
+ if len(sessions) == 0 {
+ sessions = []string{session}
+ }
+ return `{"s":[` + strings.Join(sessions, ",") + `],"m":[]}`
+}
+
+type harness struct {
+ t *testing.T
+ srv *Server
+ h http.Handler
+}
+
+func newHarness(t *testing.T, mod func(*Config)) *harness {
+ t.Helper()
+ cfg := Config{Addr: ":0", DataDir: t.TempDir(), PublicURL: "https://x.test",
+ MaxBytes: 1 << 20, MaxSessions: 1, TrustedProxy: "10.0.0.1",
+ CreatesPerH: 100, WritesPerH: 100}
+ if mod != nil {
+ mod(&cfg)
+ }
+ srv, err := NewServer(cfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return &harness{t: t, srv: srv, h: srv.Routes()}
+}
+
+func (h *harness) do(method, path, body, secret string) *httptest.ResponseRecorder {
+ req := httptest.NewRequest(method, path, strings.NewReader(body))
+ req.RemoteAddr = "192.0.2.1:1234"
+ if secret != "" {
+ req.Header.Set("Authorization", "Bearer "+secret)
+ }
+ rec := httptest.NewRecorder()
+ h.h.ServeHTTP(rec, req)
+ return rec
+}
+
+func (h *harness) create(body string) shareResp {
+ h.t.Helper()
+ rec := h.do("POST", "/api/shares", body, "")
+ if rec.Code != http.StatusCreated {
+ h.t.Fatalf("create: %d %s", rec.Code, rec.Body)
+ }
+ var r shareResp
+ if err := json.Unmarshal(rec.Body.Bytes(), &r); err != nil {
+ h.t.Fatal(err)
+ }
+ return r
+}
+
+func TestCreateAndRead(t *testing.T) {
+ h := newHarness(t, nil)
+ r := h.create(`{"payload":` + payload() + `,"expire":"7d"}`)
+ if !ValidID(r.ID) || r.Secret == "" || r.URL != "https://x.test/share/"+r.ID {
+ t.Fatalf("bad response %+v", r)
+ }
+ if d := time.Until(r.Expires); d < 6*24*time.Hour || d > 8*24*time.Hour {
+ t.Fatalf("expiry %v", r.Expires)
+ }
+
+ page := h.do("GET", "/share/"+r.ID, "", "")
+ if page.Code != 200 || !strings.Contains(page.Header().Get("Content-Type"), "text/html") {
+ t.Fatalf("html: %d", page.Code)
+ }
+ csp := page.Header().Get("Content-Security-Policy")
+ if !strings.Contains(csp, "script-src 'sha256-") || strings.Contains(csp, "unsafe-inline' ;") {
+ t.Fatalf("csp: %s", csp)
+ }
+ if page.Header().Get("X-Robots-Tag") == "" {
+ t.Fatal("no X-Robots-Tag")
+ }
+ body := page.Body.String()
+ // The data block must hold exactly one </script>: its own end.
+ start := strings.Index(body, `<script id="payload" type="application/json">`)
+ end := strings.Index(body[start:], "</script>")
+ if strings.Contains(body[start:start+end], "<script>alert") || !strings.Contains(body[start:start+end], "Fix the build") {
+ t.Fatal("payload escaped wrong")
+ }
+
+ txt := h.do("GET", "/share/"+r.ID+".txt", "", "")
+ want := []string{"# Fix the build", "project: /home/u/proj", "## User",
+ "why does the build fail?", "> tool: Bash: make", "## Claude"}
+ for _, w := range want {
+ if !strings.Contains(txt.Body.String(), w) {
+ t.Fatalf("txt lacks %q:\n%s", w, txt.Body)
+ }
+ }
+
+ js := h.do("GET", "/share/"+r.ID+".json", "", "")
+ var p Payload
+ if err := json.Unmarshal(js.Body.Bytes(), &p); err != nil || p.S[0].C[2].X != "A missing </script><script>alert(1)</script> tag." {
+ t.Fatalf("json roundtrip: %v %s", err, js.Body)
+ }
+
+ if rec := h.do("GET", "/share/"+r.ID+".pdf", "", ""); rec.Code != 404 {
+ t.Fatalf("unknown extension: %d", rec.Code)
+ }
+}
+
+func TestUpdateAndDeleteNeedTheSecret(t *testing.T) {
+ h := newHarness(t, nil)
+ r := h.create(`{"payload":` + payload() + `}`)
+ newer := strings.Replace(payload(), "Fix the build", "Fixed the build", 1)
+
+ if rec := h.do("PUT", "/api/shares/"+r.ID, `{"payload":`+newer+`}`, ""); rec.Code != 401 {
+ t.Fatalf("no secret: %d", rec.Code)
+ }
+ if rec := h.do("PUT", "/api/shares/"+r.ID, `{"payload":`+newer+`}`, "nope"); rec.Code != 403 {
+ t.Fatalf("wrong secret: %d", rec.Code)
+ }
+ if rec := h.do("PUT", "/api/shares/"+r.ID, `{"payload":`+newer+`}`, r.Secret); rec.Code != 200 {
+ t.Fatalf("update: %d %s", rec.Code, rec.Body)
+ }
+ if !strings.Contains(h.do("GET", "/share/"+r.ID+".txt", "", "").Body.String(), "Fixed the build") {
+ t.Fatal("update not visible")
+ }
+ if rec := h.do("DELETE", "/api/shares/"+r.ID, "", "nope"); rec.Code != 403 {
+ t.Fatalf("delete wrong secret: %d", rec.Code)
+ }
+ if rec := h.do("DELETE", "/api/shares/"+r.ID, "", r.Secret); rec.Code != 204 {
+ t.Fatalf("delete: %d", rec.Code)
+ }
+ if rec := h.do("GET", "/share/"+r.ID, "", ""); rec.Code != 404 {
+ t.Fatalf("after delete: %d", rec.Code)
+ }
+ if rec := h.do("DELETE", "/api/shares/"+r.ID, "", r.Secret); rec.Code != 404 {
+ t.Fatalf("delete twice: %d", rec.Code)
+ }
+}
+
+func TestExpiry(t *testing.T) {
+ h := newHarness(t, nil)
+ r := h.create(`{"payload":` + payload() + `,"expire":"1d"}`)
+ h.srv.store.now = func() time.Time { return time.Now().Add(25 * time.Hour) }
+ if rec := h.do("GET", "/share/"+r.ID, "", ""); rec.Code != 404 {
+ t.Fatalf("expired share served: %d", rec.Code)
+ }
+ if n := h.srv.store.Sweep(); n != 1 {
+ t.Fatalf("swept %d", n)
+ }
+}
+
+func TestNotFound(t *testing.T) {
+ h := newHarness(t, nil)
+ for _, p := range []string{"/share/AAAAAAAAAA", "/share/..%2Fetc%2Fpasswd", "/share/x", "/nope"} {
+ if rec := h.do("GET", p, "", ""); rec.Code != 404 {
+ t.Fatalf("%s: %d", p, rec.Code)
+ }
+ }
+}
+
+func TestRejectsBadPayloads(t *testing.T) {
+ h := newHarness(t, nil)
+ bad := map[string]string{
+ "html": `{"payload":"<html><script>alert(1)</script>"}`,
+ "no sessions": `{"payload":{"s":[],"m":[]}}`,
+ "two sessions": `{"payload":` + payload(session, session) + `}`,
+ "bad role": `{"payload":` + payload(strings.Replace(session, `"r":"t"`, `"r":"z"`, 1)) + `}`,
+ "bad type": `{"payload":` + payload(strings.Replace(session, `"u":1`, `"u":"1"`, 1)) + `}`,
+ "memories": `{"payload":{"s":[` + session + `],"m":[{"name":"x"}]}}`,
+ "bad expire": `{"payload":` + payload() + `,"expire":"never"}`,
+ "extra field": `{"payload":` + payload() + `,"html":"x"}`,
+ "no payload": `{"expire":"1d"}`,
+ "not json": `hello`,
+ }
+ for name, body := range bad {
+ if rec := h.do("POST", "/api/shares", body, ""); rec.Code != 400 {
+ t.Errorf("%s: %d %s", name, rec.Code, rec.Body)
+ }
+ }
+}
+
+func TestUnknownKeysAreDropped(t *testing.T) {
+ h := newHarness(t, nil)
+ evil := strings.Replace(session, `"ai":true`, `"ai":true,"evil":"<x>"`, 1)
+ r := h.create(`{"payload":{"s":[` + evil + `],"m":[],"x":1}}`)
+ js := h.do("GET", "/share/"+r.ID+".json", "", "").Body.String()
+ if strings.Contains(js, "evil") || strings.Contains(js, `"x":1`) {
+ t.Fatalf("unknown keys kept: %s", js)
+ }
+}
+
+func TestTooLarge(t *testing.T) {
+ h := newHarness(t, func(c *Config) { c.MaxBytes = 512 })
+ big := payload(strings.Replace(session, "why does", strings.Repeat("x", 1000), 1))
+ if rec := h.do("POST", "/api/shares", `{"payload":`+big+`}`, ""); rec.Code != 413 {
+ t.Fatalf("too large: %d %s", rec.Code, rec.Body)
+ }
+}
+
+func TestRateLimit(t *testing.T) {
+ h := newHarness(t, func(c *Config) { c.CreatesPerH = 2 })
+ body := `{"payload":` + payload() + `}`
+ h.create(body)
+ h.create(body)
+ if rec := h.do("POST", "/api/shares", body, ""); rec.Code != 429 {
+ t.Fatalf("third create: %d", rec.Code)
+ }
+}
+
+func TestClientIP(t *testing.T) {
+ c, err := NewClientIP("10.0.0.1, 172.16.0.0/12")
+ if err != nil {
+ t.Fatal(err)
+ }
+ cases := []struct{ remote, xff, want string }{
+ {"192.0.2.1:1", "203.0.113.9", "192.0.2.1"}, // not the proxy: header ignored
+ {"10.0.0.1:1", "203.0.113.9", "203.0.113.9"}, // the proxy
+ {"10.0.0.1:1", "6.6.6.6, 203.0.113.9", "203.0.113.9"}, // client forged the left part
+ {"172.20.0.5:1", "203.0.113.9, 10.0.0.1", "203.0.113.9"}, // chain of proxies
+ {"10.0.0.1:1", "", "10.0.0.1"},
+ }
+ for _, tc := range cases {
+ r := httptest.NewRequest("GET", "/", nil)
+ r.RemoteAddr = tc.remote
+ if tc.xff != "" {
+ r.Header.Set("X-Forwarded-For", tc.xff)
+ }
+ if got := c.Of(r); got != tc.want {
+ t.Errorf("%s %q: got %s want %s", tc.remote, tc.xff, got, tc.want)
+ }
+ }
+}
+
+func TestHome(t *testing.T) {
+ h := newHarness(t, nil)
+ rec := h.do("GET", "/", "", "")
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "https://x.test/share/&lt;id&gt;.txt") {
+ t.Fatalf("home: %d", rec.Code)
+ }
+ if strings.Contains(rec.Body.String(), "{{") {
+ t.Fatal("placeholder left")
+ }
+ if rec := h.do("GET", "/healthz", "", ""); rec.Code != 200 {
+ t.Fatalf("healthz: %d", rec.Code)
+ }
+}
+
+func TestStoredFilesArePrivate(t *testing.T) {
+ h := newHarness(t, nil)
+ r := h.create(`{"payload":` + payload() + `}`)
+ raw, meta, err := h.srv.store.Get(r.ID)
+ if err != nil || len(raw) == 0 {
+ t.Fatal(err)
+ }
+ if meta.SecretHash == r.Secret || !bytes.Equal([]byte(meta.SecretHash), []byte(hashOf(r.Secret))) {
+ t.Fatal("secret must be stored hashed")
+ }
+}