Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,314 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func searchServerFixture(t *testing.T) (*Server, string, string) {
|
||||
t.Helper()
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/search", s.handleSearch)
|
||||
for _, item := range []struct{ path, body, filename string }{
|
||||
{"/docs/nebula guide?#%", `<h1>Nebula Handbook</h1><p>Introduction to the NEBULA and nearby stars.</p><script>PRIVATE_SCRIPT</script><p><img src=x onerror=alert(1)></p>`, "nebula <svg onload=alert(1)>.html"},
|
||||
{"/docs/hidden", "nebula SECRET_EXCERPT", "SECRET_FILENAME.txt"},
|
||||
} {
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, item.path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.SaveRevisionContent(object, []byte(item.body), "", "en", "text/html", item.filename); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domain, token)
|
||||
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.domain_uuid=? AND m.principal_uuid=? LIMIT 1`, domain, principal.UUID).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)
|
||||
}
|
||||
for i, path := range []string{"/", "/docs/nebula guide?#%"} {
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,'read','path',?,1)`, "search-read-"+string(rune('0'+i)), role, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return s, token, domain
|
||||
}
|
||||
|
||||
func TestSearchSuggestionsDirectACLAndHeading(t *testing.T) {
|
||||
s, token, _ := searchServerFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/search/suggest", s.handleSearchSuggestAPI)
|
||||
response := configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=handbook", nil)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("suggest: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Items []searchSuggestionItem `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Items) != 1 || payload.Items[0].Label != "Nebula Handbook" || payload.Items[0].Path != "/docs/nebula guide?#%" || payload.Items[0].Href != "/docs/nebula%20guide%3F%23%25" {
|
||||
t.Fatalf("suggestions: %#v", payload.Items)
|
||||
}
|
||||
response = configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=Nebula%20Handbook", nil)
|
||||
if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &payload) != nil || len(payload.Items) == 0 || !payload.Items[0].Exact {
|
||||
t.Fatalf("exact suggestion: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
response = configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=SECRET", nil)
|
||||
payload.Items = nil
|
||||
if response.Code != http.StatusOK || json.Unmarshal(response.Body.Bytes(), &payload) != nil || len(payload.Items) != 0 {
|
||||
t.Fatalf("hidden suggestion leaked: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
response = configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=n", nil)
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"items":[]`) {
|
||||
t.Fatalf("short suggestion query: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchResultsSafeContextAndACL(t *testing.T) {
|
||||
s, token, _ := searchServerFixture(t)
|
||||
response := configRequest(s, token, "GET", "/v/search?q=nebula&scope=/", nil)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("search: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
body := response.Body.String()
|
||||
for _, required := range []string{`class="search-result-name"`, `class="search-result-path"`, `class="search-result-excerpt"`, `data-match-source="path"`, `data-match-source="filename"`, `data-match-source="body"`, `<mark>NEBULA</mark>`, `/docs/nebula%20guide%3F%23%25`, `<svg onload=alert(1)>`, `data-search-view="list"`} {
|
||||
if !strings.Contains(body, required) {
|
||||
t.Errorf("missing search presentation %q", required)
|
||||
}
|
||||
}
|
||||
for _, forbidden := range []string{"SECRET_EXCERPT", "SECRET_FILENAME", "/docs/hidden", "PRIVATE_SCRIPT", "<svg onload=", "<img src=x"} {
|
||||
if strings.Contains(body, forbidden) {
|
||||
t.Errorf("unsafe or unreadable search data leaked: %s", forbidden)
|
||||
}
|
||||
}
|
||||
if response := configRequest(s, token, "GET", "/v/search?q=SECRET&scope=/", nil); response.Code != 200 || !strings.Contains(response.Body.String(), `class="search-empty"`) || strings.Contains(response.Body.String(), "SECRET_EXCERPT") {
|
||||
t.Fatal("hidden-only results differ from an empty result set")
|
||||
}
|
||||
if response := configRequest(s, token, "GET", "/v/search?q=nebula&scope=/docs/hidden", nil); response.Code != 403 {
|
||||
t.Fatalf("unreadable scope accepted: %d", response.Code)
|
||||
}
|
||||
if response := configRequest(s, token, "GET", "/v/search?q="+strings.Repeat("a", 513), nil); response.Code != 400 {
|
||||
t.Fatalf("oversized query accepted: %d", response.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchExcerptTextAndHighlight(t *testing.T) {
|
||||
value := searchExcerpt(`<p>Hello & <b>nebula</b>.</p><script>evil()</script><style>secret</style>`, "text/html", "nebula")
|
||||
if strings.Contains(value, "evil") || strings.Contains(value, "secret") || strings.Contains(value, "<") || !strings.Contains(value, "Hello & nebula") {
|
||||
t.Fatalf("HTML text extraction: %q", value)
|
||||
}
|
||||
long := strings.Repeat("遠い星 ", 100) + "NEBULA nearby context " + strings.Repeat("stars ", 100)
|
||||
value = searchExcerpt(long, "text/plain", "nebula")
|
||||
if !strings.Contains(value, "NEBULA") || len([]rune(value)) > 265 {
|
||||
t.Fatalf("context window: %q", value)
|
||||
}
|
||||
parts := highlightSearchText(`日本語 NEBULA <script>`, "日本 nebula")
|
||||
var joined strings.Builder
|
||||
matched := 0
|
||||
for _, part := range parts {
|
||||
joined.WriteString(part.Text)
|
||||
if part.Match {
|
||||
matched++
|
||||
}
|
||||
}
|
||||
if joined.String() != `日本語 NEBULA <script>` || matched != 2 {
|
||||
t.Fatalf("Unicode highlights: %#v", parts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchResultsOmittedExcerptRetainsBodyMatch(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/search", s.handleSearch)
|
||||
const pagePath = "/docs/large-file"
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, pagePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Match only in the body, beyond the bounded snippet input. The result
|
||||
// must remain visible without reading or rendering the large excerpt.
|
||||
body := strings.Repeat("background ", 7000) + "nebula"
|
||||
if _, err := s.store.SaveRevisionContent(object, []byte(body), "", "en", "text/plain", "large-file.txt"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, locale := range []string{"ja", "en"} {
|
||||
if err := s.store.SetConfigInDomain(domain, "locale", locale); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := configRequest(s, token, "GET", "/v/search?q=nebula&scope=/", nil)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("search: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
page := response.Body.String()
|
||||
for _, required := range []string{`data-search-view="list"`, pagePath, "large-file.txt"} {
|
||||
if !strings.Contains(page, required) {
|
||||
t.Errorf("omitted excerpt lost result context %q", required)
|
||||
}
|
||||
}
|
||||
for _, required := range []string{`class="search-excerpt-omitted"`, `data-match-source="body"`, translate(locale, "search.excerpt_omitted")} {
|
||||
if count := strings.Count(page, required); count != 3 {
|
||||
t.Errorf("%s: each of the three views should show %q; got %d", locale, required, count)
|
||||
}
|
||||
}
|
||||
if strings.Contains(page, `class="search-result-excerpt"`) || strings.Contains(page, "background background") {
|
||||
t.Error("omitted source text was rendered as an excerpt")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchFailureResponses(t *testing.T) {
|
||||
s, token, _ := searchServerFixture(t)
|
||||
query, scope := `nebula <img src=x>`, "/docs/space ?#%"
|
||||
const secret = "PRIVATE_DATABASE_CONNECTION_DETAIL"
|
||||
for _, test := range []struct {
|
||||
name, method, retryAfter, title string
|
||||
err error
|
||||
status int
|
||||
}{
|
||||
{"deadline", "GET", "5", "search.timeout_title", fmt.Errorf("%s: %w", secret, context.DeadlineExceeded), http.StatusServiceUnavailable},
|
||||
{"database", "GET", "", "search.failed_title", errors.New(secret), http.StatusInternalServerError},
|
||||
{"deadline head", "HEAD", "5", "", context.DeadlineExceeded, http.StatusServiceUnavailable},
|
||||
{"database head", "HEAD", "", "", errors.New(secret), http.StatusInternalServerError},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
r := httptest.NewRequest(test.method, "http://localhost/v/search", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
response := httptest.NewRecorder()
|
||||
s.renderSearchFailure(response, r, query, scope, "street", "encoded ?#%", test.err)
|
||||
if response.Code != test.status || response.Header().Get("Retry-After") != test.retryAfter {
|
||||
t.Fatalf("status=%d, Retry-After=%q", response.Code, response.Header().Get("Retry-After"))
|
||||
}
|
||||
if response.Header().Get("Content-Type") != "text/html; charset=utf-8" || response.Header().Get("Cache-Control") != "private, no-store" {
|
||||
t.Fatalf("error response headers: %v", response.Header())
|
||||
}
|
||||
body := response.Body.String()
|
||||
if test.method == "HEAD" {
|
||||
if body != "" {
|
||||
t.Fatal("HEAD error rendered a body")
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, required := range []string{`body class="mode-search"`, `class="search-error" role="alert"`, translate("ja", test.title), `name="q" value="` + html.EscapeString(query) + `"`, `name="scope" value="` + html.EscapeString(scope) + `"`} {
|
||||
if !strings.Contains(body, required) {
|
||||
t.Errorf("missing themed, localized error element: %q", required)
|
||||
}
|
||||
}
|
||||
retry := "/v/search?" + url.Values{"q": {query}, "scope": {scope}, "locality": {"street"}, "locality_id": {"encoded ?#%"}}.Encode()
|
||||
_, href, found := strings.Cut(body, `class="search-retry" href="`)
|
||||
href, _, _ = strings.Cut(href, `"`)
|
||||
if !found || html.UnescapeString(href) != retry {
|
||||
t.Error("retry URL did not preserve the query, scope and locality")
|
||||
}
|
||||
for _, forbidden := range []string{secret, "context deadline exceeded", `<img src=x>`, `class="search-empty"`, `class="search-unified-results"`} {
|
||||
if strings.Contains(body, forbidden) {
|
||||
t.Errorf("error page leaked data or rendered a misleading result: %q", forbidden)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchFailureCanceledRequestSkipsRendering(t *testing.T) {
|
||||
// No Store or templates: a canceled request must not start render-time reads.
|
||||
s := &Server{}
|
||||
for _, requestCanceled := range []bool{false, true} {
|
||||
r := httptest.NewRequest("GET", "http://localhost/v/search?q=test", nil)
|
||||
err := context.Canceled
|
||||
if requestCanceled {
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
cancel()
|
||||
r = r.WithContext(ctx)
|
||||
err = errors.New("unrelated query error")
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
s.renderSearchFailure(response, r, "test", "/", "", "", err)
|
||||
if response.Body.Len() != 0 || len(response.Header()) != 0 {
|
||||
t.Fatal("canceled search wrote a response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDatabaseFailureUsesGenericPage(t *testing.T) {
|
||||
s, token, _ := searchServerFixture(t)
|
||||
if _, err := s.store.DB.Exec("DROP TABLE latest_revision_index"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := configRequest(s, token, "GET", "/v/search?q=nebula&scope=/", nil)
|
||||
if response.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("search database failure: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
body := response.Body.String()
|
||||
if !strings.Contains(body, `class="search-error"`) || !strings.Contains(body, translate("ja", "search.failed_title")) {
|
||||
t.Fatal("database failure did not render a themed search error")
|
||||
}
|
||||
for _, forbidden := range []string{"SQL logic error", "no such table", "latest_revision_index", "context deadline exceeded"} {
|
||||
if strings.Contains(body, forbidden) {
|
||||
t.Errorf("database detail leaked: %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchResultsChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := searchServerFixture(t)
|
||||
seedSearchCity(t, s, token, domain)
|
||||
script, err := os.ReadFile("internal/server/testdata/search-results.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/v/search" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, value := range rec.Header() {
|
||||
w.Header()[key] = value
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = io.WriteString(w, strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, view := range []struct{ name, width, height string }{{"desktop", "1440", "1000"}, {"mobile", "390", "844"}} {
|
||||
t.Run(view.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
||||
defer cancel()
|
||||
command := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/search?q="+url.QueryEscape("nebula"), t.TempDir())
|
||||
screenshot := ""
|
||||
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
screenshot = filepath.Join(dir, "search-results-"+view.name+".png")
|
||||
}
|
||||
command.Args = append(command.Args, screenshot, view.width, view.height)
|
||||
if out, err := command.CombinedOutput(); err != nil {
|
||||
t.Fatalf("browser: %v\n%s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user