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:
Anya Tokugawa committed 2026-09-27 14:44:43 +09:00
1 parent 288e6e94ac
commit c1175d389f
699 files changed
+110197 -4117

No files matched your search

+212
View File
@@ -0,0 +1,212 @@
package server
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"testing"
"time"
"pageswiki/internal/calendar"
)
func TestCalendarSourceErrorsDoNotExposeSubscriptionURL(t *testing.T) {
for _, cause := range []error{errCalendarSourceBlocked, errCalendarSourceDNS, context.DeadlineExceeded} {
err := calendarSourceRequestError(fmt.Errorf("https://provider.example/private-token: %w", cause))
if strings.Contains(err.Error(), "private-token") || strings.Contains(err.Error(), "https://") {
t.Fatal("subscription URL exposed")
}
if cause == errCalendarSourceBlocked && !strings.Contains(err.Error(), "blocked") {
t.Fatal(err)
}
}
}
type calendarTestTransport func(*http.Request) (*http.Response, error)
func (f calendarTestTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func sourceResponse(code int, body string) *http.Response {
return &http.Response{StatusCode: code, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}
}
func TestCalendarSourceSSRFAndConditionalGET(t *testing.T) {
for _, address := range []string{"127.0.0.1", "::1", "::ffff:127.0.0.1", "10.0.0.1", "192.168.1.1", "169.254.169.254", "100.64.0.1", "2001:db8::1", "198.18.0.1", "64:ff9b::7f00:1"} {
if publicCalendarIP(netip.MustParseAddr(address)) {
t.Fatal("allowed", address)
}
}
if !publicCalendarIP(netip.MustParseAddr("8.8.8.8")) {
t.Fatal("public blocked")
}
for _, u := range []string{"http://example.org/a", "https://user:pass@example.org/a", "https://example.org:22/a", "file:///etc/passwd", "https://localhost/a", "https://[::1]/a", "https://127.0.0.1/a"} {
if validCalendarSourceURL(u) {
t.Fatal(u)
}
}
c := calendar.Calendar{Source: &calendar.Source{URL: "https://source.example/feed.ics", ETag: `"etag"`, Success: 1}}
client := &http.Client{Transport: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
if r.Header.Get("If-None-Match") != `"etag"` {
t.Fatal(r.Header)
}
return sourceResponse(304, ""), nil
})}
_, _, _, unchanged, e := fetchCalendarSource(context.Background(), client, c)
if e != nil || !unchanged {
t.Fatal(unchanged, e)
}
}
func TestFetchCalendarSourceOutlookProjection(t *testing.T) {
feed := "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Microsoft Corporation//Outlook//EN\r\nMETHOD:PUBLISH\r\nX-WR-CALNAME:Synthetic\r\n" +
"BEGIN:VTIMEZONE\r\nTZID:Tokyo Standard Time\r\nBEGIN:STANDARD\r\nDTSTART:16010101T000000\r\nTZOFFSETFROM:+0900\r\nTZOFFSETTO:+0900\r\nRRULE:FREQ=YEARLY;BYMONTH=1;BYDAY=1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n" +
"BEGIN:VEVENT\r\nUID:synthetic\r\nDTSTAMP:20260901T000000Z\r\nDTSTART;TZID=\"Tokyo Standard Time\":20260922T090000\r\nDTEND;TZID=\"Tokyo Standard Time\":20260922T100000\r\nRRULE:FREQ=DAILY;COUNT=2\r\nSUMMARY:Synthetic\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
client := &http.Client{Transport: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
response := sourceResponse(200, feed)
response.Header.Set("ETag", `"outlook"`)
return response, nil
})}
c := calendar.Calendar{Source: &calendar.Source{URL: "https://outlook.example/calendar.ics"}}
events, etag, _, unchanged, err := fetchCalendarSource(context.Background(), client, c)
if err != nil || unchanged || etag != `"outlook"` || len(events) != 2 {
t.Fatal(len(events), etag, unchanged, err)
}
if events[0].Start.Format(time.RFC3339) != "2026-09-22T00:00:00Z" {
t.Fatal(events[0].Start)
}
}
func TestCalendarCalDAVETagsAndCredentialIsolation(t *testing.T) {
s, token, domain := configFixture(t)
s.registerCalendarRoutes()
repo, e := s.calendars()
if e != nil {
t.Fatal(e)
}
owner, e := s.store.PrincipalForSession(domain, token)
if e != nil {
t.Fatal(e)
}
c, e := repo.Create(context.Background(), domain, owner.UUID, "Remote", "remote")
if e != nil {
t.Fatal(e)
}
cipher, e := s.store.SealCalendarCredential(domain, c.ID, "remote-user", "secret-password")
if e != nil {
t.Fatal(e)
}
if strings.Contains(cipher, "secret-password") {
t.Fatal("plaintext secret")
}
if _, _, e = s.store.OpenCalendarCredential(domain, "wrong-calendar", cipher); e == nil {
t.Fatal("credential context bypass")
}
c, e = repo.SetSource(context.Background(), domain, c.ID, owner.UUID, c.Version, calendar.Source{Kind: "caldav", URL: "https://source.example/remote/", IntervalSeconds: 60, Enabled: true, Credential: cipher})
if e != nil {
t.Fatal(e)
}
event, e := calendar.Build("", calendar.Event{UID: "remote-event", Summary: "Remote", Start: time.Now().UTC().Truncate(time.Hour), End: time.Now().UTC().Truncate(time.Hour).Add(time.Hour)})
if e != nil {
t.Fatal(e)
}
queries, multigets := 0, 0
client := &http.Client{Transport: calendarAuthTransport{origin: c.Source.URL, user: "remote-user", password: "secret-password", base: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
user, password, ok := r.BasicAuth()
if !ok || user != "remote-user" || password != "secret-password" {
t.Fatal("Basic auth missing")
}
b, _ := io.ReadAll(r.Body)
withData := strings.Contains(string(b), "calendar-multiget")
if withData {
multigets++
} else {
queries++
}
row := eventDAVRow(c, event)
row.href = "/remote/a.ics"
wanted := []davNode{{XMLName: xml.Name{Space: davNS, Local: "getetag"}}}
if withData {
wanted = append(wanted, davNode{XMLName: xml.Name{Space: calNS, Local: "calendar-data"}})
}
w := httptest.NewRecorder()
writeDAV(w, []davResponse{row}, wanted)
return sourceResponse(207, w.Body.String()), nil
})}}
events, resources, e := fetchCalDAVSource(context.Background(), client, repo, domain, c)
if e != nil || len(events) != 1 {
t.Fatal(events, e)
}
if e = repo.ApplySource(context.Background(), domain, c, events, "", "", false, nil, resources); e != nil {
t.Fatal(e)
}
c, _ = repo.Get(domain, c.ID, owner.UUID)
events, _, e = fetchCalDAVSource(context.Background(), client, repo, domain, c)
if e != nil || len(events) != 1 || queries != 2 || multigets != 1 {
t.Fatal(queries, multigets, e)
}
req, _ := http.NewRequest("GET", "https://other.example/remote/", nil)
if _, e = client.Transport.RoundTrip(req); e == nil {
t.Fatal("credential forwarded to another origin")
}
w := calendarRequest(s, token, "GET", calendarAPI+"calendars", "", "")
if w.Code != 200 || strings.Contains(w.Body.String(), cipher) || strings.Contains(w.Body.String(), "secret-password") || strings.Contains(w.Body.String(), "/remote/a.ics") {
t.Fatal("source secret/cache exposed", w.Code)
}
var payload struct{ Calendars []calendar.Calendar }
if e = json.Unmarshal(w.Body.Bytes(), &payload); e != nil || len(payload.Calendars) != 1 || !payload.Calendars[0].Source.HasCredential {
t.Fatal(e, w.Body.String())
}
}
func TestCalendarSourceAPIQueueAndDisable(t *testing.T) {
s, token, domain := configFixture(t)
s.registerCalendarRoutes()
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
owner, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
c, err := repo.Create(context.Background(), domain, owner.UUID, "Import", "import")
if err != nil {
t.Fatal(err)
}
endpoint := calendarAPI + "calendars/" + c.ID + "/source"
body := func(version string, enabled bool, interval int) string {
b, _ := json.Marshal(map[string]any{"version": version, "url": "https://source.example/feed.ics", "kind": "ics", "enabled": enabled, "interval_seconds": interval})
return string(b)
}
if w := calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 59), ""); w.Code != 422 {
t.Fatal(w.Code, w.Body.String())
}
w := calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 600), "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
if w = calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 600), ""); w.Code != 412 {
t.Fatal("stale config accepted", w.Code)
}
if w = calendarRequest(s, token, "POST", endpoint, "{}", ""); w.Code != 202 {
t.Fatal(w.Code, w.Body.String())
}
var jobs int
if err = s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, calendarSyncType).Scan(&jobs); err != nil || jobs != 1 {
t.Fatal("queue deduplication", jobs, err)
}
c, err = repo.Get(domain, c.ID, owner.UUID)
if err != nil {
t.Fatal(err)
}
if w = calendarRequest(s, token, "PUT", endpoint, body(c.Version, false, 600), ""); w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
if w = calendarRequest(s, token, "POST", endpoint, "{}", ""); w.Code != 409 {
t.Fatal("disabled sync accepted", w.Code)
}
}