package server import ( "io" "net/http" "net/http/httptest" "regexp" "strings" "sync" "sync/atomic" "testing" ) func TestDiagramProjectionCacheIsolation(t *testing.T) { s, token, domain := configFixture(t) s.diagramRoot = t.TempDir() s.mux.HandleFunc("/:/compile/diagram/", s.handleCompileDiagram) var renders atomic.Int32 renderer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { renders.Add(1) _, _ = io.WriteString(w, `diagram`) })) defer renderer.Close() t.Setenv("KROKI_URL", renderer.URL) r := httptest.NewRequest("GET", "http://localhost/guide", nil) r.Header.Set("Authorization", "Bearer "+token) r = cacheRequestDomain(withReadPolicyContext(r)) input := `
graph LR; A-->B;
` out := s.safeHTMLProjection(r, input, "/guide", true) m := regexp.MustCompile(`src="([^"]+\.svg)"`).FindStringSubmatch(out) if len(m) != 2 { t.Fatalf("diagram formatting lost: %s", out) } other := s.safeHTMLProjection(r, input, "/other", true) if strings.Contains(other, m[1]) { t.Fatal("identical diagrams at different Paths share authority references") } for i := 0; i < 2; i++ { w := configRequest(s, token, "GET", m[1], nil) if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Security-Policy"), "sandbox;") || w.Header().Get("Cache-Control") != "private, no-store" { t.Fatalf("diagram/cache=%d %s", w.Code, w.Body.String()) } } if renders.Load() != 1 { t.Fatalf("cache missed: %d", renders.Load()) } // Even a populated on-disk cache cannot bypass the requesting reader's // Path grant or a mismatching Domain provenance record. reader, err := s.store.CreatePasswordAccount(domain, "diagram-reader", "a long reader password", "Reader", "") if err != nil { t.Fatal(err) } readerToken, err := s.store.CreateAuthSession(domain, reader.UUID) if err != nil { t.Fatal(err) } var role string if err = s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.principal_uuid=? AND m.domain_uuid=?`, reader.UUID, domain).Scan(&role); err != nil { t.Fatal(err) } if _, err = s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil { t.Fatal(err) } w := httptest.NewRecorder() denied := httptest.NewRequest("GET", "http://localhost"+m[1], nil) denied.Header.Set("Authorization", "Bearer "+readerToken) s.handleCompileDiagram(w, denied) if w.Code != 404 { t.Fatalf("cached hidden diagram=%d", w.Code) } hash := strings.TrimSuffix(m[1][strings.LastIndex(m[1], "/")+1:], ".svg") diagramSourceMu.Lock() entry := diagramSourceCache[hash] entry.Domain = "foreign" diagramSourceCache[hash] = entry diagramSourceMu.Unlock() w = httptest.NewRecorder() s.handleCompileDiagram(w, httptest.NewRequest("GET", "http://localhost"+m[1], nil)) if w.Code != 404 { t.Fatal("foreign cached diagram exposed") } var wg sync.WaitGroup for i := 0; i < 8; i++ { wg.Add(1) go func() { defer wg.Done(); _ = s.safeHTMLProjection(r, input, "/guide", true) }() } wg.Wait() if strings.Contains(s.safeHTMLProjection(r, `
`+strings.Repeat("A", 33<<10)+`
`, "/guide", true), "/:/compile/diagram/") { t.Fatal("oversized diagram enqueued") } }