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

+1 -1
View File
@@ -8,7 +8,7 @@ const (
SchemePath Scheme = "path"
// UUID is described 'uuid:123e4567-e89b-42d3-a456-426614174000'
SchemeUUID Scheme = "uuid"
// Revision described 'rev:1'
// Revision is described 'rev:018f6b5d-...'
SchemeRevision Scheme = "rev"
// Revision is described 'sha256:9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa'
SchemeSHA256 Scheme = "sha256"
+247
View File
@@ -0,0 +1,247 @@
package calendar
import (
"context"
"database/sql"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
_ "modernc.org/sqlite"
)
func fixture(t *testing.T) (*Repository, string, string, Calendar) {
t.Helper()
db, e := sql.Open("sqlite", filepath.Join(t.TempDir(), "cache.db"))
if e != nil {
t.Fatal(e)
}
db.SetMaxOpenConns(1)
t.Cleanup(func() { db.Close() })
r, e := New(t.TempDir(), db)
if e != nil {
t.Fatal(e)
}
domain, actor := uuid.NewString(), uuid.NewString()
c, e := r.Create(context.Background(), domain, actor, "予定")
if e != nil {
t.Fatal(e)
}
return r, domain, actor, c
}
func sample(t *testing.T, uid string, start, end string, allDay bool) Event {
t.Helper()
a, e := time.Parse(time.RFC3339, start)
if e != nil {
t.Fatal(e)
}
b, e := time.Parse(time.RFC3339, end)
if e != nil {
t.Fatal(e)
}
v, e := Build("", Event{UID: uid, Summary: "会議, 東京; test\n次行", Start: a, End: b, AllDay: allDay})
if e != nil {
t.Fatal(e)
}
return v
}
func TestCalendarICalendarValidation(t *testing.T) {
v := sample(t, "a@example.org", "2026-09-30T23:00:00Z", "2026-10-01T02:00:00Z", false)
long := v
long.Summary = strings.Repeat("日本語", 100)
long, e := Build(v.Data, long)
if e != nil {
t.Fatal(e)
}
for _, line := range strings.Split(long.Data, "\r\n") {
if len(line) > 75 {
t.Fatal("unfolded long output line")
}
}
if !strings.Contains(v.Data, "\r\n") || !strings.Contains(v.Data, `SUMMARY:会議\, 東京\; test\n次行`) {
t.Fatal(v.Data)
}
for _, data := range []string{
strings.Replace(v.Data, "DTSTART:", "DTSTART;TZID=Asia/Tokyo:", 1),
strings.Replace(v.Data, "20260930T230000Z", "20260930T230000", 1),
strings.Replace(v.Data, "END:VEVENT", "RRULE:FREQ=DAILY\r\nEND:VEVENT", 1),
strings.Replace(v.Data, "END:VEVENT", "UID:duplicate\r\nEND:VEVENT", 1),
strings.Replace(v.Data, "20261001T020000Z", "20260930T220000Z", 1),
strings.Replace(v.Data, "VERSION:2.0", "VERSION:1.0", 1),
v.Data + v.Data,
} {
if _, e := Parse(data); !errors.Is(e, ErrInvalid) {
t.Fatalf("accepted invalid input: %v\n%s", e, data)
}
}
extended := strings.Replace(v.Data, "END:VEVENT", "X-PAGESWIKI-TEST:keep-me\r\nEND:VEVENT", 1)
v, e = Parse(extended)
if e != nil {
t.Fatal(e)
}
v.Summary = "changed"
changed, e := Build(v.Data, v)
if e != nil || !strings.Contains(changed.Data, "X-PAGESWIKI-TEST:keep-me") {
t.Fatal(e, changed.Data)
}
}
func TestCalendarMonthlyCommitIndexAndACL(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
v := sample(t, "stable", "2026-09-30T23:00:00Z", "2026-10-01T02:00:00Z", false)
if _, e := r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
lo := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
items, e := r.Query(ctx, domain, c.ID, actor, lo, lo.Add(24*time.Hour))
if e != nil || len(items) != 1 {
t.Fatal(items, e)
}
if _, e = r.Event(ctx, domain, c.ID, "", v.UID); !errors.Is(e, ErrMissing) {
t.Fatal("anonymous read", e)
}
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); !errors.Is(e, ErrConflict) {
t.Fatal("duplicate overwrite", e)
}
moved := sample(t, v.UID, "2026-11-01T01:00:00Z", "2026-11-01T02:00:00Z", false)
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, v.ETag, &moved); e != nil {
t.Fatal(e)
}
// Simulated lost/discarded cache must reconstruct only the committed manifest.
if _, e = r.DB.Exec(`DELETE FROM calendar_event_cache;DELETE FROM calendar_month_cache;DELETE FROM calendar_index_state`); e != nil {
t.Fatal(e)
}
got, e := r.Event(ctx, domain, c.ID, actor, v.UID)
if e != nil || !got.Start.Equal(moved.Start) {
t.Fatal(got, e)
}
c, e = r.Get(domain, c.ID, actor)
if e != nil {
t.Fatal(e)
}
if len(c.Months) != 1 || c.Months["2026-11"] == "" {
t.Fatal(c.Months)
}
other := uuid.NewString()
c, e = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, true, map[string]string{other: "read"})
if e != nil {
t.Fatal(e)
}
if _, e = r.Event(ctx, domain, c.ID, "", v.UID); e != nil {
t.Fatal(e)
}
if _, e = r.Change(ctx, domain, c.ID, other, v.UID, moved.ETag, nil); !errors.Is(e, ErrForbidden) {
t.Fatal(e)
}
if _, e = r.Settings(ctx, domain, c.ID, other, c.Version, "bad", false, nil); !errors.Is(e, ErrForbidden) {
t.Fatal(e)
}
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, moved.ETag, nil); e != nil {
t.Fatal(e)
}
if _, e = r.Event(ctx, domain, c.ID, actor, v.UID); !errors.Is(e, ErrMissing) {
t.Fatal(e)
}
}
func TestCalendarConcurrentConditionalWrite(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
v := sample(t, "race", "2026-09-20T00:00:00Z", "2026-09-21T00:00:00Z", true)
if _, e := r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
var wg sync.WaitGroup
results := make(chan error, 2)
for _, title := range []string{"one", "two"} {
wg.Add(1)
go func(title string) {
defer wg.Done()
n := v
n.Summary = title
n, e := Build(v.Data, n)
if e == nil {
_, e = r.Change(ctx, domain, c.ID, actor, v.UID, v.ETag, &n)
}
results <- e
}(title)
}
wg.Wait()
close(results)
ok, conflict := 0, 0
for e := range results {
if e == nil {
ok++
} else if errors.Is(e, ErrConflict) {
conflict++
} else {
t.Fatal(e)
}
}
if ok != 1 || conflict != 1 {
t.Fatal(ok, conflict)
}
}
func TestCalendarDeleteRemovesCanonicalFilesCacheAndDefaults(t *testing.T) {
r, domain, actor, created := fixture(t)
ctx := context.Background()
event := sample(t, "delete-me", "2026-09-22T09:00:00Z", "2026-09-22T10:00:00Z", false)
if _, err := r.Change(ctx, domain, created.ID, actor, event.UID, "*", &event); err != nil {
t.Fatal(err)
}
if err := r.SaveDefaults(ctx, domain, actor, []string{created.ID}); err != nil {
t.Fatal(err)
}
fresh, err := r.Get(domain, created.ID, actor)
if err != nil {
t.Fatal(err)
}
if err = r.Delete(ctx, domain, created.ID, uuid.NewString(), fresh.Version); !errors.Is(err, ErrForbidden) {
t.Fatal("non-owner delete", err)
}
if err = r.Delete(ctx, domain, created.ID, actor, created.Version); !errors.Is(err, ErrConflict) {
t.Fatal("stale delete", err)
}
dir, _ := r.dir(domain, created.ID)
if err = r.Delete(ctx, domain, created.ID, actor, fresh.Version); err != nil {
t.Fatal(err)
}
if _, err = os.Stat(dir); !os.IsNotExist(err) {
t.Fatal("canonical calendar remains", err)
}
if _, err = r.Get(domain, created.ID, actor); !errors.Is(err, ErrMissing) {
t.Fatal("deleted calendar readable", err)
}
defaults, err := r.Defaults(domain, actor)
if err != nil || len(defaults) != 0 {
t.Fatal("deleted default retained", defaults, err)
}
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
var count int
if err = r.DB.QueryRow(`SELECT COUNT(*) FROM `+table+` WHERE domain=? AND calendar=?`, domain, created.ID).Scan(&count); err != nil || count != 0 {
t.Fatal(table, count, err)
}
}
}
func TestCalendarSnapshotsVerified(t *testing.T) {
r, d, a, c := fixture(t)
v := sample(t, "broken", "2026-09-01T00:00:00Z", "2026-09-02T00:00:00Z", true)
if _, e := r.Change(context.Background(), d, c.ID, a, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
c, _ = r.Get(d, c.ID, a)
dir, _ := r.dir(d, c.ID)
for m, h := range c.Months {
if e := os.WriteFile(filepath.Join(dir, m+"."+h+".ics"), []byte("corrupt"), 0600); e != nil {
t.Fatal(e)
}
}
if _, e := r.Event(context.Background(), d, c.ID, a, v.UID); e == nil {
t.Fatal("corrupt snapshot accepted")
}
}
+135
View File
@@ -0,0 +1,135 @@
package calendar
import (
"context"
"database/sql"
"encoding/base64"
"strings"
)
func DAVFilename(c Calendar, uid string) string {
if name := c.DAVNames[uid]; name != "" {
return name
}
return base64.RawURLEncoding.EncodeToString([]byte(uid)) + ".ics"
}
func ValidDAVFilename(name string) bool {
if len(name) < 5 || len(name) > 700 || !strings.HasSuffix(name, ".ics") {
return false
}
for _, ch := range name {
if !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '-' || ch == '_' || ch == '.') {
return false
}
}
return true
}
func DAVUID(c Calendar, name string) (string, error) {
if !ValidDAVFilename(name) {
return "", ErrMissing
}
for uid, n := range c.DAVNames {
if n == name {
return uid, nil
}
}
b, err := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(name, ".ics"))
if err != nil || len(b) == 0 || len(b) > 512 {
return "", ErrMissing
}
return string(b), nil
}
// DAVSnapshot is complete or fails; a truncated listing can cause client deletes.
// The metadata lock keeps href mappings and event generations in one snapshot.
func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string, withData bool) (Calendar, []Event, error) {
c, _, unlock, err := r.locked(ctx, domain, id, actor)
if err != nil {
return c, nil, err
}
defer unlock()
cols := "uid,etag,''"
if withData {
cols = "uid,etag,data"
}
rows, err := r.DB.QueryContext(ctx, `SELECT `+cols+` FROM calendar_event_cache WHERE domain=? AND calendar=? ORDER BY uid LIMIT 10001`, domain, id)
if err != nil {
return c, nil, err
}
defer rows.Close()
items := []Event{}
size := 0
for rows.Next() {
var item Event
if err = rows.Scan(&item.UID, &item.ETag, &item.Data); err != nil {
return c, nil, err
}
size += len(item.Data) + len(item.UID) + len(item.ETag)
if len(items) >= 10000 || size > 32<<20 {
return c, nil, ErrLimit
}
items = append(items, item)
}
return c, items, rows.Err()
}
// ChangeDAV reserves resource identities in the same manifest commit as the
// monthly snapshots. Existing resources still require an exact strong ETag.
func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, match string, item *Event, authorize func() error) (Event, bool, error) {
if !ValidDAVFilename(name) {
return Event{}, false, ErrInvalid
}
c, dir, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.CanWrite(actor) })
if err != nil {
return Event{}, false, err
}
defer unlock()
if authorize != nil {
if err = authorize(); err != nil {
return Event{}, false, ErrForbidden
}
}
uid, _ := DAVUID(c, name)
var exists bool
if uid != "" {
if err = r.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?)`, domain, id, uid).Scan(&exists); err != nil {
return Event{}, false, err
}
}
if exists {
if match == "" {
return Event{}, false, ErrPreconditionRequired
}
if item != nil && item.UID != uid {
return Event{}, false, ErrConflict
}
} else {
if item == nil {
return Event{}, false, ErrMissing
}
if match != "" && match != "*" {
return Event{}, false, ErrConflict
}
uid = item.UID
var tag string
err = r.DB.QueryRowContext(ctx, `SELECT etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&tag)
if err == nil {
return Event{}, false, ErrConflict
}
if err != sql.ErrNoRows {
return Event{}, false, err
}
if len(c.DAVNames) >= 10000 {
return Event{}, false, ErrLimit
}
if c.DAVNames == nil {
c.DAVNames = map[string]string{}
}
c.DAVNames[uid] = name
match = "*"
}
result, err := r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
return result, !exists, err
}
+111
View File
@@ -0,0 +1,111 @@
package calendar
import (
"context"
"errors"
"sync"
"testing"
"github.com/google/uuid"
)
func TestCalendarDAVIdentityAtomicAndRebuild(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
item := sample(t, "client@example.test", "2026-10-01T09:00:00Z", "2026-10-01T10:00:00Z", false)
var wg sync.WaitGroup
results := make(chan error, 2)
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _, err := r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", "", &item, nil)
results <- err
}()
}
wg.Wait()
close(results)
success := 0
for err := range results {
if err == nil {
success++
} else if !errors.Is(err, ErrPreconditionRequired) {
t.Fatal(err)
}
}
if success != 1 {
t.Fatal(success)
}
// Filename metadata survives an index rebuild and a settings change.
c, err := r.Get(domain, c.ID, actor)
if err != nil {
t.Fatal(err)
}
if _, err = r.Settings(ctx, domain, c.ID, actor, c.Version, "Renamed", false, c.Members); err != nil {
t.Fatal(err)
}
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
if _, err = r.DB.Exec("DELETE FROM " + table); err != nil {
t.Fatal(err)
}
}
c, items, err := r.DAVSnapshot(ctx, domain, c.ID, actor, true)
if err != nil || len(items) != 1 || DAVFilename(c, items[0].UID) != "arbitrary.ics" || items[0].ETag != item.ETag {
t.Fatal(err, items, c.DAVNames)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "other.ics", "", &item, nil); err != ErrConflict {
t.Fatal("duplicate UID", err)
}
other := sample(t, "other@example.test", "2026-10-01T09:00:00Z", "2026-10-01T10:00:00Z", false)
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", item.ETag, &other, nil); err != ErrConflict {
t.Fatal("identity changed", err)
}
member := uuid.NewString()
c, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{member: "read"})
if err != nil {
t.Fatal(err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, member, "arbitrary.ics", item.ETag, &item, nil); err != ErrForbidden {
t.Fatal("read member wrote", err)
}
c, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{member: "write"})
if err != nil {
t.Fatal(err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, member, "arbitrary.ics", item.ETag, &item, nil); err != nil {
t.Fatal(err)
}
c, err = r.Get(domain, c.ID, actor)
if err != nil {
t.Fatal(err)
}
if _, err = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{}); err != nil {
t.Fatal(err)
}
if _, _, err = r.DAVSnapshot(ctx, domain, c.ID, member, true); err != ErrMissing {
t.Fatal("revoked ACL leaked", err)
}
if _, _, err = r.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary.ics", item.ETag, nil, nil); err != nil {
t.Fatal(err)
}
c, err = r.Get(domain, c.ID, actor)
if err != nil || len(c.DAVNames) != 0 {
t.Fatal("alias retained after delete", err, c.DAVNames)
}
}
func TestCalendarDAVSnapshotLimit(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
if _, _, err := r.DAVSnapshot(ctx, domain, c.ID, actor, false); err != nil {
t.Fatal(err)
}
// Synthetic disposable cache only; exceed the collection bound deliberately.
_, err := r.DB.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<10001) INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,end,etag,data) SELECT ?,?,CAST(x AS TEXT),'2026-10',0,0,'"test"','' FROM n`, domain, c.ID)
if err != nil {
t.Fatal(err)
}
if _, items, err := r.DAVSnapshot(ctx, domain, c.ID, actor, false); err != ErrLimit || items != nil {
t.Fatal("returned truncated collection", len(items), err)
}
}
+719
View File
@@ -0,0 +1,719 @@
package calendar
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"sort"
"strconv"
"strings"
"time"
_ "time/tzdata"
"unicode/utf8"
ical "github.com/emersion/go-ical"
"github.com/teambition/rrule-go"
)
type feedTransition struct {
wall time.Time
offset int
}
type feedZone struct {
id string
location *time.Location
transitions []feedTransition
fallback int
}
func validateExternalRule(option *rrule.ROption, timezone bool) error {
if timezone && option.Freq != rrule.YEARLY {
return fmt.Errorf("%w: VTIMEZONE recurrence must be yearly", ErrInvalid)
}
if option.Freq > rrule.DAILY || len(option.Byhour) > 0 || len(option.Byminute) > 0 || len(option.Bysecond) > 0 {
return fmt.Errorf("%w: sub-daily or expanded-time RRULE is not supported", ErrInvalid)
}
return nil
}
var windowsZones = map[string]string{
"UTC": "UTC", "Tokyo Standard Time": "Asia/Tokyo",
"Korea Standard Time": "Asia/Seoul", "China Standard Time": "Asia/Shanghai",
"Singapore Standard Time": "Asia/Singapore", "India Standard Time": "Asia/Kolkata",
"GMT Standard Time": "Europe/London", "W. Europe Standard Time": "Europe/Berlin",
"Central Europe Standard Time": "Europe/Budapest", "Romance Standard Time": "Europe/Paris",
"Eastern Standard Time": "America/New_York", "Central Standard Time": "America/Chicago",
"Mountain Standard Time": "America/Denver", "Pacific Standard Time": "America/Los_Angeles",
"AUS Eastern Standard Time": "Australia/Sydney", "New Zealand Standard Time": "Pacific/Auckland",
}
func parseWall(value string) (time.Time, error) {
value = strings.TrimSuffix(value, "Z")
layout := "20060102T150405"
if len(value) == 8 {
layout = "20060102"
}
return time.ParseInLocation(layout, value, time.UTC)
}
func parseUTCOffset(raw string) (int, error) {
if len(raw) != 5 && len(raw) != 7 || raw[0] != '+' && raw[0] != '-' {
return 0, fmt.Errorf("%w: invalid TZOFFSET", ErrInvalid)
}
h, e1 := strconv.Atoi(raw[1:3])
m, e2 := strconv.Atoi(raw[3:5])
s := 0
var e3 error
if len(raw) == 7 {
s, e3 = strconv.Atoi(raw[5:7])
}
if e1 != nil || e2 != nil || e3 != nil || h > 23 || m > 59 || s > 59 {
return 0, fmt.Errorf("%w: invalid TZOFFSET", ErrInvalid)
}
seconds := h*3600 + m*60 + s
if raw[0] == '-' {
seconds = -seconds
}
return seconds, nil
}
func zoneName(raw string) string { return strings.Trim(strings.TrimSpace(raw), `"`) }
func buildFeedZones(cal *ical.Calendar, from, to time.Time) (map[string]*feedZone, error) {
zones := map[string]*feedZone{}
for _, child := range cal.Children {
if child.Name != "VTIMEZONE" {
continue
}
id, e := child.Props.Text("TZID")
id = zoneName(id)
if e != nil || id == "" || len(id) > 256 || zones[id] != nil {
return nil, fmt.Errorf("%w: invalid or duplicate VTIMEZONE TZID", ErrInvalid)
}
zone := &feedZone{id: id}
iana := id
if mapped := windowsZones[id]; mapped != "" {
iana = mapped
}
if loc, err := time.LoadLocation(iana); err == nil {
zone.location = loc
zones[id] = zone
continue
}
for _, observance := range child.Children {
if observance.Name != "STANDARD" && observance.Name != "DAYLIGHT" {
return nil, fmt.Errorf("%w: unsupported VTIMEZONE component %s", ErrInvalid, observance.Name)
}
startProp, offsetProp := observance.Props.Get("DTSTART"), observance.Props.Get("TZOFFSETTO")
if startProp == nil || offsetProp == nil {
return nil, fmt.Errorf("%w: incomplete VTIMEZONE observance", ErrInvalid)
}
base, err := parseWall(startProp.Value)
if err != nil {
return nil, fmt.Errorf("%w: invalid VTIMEZONE DTSTART", ErrInvalid)
}
offset, err := parseUTCOffset(offsetProp.Value)
if err != nil {
return nil, err
}
if len(zone.transitions) == 0 || base.Before(zone.transitions[0].wall) {
zone.fallback = offset
}
if observance.Props.Get("RDATE") != nil {
return nil, fmt.Errorf("%w: VTIMEZONE RDATE is not yet supported", ErrInvalid)
}
if rule := observance.Props.Get("RRULE"); rule != nil {
option, err := rrule.StrToROption(rule.Value)
if err != nil {
return nil, fmt.Errorf("%w: invalid VTIMEZONE RRULE", ErrInvalid)
}
if err = validateExternalRule(option, true); err != nil {
return nil, err
}
option.Dtstart = base
r, err := rrule.NewRRule(*option)
if err != nil {
return nil, fmt.Errorf("%w: invalid VTIMEZONE RRULE", ErrInvalid)
}
for _, occurrence := range r.Between(from.AddDate(-2, 0, 0), to.AddDate(2, 0, 0), true) {
zone.transitions = append(zone.transitions, feedTransition{wall: occurrence, offset: offset})
}
} else {
zone.transitions = append(zone.transitions, feedTransition{wall: base, offset: offset})
}
}
if len(zone.transitions) == 0 {
return nil, fmt.Errorf("%w: empty VTIMEZONE", ErrInvalid)
}
sort.Slice(zone.transitions, func(i, j int) bool { return zone.transitions[i].wall.Before(zone.transitions[j].wall) })
zones[id] = zone
}
return zones, nil
}
func (z *feedZone) resolve(wall time.Time) time.Time {
if z.location != nil {
return time.Date(wall.Year(), wall.Month(), wall.Day(), wall.Hour(), wall.Minute(), wall.Second(), wall.Nanosecond(), z.location)
}
offset := z.fallback
for _, transition := range z.transitions {
if transition.wall.After(wall) {
break
}
offset = transition.offset
}
return time.Date(wall.Year(), wall.Month(), wall.Day(), wall.Hour(), wall.Minute(), wall.Second(), wall.Nanosecond(), time.FixedZone(z.id, offset))
}
func feedTime(prop *ical.Prop, zones map[string]*feedZone) (time.Time, time.Time, bool, error) {
if prop == nil {
return time.Time{}, time.Time{}, false, fmt.Errorf("%w: missing date property", ErrInvalid)
}
wall, err := parseWall(prop.Value)
if err != nil {
return time.Time{}, time.Time{}, false, fmt.Errorf("%w: invalid %s", ErrInvalid, prop.Name)
}
allDay := prop.ValueType() == ical.ValueDate
if allDay {
return wall, wall, true, nil
}
if strings.HasSuffix(prop.Value, "Z") {
return wall, wall, false, nil
}
tzid := zoneName(prop.Params.Get("TZID"))
if tzid == "" {
return time.Time{}, time.Time{}, false, fmt.Errorf("%w: floating %s has no TZID", ErrInvalid, prop.Name)
}
zone := zones[tzid]
if zone == nil {
iana := tzid
if mapped := windowsZones[tzid]; mapped != "" {
iana = mapped
}
loc, e := time.LoadLocation(iana)
if e != nil {
return time.Time{}, time.Time{}, false, fmt.Errorf("%w: unknown TZID %q", ErrInvalid, tzid)
}
zone = &feedZone{id: tzid, location: loc}
zones[tzid] = zone
}
return zone.resolve(wall).UTC(), wall, false, nil
}
func sanitizeExternalPropertyValue(value string) (string, bool) {
changed := false
var cleaned strings.Builder
for _, r := range value {
if r < 32 && r != '\t' || r == 127 || r >= 0x80 && r <= 0x9f {
changed = true
continue
}
cleaned.WriteRune(r)
}
return cleaned.String(), changed
}
func cloneExternalEvent(source *ical.Component) (*ical.Component, bool) {
dst := ical.NewComponent("VEVENT")
sanitized := false
for name, values := range source.Props {
for _, value := range values {
p := ical.NewProp(name)
cleaned, changed := sanitizeExternalPropertyValue(value.Value)
p.Value = cleaned
sanitized = sanitized || changed
for key, params := range value.Params {
for _, param := range params {
cleaned, changed := sanitizeExternalPropertyValue(param)
sanitized = sanitized || changed
p.Params.Add(key, cleaned)
}
}
dst.Props.Add(p)
}
}
return dst, sanitized
}
func occurrenceUID(uid string, wall time.Time) string {
suffix := "#" + wall.Format("20060102T150405")
if len(uid)+len(suffix) <= 512 {
return uid + suffix
}
sum := sha256.Sum256([]byte(uid + suffix))
return "recurrence-" + hex.EncodeToString(sum[:])
}
func materializeExternal(source *ical.Component, uid string, start, end time.Time, allDay, recurring bool, wall time.Time, zones map[string]*feedZone) (Event, error) {
dst, sanitized := cloneExternalEvent(source)
for _, name := range []string{"RRULE", "RDATE", "EXDATE", "RECURRENCE-ID", "DURATION"} {
dst.Props.Del(name)
}
if recurring {
dst.Props.SetText("X-PAGESWIKI-SOURCE-UID", uid)
uid = occurrenceUID(uid, wall)
}
dst.Props.SetText("UID", uid)
if sanitized {
dst.Props.SetText("X-PAGESWIKI-SANITIZED", "CONTROL-CHARACTER")
}
for _, pair := range []struct {
name string
time time.Time
}{{"DTSTART", start}, {"DTEND", end}} {
if pair.name == "DTEND" && !end.After(start) {
dst.Props.Del("DTEND")
continue
}
p := ical.NewProp(pair.name)
if allDay {
p.SetDate(pair.time.UTC())
} else {
p.SetDateTime(pair.time.UTC())
}
dst.Props.Set(p)
}
for _, name := range []string{"DTSTAMP", "CREATED", "LAST-MODIFIED"} {
p := dst.Props.Get(name)
if p == nil {
continue
}
value, _, date, err := feedTime(p, zones)
if err != nil || date {
return Event{}, fmt.Errorf("%w: invalid %s", ErrInvalid, name)
}
p = ical.NewProp(name)
p.SetDateTime(value.UTC())
dst.Props.Set(p)
}
data, err := encode(envelope(dst))
if err != nil {
return Event{}, err
}
return Parse(data)
}
type externalEventTimes struct {
start, end time.Time
startWall, endWall time.Time
allDay bool
}
func externalTimes(child *ical.Component, zones map[string]*feedZone) (externalEventTimes, error) {
start, startWall, allDay, err := feedTime(child.Props.Get("DTSTART"), zones)
if err != nil {
return externalEventTimes{}, err
}
microsoftAllDay := false
if prop := child.Props.Get("X-MICROSOFT-CDO-ALLDAYEVENT"); prop != nil {
microsoftAllDay = strings.EqualFold(strings.TrimSpace(prop.Value), "TRUE")
}
if microsoftAllDay && !allDay {
if startWall.Hour() != 0 || startWall.Minute() != 0 || startWall.Second() != 0 {
return externalEventTimes{}, fmt.Errorf("%w: Microsoft all-day DTSTART is not midnight", ErrInvalid)
}
allDay, start = true, startWall
}
end, endWall := start, startWall
if p := child.Props.Get("DTEND"); p != nil {
var endAllDay bool
end, endWall, endAllDay, err = feedTime(p, zones)
if err != nil {
return externalEventTimes{}, err
}
if microsoftAllDay && !endAllDay {
if endWall.Hour() != 0 || endWall.Minute() != 0 || endWall.Second() != 0 {
return externalEventTimes{}, fmt.Errorf("%w: Microsoft all-day DTEND is not midnight", ErrInvalid)
}
endAllDay, end = true, endWall
}
if endAllDay != allDay {
return externalEventTimes{}, fmt.Errorf("%w: invalid DTEND (value type differs from DTSTART)", ErrInvalid)
}
// RFC 5545 defines DTEND as an exclusive boundary. Outlook feeds in
// the wild nevertheless emit DTSTART == DTEND for zero-duration timed
// appointments and, occasionally, for a one-day Microsoft all-day
// appointment. Preserve the former as an instantaneous event (the
// canonical encoder omits DTEND) and normalize only the latter to the
// required exclusive next-day boundary.
if end.Equal(start) && allDay {
end, endWall = start.AddDate(0, 0, 1), startWall.AddDate(0, 0, 1)
} else if end.Before(start) {
return externalEventTimes{}, fmt.Errorf("%w: invalid DTEND (precedes DTSTART)", ErrInvalid)
}
} else if allDay {
end, endWall = start.AddDate(0, 0, 1), startWall.AddDate(0, 0, 1)
}
return externalEventTimes{start: start, end: end, startWall: startWall, endWall: endWall, allDay: allDay}, nil
}
func occurrenceKey(value time.Time, allDay bool) string {
if allDay {
return "D:" + value.Format("20060102")
}
return "T:" + value.UTC().Format("20060102T150405Z")
}
type externalDateValue struct {
instant time.Time
wall time.Time
}
func externalDateList(props []ical.Prop, reference *ical.Prop, zones map[string]*feedZone, allDay bool, property string) ([]externalDateValue, error) {
result := []externalDateValue{}
for _, original := range props {
if original.ValueType() == ical.ValuePeriod || strings.Contains(original.Value, "/") {
return nil, fmt.Errorf("%w: %s PERIOD is not supported", ErrInvalid, property)
}
for _, value := range strings.Split(original.Value, ",") {
p := ical.NewProp(property)
p.Value = strings.TrimSpace(value)
for key, values := range original.Params {
for _, value := range values {
p.Params.Add(key, value)
}
}
if p.Params.Get("TZID") == "" && !strings.HasSuffix(p.Value, "Z") && len(p.Value) != 8 && reference != nil && reference.Params.Get("TZID") != "" {
p.Params.Set("TZID", reference.Params.Get("TZID"))
}
if len(p.Value) == 8 {
p.SetValueType(ical.ValueDate)
}
instant, wall, date, err := feedTime(p, zones)
if err != nil || date != allDay {
return nil, fmt.Errorf("%w: invalid %s", ErrInvalid, property)
}
result = append(result, externalDateValue{instant: instant, wall: wall})
}
}
return result, nil
}
func externalOccurrenceBounds(child *ical.Component, base externalEventTimes, wall time.Time, zones map[string]*feedZone) (time.Time, time.Time, error) {
startProp := child.Props.Get("DTSTART")
start := wall.UTC()
if !base.allDay && !strings.HasSuffix(startProp.Value, "Z") {
zone := zones[zoneName(startProp.Params.Get("TZID"))]
if zone == nil {
return time.Time{}, time.Time{}, fmt.Errorf("%w: unresolved recurrence TZID", ErrInvalid)
}
start = zone.resolve(wall).UTC()
}
duration := base.endWall.Sub(base.startWall)
if duration == 0 {
return start, start, nil
}
endWall := wall.Add(duration)
endProp := child.Props.Get("DTEND")
if base.allDay || endProp != nil && strings.HasSuffix(endProp.Value, "Z") {
return start, endWall.UTC(), nil
}
if endProp == nil {
return time.Time{}, time.Time{}, fmt.Errorf("%w: recurring duration is not supported", ErrInvalid)
}
zone := zones[zoneName(endProp.Params.Get("TZID"))]
if zone == nil {
return time.Time{}, time.Time{}, fmt.Errorf("%w: unresolved recurrence DTEND TZID", ErrInvalid)
}
return start, zone.resolve(endWall).UTC(), nil
}
func externalCancelled(child *ical.Component) bool {
status, _ := child.Props.Text("STATUS")
return strings.EqualFold(status, "CANCELLED")
}
type externalException struct {
component *ical.Component
wall time.Time
allDay bool
}
type externalOccurrence struct {
wall time.Time
direct *time.Time
}
type externalGroup struct {
uid string
master *ical.Component
exceptions map[string]externalException
}
// ParseExternalFeed converts a bounded subscription projection into the
// strict UTC/DATE representation used by the canonical calendar store.
func ParseExternalFeed(data []byte, from, to time.Time) ([]Event, error) {
if len(data) > MaxFeed || !to.After(from) || to.Sub(from) > 370*24*time.Hour {
return nil, ErrLimit
}
if !utf8.Valid(data) || strings.ContainsRune(string(data), 0) {
return nil, fmt.Errorf("%w: invalid subscription encoding", ErrInvalid)
}
dec := ical.NewDecoder(strings.NewReader(string(data)))
cal, err := dec.Decode()
if err != nil {
return nil, fmt.Errorf("%w: malformed VCALENDAR", ErrInvalid)
}
if _, err = dec.Decode(); err != io.EOF {
return nil, fmt.Errorf("%w: one VCALENDAR required", ErrInvalid)
}
version, _ := cal.Props.Text("VERSION")
product, _ := cal.Props.Text("PRODID")
method, _ := cal.Props.Text("METHOD")
if len(cal.Props["VERSION"]) != 1 || version != "2.0" || len(cal.Props["PRODID"]) != 1 || product == "" || len(cal.Props["METHOD"]) > 1 || method != "" && !strings.EqualFold(method, "PUBLISH") {
return nil, fmt.Errorf("%w: invalid subscription envelope", ErrInvalid)
}
if scale := cal.Props.Get("CALSCALE"); scale != nil && !strings.EqualFold(scale.Value, "GREGORIAN") {
return nil, fmt.Errorf("%w: unsupported calendar scale", ErrInvalid)
}
zones, err := buildFeedZones(cal, from, to)
if err != nil {
return nil, err
}
groups := map[string]*externalGroup{}
groupOrder := []string{}
for index, child := range cal.Children {
if child.Name == "VTIMEZONE" {
continue
}
if child.Name != "VEVENT" {
return nil, fmt.Errorf("%w: unsupported subscription component %s", ErrInvalid, child.Name)
}
if len(child.Children) > 0 {
return nil, fmt.Errorf("%w: VEVENT %d subcomponents are not yet supported", ErrInvalid, index+1)
}
cancelled := externalCancelled(child)
for _, name := range []string{"UID", "DTSTAMP"} {
if len(child.Props[name]) != 1 {
return nil, fmt.Errorf("%w: VEVENT %d %s must occur exactly once", ErrInvalid, index+1, name)
}
}
if len(child.Props["DTSTART"]) != 1 && !(cancelled && len(child.Props["RECURRENCE-ID"]) == 1) {
return nil, fmt.Errorf("%w: VEVENT %d DTSTART must occur exactly once", ErrInvalid, index+1)
}
if len(child.Props["DTEND"]) > 1 || len(child.Props["RRULE"]) > 1 || child.Props.Get("DURATION") != nil {
return nil, fmt.Errorf("%w: VEVENT %d unsupported or duplicate event duration", ErrInvalid, index+1)
}
uid, err := child.Props.Text("UID")
if err != nil || uid == "" || len(uid) > 512 {
return nil, fmt.Errorf("%w: VEVENT %d invalid UID", ErrInvalid, index+1)
}
if cleaned, changed := sanitizeExternalPropertyValue(uid); changed || cleaned != uid {
return nil, fmt.Errorf("%w: VEVENT %d invalid UID", ErrInvalid, index+1)
}
group := groups[uid]
if group == nil {
group = &externalGroup{uid: uid, exceptions: map[string]externalException{}}
groups[uid] = group
groupOrder = append(groupOrder, uid)
}
recurrenceID := child.Props.Get("RECURRENCE-ID")
if recurrenceID == nil {
if group.master != nil {
return nil, fmt.Errorf("%w: duplicate master VEVENT", ErrInvalid)
}
group.master = child
continue
}
if len(child.Props["RECURRENCE-ID"]) != 1 || recurrenceID.Params.Get("RANGE") != "" || child.Props.Get("RRULE") != nil || child.Props.Get("RDATE") != nil || child.Props.Get("EXDATE") != nil {
return nil, fmt.Errorf("%w: unsupported RECURRENCE-ID form", ErrInvalid)
}
instant, wall, date, err := feedTime(recurrenceID, zones)
if err != nil {
return nil, fmt.Errorf("%w: invalid RECURRENCE-ID", ErrInvalid)
}
key := occurrenceKey(instant, date)
if _, exists := group.exceptions[key]; exists {
return nil, fmt.Errorf("%w: duplicate RECURRENCE-ID", ErrInvalid)
}
group.exceptions[key] = externalException{component: child, wall: wall, allDay: date}
}
result := []Event{}
seen := map[string]bool{}
totalMaterialized := 0
appendEvent := func(event Event) error {
if seen[event.UID] {
return fmt.Errorf("%w: duplicate materialized UID", ErrInvalid)
}
seen[event.UID] = true
totalMaterialized += len(event.Data)
if totalMaterialized > MaxFeed || len(result) >= 5000 {
return ErrLimit
}
result = append(result, event)
return nil
}
for groupIndex, uid := range groupOrder {
group := groups[uid]
if group.master == nil {
// Some subscription projections (notably Outlook published ICS)
// contain only modified instances and omit the series master. A
// non-cancelled exception is still a complete VEVENT when it has
// DTSTART; project it as a stable single occurrence. Without the
// master an omitted DTEND cannot inherit a duration and therefore
// remains a zero-duration event.
keys := make([]string, 0, len(group.exceptions))
for key := range group.exceptions {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
exception := group.exceptions[key]
if externalCancelled(exception.component) {
continue
}
times, e := externalTimes(exception.component, zones)
if e != nil {
return nil, fmt.Errorf("VEVENT group %d orphan recurrence exception timing validation failed: %w", groupIndex+1, e)
}
if exception.allDay != times.allDay {
return nil, fmt.Errorf("%w: orphan RECURRENCE-ID value type differs from DTSTART", ErrInvalid)
}
if !(times.start.Before(to) && (times.end.After(from) || times.end.Equal(times.start) && !times.start.Before(from))) {
continue
}
event, e := materializeExternal(exception.component, uid, times.start, times.end, times.allDay, true, exception.wall, zones)
if e != nil {
return nil, e
}
if e = appendEvent(event); e != nil {
return nil, e
}
}
continue
}
if externalCancelled(group.master) {
continue
}
base, err := externalTimes(group.master, zones)
if err != nil {
return nil, fmt.Errorf("VEVENT group %d master timing validation failed: %w", groupIndex+1, err)
}
duration := base.endWall.Sub(base.startWall)
rule := group.master.Props.Get("RRULE")
recurring := rule != nil || len(group.master.Props["RDATE"]) > 0 || len(group.exceptions) > 0
occurrences := []externalOccurrence{{wall: base.startWall}}
if rule != nil {
option, e := rrule.StrToROption(rule.Value)
if e != nil {
return nil, fmt.Errorf("%w: invalid RRULE", ErrInvalid)
}
if e = validateExternalRule(option, false); e != nil {
return nil, e
}
option.Dtstart = base.startWall
r, e := rrule.NewRRule(*option)
if e != nil {
return nil, fmt.Errorf("%w: invalid RRULE", ErrInvalid)
}
walls := r.Between(from.Add(-duration-48*time.Hour), to.Add(48*time.Hour), true)
occurrences = make([]externalOccurrence, 0, len(walls))
for _, wall := range walls {
occurrences = append(occurrences, externalOccurrence{wall: wall})
}
}
rdates, err := externalDateList(group.master.Props["RDATE"], group.master.Props.Get("DTSTART"), zones, base.allDay, "RDATE")
if err != nil {
return nil, err
}
for _, value := range rdates {
start := value.instant
occurrences = append(occurrences, externalOccurrence{wall: value.wall, direct: &start})
}
exdates, err := externalDateList(group.master.Props["EXDATE"], group.master.Props.Get("DTSTART"), zones, base.allDay, "EXDATE")
if err != nil {
return nil, err
}
excluded := map[string]bool{}
for _, value := range exdates {
excluded[occurrenceKey(value.instant, base.allDay)] = true
}
sort.Slice(occurrences, func(i, j int) bool {
left, right := occurrences[i].wall, occurrences[j].wall
if occurrences[i].direct != nil {
left = *occurrences[i].direct
}
if occurrences[j].direct != nil {
right = *occurrences[j].direct
}
return left.Before(right)
})
used := map[string]bool{}
previous := ""
for _, occurrence := range occurrences {
occurrenceWall := occurrence.wall
occurrenceStart, occurrenceEnd, err := externalOccurrenceBounds(group.master, base, occurrenceWall, zones)
if occurrence.direct != nil {
occurrenceStart = *occurrence.direct
occurrenceEnd = occurrenceStart.Add(base.end.Sub(base.start))
}
if err != nil {
return nil, err
}
key := occurrenceKey(occurrenceStart, base.allDay)
if key == previous {
continue
}
previous = key
exception, hasException := group.exceptions[key]
if excluded[key] && !hasException {
continue
}
source, allDay := group.master, base.allDay
if hasException {
used[key] = true
if exception.allDay != base.allDay {
return nil, fmt.Errorf("%w: RECURRENCE-ID value type differs from master", ErrInvalid)
}
if externalCancelled(exception.component) {
continue
}
times, e := externalTimes(exception.component, zones)
if e != nil {
return nil, fmt.Errorf("VEVENT group %d recurrence exception timing validation failed: %w", groupIndex+1, e)
}
if exception.component.Props.Get("DTEND") == nil && base.end.After(base.start) {
times.end = times.start.Add(base.end.Sub(base.start))
}
source, allDay, occurrenceStart, occurrenceEnd = exception.component, times.allDay, times.start, times.end
}
if !(occurrenceStart.Before(to) && (occurrenceEnd.After(from) || occurrenceEnd.Equal(occurrenceStart) && !occurrenceStart.Before(from))) {
continue
}
event, e := materializeExternal(source, uid, occurrenceStart, occurrenceEnd, allDay, recurring, occurrenceWall, zones)
if e != nil {
return nil, e
}
if e = appendEvent(event); e != nil {
return nil, e
}
}
// A moved exception can enter the synchronization window even when its
// original occurrence is just outside it.
for key, exception := range group.exceptions {
if used[key] || externalCancelled(exception.component) {
continue
}
times, e := externalTimes(exception.component, zones)
if e != nil {
return nil, fmt.Errorf("VEVENT group %d detached recurrence exception timing validation failed: %w", groupIndex+1, e)
}
if exception.component.Props.Get("DTEND") == nil && base.end.After(base.start) {
times.end = times.start.Add(base.end.Sub(base.start))
}
if !(times.start.Before(to) && (times.end.After(from) || times.end.Equal(times.start) && !times.start.Before(from))) {
continue
}
event, e := materializeExternal(exception.component, uid, times.start, times.end, times.allDay, true, exception.wall, zones)
if e != nil {
return nil, e
}
if e = appendEvent(event); e != nil {
return nil, e
}
}
}
sort.Slice(result, func(i, j int) bool {
if result[i].Start.Equal(result[j].Start) {
return result[i].UID < result[j].UID
}
return result[i].Start.Before(result[j].Start)
})
return result, nil
}
+185
View File
@@ -0,0 +1,185 @@
package calendar
import (
"errors"
"strconv"
"strings"
"testing"
"time"
)
const outlookFixture = "BEGIN:VCALENDAR\r\n" +
"VERSION:2.0\r\nPRODID:-//Microsoft Corporation//Outlook//EN\r\nMETHOD:PUBLISH\r\nX-WR-CALNAME:Outlook\r\n" +
"BEGIN:VTIMEZONE\r\nTZID:Tokyo Standard Time\r\n" +
"BEGIN: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:outlook-recurring\r\nDTSTAMP:20260901T000000Z\r\n" +
"DTSTART;TZID=\"Tokyo Standard Time\":20260922T090000\r\nDTEND;TZID=\"Tokyo Standard Time\":20260922T100000\r\n" +
"RRULE:FREQ=DAILY;COUNT=3\r\nCLASS:PUBLIC\r\nPRIORITY:5\r\nSEQUENCE:2\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\n" +
"SUMMARY:Outlook recurrence\r\nDESCRIPTION:Synthetic fixture\r\nLOCATION:Tokyo\r\n" +
"X-MICROSOFT-CDO-ALLDAYEVENT:FALSE\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nX-MICROSOFT-DISALLOW-COUNTER:TRUE\r\n" +
"END:VEVENT\r\nEND:VCALENDAR\r\n"
func TestParseExternalOutlookFeedMaterializesBoundedRecurrence(t *testing.T) {
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(outlookFixture), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 3 {
t.Fatal(len(events), err)
}
for i, event := range events {
want := time.Date(2026, 9, 22+i, 0, 0, 0, 0, time.UTC)
if !event.Start.Equal(want) || !event.End.Equal(want.Add(time.Hour)) {
t.Fatalf("occurrence %d = %v..%v", i, event.Start, event.End)
}
if event.UID == "outlook-recurring" || !strings.Contains(event.Data, "X-PAGESWIKI-SOURCE-UID") || !strings.Contains(event.Data, "X-MICROSOFT-CDO-BUSYSTATUS:BUSY") || strings.Contains(event.Data, "RRULE:") || strings.Contains(event.Data, "METHOD:") {
t.Fatal(event.UID, event.Data)
}
}
again, err := ParseExternalFeed([]byte(outlookFixture), from, from.AddDate(0, 0, 5))
if err != nil || again[0].UID != events[0].UID || again[0].ETag != events[0].ETag {
t.Fatal("materialization is not stable", err)
}
}
func TestParseExternalFeedAcceptsMultiMegabyteOutlookShape(t *testing.T) {
var feed strings.Builder
feed.WriteString("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic Outlook//EN\r\nMETHOD:PUBLISH\r\nX-WR-CALNAME:Large\r\n")
for i := 0; i < 4500; i++ {
feed.WriteString("BEGIN:VEVENT\r\nUID:large-")
feed.WriteString(strconv.Itoa(i))
feed.WriteString("\r\nDTSTAMP:20260901T000000Z\r\nDTSTART:20260922T090000Z\r\nDTEND:20260922T100000Z\r\nSUMMARY:Large fixture\r\nDESCRIPTION:")
feed.WriteString(strings.Repeat("x", 512))
feed.WriteString("\r\nX-MICROSOFT-CDO-INSTTYPE:0\r\nEND:VEVENT\r\n")
}
feed.WriteString("END:VCALENDAR\r\n")
if feed.Len() < 2600000 || feed.Len() > MaxFeed {
t.Fatal("bad fixture size", feed.Len())
}
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed.String()), from, from.AddDate(0, 0, 3))
if err != nil || len(events) != 4500 {
t.Fatal(len(events), err)
}
}
func TestParseExternalFeedSanitizesOutlookTextControlCharacters(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3\r\n", "", 1)
feed = strings.Replace(feed, "DESCRIPTION:Synthetic fixture", "DESCRIPTION:Synthetic\vfixture\x7f", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 1 {
t.Fatal(events, err)
}
if events[0].Description != "Syntheticfixture" || !strings.Contains(events[0].Data, "X-PAGESWIKI-SANITIZED") || !strings.Contains(events[0].Data, "CONTROL-CHARACTER") || strings.ContainsAny(events[0].Data, "\v\x7f") {
t.Fatal(events[0])
}
}
func TestParseExternalFeedCustomTimezoneDST(t *testing.T) {
feed := strings.Replace(outlookFixture,
"TZID: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",
"TZID:Custom Zone\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000329T020000\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nDTSTART:20001025T030000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD", 1)
feed = strings.ReplaceAll(feed, `Tokyo Standard Time`, `Custom Zone`)
feed = strings.Replace(feed, "DTSTART;TZID=\"Custom Zone\":20260922T090000", "DTSTART;TZID=\"Custom Zone\":20260328T090000", 1)
feed = strings.Replace(feed, "DTEND;TZID=\"Custom Zone\":20260922T100000", "DTEND;TZID=\"Custom Zone\":20260328T100000", 1)
from := time.Date(2026, 3, 27, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 3 {
t.Fatal(len(events), err)
}
if events[0].Start.Hour() != 8 || events[1].Start.Hour() != 7 || events[2].Start.Hour() != 7 {
t.Fatal(events[0].Start, events[1].Start, events[2].Start)
}
}
func TestParseExternalFeedAppliesEXDATEAndRDATE(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3", "RRULE:FREQ=DAILY;COUNT=3\r\nEXDATE:20260923T000000Z\r\nRDATE;TZID=\"Tokyo Standard Time\":20260925T090000", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 6))
if err != nil || len(events) != 3 || events[0].Start.Day() != 22 || events[1].Start.Day() != 24 || events[2].Start.Day() != 25 {
t.Fatal(events, err)
}
}
func TestParseExternalFeedAppliesMovedAndCancelledExceptions(t *testing.T) {
exceptions := "BEGIN:VEVENT\r\nUID:outlook-recurring\r\nDTSTAMP:20260902T000000Z\r\nRECURRENCE-ID;TZID=\"Tokyo Standard Time\":20260923T090000\r\nDTSTART;TZID=\"Tokyo Standard Time\":20260923T120000\r\nSUMMARY:Moved synthetic occurrence\r\nEND:VEVENT\r\n" +
"BEGIN:VEVENT\r\nUID:outlook-recurring\r\nDTSTAMP:20260902T000000Z\r\nRECURRENCE-ID;TZID=\"Tokyo Standard Time\":20260924T090000\r\nSTATUS:CANCELLED\r\nEND:VEVENT\r\n"
feed := strings.Replace(outlookFixture, "END:VCALENDAR\r\n", exceptions+"END:VCALENDAR\r\n", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 2 {
t.Fatal(events, err)
}
if events[1].Summary != "Moved synthetic occurrence" || events[1].Start.Format(time.RFC3339) != "2026-09-23T03:00:00Z" || events[1].End.Format(time.RFC3339) != "2026-09-23T04:00:00Z" || !strings.HasSuffix(events[1].UID, "#20260923T090000") || strings.Contains(events[1].Data, "RECURRENCE-ID") {
t.Fatal(events[1])
}
}
func TestParseExternalFeedProjectsOrphanRecurrenceExceptions(t *testing.T) {
feed := "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic Outlook//EN\r\nMETHOD:PUBLISH\r\n" +
"BEGIN:VEVENT\r\nUID:orphan-series\r\nDTSTAMP:20260901T000000Z\r\nRECURRENCE-ID:20260922T090000Z\r\nDTSTART:20260922T120000Z\r\nDTEND:20260922T130000Z\r\nSUMMARY:Moved orphan fixture\r\nEND:VEVENT\r\n" +
"BEGIN:VEVENT\r\nUID:cancelled-orphan\r\nDTSTAMP:20260901T000000Z\r\nRECURRENCE-ID:20260923T090000Z\r\nSTATUS:CANCELLED\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 1 {
t.Fatal(events, err)
}
if events[0].Summary != "Moved orphan fixture" || events[0].Start.Format(time.RFC3339) != "2026-09-22T12:00:00Z" || !strings.HasSuffix(events[0].UID, "#20260922T090000") || strings.Contains(events[0].Data, "RECURRENCE-ID") {
t.Fatal(events[0])
}
again, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || again[0].UID != events[0].UID || again[0].ETag != events[0].ETag {
t.Fatal("orphan projection is not stable", err)
}
}
func TestParseExternalFeedRejectsUnboundedSubDailyExpansion(t *testing.T) {
bad := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3", "RRULE:FREQ=SECONDLY", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
if events, err := ParseExternalFeed([]byte(bad), from, from.AddDate(0, 0, 5)); !errors.Is(err, ErrInvalid) || events != nil {
t.Fatal(events, err)
}
}
func TestParseExternalFeedMicrosoftAllDayDateTime(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3\r\n", "", 1)
feed = strings.Replace(feed, "20260922T090000", "20260922T000000", 1)
feed = strings.Replace(feed, "20260922T100000", "20260923T000000", 1)
feed = strings.Replace(feed, "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE", "X-MICROSOFT-CDO-ALLDAYEVENT:TRUE", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 1 || !events[0].AllDay || events[0].Start.Format("2006-01-02") != "2026-09-22" || events[0].End.Format("2006-01-02") != "2026-09-23" {
t.Fatal(events, err)
}
}
func TestParseExternalFeedAcceptsZeroDurationOutlookEvent(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3\r\n", "", 1)
feed = strings.Replace(feed, "20260922T100000", "20260922T090000", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 1 || !events[0].Start.Equal(events[0].End) || strings.Contains(events[0].Data, "DTEND") {
t.Fatal(events, err)
}
}
func TestParseExternalFeedNormalizesZeroLengthMicrosoftAllDay(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3\r\n", "", 1)
feed = strings.Replace(feed, "20260922T090000", "20260922T000000", 1)
feed = strings.Replace(feed, "20260922T100000", "20260922T000000", 1)
feed = strings.Replace(feed, "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE", "X-MICROSOFT-CDO-ALLDAYEVENT:TRUE", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
events, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if err != nil || len(events) != 1 || !events[0].AllDay || events[0].End.Sub(events[0].Start) != 24*time.Hour {
t.Fatal(events, err)
}
}
func TestParseExternalFeedRejectsEndBeforeStartWithoutLeakingValues(t *testing.T) {
feed := strings.Replace(outlookFixture, "RRULE:FREQ=DAILY;COUNT=3\r\n", "", 1)
feed = strings.Replace(feed, "20260922T100000", "20260922T080000", 1)
from := time.Date(2026, 9, 21, 0, 0, 0, 0, time.UTC)
_, err := ParseExternalFeed([]byte(feed), from, from.AddDate(0, 0, 5))
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "precedes DTSTART") || strings.Contains(err.Error(), "20260922") {
t.Fatal(err)
}
}
+227
View File
@@ -0,0 +1,227 @@
// Package calendar implements file-backed calendars. SQLite is a disposable index.
package calendar
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
"unicode/utf8"
ical "github.com/emersion/go-ical"
)
const MaxResource = 256 << 10
var ErrInvalid = errors.New("invalid or unsupported iCalendar data")
type Event struct {
UID string `json:"uid"`
Summary string `json:"summary"`
Description string `json:"description"`
Location string `json:"location"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
AllDay bool `json:"all_day"`
ETag string `json:"etag"`
Data string `json:"-"`
}
func digest(data []byte) string { h := sha256.Sum256(data); return hex.EncodeToString(h[:]) }
func envelope(events ...*ical.Component) *ical.Calendar {
c := ical.NewCalendar()
c.Props.SetText("VERSION", "2.0")
c.Props.SetText("PRODID", "-//PagesWiki//Calendar 1//EN")
c.Children = events
return c
}
func encode(c *ical.Calendar) (string, error) {
var b bytes.Buffer
err := ical.NewEncoder(&b).Encode(c)
if err != nil {
return "", err
}
// RFC 5545 3.1: fold at 75 octets without splitting a UTF-8 sequence.
var out strings.Builder
for _, line := range strings.Split(strings.TrimSuffix(b.String(), "\r\n"), "\r\n") {
limit := 75
for len(line) > limit {
n := limit
for n > 0 && !utf8.RuneStart(line[n]) {
n--
}
out.WriteString(line[:n])
out.WriteString("\r\n ")
line = line[n:]
limit = 74
}
out.WriteString(line)
out.WriteString("\r\n")
}
return out.String(), nil
}
// Parse deliberately accepts a declared, lossless subset. Unsupported temporal
// semantics are rejected, never accepted and silently omitted from REPORT.
func Parse(data string) (Event, error) {
bad := func(reason string) (Event, error) { return Event{}, fmt.Errorf("%w: %s", ErrInvalid, reason) }
if len(data) > MaxResource || !utf8.ValidString(data) || strings.ContainsRune(data, 0) {
return bad("size or encoding")
}
for _, ch := range data {
if ch < 32 && ch != '\r' && ch != '\n' && ch != '\t' || ch == 127 {
return bad("control character")
}
}
dec := ical.NewDecoder(strings.NewReader(data))
c, err := dec.Decode()
if err != nil {
return bad(err.Error())
}
if _, err = dec.Decode(); err != io.EOF {
return bad("one VCALENDAR required")
}
if len(c.Children) != 1 || c.Children[0].Name != "VEVENT" {
return bad("exactly one VEVENT required; recurrence and VTIMEZONE are not yet supported")
}
if c.Props.Get("METHOD") != nil {
return bad("scheduling METHOD is not supported")
}
v, _ := c.Props.Text("VERSION")
if v != "2.0" {
return bad("VERSION must be 2.0")
}
if product, err := c.Props.Text("PRODID"); err != nil || product == "" {
return bad("PRODID is required")
}
// Collection-level properties cannot be merged losslessly into monthly files.
for name := range c.Props {
if name != "VERSION" && name != "PRODID" && name != "CALSCALE" {
return bad("unsupported VCALENDAR property: " + name)
}
}
if scale, _ := c.Props.Text("CALSCALE"); scale != "" && scale != "GREGORIAN" {
return bad("unsupported calendar scale")
}
e := ical.Event{Component: c.Children[0]}
for _, key := range []string{"RRULE", "RDATE", "EXDATE", "RECURRENCE-ID", "DURATION"} {
if e.Props.Get(key) != nil {
return bad(key + " is not yet supported")
}
}
if len(e.Children) > 0 {
return bad("VALARM delivery is not yet supported")
}
for _, key := range []string{"UID", "DTSTAMP", "DTSTART"} {
if len(e.Props[key]) != 1 {
return bad(key + " must occur exactly once")
}
}
uid, err := e.Props.Text("UID")
if err != nil || uid == "" || len(uid) > 512 || strings.ContainsAny(uid, "\r\n\x00") {
return bad("invalid UID")
}
startProp := e.Props.Get("DTSTART")
allDay := startProp.ValueType() == ical.ValueDate
for _, key := range []string{"DTSTART", "DTEND", "DTSTAMP", "CREATED", "LAST-MODIFIED"} {
p := e.Props.Get(key)
if p == nil {
continue
}
date := p.ValueType() == ical.ValueDate
if p.Params.Get("TZID") != "" || (!date && !strings.HasSuffix(p.Value, "Z")) {
return bad("only UTC DATE-TIME or DATE supported")
}
if (key == "DTEND" && date != allDay) || (key != "DTSTART" && key != "DTEND" && date) {
return bad("inconsistent date types")
}
t, err := p.DateTime(time.UTC)
if err != nil || t.Year() < 1 || t.Year() > 9998 {
return bad("invalid " + key)
}
}
start, err := e.DateTimeStart(time.UTC)
if err != nil {
return bad("invalid DTSTART")
}
end, err := e.DateTimeEnd(time.UTC)
if err != nil || end.Before(start) || (e.Props.Get("DTEND") != nil && !end.After(start)) {
return bad("DTEND must follow DTSTART")
}
if _, err = e.Status(); err != nil {
return bad(err.Error())
}
// Encoder validates required properties, cardinality and child components.
canonical, err := encode(c)
if err != nil {
return bad(err.Error())
}
// Normalize the envelope, preserving every accepted event property/parameter.
canonical, err = encode(envelope(e.Component))
if err != nil {
return bad(err.Error())
}
result := Event{UID: uid, Start: start, End: end, AllDay: allDay, Data: canonical, ETag: "\"" + digest([]byte(canonical)) + "\""}
for key, dst := range map[string]*string{"SUMMARY": &result.Summary, "DESCRIPTION": &result.Description, "LOCATION": &result.Location} {
s, err := e.Props.Text(key)
if err != nil {
return bad(key)
}
*dst = s
}
return result, nil
}
func component(data string) (*ical.Component, error) {
c, e := ical.NewDecoder(strings.NewReader(data)).Decode()
if e != nil {
return nil, e
}
return c.Children[0], nil
}
// Build changes only UI-owned fields. Unknown properties survive browser edits.
func Build(old string, item Event) (Event, error) {
e := ical.NewEvent()
if old != "" {
c, err := component(old)
if err != nil {
return Event{}, err
}
e.Component = c
}
e.Props.SetText("UID", item.UID)
e.Props.SetText("SUMMARY", item.Summary)
e.Props.SetText("DESCRIPTION", item.Description)
e.Props.SetText("LOCATION", item.Location)
e.Props.SetDateTime("DTSTAMP", time.Now().UTC().Truncate(time.Second))
e.Props.SetDateTime("LAST-MODIFIED", time.Now().UTC().Truncate(time.Second))
for name, t := range map[string]time.Time{"DTSTART": item.Start, "DTEND": item.End} {
p := ical.NewProp(name)
if item.AllDay {
p.SetDate(t.UTC())
} else {
p.SetDateTime(t.UTC())
}
e.Props.Set(p)
}
if old != "" {
n := 0
if p := e.Props.Get("SEQUENCE"); p != nil {
n, _ = strconv.Atoi(p.Value)
}
p := ical.NewProp("SEQUENCE")
p.Value = strconv.Itoa(n + 1)
e.Props.Set(p)
}
data, err := encode(envelope(e.Component))
if err != nil {
return Event{}, err
}
return Parse(data)
}
+158
View File
@@ -0,0 +1,158 @@
package calendar
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"unicode"
ical "github.com/emersion/go-ical"
"golang.org/x/text/unicode/norm"
)
var ErrNameTaken = errors.New("pretty_name is already in use")
func ValidPrettyName(s string) bool {
if s == "" || len(s) > 120 || norm.NFC.String(s) != s || validID(s) {
return false
}
for _, c := range s {
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '-' && c != '_' {
return false
}
}
return true
}
func (r *Repository) uniqueName(domain, id, name string) error {
entries, e := os.ReadDir(filepath.Join(r.Root, domain))
if e != nil {
return e
}
for _, entry := range entries {
if !entry.IsDir() || !validID(entry.Name()) || entry.Name() == id {
continue
}
c, e := readMeta(filepath.Join(r.Root, domain, entry.Name()))
if errors.Is(e, ErrMissing) {
continue
}
if e != nil {
return e
}
if strings.EqualFold(c.PrettyName, name) {
return ErrNameTaken
}
}
return nil
}
func renameSnapshots(dir string, c *Calendar) error {
for month, hash := range c.Months {
b, e := os.ReadFile(filepath.Join(dir, month+"."+hash+".ics"))
if e != nil {
return e
}
if digest(b) != hash {
return ErrInvalid
}
cal, e := ical.NewDecoder(strings.NewReader(string(b))).Decode()
if e != nil {
return e
}
cal.Props.SetText("NAME", c.PrettyName)
data, e := encode(cal)
if e != nil {
return e
}
h := digest([]byte(data))
if e = atomicFile(filepath.Join(dir, month+"."+h+".ics"), []byte(data)); e != nil {
return e
}
c.Months[month] = h
}
return nil
}
func pruneSnapshots(dir string, c Calendar) {
files, _ := filepath.Glob(filepath.Join(dir, "*.ics"))
keep := map[string]bool{}
for month, hash := range c.Months {
keep[month+"."+hash+".ics"] = true
}
for _, file := range files {
if !keep[filepath.Base(file)] {
_ = os.Remove(file)
}
}
}
// Defaults belong to a principal within a Domain. Persist identities, not names.
// Inaccessible calendars are filtered on every read, never used as grants.
func (r *Repository) Defaults(domain, actor string) ([]string, error) {
if actor == "" {
return []string{}, nil
}
if !validID(domain) || !validID(actor) {
return nil, ErrForbidden
}
b, e := os.ReadFile(filepath.Join(r.Root, domain, "preferences", actor+".json"))
if os.IsNotExist(e) {
return []string{}, nil
}
if e != nil {
return nil, e
}
var ids []string
e = json.Unmarshal(b, &ids)
if e != nil {
return nil, e
}
visible, e := r.List(domain, actor)
if e != nil {
return nil, e
}
allowed := map[string]bool{}
for _, c := range visible {
allowed[c.ID] = true
}
out := []string{}
for _, id := range ids {
if allowed[id] {
out = append(out, id)
}
}
return out, nil
}
func (r *Repository) SaveDefaults(ctx context.Context, domain, actor string, ids []string) error {
if !validID(domain) || !validID(actor) {
return ErrForbidden
}
if len(ids) > 32 {
return ErrInvalid
}
seen := map[string]bool{}
for _, id := range ids {
if seen[id] {
return ErrInvalid
}
seen[id] = true
if _, e := r.Get(domain, id, actor); e != nil {
return e
}
}
dir := filepath.Join(r.Root, domain, "preferences")
if e := os.MkdirAll(dir, 0700); e != nil {
return e
}
unlock, e := lock(ctx, dir)
if e != nil {
return e
}
defer unlock()
b, e := json.Marshal(ids)
if e != nil {
return e
}
return atomicFile(filepath.Join(dir, actor+".json"), b)
}
+99
View File
@@ -0,0 +1,99 @@
package calendar
import (
"context"
"errors"
"github.com/google/uuid"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
func TestCalendarPrettyNamesDefaultsAndRename(t *testing.T) {
r, d, a, c := fixture(t)
ctx := context.Background()
c, e := r.Settings(ctx, d, c.ID, a, c.Version, c.Name, false, nil, "work")
if e != nil {
t.Fatal(e)
}
second, e := r.Create(ctx, d, a, "Personal", "personal")
if e != nil {
t.Fatal(e)
}
if _, e = r.Create(ctx, d, a, "Duplicate", "WORK"); !errors.Is(e, ErrNameTaken) {
t.Fatal(e)
}
for _, name := range []string{"", "a,b", "../private", "two words", uuid.NewString()} {
if _, e = r.Create(ctx, d, a, "Invalid", name); !errors.Is(e, ErrInvalid) {
t.Fatal(name, e)
}
}
if e = r.SaveDefaults(ctx, d, a, []string{c.ID, second.ID}); e != nil {
t.Fatal(e)
}
v := sample(t, "meeting", "2026-09-01T09:00:00Z", "2026-09-01T10:00:00Z", false)
if _, e = r.Change(ctx, d, c.ID, a, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
c, e = r.Get(d, c.ID, a)
if e != nil {
t.Fatal(e)
}
c, e = r.Settings(ctx, d, c.ID, a, c.Version, c.Name, false, nil, "office")
if e != nil {
t.Fatal(e)
}
ids, e := r.Defaults(d, a)
if e != nil || len(ids) != 2 || ids[0] != c.ID {
t.Fatal(ids, e)
}
dir, _ := r.dir(d, c.ID)
for m, h := range c.Months {
b, e := os.ReadFile(filepath.Join(dir, m+"."+h+".ics"))
if e != nil || !strings.Contains(string(b), "NAME:office\r\n") {
t.Fatal(string(b), e)
}
}
got, e := r.Event(ctx, d, c.ID, a, v.UID)
if e != nil || got.ETag != v.ETag {
t.Fatal("rename changed event identity", e)
}
other := uuid.NewString()
if e = r.SaveDefaults(ctx, d, other, []string{c.ID}); !errors.Is(e, ErrMissing) {
t.Fatal("private default accepted", e)
}
ids, e = r.Defaults(d, other)
if e != nil || len(ids) != 0 {
t.Fatal(ids, e)
}
}
func TestCalendarConcurrentNameReservation(t *testing.T) {
r, d, a, _ := fixture(t)
var wg sync.WaitGroup
results := make(chan error, 2)
for range 2 {
wg.Add(1)
go func() {
defer wg.Done()
_, e := r.Create(context.Background(), d, a, "Shared", "shared")
results <- e
}()
}
wg.Wait()
close(results)
success, conflict := 0, 0
for e := range results {
if e == nil {
success++
} else if errors.Is(e, ErrNameTaken) {
conflict++
} else {
t.Fatal(e)
}
}
if success != 1 || conflict != 1 {
t.Fatal(success, conflict)
}
}
+723
View File
@@ -0,0 +1,723 @@
package calendar
import (
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
ical "github.com/emersion/go-ical"
"github.com/google/uuid"
)
var (
ErrForbidden = errors.New("calendar access denied")
ErrConflict = errors.New("calendar precondition failed")
ErrPreconditionRequired = errors.New("calendar If-Match required")
ErrMissing = errors.New("calendar not found")
ErrLimit = errors.New("calendar result limit exceeded; narrow the time range")
)
type Calendar struct {
ID string `json:"id"`
Name string `json:"name"`
PrettyName string `json:"pretty_name"`
Source *Source `json:"source,omitempty"`
ReadOnly bool `json:"read_only"`
Owner string `json:"owner"`
Public bool `json:"public"`
Members map[string]string `json:"members"` // principal UUID -> read/write
Months map[string]string `json:"months"` // month -> immutable generation hash
Version string `json:"version"`
DAVNames map[string]string `json:"dav_names,omitempty"` // UID -> client filename; canonical metadata, not a DB-only alias
}
func (c Calendar) CanRead(actor string) bool {
return c.Public || actor != "" && (actor == c.Owner || c.Members[actor] != "")
}
func (c Calendar) CanWrite(actor string) bool {
return c.Source == nil && actor != "" && (actor == c.Owner || c.Members[actor] == "write")
}
type Repository struct {
Root string
DB *sql.DB
PublicAllowed func(string) bool
}
func (r *Repository) canRead(domain string, c Calendar, actor string) bool {
if c.Public && r.PublicAllowed != nil && !r.PublicAllowed(domain) {
c.Public = false
}
return c.CanRead(actor)
}
func New(root string, db *sql.DB) (*Repository, error) {
if root == "" {
return nil, errors.New("calendar document root unavailable")
}
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS calendar_index_state(domain TEXT NOT NULL,calendar TEXT NOT NULL,version TEXT NOT NULL,PRIMARY KEY(domain,calendar));
CREATE TABLE IF NOT EXISTS calendar_month_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,month TEXT NOT NULL,hash TEXT NOT NULL,PRIMARY KEY(domain,calendar,month));
CREATE TABLE IF NOT EXISTS calendar_event_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,uid TEXT NOT NULL,month TEXT NOT NULL,start INTEGER NOT NULL,end INTEGER NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid));
CREATE INDEX IF NOT EXISTS calendar_event_range ON calendar_event_cache(domain,calendar,start,end);`)
return &Repository{Root: root, DB: db}, err
}
func validID(s string) bool { u, err := uuid.Parse(s); return err == nil && u.String() == s }
func (r *Repository) dir(domain, id string) (string, error) {
if !validID(domain) || !validID(id) {
return "", ErrMissing
}
return filepath.Join(r.Root, domain, id), nil
}
func atomicFile(path string, data []byte) error {
f, err := os.CreateTemp(filepath.Dir(path), ".calendar-")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err = f.Write(data); err != nil {
f.Close()
return err
}
if err = f.Sync(); err != nil {
f.Close()
return err
}
if err = f.Close(); err != nil {
return err
}
if err = os.Rename(f.Name(), path); err != nil {
return err
}
d, err := os.Open(filepath.Dir(path))
if err != nil {
return err
}
defer d.Close()
return d.Sync()
}
func lock(ctx context.Context, dir string) (func(), error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
f, err := os.OpenFile(filepath.Join(dir, ".lock"), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}
for {
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
return func() { syscall.Flock(int(f.Fd()), syscall.LOCK_UN); f.Close() }, nil
}
if err != syscall.EWOULDBLOCK && err != syscall.EAGAIN {
f.Close()
return nil, err
}
select {
case <-ctx.Done():
f.Close()
return nil, ctx.Err()
case <-time.After(10 * time.Millisecond):
}
}
}
func readMeta(dir string) (Calendar, error) {
var c Calendar
b, e := os.ReadFile(filepath.Join(dir, "calendar.json"))
if os.IsNotExist(e) {
return c, ErrMissing
}
if e != nil {
return c, e
}
e = json.Unmarshal(b, &c)
if e != nil {
return c, e
}
c.ReadOnly = c.Source != nil
if c.PrettyName == "" {
c.PrettyName = "calendar-" + c.ID
} // deterministic migration, no read-side writes
if !ValidPrettyName(c.PrettyName) {
return c, ErrInvalid
}
if c.ID != filepath.Base(dir) || !validID(c.ID) || !validID(c.Owner) || !validID(c.Version) || strings.TrimSpace(c.Name) == "" {
return c, ErrInvalid
}
for month, hash := range c.Months {
if _, e = time.Parse("2006-01", month); e != nil {
return c, ErrInvalid
}
if bytes, e := hex.DecodeString(hash); e != nil || len(bytes) != 32 {
return c, ErrInvalid
}
}
if c.Months == nil {
c.Months = map[string]string{}
}
for id, role := range c.Members {
if !validID(id) || (role != "read" && role != "write") {
return c, ErrInvalid
}
}
return c, e
}
func saveMeta(dir string, c *Calendar) error {
c.Version = uuid.NewString()
b, e := json.MarshalIndent(c, "", " ")
if e != nil {
return e
}
return atomicFile(filepath.Join(dir, "calendar.json"), b)
}
func (r *Repository) Create(ctx context.Context, domain, actor, name string, pretty ...string) (Calendar, error) {
c := Calendar{ID: uuid.NewString(), Name: strings.TrimSpace(name), Owner: actor, Members: map[string]string{}, Months: map[string]string{}}
c.PrettyName = "calendar-" + c.ID
if len(pretty) > 0 {
c.PrettyName = pretty[0]
}
if !ValidPrettyName(c.PrettyName) {
return c, ErrInvalid
}
if actor == "" || c.Name == "" || len(c.Name) > 200 {
return c, ErrInvalid
}
dir, e := r.dir(domain, c.ID)
if e != nil {
return c, e
}
parent := filepath.Dir(dir)
if e = os.MkdirAll(parent, 0700); e != nil {
return c, e
}
unlock, e := lock(ctx, parent)
if e != nil {
return c, e
}
defer unlock()
if e = r.uniqueName(domain, c.ID, c.PrettyName); e != nil {
return c, e
}
dirs, e := os.ReadDir(parent)
if e != nil {
return c, e
}
count := 0
for _, entry := range dirs {
if entry.IsDir() && validID(entry.Name()) {
if _, e := os.Stat(filepath.Join(parent, entry.Name(), "calendar.json")); e == nil {
count++
}
}
}
if count >= 256 {
return c, ErrLimit
}
if e = os.MkdirAll(dir, 0700); e != nil {
return c, e
}
e = saveMeta(dir, &c)
if e == nil {
for _, path := range []string{parent, r.Root, filepath.Dir(r.Root)} {
f, err := os.Open(path)
if err != nil {
return c, err
}
err = f.Sync()
f.Close()
if err != nil {
return c, err
}
}
}
return c, e
}
func (r *Repository) List(domain, actor string) ([]Calendar, error) {
if !validID(domain) {
return nil, ErrMissing
}
dirs, e := os.ReadDir(filepath.Join(r.Root, domain))
if os.IsNotExist(e) {
return []Calendar{}, nil
}
if e != nil {
return nil, e
}
out := []Calendar{}
for _, d := range dirs {
if !d.IsDir() || !validID(d.Name()) {
continue
}
c, e := readMeta(filepath.Join(r.Root, domain, d.Name()))
if errors.Is(e, ErrMissing) {
continue
} // interrupted, uncommitted creation
if e != nil {
return nil, e
}
if r.canRead(domain, c, actor) {
out = append(out, c)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out, nil
}
func (r *Repository) Get(domain, id, actor string) (Calendar, error) {
dir, e := r.dir(domain, id)
if e != nil {
return Calendar{}, e
}
c, e := readMeta(dir)
if e == nil && !r.canRead(domain, c, actor) {
e = ErrMissing
}
return c, e
}
func (r *Repository) Settings(ctx context.Context, domain, id, actor, version, name string, public bool, members map[string]string, pretty ...string) (Calendar, error) {
dir, e := r.dir(domain, id)
if e != nil {
return Calendar{}, e
}
// Domain lock first, then calendar lock: serialize name reservations.
release, e := lock(ctx, filepath.Dir(dir))
if e != nil {
return Calendar{}, e
}
defer release()
unlock, e := lock(ctx, dir)
if e != nil {
return Calendar{}, e
}
defer unlock()
c, e := readMeta(dir)
if e != nil {
return c, e
}
if actor == "" || c.Owner != actor {
return c, ErrForbidden
}
if version != c.Version {
return c, ErrConflict
}
nameChanged := len(pretty) > 0 && pretty[0] != c.PrettyName
if nameChanged {
if !ValidPrettyName(pretty[0]) {
return c, ErrInvalid
}
if e = r.uniqueName(domain, id, pretty[0]); e != nil {
return c, e
}
c.PrettyName = pretty[0]
}
if public && r.PublicAllowed != nil && !r.PublicAllowed(domain) {
return c, ErrForbidden
}
if strings.TrimSpace(name) == "" || len(name) > 200 || len(members) > 200 {
return c, ErrInvalid
}
for id, role := range members {
if !validID(id) || (role != "read" && role != "write") {
return c, ErrInvalid
}
}
c.Name = name
c.Public = public
c.Members = members
if nameChanged {
if e = renameSnapshots(dir, &c); e != nil {
return c, e
}
}
e = saveMeta(dir, &c)
if e == nil {
pruneSnapshots(dir, c)
}
return c, e
}
// Delete removes one complete calendar. The Domain lock serializes deletion
// with creation/pretty-name reservation and the calendar lock serializes it
// with event/source writes. Cache rows are disposable, but deleting them
// before the atomic directory rename means a failed rename can safely rebuild
// them from the still-visible canonical files on the next access.
func (r *Repository) Delete(ctx context.Context, domain, id, actor, version string) error {
dir, err := r.dir(domain, id)
if err != nil {
return err
}
parent := filepath.Dir(dir)
release, err := lock(ctx, parent)
if err != nil {
return err
}
defer release()
unlock, err := lock(ctx, dir)
if os.IsNotExist(err) {
return ErrMissing
}
if err != nil {
return err
}
defer unlock()
c, err := readMeta(dir)
if err != nil {
return err
}
if actor == "" || c.Owner != actor {
return ErrForbidden
}
if version == "" || version != c.Version {
return ErrConflict
}
tx, err := r.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain=? AND calendar=?`, domain, id); err != nil {
return err
}
}
if err = tx.Commit(); err != nil {
return err
}
tombstone := filepath.Join(parent, ".deleted-calendar-"+id+"-"+uuid.NewString())
if err = os.Rename(dir, tombstone); err != nil {
return err
}
if err = syncDirectory(parent); err != nil {
return err
}
if err = os.RemoveAll(tombstone); err != nil {
return err
}
return syncDirectory(parent)
}
func syncDirectory(path string) error {
dir, err := os.Open(path)
if err != nil {
return err
}
defer dir.Close()
return dir.Sync()
}
// refresh reconciles only changed monthly generations. A crash after manifest
// commit but before index commit repairs itself on the next request.
func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir string) error {
var version string
if err := r.DB.QueryRowContext(ctx, `SELECT version FROM calendar_index_state WHERE domain=? AND calendar=?`, domain, c.ID).Scan(&version); err == nil && version == c.Version {
return nil
} else if err != nil && err != sql.ErrNoRows {
return err
}
tx, e := r.DB.BeginTx(ctx, nil)
if e != nil {
return e
}
defer tx.Rollback()
rows, e := tx.QueryContext(ctx, `SELECT month,hash FROM calendar_month_cache WHERE domain=? AND calendar=?`, domain, c.ID)
if e != nil {
return e
}
known := map[string]string{}
for rows.Next() {
var m, h string
if e = rows.Scan(&m, &h); e != nil {
rows.Close()
return e
}
known[m] = h
}
e = rows.Err()
rows.Close()
if e != nil {
return e
}
for m, h := range known {
if c.Months[m] != h {
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
return e
}
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_month_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
return e
}
}
}
for m, h := range c.Months {
if known[m] == h {
continue
}
if len(h) != 64 || strings.ContainsAny(h, "/\\") || len(m) != 7 {
return ErrInvalid
}
b, e := os.ReadFile(filepath.Join(dir, m+"."+h+".ics"))
if e != nil {
return e
}
if digest(b) != h {
return fmt.Errorf("calendar snapshot checksum mismatch")
}
cal, e := ical.NewDecoder(strings.NewReader(string(b))).Decode()
if e != nil {
return e
}
for _, child := range cal.Children {
data, e := encode(envelope(child))
if e != nil {
return e
}
event, e := Parse(data)
if e != nil {
return e
}
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,end,etag,data) VALUES(?,?,?,?,?,?,?,?)`, domain, c.ID, event.UID, m, event.Start.Unix(), event.End.Unix(), event.ETag, event.Data); e != nil {
return e
}
}
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_month_cache(domain,calendar,month,hash) VALUES(?,?,?,?)`, domain, c.ID, m, h); e != nil {
return e
}
}
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_index_state(domain,calendar,version) VALUES(?,?,?) ON CONFLICT(domain,calendar) DO UPDATE SET version=excluded.version`, domain, c.ID, c.Version); e != nil {
return e
}
return tx.Commit()
}
func (r *Repository) locked(ctx context.Context, domain, id, actor string) (Calendar, string, func(), error) {
return r.lockedWithGuard(ctx, domain, id, actor, nil)
}
// The guard runs under the metadata lock, BEFORE reading any ICS snapshots.
func (r *Repository) lockedWithGuard(ctx context.Context, domain, id, actor string, guard func(Calendar) bool) (Calendar, string, func(), error) {
dir, e := r.dir(domain, id)
if e != nil {
return Calendar{}, "", nil, e
}
unlock, e := lock(ctx, dir)
if os.IsNotExist(e) {
e = ErrMissing
}
if e != nil {
return Calendar{}, "", nil, e
}
c, e := readMeta(dir)
if e == nil && !r.canRead(domain, c, actor) {
e = ErrMissing
}
if e == nil && guard != nil && !guard(c) {
e = ErrForbidden
}
if e == nil {
e = r.refresh(ctx, domain, c, dir)
}
if e != nil {
unlock()
return c, dir, nil, e
}
return c, dir, unlock, nil
}
func (r *Repository) Query(ctx context.Context, domain, id, actor string, start, end time.Time) ([]Event, error) {
return r.query(ctx, domain, id, actor, start, end, true)
}
func (r *Repository) QueryETags(ctx context.Context, domain, id, actor string, start, end time.Time) ([]Event, error) {
return r.query(ctx, domain, id, actor, start, end, false)
}
func (r *Repository) query(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool) ([]Event, error) {
return r.queryLimit(ctx, domain, id, actor, start, end, withData, 5001)
}
// Upcoming bounds body parsing for the navigation modal, even in busy calendars.
func (r *Repository) Upcoming(ctx context.Context, domain, id, actor string, start, end time.Time) ([]Event, error) {
return r.queryLimit(ctx, domain, id, actor, start, end, true, 20)
}
func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool, limit int) ([]Event, error) {
if !end.After(start) || end.Sub(start) > 370*24*time.Hour {
return nil, ErrInvalid
}
_, _, unlock, e := r.locked(ctx, domain, id, actor)
if e != nil {
return nil, e
}
defer unlock()
columns := "uid,etag"
if withData {
columns = "data"
}
rows, e := r.DB.QueryContext(ctx, `SELECT `+columns+` FROM calendar_event_cache WHERE domain=? AND calendar=? AND start<? AND (end>? OR (start=end AND start>=?)) ORDER BY start,uid LIMIT ?`, domain, id, end.Unix(), start.Unix(), start.Unix(), limit)
if e != nil {
return nil, e
}
defer rows.Close()
out := []Event{}
for rows.Next() {
if !withData {
var v Event
if e = rows.Scan(&v.UID, &v.ETag); e != nil {
return nil, e
}
out = append(out, v)
continue
}
var data string
if e = rows.Scan(&data); e != nil {
return nil, e
}
v, e := Parse(data)
if e != nil {
return nil, e
}
out = append(out, v)
}
if len(out) > 5000 {
return nil, ErrLimit
}
return out, rows.Err()
}
func (r *Repository) Event(ctx context.Context, domain, id, actor, uid string) (Event, error) {
_, _, unlock, e := r.locked(ctx, domain, id, actor)
if e != nil {
return Event{}, e
}
defer unlock()
var data string
e = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&data)
if e == sql.ErrNoRows {
e = ErrMissing
}
if e != nil {
return Event{}, e
}
return Parse(data)
}
// Change commits a replacement manifest only after every affected month is
// durable. Moving an event across months is atomic, even across process crashes.
func (r *Repository) Change(ctx context.Context, domain, id, actor, uid, match string, item *Event) (Event, error) {
c, dir, unlock, e := r.locked(ctx, domain, id, actor)
if e != nil {
return Event{}, e
}
defer unlock()
return r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
}
func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match string, item *Event, c Calendar, dir string) (Event, error) {
id := c.ID
if !c.CanWrite(actor) {
return Event{}, ErrForbidden
}
var oldData, oldMonth, oldTag string
e := r.DB.QueryRowContext(ctx, `SELECT data,month,etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&oldData, &oldMonth, &oldTag)
exists := e == nil
if e != nil && e != sql.ErrNoRows {
return Event{}, e
}
if (exists && match != oldTag) || (!exists && match != "*") {
return Event{}, ErrConflict
}
if !exists && item == nil {
return Event{}, ErrMissing
}
next := Event{}
newMonth := ""
if item != nil {
next, e = Parse(item.Data)
if e != nil {
return Event{}, e
}
if next.UID != uid {
return Event{}, ErrInvalid
}
newMonth = next.Start.Format("2006-01")
} else {
delete(c.DAVNames, uid)
}
months := map[string]bool{}
if oldMonth != "" {
months[oldMonth] = true
}
if newMonth != "" {
months[newMonth] = true
}
for month := range months {
rows, e := r.DB.QueryContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=? AND uid<>? ORDER BY uid`, domain, id, month, uid)
if e != nil {
return Event{}, e
}
children := []*ical.Component{}
for rows.Next() {
var data string
if e = rows.Scan(&data); e != nil {
rows.Close()
return Event{}, e
}
ch, e := component(data)
if e != nil {
rows.Close()
return Event{}, e
}
children = append(children, ch)
}
e = rows.Err()
rows.Close()
if e != nil {
return Event{}, e
}
if newMonth == month {
ch, e := component(next.Data)
if e != nil {
return Event{}, e
}
children = append(children, ch)
}
if len(children) == 0 {
delete(c.Months, month)
continue
}
if len(children) > 5000 {
return Event{}, fmt.Errorf("monthly event limit exceeded")
}
cal := envelope(children...)
cal.Props.SetText("NAME", c.PrettyName)
data, e := encode(cal)
if e != nil {
return Event{}, e
}
if len(data) > 16<<20 {
return Event{}, fmt.Errorf("monthly calendar size limit exceeded")
}
hash := digest([]byte(data))
if e = atomicFile(filepath.Join(dir, month+"."+hash+".ics"), []byte(data)); e != nil {
return Event{}, e
}
c.Months[month] = hash
}
if e = saveMeta(dir, &c); e != nil {
return Event{}, e
}
// Only current generations are retained. Interrupted writes are harmless
// unreferenced files; clean them on the next successful commit.
files, _ := filepath.Glob(filepath.Join(dir, "*.ics"))
keep := map[string]bool{}
for month, hash := range c.Months {
keep[month+"."+hash+".ics"] = true
}
for _, file := range files {
if !keep[filepath.Base(file)] {
_ = os.Remove(file)
}
}
// The committed files, not SQLite, determine success. A subsequent read
// reconciles the cache; retries with the old ETag correctly get 412.
return next, nil
}
+245
View File
@@ -0,0 +1,245 @@
package calendar
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
ical "github.com/emersion/go-ical"
)
const MaxFeed = 8 << 20
type Source struct {
Kind string `json:"kind"`
Credential string `json:"credential,omitempty"`
HasCredential bool `json:"has_credential"`
Resources map[string]RemoteResource `json:"resources,omitempty"`
URL string `json:"url"`
IntervalSeconds int64 `json:"interval_seconds"`
Enabled bool `json:"enabled"`
ETag string `json:"etag,omitempty"`
Modified string `json:"last_modified,omitempty"`
Next int64 `json:"next_at_ms"`
Checked int64 `json:"checked_at_ms"`
Success int64 `json:"success_at_ms"`
Error string `json:"error,omitempty"`
}
type RemoteResource struct {
UID string `json:"uid"`
ETag string `json:"etag"`
}
func (r *Repository) SourceCalendar(domain, id string) (Calendar, error) {
dir, e := r.dir(domain, id)
if e != nil {
return Calendar{}, e
}
c, e := readMeta(dir)
if e != nil {
return c, e
}
if c.Source == nil {
return c, ErrMissing
}
return c, nil
}
func ParseFeed(data []byte) ([]Event, error) {
if len(data) > MaxFeed {
return nil, ErrLimit
}
dec := ical.NewDecoder(strings.NewReader(string(data)))
cal, e := dec.Decode()
if e != nil {
return nil, fmt.Errorf("%w: malformed VCALENDAR", ErrInvalid)
}
if _, e = dec.Decode(); e != io.EOF {
return nil, ErrInvalid
}
version, _ := cal.Props.Text("VERSION")
product, _ := cal.Props.Text("PRODID")
if len(cal.Props["VERSION"]) != 1 || version != "2.0" || len(cal.Props["PRODID"]) != 1 || product == "" || cal.Props.Get("METHOD") != nil {
return nil, ErrInvalid
}
if scale := cal.Props.Get("CALSCALE"); scale != nil && (len(cal.Props["CALSCALE"]) != 1 || !strings.EqualFold(scale.Value, "GREGORIAN")) {
return nil, ErrInvalid
}
result := []Event{}
seen := map[string]bool{}
for _, child := range cal.Children {
if child.Name != "VEVENT" {
return nil, fmt.Errorf("%w: source contains unsupported %s", ErrInvalid, child.Name)
}
payload, e := encode(envelope(child))
if e != nil {
return nil, ErrInvalid
}
event, e := Parse(payload)
if e != nil {
return nil, e
}
if seen[event.UID] {
return nil, fmt.Errorf("%w: duplicate UID or recurrence exception", ErrInvalid)
}
seen[event.UID] = true
result = append(result, event)
if len(result) > 5000 {
return nil, ErrLimit
}
}
return result, nil
}
func (r *Repository) SetSource(ctx context.Context, domain, id, actor, version string, source Source) (Calendar, error) {
dir, e := r.dir(domain, id)
if e != nil {
return Calendar{}, e
}
unlock, e := lock(ctx, dir)
if e != nil {
return Calendar{}, e
}
defer unlock()
c, e := readMeta(dir)
if e != nil {
return c, e
}
if actor == "" || c.Owner != actor {
return c, ErrForbidden
}
if c.Version != version {
return c, ErrConflict
}
if source.IntervalSeconds < 60 || source.IntervalSeconds > 604800 || source.URL == "" {
return c, ErrInvalid
}
if source.Kind == "" {
source.Kind = "ics"
}
if source.Kind != "ics" && source.Kind != "caldav" {
return c, ErrInvalid
}
if c.Source == nil && len(c.Months) > 0 {
return c, fmt.Errorf("%w: use an empty calendar for an external source", ErrInvalid)
}
if c.Source != nil && c.Source.URL == source.URL && c.Source.Kind == source.Kind && c.Source.Credential == source.Credential {
previous := *c.Source
previous.IntervalSeconds = source.IntervalSeconds
previous.Enabled = source.Enabled
previous.Next = 0
source = previous
}
c.Source = &source
c.ReadOnly = true
e = saveMeta(dir, &c)
return c, e
}
func (r *Repository) Sources(domain string) ([]Calendar, error) {
if !validID(domain) {
return nil, ErrMissing
}
entries, e := os.ReadDir(filepath.Join(r.Root, domain))
if os.IsNotExist(e) {
return []Calendar{}, nil
}
if e != nil {
return nil, e
}
out := []Calendar{}
for _, entry := range entries {
if !entry.IsDir() || !validID(entry.Name()) {
continue
}
c, e := readMeta(filepath.Join(r.Root, domain, entry.Name()))
if e != nil {
continue
}
if c.Source != nil {
out = append(out, c)
}
}
return out, nil
}
// A complete, validated source response replaces the imported snapshot in a
// single manifest commit. Transport/validation failures never erase events.
func (r *Repository) ApplySource(ctx context.Context, domain string, before Calendar, events []Event, etag, modified string, notModified bool, syncErr error, resources ...map[string]RemoteResource) error {
dir, e := r.dir(domain, before.ID)
if e != nil {
return e
}
unlock, e := lock(ctx, dir)
if e != nil {
return e
}
defer unlock()
c, e := readMeta(dir)
if e != nil {
return e
}
if c.Source == nil || c.Version != before.Version {
return ErrConflict
}
if syncErr == nil && !notModified {
// Provider response ordering must not rewrite unchanged monthly files.
events = append([]Event(nil), events...)
sort.Slice(events, func(i, j int) bool { return events[i].UID < events[j].UID })
months := map[string][]*ical.Component{}
for _, event := range events {
child, e := component(event.Data)
if e != nil {
return e
}
month := event.Start.Format("2006-01")
months[month] = append(months[month], child)
}
hashes := map[string]string{}
for month, children := range months {
if len(children) > 5000 {
return ErrLimit
}
cal := envelope(children...)
cal.Props.SetText("NAME", c.PrettyName)
data, e := encode(cal)
if e != nil {
return e
}
if len(data) > 16<<20 {
return ErrLimit
}
hash := digest([]byte(data))
if c.Months[month] != hash {
if e = atomicFile(filepath.Join(dir, month+"."+hash+".ics"), []byte(data)); e != nil {
return e
}
}
hashes[month] = hash
}
c.Months = hashes
c.Source.ETag = etag
c.Source.Modified = modified
if len(resources) > 0 {
c.Source.Resources = resources[0]
}
}
now := time.Now()
c.Source.Checked = now.UnixMilli()
c.Source.Next = now.Add(time.Duration(c.Source.IntervalSeconds) * time.Second).UnixMilli()
if syncErr != nil {
c.Source.Error = syncErr.Error()
} else {
c.Source.Error = ""
c.Source.Success = now.UnixMilli()
}
if e = saveMeta(dir, &c); e != nil {
return e
}
pruneSnapshots(dir, c)
return nil
}
+53
View File
@@ -0,0 +1,53 @@
package calendar
import (
"context"
"errors"
"testing"
"time"
)
func TestCalendarSourceAtomicSnapshotAndFailure(t *testing.T) {
r, d, a, c := fixture(t)
ctx := context.Background()
c, e := r.SetSource(ctx, d, c.ID, a, c.Version, Source{Kind: "ics", URL: "https://example.org/feed.ics", IntervalSeconds: 60, Enabled: true})
if e != nil {
t.Fatal(e)
}
event := sample(t, "external", "2026-09-22T09:00:00Z", "2026-09-22T10:00:00Z", false)
events, e := ParseFeed([]byte(event.Data))
if e != nil || len(events) != 1 {
t.Fatal(e)
}
if e = r.ApplySource(ctx, d, c, events, `"tag"`, "", false, nil); e != nil {
t.Fatal(e)
}
c, _ = r.Get(d, c.ID, a)
if _, e = r.Change(ctx, d, c.ID, a, event.UID, event.ETag, nil); !errors.Is(e, ErrForbidden) {
t.Fatal("external calendar editable", e)
}
if e = r.ApplySource(ctx, d, c, nil, "", "", false, errors.New("network failed")); e != nil {
t.Fatal(e)
}
retained, e := r.Event(ctx, d, c.ID, a, event.UID)
if e != nil || retained.UID != event.UID {
t.Fatal("failure erased snapshot", e)
}
c, _ = r.Get(d, c.ID, a)
if c.Source.Error == "" || c.Source.Next <= time.Now().UnixMilli() {
t.Fatal(c.Source)
}
if e = r.ApplySource(ctx, d, c, nil, "", "", true, nil); e != nil {
t.Fatal(e)
}
c, _ = r.Get(d, c.ID, a)
if c.Source.ETag != `"tag"` || c.Source.Error != "" {
t.Fatal(c.Source)
}
if e = r.ApplySource(ctx, d, c, []Event{}, "new", "", false, nil); e != nil {
t.Fatal(e)
}
if _, e = r.Event(ctx, d, c.ID, a, event.UID); !errors.Is(e, ErrMissing) {
t.Fatal("confirmed removal retained", e)
}
}
+48
View File
@@ -0,0 +1,48 @@
package calendar
import (
"context"
"database/sql"
)
// PersonalLocal excludes shared, public and imported calendars. In particular,
// a task integration must never load external calendar event contents.
func (c Calendar) PersonalLocal(actor string) bool {
return actor != "" && c.Owner == actor && c.Source == nil && !c.Public && len(c.Members) == 0
}
// ReadPersonalLocalIndex permits a bounded metadata projection against the
// refreshed cache. The callback must not call back into this repository.
func (r *Repository) ReadPersonalLocalIndex(ctx context.Context, domain, id, actor string, read func(Calendar) error) error {
c, _, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.PersonalLocal(actor) })
if err != nil {
return err
}
defer unlock()
return read(c)
}
// TaskBlock gets a current event, or creates it if absent and a draft is supplied.
// Retries NEVER overwrite an event moved/edited in the calendar. Metadata policy
// is checked under the same lock as snapshot access and the eventual commit.
func (r *Repository) TaskBlock(ctx context.Context, domain, id, actor, uid string, draft *Event) (Calendar, Event, error) {
c, dir, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.PersonalLocal(actor) })
if err != nil {
return Calendar{}, Event{}, err
}
defer unlock()
var data string
err = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&data)
if err == nil {
event, e := Parse(data)
return c, event, e
}
if err != sql.ErrNoRows {
return c, Event{}, err
}
if draft == nil {
return c, Event{}, ErrMissing
}
event, err := r.changeLocked(ctx, domain, actor, uid, "*", draft, c, dir)
return c, event, err
}
+47
View File
@@ -0,0 +1,47 @@
package calendar
import (
"context"
"errors"
"strings"
"testing"
"github.com/google/uuid"
)
func TestTaskBlockGuardBeforeSnapshotAccess(t *testing.T) {
for _, mode := range []string{"external", "public", "shared", "other-owner"} {
t.Run(mode, func(t *testing.T) {
r, domain, actor, c := fixture(t)
dir, _ := r.dir(domain, c.ID)
// A deliberately absent snapshot detects ANY attempted refresh.
c.Months["2026-09"] = strings.Repeat("a", 64)
switch mode {
case "external":
c.Source = &Source{Kind: "ics", URL: "https://example.invalid/private.ics"}
case "public":
c.Public = true
case "shared":
c.Members = map[string]string{uuid.NewString(): "read"}
case "other-owner":
actor = "someone-else"
}
if err := saveMeta(dir, &c); err != nil {
t.Fatal(err)
}
_, _, err := r.TaskBlock(context.Background(), domain, c.ID, actor, "fake", nil)
if !errors.Is(err, ErrForbidden) && !errors.Is(err, ErrMissing) {
t.Fatalf("snapshot accessed: %v", err)
}
called := false
err = r.ReadPersonalLocalIndex(context.Background(), domain, c.ID, actor, func(Calendar) error { called = true; return nil })
if called || (!errors.Is(err, ErrForbidden) && !errors.Is(err, ErrMissing)) {
t.Fatalf("range projection accessed blocked snapshot: %v", err)
}
var n int
if err := r.DB.QueryRow(`SELECT count(*) FROM calendar_index_state`).Scan(&n); err != nil || n != 0 {
t.Fatalf("cache populated %d %v", n, err)
}
})
}
}
+21 -2
View File
@@ -1,7 +1,9 @@
package db
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"github.com/google/uuid"
)
@@ -28,6 +30,14 @@ func Bootstrap(
rootUUID :=
uuid.NewString()
rootBody :=
"<h1>Pages Wiki</h1><p>Bootstrap complete.</p>"
rootBodySum :=
sha256.Sum256(
[]byte(rootBody),
)
_, err = database.Exec(`
INSERT INTO objects ( uuid, created_at ) VALUES ( ?, strftime('%s','now') )`,
rootUUID,
@@ -61,18 +71,27 @@ func Bootstrap(
uuid,
object_uuid,
body,
document_hash,
lang,
message,
created_at_ms,
created_at
)
VALUES (
'bootstrap-root',
?,
'<h1>Pages Wiki</h1><p>Bootstrap complete.</p>',
?,
?,
?,
'und',
'bootstrap',
CAST(strftime('%s','now') AS INTEGER) * 1000,
strftime('%s','now')
)
`,
uuid.NewString(),
rootUUID,
rootBody,
hex.EncodeToString(rootBodySum[:]),
)
return err
+33 -2
View File
@@ -2,12 +2,29 @@ package db
import (
"database/sql"
"net/url"
"path/filepath"
_ "modernc.org/sqlite"
)
func Open(path string) (*sql.DB, error) {
return sql.Open("sqlite", path)
absolutePath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
dsn := (&url.URL{
Scheme: "file",
Path: absolutePath,
RawQuery: url.Values{
"_pragma": {
"busy_timeout(5000)",
"journal_mode(WAL)",
"synchronous(NORMAL)",
},
}.Encode(),
}).String()
return sql.Open("sqlite", dsn)
}
func Init(db *sql.DB) error {
@@ -22,13 +39,18 @@ func Init(db *sql.DB) error {
created_at INTEGER NOT NULL
)
`,
// revisions: has UUID, object uuid, body, message, created_at
// revisions: immutable event metadata referencing a Document hash
`
CREATE TABLE IF NOT EXISTS revisions (
uuid TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
body TEXT NOT NULL,
document_hash TEXT NOT NULL,
lang TEXT NOT NULL DEFAULT 'und',
content_type TEXT NOT NULL DEFAULT 'text/html; charset=utf-8',
filename TEXT NOT NULL DEFAULT '',
message TEXT NOT NULL,
created_at_ms INTEGER NOT NULL,
created_at INTEGER NOT NULL
)
`,
@@ -46,6 +68,15 @@ func Init(db *sql.DB) error {
object_uuid TEXT NOT NULL,
tag TEXT NOT NULL
)
`,
`
CREATE TABLE IF NOT EXISTS assets (
address TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
head_document_hash TEXT,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL
)
`,
}
+177 -3
View File
@@ -1,6 +1,10 @@
package db
import "database/sql"
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
)
func Migrate(
db *sql.DB,
@@ -16,6 +20,14 @@ func Migrate(
strftime('%s','now')
)
)
`,
`
CREATE TABLE IF NOT EXISTS object_origins (
object_uuid TEXT PRIMARY KEY REFERENCES objects(uuid) ON DELETE CASCADE,
domain_uuid TEXT NOT NULL,
created_at INTEGER NOT NULL
)
`,
`
@@ -23,7 +35,12 @@ func Migrate(
uuid TEXT PRIMARY KEY,
object_uuid TEXT,
body TEXT,
message TEXT,
document_hash TEXT,
lang TEXT NOT NULL DEFAULT 'und',
content_type TEXT NOT NULL DEFAULT 'text/html; charset=utf-8',
filename TEXT NOT NULL DEFAULT '',
message TEXT,
created_at_ms INTEGER,
created_at INTEGER DEFAULT (
strftime('%s','now')
)
@@ -49,6 +66,25 @@ func Migrate(
)
)
`,
`
CREATE TABLE IF NOT EXISTS assets (
address TEXT PRIMARY KEY,
object_uuid TEXT NOT NULL,
head_document_hash TEXT,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL
)
`,
`
CREATE TABLE IF NOT EXISTS revision_document_refs (
revision_uuid TEXT NOT NULL,
document_hash TEXT NOT NULL,
PRIMARY KEY (revision_uuid, document_hash),
FOREIGN KEY (revision_uuid) REFERENCES revisions(uuid) ON DELETE CASCADE
)
`,
}
for _, stmt := range stmts {
@@ -75,6 +111,29 @@ func Migrate(
`
ALTER TABLE revisions
ADD COLUMN created_at INTEGER
`,
`
ALTER TABLE revisions
ADD COLUMN document_hash TEXT
`,
`
ALTER TABLE revisions
ADD COLUMN lang TEXT NOT NULL DEFAULT 'und'
`,
`
ALTER TABLE revisions
ADD COLUMN created_at_ms INTEGER
`,
`
ALTER TABLE revisions
ADD COLUMN content_type TEXT NOT NULL DEFAULT 'text/html; charset=utf-8'
`,
`
ALTER TABLE revisions
ADD COLUMN filename TEXT NOT NULL DEFAULT ''
`,
`
@@ -87,5 +146,120 @@ func Migrate(
_, _ = db.Exec(stmt)
}
return nil
if err := migrateRevisionMetadata(db); err != nil {
return err
}
if _, err := db.Exec(`CREATE INDEX IF NOT EXISTS idx_revision_document_refs_hash ON revision_document_refs(document_hash,revision_uuid)`); err != nil {
return err
}
// Older databases can already carry revision_metadata_v1 without the
// subsequently added index. Ensure it independently of the data backfill.
if _, err := db.Exec(`CREATE INDEX IF NOT EXISTS idx_revisions_object_history ON revisions(object_uuid, created_at_ms)`); err != nil {
return err
}
_, err := db.Exec(`CREATE INDEX IF NOT EXISTS idx_revisions_object_created
ON revisions(object_uuid, created_at_ms DESC)`)
return err
}
func migrateRevisionMetadata(db *sql.DB) error {
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS schema_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
)
`); err != nil {
return err
}
var applied int
if err := db.QueryRow(`
SELECT EXISTS(
SELECT 1
FROM schema_migrations
WHERE name = 'revision_metadata_v1'
)
`).Scan(&applied); err != nil {
return err
}
if applied != 0 {
return nil
}
rows, err := db.Query(`
SELECT
uuid,
COALESCE(body, '')
FROM revisions
WHERE document_hash IS NULL
OR document_hash = ''
`)
if err != nil {
return err
}
type legacyRevision struct {
uuid string
body string
}
var revisions []legacyRevision
for rows.Next() {
var revision legacyRevision
if err := rows.Scan(&revision.uuid, &revision.body); err != nil {
rows.Close()
return err
}
revisions = append(revisions, revision)
}
if err := rows.Close(); err != nil {
return err
}
for _, revision := range revisions {
sum := sha256.Sum256([]byte(revision.body))
documentHash := hex.EncodeToString(sum[:])
if _, err := db.Exec(`
UPDATE revisions
SET document_hash = ?
WHERE uuid = ?
`, documentHash, revision.uuid); err != nil {
return err
}
}
if _, err = db.Exec(`
UPDATE revisions
SET
lang = COALESCE(NULLIF(lang, ''), 'und'),
created_at_ms = COALESCE(
created_at_ms,
COALESCE(created_at, 0) * 1000
)
WHERE lang IS NULL
OR lang = ''
OR created_at_ms IS NULL
`); err != nil {
return err
}
if _, err = db.Exec(`
CREATE INDEX IF NOT EXISTS idx_revisions_object_created
ON revisions (
object_uuid,
created_at_ms DESC
)
`); err != nil {
return err
}
_, err = db.Exec(`
INSERT OR IGNORE INTO schema_migrations (name, applied_at)
VALUES ('revision_metadata_v1', strftime('%s','now'))
`)
return err
}
+44
View File
@@ -0,0 +1,44 @@
package db
import (
"path/filepath"
"testing"
)
func TestRevisionMetadataMigrationIsRecorded(t *testing.T) {
database, err := Open(filepath.Join(t.TempDir(), "pages.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = database.Close() })
if err := Migrate(database); err != nil {
t.Fatal(err)
}
var applied int
if err := database.QueryRow(`
SELECT count(*)
FROM schema_migrations
WHERE name = 'revision_metadata_v1'
`).Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 1 {
t.Fatalf("migration marker count = %d, want 1", applied)
}
// Simulate a deployment which applied the data backfill before the index
// was introduced. A completed migration must not skip missing indexes.
if _, err := database.Exec(`DROP INDEX idx_revisions_object_created`); err != nil {
t.Fatal(err)
}
if err := Migrate(database); err != nil {
t.Fatalf("second migration run failed: %v", err)
}
var indexes int
if err := database.QueryRow(`SELECT count(*) FROM sqlite_master WHERE type='index' AND name='idx_revisions_object_created'`).Scan(&indexes); err != nil {
t.Fatal(err)
}
if indexes != 1 {
t.Fatal("missing revision index after migration")
}
}
+32
View File
@@ -0,0 +1,32 @@
// Package extensions describes trusted system services, separate from MIME
// viewers/editors. The initial registry is built-in only: a manifest is not
// permission to load arbitrary Go, JavaScript, executables, or remote URLs.
package extensions
type Manifest struct {
ID string `json:"id"`
APIVersion int `json:"api_version"`
Version string `json:"version"`
Kind string `json:"kind"`
Scope string `json:"scope"`
NameKey string `json:"name_key"`
DescriptionKey string `json:"description_key"`
Capabilities []string `json:"capabilities"`
Events []string `json:"events"`
SettingsPanel string `json:"settings_panel"`
Builtin bool `json:"builtin"`
Enabled bool `json:"enabled"`
ResourceClass string `json:"resource_class,omitempty"`
}
func Catalog() []Manifest {
return []Manifest{
{ID: "organizer.tasks", APIVersion: 1, Version: "0.11.1", Kind: "tasks", Scope: "domain", NameKey: "tasks.title", DescriptionKey: "tasks.help", Capabilities: []string{"tasks.own.read", "tasks.own.write"}, Events: []string{"organizer.task.changed", "organizer.project.changed"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+background+index"},
{ID: "organizer.calendar", APIVersion: 1, Version: "0.2.0", Kind: "calendar", Scope: "domain", NameKey: "calendar.title", DescriptionKey: "calendar.help", Capabilities: []string{"calendar.read", "calendar.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "content.diary", APIVersion: 1, Version: "0.1.0", Kind: "diary", Scope: "domain", NameKey: "periodical.diary", DescriptionKey: "periodical.diary_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "diary", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "content.blog", APIVersion: 1, Version: "0.1.0", Kind: "blog", Scope: "domain", NameKey: "periodical.blog", DescriptionKey: "periodical.blog_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "blog", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "system.resources", APIVersion: 1, Version: "1.0.0", Kind: "monitor", Scope: "instance", NameKey: "config.resource_status", DescriptionKey: "extension.resources", Capabilities: []string{"instance.metrics.read", "instance.metrics.store"}, Events: []string{"resource.sample"}, SettingsPanel: "resources", Builtin: true, Enabled: true, ResourceClass: "background+io"},
{ID: "navigation.bookmarks", APIVersion: 1, Version: "1.0.0", Kind: "navigation", Scope: "domain", NameKey: "home.bookmarks", DescriptionKey: "extension.bookmarks", Capabilities: []string{"domain.bookmarks.manage", "path.read"}, Events: []string{}, SettingsPanel: "bookmarks", Builtin: true, Enabled: true, ResourceClass: "foreground+read"},
{ID: "sharing.x", APIVersion: 1, Version: "1.0.0", Kind: "sharing", Scope: "domain", NameKey: "extension.share_x", DescriptionKey: "extension.share_x_help", Capabilities: []string{"page.share"}, Events: []string{"share.intent.created"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+network"},
}
}
+8 -4
View File
@@ -1,8 +1,12 @@
package revision
type Revision struct {
UUID string
ObjectUUID string
Message string
CreatedAt int64
UUID string
ObjectUUID string
DocumentHash string
Language string
ContentType string
Filename string
Message string
CreatedAtMS int64
}
+42
View File
@@ -0,0 +1,42 @@
package server
import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
)
const adapterGatewayPrefix = "/_/adaptor"
func newAdapterGatewayProxy() http.Handler {
origin := strings.TrimSpace(os.Getenv("PAGESWIKI_ADAPTER_GATEWAY_ORIGIN"))
if origin == "" {
origin = "http://adaptor-gateway:3000"
}
target, err := url.Parse(origin)
if err != nil {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "invalid adapter gateway origin", http.StatusBadGateway)
})
}
proxy := httputil.NewSingleHostReverseProxy(target)
director := proxy.Director
proxy.Director = func(request *http.Request) {
request.URL.Path = strings.TrimPrefix(request.URL.Path, adapterGatewayPrefix)
if request.URL.Path == "" {
request.URL.Path = "/"
}
request.Header.Set("X-Forwarded-Prefix", adapterGatewayPrefix)
if request.Header.Get("X-Forwarded-Proto") == "" {
if request.TLS != nil {
request.Header.Set("X-Forwarded-Proto", "https")
} else {
request.Header.Set("X-Forwarded-Proto", "http")
}
}
director(request)
}
return proxy
}
+477
View File
@@ -0,0 +1,477 @@
package server
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"net/url"
"path/filepath"
"regexp"
"sort"
"strings"
)
const adapterPluginsConfigKey = "projection_adapter_plugins"
type adapterPlugin struct {
ID string `json:"id"`
Name string `json:"name"`
Mode string `json:"mode"` // hosted viewer or hosted editor
URLTemplate string `json:"url_template"`
MediaTypes []string `json:"media_types,omitempty"`
Extensions []string `json:"extensions,omitempty"`
Enabled bool `json:"enabled"`
NewTab bool `json:"new_tab,omitempty"`
HTMLMarkdown bool `json:"html_markdown,omitempty"`
DefaultEditor bool `json:"default_editor,omitempty"`
Unsupported bool `json:"unsupported,omitempty"`
}
type adapterAction struct {
ID string `json:"id"`
Name string `json:"name"`
Mode string `json:"mode"`
Builtin bool `json:"builtin"`
OpenURL string `json:"open_url"`
NewTab bool `json:"new_tab,omitempty"`
Default bool `json:"default,omitempty"`
}
type builtinAdapterInfo struct {
ID string `json:"id"`
Kind string `json:"kind"`
Delivery string `json:"delivery"`
Selectable bool `json:"selectable"`
}
var adapterIDPattern = regexp.MustCompile(`^[a-z][a-z0-9._-]{1,63}$`)
func builtinAdapterManifest() []builtinAdapterInfo {
out := make([]builtinAdapterInfo, 0, len(projectionAdapters))
for _, adapter := range projectionAdapters {
out = append(out, builtinAdapterInfo{
ID: adapter.ID, Kind: adapter.Kind,
Delivery: string(adapter.Delivery), Selectable: true,
})
}
return out
}
func (s *Server) adapterPlugins(domainUUID string) ([]adapterPlugin, error) {
raw, err := s.store.GetConfigInDomain(domainUUID, adapterPluginsConfigKey)
if err != nil || strings.TrimSpace(raw) == "" {
return []adapterPlugin{}, err
}
var plugins []adapterPlugin
if err := json.Unmarshal([]byte(raw), &plugins); err != nil {
return nil, err
}
// Native execution is reserved for a future local-helper protocol. Keep
// legacy records visible, but never advertise or launch them.
for index := range plugins {
validated := plugins[index]
if err := validateAdapterPlugin(&validated); err != nil {
plugins[index].Enabled = false
plugins[index].Unsupported = true
}
}
return plugins, nil
}
func (s *Server) saveAdapterPlugins(domainUUID string, plugins []adapterPlugin) error {
sort.Slice(plugins, func(i, j int) bool { return plugins[i].ID < plugins[j].ID })
data, err := json.Marshal(plugins)
if err != nil {
return err
}
return s.store.SetConfigInDomain(domainUUID, adapterPluginsConfigKey, string(data))
}
func validateAdapterPlugin(plugin *adapterPlugin) error {
plugin.ID = strings.ToLower(strings.TrimSpace(plugin.ID))
plugin.Name = strings.TrimSpace(plugin.Name)
plugin.Mode = strings.ToLower(strings.TrimSpace(plugin.Mode))
plugin.URLTemplate = strings.TrimSpace(plugin.URLTemplate)
if !adapterIDPattern.MatchString(plugin.ID) || plugin.Name == "" {
return errors.New("invalid adapter id or name")
}
switch plugin.Mode {
case "viewer", "editor":
default:
return errors.New("mode must be viewer or editor; native integration is not implemented")
}
if !strings.Contains(plugin.URLTemplate, "{url}") &&
!strings.Contains(plugin.URLTemplate, "{path}") &&
!strings.Contains(plugin.URLTemplate, "{ticket}") {
return errors.New("URL template must contain {url}, {path}, or {ticket}")
}
if plugin.Mode == "editor" && !strings.Contains(plugin.URLTemplate, "{ticket}") {
return errors.New("hosted editor URL template must contain {ticket}")
}
sample := strings.NewReplacer(
"{url}", "https://example.invalid/v/raw/path:/sample.txt",
"{path}", "/sample.txt",
"{filename}", "sample.txt",
"{ticket}", "pwa_t_sample",
"{origin}", "https://pages.example.invalid",
).Replace(plugin.URLTemplate)
parsed, err := url.Parse(sample)
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") {
return errors.New("hosted adapter URL must use http or https")
}
for index, extension := range plugin.Extensions {
extension = strings.ToLower(strings.TrimSpace(extension))
if extension != "" && !strings.HasPrefix(extension, ".") {
extension = "." + extension
}
plugin.Extensions[index] = extension
}
for index, mediaType := range plugin.MediaTypes {
plugin.MediaTypes[index] = strings.ToLower(strings.TrimSpace(mediaType))
}
return nil
}
func pluginMatches(plugin adapterPlugin, mediaType, filename string) bool {
if !plugin.Enabled || plugin.Unsupported || plugin.Mode == "native" {
return false
}
if len(plugin.MediaTypes) == 0 && len(plugin.Extensions) == 0 {
return true
}
for _, candidate := range plugin.MediaTypes {
if candidate == mediaType ||
(strings.HasSuffix(candidate, "/*") &&
strings.HasPrefix(mediaType, strings.TrimSuffix(candidate, "*"))) {
return true
}
}
extension := strings.ToLower(filepath.Ext(filename))
for _, candidate := range plugin.Extensions {
if extension == candidate {
return true
}
}
return false
}
func (s *Server) adapterActionsForPath(r *http.Request, semanticPath string) ([]adapterAction, error) {
domainUUID := s.domainUUIDForRequest(r)
objectUUID, err := s.store.ResolvePathInDomain(domainUUID, semanticPath)
if err != nil {
return nil, err
}
revision, err := s.latestRevisionForRequest(r, objectUUID)
if err != nil {
return nil, err
}
mediaType, _, _ := mime.ParseMediaType(revision.ContentType)
filename := revision.Filename
if filename == "" {
filename = filepath.Base(semanticPath)
}
selected := selectProjectionAdapter(contentDescriptor{
MediaType: strings.ToLower(mediaType), Filename: filename,
SizeBytes: int64(len(revision.Body)),
})
actions := []adapterAction{
{ID: selected.ID, Name: selected.ID, Mode: "viewer", Builtin: true, OpenURL: semanticPath},
{ID: "download", Name: "download", Mode: "viewer", Builtin: true,
OpenURL: rawProjectionURL("rev:" + revision.UUID)},
}
if selected.NeedsText || selected.ID == "html" || selected.ID == "excalidraw" {
actions = append(actions, adapterAction{
ID: "editor", Name: "Pages Wiki Editor", Mode: "editor",
Builtin: true, OpenURL: "/e" + semanticPath,
})
}
plugins, err := s.adapterPlugins(domainUUID)
if err != nil {
return nil, err
}
sort.SliceStable(plugins, func(i, j int) bool {
return plugins[i].DefaultEditor && !plugins[j].DefaultEditor
})
for _, plugin := range plugins {
if pluginMatches(plugin, strings.ToLower(mediaType), filename) {
actions = append(actions, adapterAction{
ID: plugin.ID, Name: plugin.Name, Mode: plugin.Mode,
NewTab: plugin.NewTab,
Default: plugin.DefaultEditor,
OpenURL: "/v/open-with?adapter=" + url.QueryEscape(plugin.ID) +
"&path=" + url.QueryEscape(semanticPath),
})
}
}
return actions, nil
}
func (s *Server) handleAdapterAPI(w http.ResponseWriter, r *http.Request) {
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter"), "/")
domainUUID := s.domainUUIDForRequest(r)
switch action {
case "":
if r.Method == http.MethodGet {
plugins, err := s.adapterPlugins(domainUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
response := map[string]any{
"builtin": builtinAdapterManifest(), "plugins": plugins,
}
if path := strings.TrimSpace(r.URL.Query().Get("path")); path != "" {
actions, err := s.adapterActionsForPath(r, path)
if err != nil {
status := 500
if errors.Is(err, sql.ErrNoRows) {
status = 404
}
writeEventJSON(w, status, map[string]string{"error": err.Error()})
return
}
response["actions"] = actions
}
writeEventJSON(w, 200, response)
return
}
if r.Method == http.MethodPost || r.Method == http.MethodPut {
var plugin adapterPlugin
if !decodeEventJSON(w, r, &plugin) {
return
}
if err := validateAdapterPlugin(&plugin); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
plugins, err := s.adapterPlugins(domainUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
if plugin.DefaultEditor {
if plugin.Mode != "editor" {
writeEventJSON(w, 400, map[string]string{"error": "only an editor can be the default editor"})
return
}
for index := range plugins {
plugins[index].DefaultEditor = false
}
}
replaced := false
for index := range plugins {
if plugins[index].ID == plugin.ID {
plugins[index] = plugin
replaced = true
}
}
if !replaced {
plugins = append(plugins, plugin)
}
if err := s.saveAdapterPlugins(domainUUID, plugins); err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, plugin)
return
}
case "delete":
if r.Method == http.MethodPost || r.Method == http.MethodDelete {
var input struct {
ID string `json:"id"`
}
if !decodeEventJSON(w, r, &input) {
return
}
plugins, err := s.adapterPlugins(domainUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
filtered := plugins[:0]
for _, plugin := range plugins {
if plugin.ID != input.ID {
filtered = append(filtered, plugin)
}
}
if err := s.saveAdapterPlugins(domainUUID, filtered); err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]bool{"deleted": true})
return
}
}
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported adapter operation"})
}
func (s *Server) handleOpenWith(w http.ResponseWriter, r *http.Request) {
semanticPath := strings.TrimSpace(r.URL.Query().Get("path"))
adapterID := strings.TrimSpace(r.URL.Query().Get("adapter"))
if semanticPath == "" || adapterID == "" {
http.Error(w, "path and adapter are required", http.StatusBadRequest)
return
}
if s.authorizePathRequest(r, "read", semanticPath) != nil {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
plugins, err := s.adapterPlugins(s.domainUUIDForRequest(r))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
var selected *adapterPlugin
for index := range plugins {
if plugins[index].ID == adapterID && plugins[index].Enabled {
selected = &plugins[index]
break
}
}
if selected == nil {
http.NotFound(w, r)
return
}
actions, err := s.adapterActionsForPath(r, semanticPath)
if err != nil {
http.Error(w, err.Error(), 404)
return
}
allowed := false
for _, action := range actions {
if action.ID == selected.ID {
allowed = true
}
}
if !allowed {
http.Error(w, "adapter does not support this document", http.StatusBadRequest)
return
}
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), semanticPath)
if err != nil {
http.NotFound(w, r)
return
}
revision, err := s.latestRevisionForRequest(r, objectUUID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
scheme := "https"
if r.TLS == nil {
scheme = "http"
}
if forwarded := strings.ToLower(strings.TrimSpace(
strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0],
)); forwarded == "http" || forwarded == "https" {
scheme = forwarded
}
rawURL := fmt.Sprintf("%s://%s%s", scheme, r.Host, rawProjectionURL("rev:"+revision.UUID))
filename := revision.Filename
if filename == "" {
filename = filepath.Base(semanticPath)
}
ticket := ""
if selected.Mode == "editor" {
if s.authorizePathRequest(r, "write", semanticPath) != nil {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
principalUUID := ""
if principal, principalErr := s.store.PrincipalForSession(s.domainUUIDForRequest(r), s.authTokenFromRequest(r)); principalErr == nil {
principalUUID = principal.UUID
}
ticket, err = s.store.IssueAdapterTicketForPrincipal(
principalUUID, s.domainUUIDForRequest(r), selected.ID, objectUUID, revision.UUID, semanticPath,
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
origin := fmt.Sprintf("%s://%s", scheme, r.Host)
target := strings.NewReplacer(
"{url}", url.QueryEscape(rawURL),
"{path}", url.QueryEscape(semanticPath),
"{filename}", url.QueryEscape(filename),
"{ticket}", url.QueryEscape(ticket),
"{origin}", url.QueryEscape(origin),
).Replace(selected.URLTemplate)
http.Redirect(w, r, target, http.StatusFound)
}
func (s *Server) handleAdapterPluginConfig(w http.ResponseWriter, r *http.Request) {
domainUUID := s.domainUUIDForRequest(r)
id := strings.TrimSpace(r.URL.Query().Get("id"))
if r.Method == http.MethodPost {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id = strings.TrimSpace(r.FormValue("id"))
}
plugins, err := s.adapterPlugins(domainUUID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
index := -1
for candidate := range plugins {
if plugins[candidate].ID == id {
index = candidate
break
}
}
if index < 0 {
http.NotFound(w, r)
return
}
if r.Method == http.MethodPost {
if r.FormValue("connection_fields") == "1" {
plugins[index].Name = strings.TrimSpace(r.FormValue("name"))
plugins[index].Mode = strings.TrimSpace(r.FormValue("mode"))
plugins[index].URLTemplate = strings.TrimSpace(r.FormValue("url_template"))
plugins[index].Enabled = r.FormValue("enabled") != ""
plugins[index].MediaTypes = splitAdapterList(r.FormValue("media_types"))
plugins[index].Extensions = splitAdapterList(r.FormValue("extensions"))
if err := validateAdapterPlugin(&plugins[index]); err != nil {
http.Error(w, err.Error(), 400)
return
}
}
plugins[index].NewTab = boolFormValue(r.FormValue("new_tab")) == "true"
plugins[index].HTMLMarkdown = boolFormValue(r.FormValue("html_markdown")) == "true"
makeDefault := boolFormValue(r.FormValue("default_editor")) == "true"
if makeDefault && plugins[index].Mode != "editor" {
http.Error(w, "only an editor can be the default editor", http.StatusBadRequest)
return
}
if makeDefault {
for candidate := range plugins {
plugins[candidate].DefaultEditor = candidate == index
}
} else {
plugins[index].DefaultEditor = false
}
if err := s.saveAdapterPlugins(domainUUID, plugins); err != nil {
http.Error(w, err.Error(), 500)
return
}
http.Redirect(w, r, "/-/sys/config?panel="+url.QueryEscape("adapter-"+id)+"&saved=1", http.StatusFound)
return
}
http.Redirect(w, r, "/-/sys/config?panel="+url.QueryEscape("adapter-"+id), http.StatusFound)
}
func splitAdapterList(value string) []string {
var values []string
for _, item := range strings.Split(value, ",") {
if item = strings.TrimSpace(item); item != "" {
values = append(values, item)
}
}
return values
}
+72
View File
@@ -0,0 +1,72 @@
package server
import "testing"
func TestValidateAdapterPlugin(t *testing.T) {
plugin := adapterPlugin{
ID: "Code.Server", Name: "code-server", Mode: "editor",
URLTemplate: "https://code.example.test/open?ticket={ticket}&origin={origin}",
Extensions: []string{"md", ".txt"},
Enabled: true,
}
if err := validateAdapterPlugin(&plugin); err != nil {
t.Fatal(err)
}
if plugin.ID != "code.server" || plugin.Extensions[0] != ".md" {
t.Fatalf("normalized plugin = %#v", plugin)
}
}
func TestWorkspaceFilenamePreservesHierarchyAndMarkdownExtension(t *testing.T) {
if got := workspaceFilename("/guide/install", "/guide", "install.html", true); got != "install.md" {
t.Fatalf("child workspace filename = %q", got)
}
if got := workspaceFilename("/guide/linux/setup", "/guide", "setup.py", false); got != "linux/setup.py" {
t.Fatalf("nested workspace filename = %q", got)
}
}
func TestValidateAdapterPluginRejectsUnsafeOrUnboundTemplate(t *testing.T) {
tests := []adapterPlugin{
{ID: "bad.scheme", Name: "Bad", Mode: "viewer", URLTemplate: "javascript:{path}"},
{ID: "native.todo", Name: "Vim", Mode: "native", URLTemplate: "vim:{path}"},
{ID: "no.binding", Name: "Bad", Mode: "viewer", URLTemplate: "https://example.invalid/open"},
{ID: "bad.mode", Name: "Bad", Mode: "execute", URLTemplate: "https://example.invalid/{url}"},
{ID: "editor.raw", Name: "Bad", Mode: "editor", URLTemplate: "https://example.invalid/{url}"},
}
for _, plugin := range tests {
if err := validateAdapterPlugin(&plugin); err == nil {
t.Fatalf("plugin unexpectedly accepted: %#v", plugin)
}
}
}
func TestPluginMatchesMimeWildcardAndExtension(t *testing.T) {
plugin := adapterPlugin{
Enabled: true, MediaTypes: []string{"image/*"}, Extensions: []string{".drawio"},
}
if !pluginMatches(plugin, "image/png", "image.bin") {
t.Fatal("MIME wildcard did not match")
}
if !pluginMatches(plugin, "application/xml", "diagram.drawio") {
t.Fatal("extension did not match")
}
if pluginMatches(plugin, "text/plain", "notes.txt") {
t.Fatal("unrelated document matched")
}
}
func TestPluginMatchesNeverOffersLegacyNativeAdapter(t *testing.T) {
plugin := adapterPlugin{Enabled: true, Mode: "native"}
if pluginMatches(plugin, "text/plain", "notes.txt") {
t.Fatal("legacy native adapter was offered as a runtime action")
}
}
func TestValidateAdapterPluginRejectsLegacyEditorWithoutTicket(t *testing.T) {
plugin := adapterPlugin{ID: "vscode", Name: "VS Code", Mode: "editor",
URLTemplate: "vscode://file/{url}", Enabled: true}
if err := validateAdapterPlugin(&plugin); err == nil {
t.Fatal("legacy executable URL was accepted")
}
}
+248
View File
@@ -0,0 +1,248 @@
package server
import (
"encoding/base64"
"errors"
"net/http"
"path/filepath"
"strings"
"pageswiki/internal/compiler"
"pageswiki/internal/render"
"pageswiki/internal/store"
)
type adapterWorkspaceFile struct {
Path string `json:"path"`
SemanticPath string `json:"semantic_path"`
RevisionUUID string `json:"revision_uuid"`
ContentType string `json:"content_type"`
ContentBase64 string `json:"content_base64"`
}
func (s *Server) adapterPluginForSession(session *store.AdapterSession) adapterPlugin {
plugins, _ := s.adapterPlugins(session.DomainUUID)
for _, plugin := range plugins {
if plugin.ID == session.PluginID {
return plugin
}
}
return adapterPlugin{}
}
func workspaceFilename(semanticPath, rootPath, filename string, markdown bool) string {
if semanticPath == rootPath {
filename = filepath.Base(filename)
if filename == "." || filename == "" || filename == "/" {
filename = "index.html"
}
if markdown {
filename = strings.TrimSuffix(filename, filepath.Ext(filename)) + ".md"
}
return filename
}
relative := strings.TrimPrefix(semanticPath, strings.TrimRight(rootPath, "/")+"/")
if filepath.Ext(relative) == "" && filepath.Ext(filename) != "" {
relative += filepath.Ext(filename)
}
if markdown {
relative = strings.TrimSuffix(relative, filepath.Ext(relative)) + ".md"
}
return relative
}
func (s *Server) workspaceFile(session *store.AdapterSession, semanticPath string, plugin adapterPlugin) (adapterWorkspaceFile, error) {
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
if err != nil {
return adapterWorkspaceFile{}, err
}
revision, err := s.store.GetLatestRevisionForLanguages(objectUUID, []string{s.getLocale(session.DomainUUID), "und"})
if err != nil {
return adapterWorkspaceFile{}, err
}
body := revision.Body
markdown := plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(revision.ContentType), "text/html")
if markdown {
body, err = compiler.HTMLToMarkdown(body)
if err != nil {
return adapterWorkspaceFile{}, err
}
}
return adapterWorkspaceFile{
Path: workspaceFilename(semanticPath, session.RootPath, revision.Filename, markdown),
SemanticPath: semanticPath,
RevisionUUID: revision.UUID, ContentType: revision.ContentType,
ContentBase64: base64.StdEncoding.EncodeToString([]byte(body)),
}, nil
}
func adapterBearer(r *http.Request) string {
value := strings.TrimSpace(r.Header.Get("Authorization"))
if !strings.HasPrefix(value, "Bearer ") {
return ""
}
return strings.TrimSpace(strings.TrimPrefix(value, "Bearer "))
}
// authorizeAdapterSession re-checks the current principal on every operation;
// exchanging a ticket must not pin permissions for the lifetime of a session.
func (s *Server) authorizeAdapterSession(r *http.Request, session *store.AdapterSession, capability string, path string) error {
if session.PrincipalUUID == "" {
if mode, _ := s.store.GetConfigInDomain(session.DomainUUID, "authorization_mode"); mode == "enforcing" {
return store.ErrAccessDenied
}
return nil
}
// The session token is intentionally separate from the browser auth token;
// use the principal captured at ticket issuance and re-evaluate its current
// grants on every request.
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: session.DomainUUID, PrincipalUUID: session.PrincipalUUID, Capability: capability, TargetType: "path", TargetID: path})
}
func (s *Server) handleAdapterSessionAPI(w http.ResponseWriter, r *http.Request) {
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter/session"), "/")
if action == "exchange" && r.Method == http.MethodPost {
var input struct {
Ticket string `json:"ticket"`
}
if !decodeEventJSON(w, r, &input) {
return
}
token, session, err := s.store.ExchangeAdapterTicket(input.Ticket)
if err != nil {
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
return
}
revision, err := s.store.GetRevision(session.BaseRevisionUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{
"session_token": token, "expires_at_ms": session.ExpiresAtMS,
"plugin_id": session.PluginID,
"revision_uuid": revision.UUID, "filename": revision.Filename,
"content_type": revision.ContentType, "lang": revision.Language,
})
return
}
if action == "content" {
session, err := s.store.GetAdapterSession(adapterBearer(r))
if err != nil {
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
return
}
if r.Method == http.MethodGet {
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
return
}
file, err := s.workspaceFile(session, session.RootPath, s.adapterPluginForSession(session))
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, file)
return
}
if r.Method == http.MethodPut {
if err := s.authorizeAdapterSession(r, session, "write", session.RootPath); err != nil {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
return
}
var input struct {
ContentBase64 string `json:"content_base64"`
Message string `json:"message"`
Path string `json:"path"`
RevisionUUID string `json:"revision_uuid"`
}
if !decodeEventJSON(w, r, &input) {
return
}
payload, err := base64.StdEncoding.DecodeString(input.ContentBase64)
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid content_base64"})
return
}
if len(payload) > 64<<20 {
writeEventJSON(w, 413, map[string]string{"error": "content exceeds 64 MiB"})
return
}
semanticPath := session.RootPath
if input.Path != "" {
semanticPath = strings.TrimSpace(input.Path)
}
rootPrefix := strings.TrimRight(session.RootPath, "/") + "/"
if semanticPath != session.RootPath && !strings.HasPrefix(semanticPath, rootPrefix) {
writeEventJSON(w, 403, map[string]string{"error": "path is outside the adapter workspace"})
return
}
if err := s.authorizeAdapterSession(r, session, "write", semanticPath); err != nil {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter path authorization revoked"})
return
}
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
if err != nil {
writeEventJSON(w, 404, map[string]string{"error": err.Error()})
return
}
baseRevisionUUID := input.RevisionUUID
if baseRevisionUUID == "" && semanticPath == session.RootPath {
baseRevisionUUID = session.BaseRevisionUUID
}
base, err := s.store.GetRevision(baseRevisionUUID)
if err != nil || base.ObjectUUID != objectUUID {
writeEventJSON(w, 409, map[string]string{"error": "invalid base revision"})
return
}
plugin := s.adapterPluginForSession(session)
if plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(base.ContentType), "text/html") {
payload = []byte(render.Markdown(string(payload)))
}
revision, err := s.store.SaveHostedRevisionAt(session, objectUUID, baseRevisionUUID, payload, input.Message)
if err != nil {
status := 500
if errors.Is(err, store.ErrRevisionConflict) {
status = http.StatusConflict
}
writeEventJSON(w, status, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"revision_uuid": revision.UUID, "document_hash": revision.DocumentHash})
return
}
}
if action == "workspace" && r.Method == http.MethodGet {
session, err := s.store.GetAdapterSession(adapterBearer(r))
if err != nil {
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
return
}
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
return
}
paths := []string{session.RootPath}
children, err := s.store.ListChildrenInDomain(session.DomainUUID, session.RootPath)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
paths = append(paths, children...)
plugin := s.adapterPluginForSession(session)
files := make([]adapterWorkspaceFile, 0, len(paths))
for _, path := range paths {
file, fileErr := s.workspaceFile(session, path, plugin)
if fileErr == nil {
files = append(files, file)
}
}
payload := map[string]any{"root_path": session.RootPath, "files": files}
if session.PluginID == "vim.wasm" {
payload["vim_settings"] = s.domainVimSettings(session.DomainUUID)
}
writeEventJSON(w, 200, payload)
return
}
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported adapter session operation"})
}
+109
View File
@@ -0,0 +1,109 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestAnonymousPublicationChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
s.mux.HandleFunc("/", s.handleHome)
admin, e := s.store.PrincipalForSession(domain, token)
if e != nil {
t.Fatal(e)
}
q, e := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public")
if e != nil {
t.Fatal(e)
}
o, e := s.store.CreatePageAtPathInDomain(domain, "/public/page")
if e != nil {
t.Fatal(e)
}
if _, e = s.store.SaveRevisionContent(o, []byte(`<h1>Public browser sentinel</h1><script>window.authoredRan=true</script><p>{{children}}</p>`), "", "und", "text/html", ""); e != nil {
t.Fatal(e)
}
child, e := s.store.CreatePageAtPathInDomain(domain, "/public/page/child")
if e != nil {
t.Fatal(e)
}
if _, e = s.store.SaveRevisionContent(child, []byte("<p>Child</p>"), "", "und", "text/html", ""); e != nil {
t.Fatal(e)
}
for _, path := range []string{"/public/page/child/grandchild", "/public/page-other"} {
object, err := s.store.CreatePageAtPathInDomain(domain, path)
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(object, []byte("<p>Navigation fixture</p>"), "", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
}
policy := "public"
for _, node := range []struct{ kind, id string }{{"domain", domain}, {"quartier", q}} {
g, e := s.store.CityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.ChangeCityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy, ConfirmRelaxation: true}, false); e != nil {
t.Fatal(e)
}
}
script, e := os.ReadFile("internal/server/testdata/anonymous-publication.js")
if e != nil {
t.Fatal(e)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
if r.Header.Get("Origin") != "" {
r.Header.Set("Origin", "http://localhost")
}
// Fixture authentication only on settings, never on public content.
if strings.HasPrefix(r.URL.Path, "/_/api/v1/city/") && r.URL.Path != "/_/api/v1/city/map" || strings.HasPrefix(r.URL.Path, "/v/city/") {
r.Header.Set("Authorization", "Bearer "+token)
}
if r.Method == "GET" && r.URL.Path == "/v/city/q/public" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "390"} {
t.Run(width, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
screenshot := ""
if dir := os.Getenv("PAGESWIKI_PUBLIC_NAV_SCREENSHOT_DIR"); dir != "" {
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
screenshot = filepath.Join(dir, "public-navigation-"+width+".png")
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/city/q/public", t.TempDir(), screenshot, width).CombinedOutput()
if err != nil {
t.Fatalf("browser %v %s", err, out)
}
})
}
}
+164
View File
@@ -0,0 +1,164 @@
package server
import (
"context"
"database/sql"
"errors"
"html/template"
"io"
"mime"
"net/http"
"net/url"
"path"
"strings"
"time"
"pageswiki/internal/store"
)
type anonymousProjectionKey struct{}
func isAnonymousProjection(r *http.Request) bool {
value, _ := r.Context().Value(anonymousProjectionKey{}).(bool)
return value
}
func (s *Server) tryAnonymousProjection(w http.ResponseWriter, r *http.Request) bool {
if r.Method != "GET" && r.Method != "HEAD" {
return false
}
// A bad or expired credential must not fall back to a weaker identity.
if len(r.Header.Values("Authorization")) > 0 {
return false
}
for _, c := range r.Cookies() {
if c.Name == authCookieName {
return false
}
}
if s.servePublicPeriodical(w, r) {
return true
}
if s.servePublicUserSpace(w, r) {
return true
}
if s.servePublicUserNavigation(w, r) {
return true
}
if s.servePublicNavigation(w, r) {
return true
}
semantic := r.URL.Path
raw := strings.HasPrefix(semantic, "/v/raw/path:")
if raw {
semantic = strings.TrimPrefix(semantic, "/v/raw/path:")
}
if !store.PublicSemanticPath(semantic) {
return false
}
v, err := s.store.AnonymousRevision(r.Context(), s.domainUUIDForRequest(r), semantic, s.revisionLanguageCandidates(r))
if errors.Is(err, sql.ErrNoRows) {
// A structural folder discloses only the already-public descendants,
// never an unapproved Page at the same Path. Raw reads stay exact-only.
if !raw {
publicRequest, navigationErr := s.withPublicNavigation(r)
if navigationErr != nil {
http.Error(w, "navigation unavailable", 503)
return true
}
if publicNavigationFor(publicRequest).hasChildren(semantic) {
s.renderPublicDirectory(w, publicRequest, semantic, true)
return true
}
}
return false
}
if err != nil {
http.Error(w, "publication unavailable", 503)
return true
}
r = r.WithContext(context.WithValue(r.Context(), anonymousProjectionKey{}, true))
r, err = s.withPublicNavigation(r)
if err != nil {
http.Error(w, "navigation unavailable", 503)
return true
}
if current, ok := publicNavigationFor(r).pages[semantic]; !ok || current.UUID != v.UUID {
http.Error(w, "publication changed; retry", 503)
return true
}
return s.serveAnonymousRevision(w, r, semantic, raw, v)
}
func (s *Server) serveAnonymousRevision(w http.ResponseWriter, r *http.Request, semantic string, raw bool, v *store.Revision) bool {
reader, err := s.store.OpenDocument(v.DocumentHash)
if err != nil {
http.Error(w, "published document unavailable", 503)
return true
}
defer reader.Close()
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Add("Vary", "Accept-Language")
contentType, _, _ := mime.ParseMediaType(v.ContentType)
filename := path.Base(v.Filename)
if filename == "." || filename == "/" || filename == "" {
filename = path.Base(semantic)
}
if raw {
// Raw authored HTML, SVG and unknown formats are attachments, never
// executable same-origin content. Native passive formats can stream.
inline := contentType == "application/pdf" || contentType == "image/png" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/webp" || strings.HasPrefix(contentType, "audio/") || strings.HasPrefix(contentType, "video/")
disposition := "attachment"
if inline {
disposition = "inline"
}
csp := "default-src 'none'; frame-ancestors 'self'"
if !inline {
csp = "sandbox; " + csp
}
w.Header().Set("Content-Security-Policy", csp)
w.Header().Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": filename}))
if contentType == "" {
contentType = "application/octet-stream"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("ETag", `"`+v.DocumentHash+`"`)
setDocumentIdentityHeaders(w.Header(), v.ObjectUUID, v.UUID, v.DocumentHash)
http.ServeContent(w, r, filename, time.UnixMilli(v.CreatedAtMS), reader)
return true
}
data := s.publicNavigationData(r, semantic)
data["Title"] = filename
data["RawURL"] = cityPageHref("/v/raw/path:"+semantic) + "?lang=" + url.QueryEscape(v.Language)
switch {
case contentType == "text/html":
body, e := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
if e != nil {
http.Error(w, "published document unavailable", 503)
return true
}
data["HTML"] = template.HTML(s.safeHTMLProjection(r, string(body), semantic, true))
data["SocialSource"] = string(body)
case contentType == "image/png" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/webp":
data["Media"] = "image"
case contentType == "application/pdf":
data["Media"] = "pdf"
case strings.HasPrefix(contentType, "audio/"):
data["Media"] = "audio"
case strings.HasPrefix(contentType, "video/"):
data["Media"] = "video"
case strings.HasPrefix(contentType, "text/") || contentType == "application/json":
body, e := io.ReadAll(io.LimitReader(reader, 64<<10))
if e != nil {
http.Error(w, "published document unavailable", 503)
return true
}
data["Text"] = string(body)
}
data["SocialMetadata"] = s.socialMetadataForRequest(r, "anonymous_view.gohtml", data)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
s.render(w, "anonymous_view.gohtml", data)
return true
}
+199
View File
@@ -0,0 +1,199 @@
package server
import (
"context"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"pageswiki/internal/store"
)
func TestAnonymousApprovedPublication(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
s.mux.HandleFunc("/", s.handleHome)
ctx := context.Background()
admin, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public")
if err != nil {
t.Fatal(err)
}
st, err := s.store.CreateZoneAtPath(domain, admin.UUID, "street", q, "private", "Private", "", "/public/private")
if err != nil {
t.Fatal(err)
}
create := func(path, body, ct string) (string, *store.Revision) {
t.Helper()
o, e := s.store.CreatePageAtPathInDomain(domain, path)
if e != nil {
t.Fatal(e)
}
v, e := s.store.SaveRevisionContent(o, []byte(body), "", "und", ct, "")
if e != nil {
t.Fatal(e)
}
return o, v
}
object, rev := create("/public/page", "<h1>Approved page</h1><script>window.stolen=true</script><p>{{children:/private}}</p>", "text/html")
create("/public/movie", "FAKE-MEDIA-CONTENT", "video/mp4")
create("/public/private/secret", "<h1>PRIVATE-SENTINEL</h1>", "text/html")
create("/outside", "<h1>OUTSIDE-SENTINEL</h1>", "text/html")
setPolicy := func(kind, id, value string) {
t.Helper()
g, e := s.store.CityGovernance(ctx, domain, admin.UUID, kind, id)
if e != nil {
t.Fatal(e)
}
_, e = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false)
if e != nil {
t.Fatal(e)
}
}
setPolicy("domain", domain, "public")
setPolicy("quartier", q, "public")
setPolicy("street", st, "authenticated_acl")
url := governanceEndpoint("quartier", q) + "/publication"
call := func(method, path, bearer string, body any, headers map[string]string) *httptest.ResponseRecorder {
t.Helper()
b, _ := json.Marshal(body)
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(string(b)))
if bearer != "" {
r.Header.Set("Authorization", bearer)
}
r.Header.Set("Content-Type", "application/json")
for k, v := range headers {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
preview := func() store.LocalityPublication {
t.Helper()
w := call("GET", url, "Bearer "+token, nil, nil)
if w.Code != 200 {
t.Fatalf("preview %d %s", w.Code, w.Body.String())
}
var p store.LocalityPublication
if err = json.Unmarshal(w.Body.Bytes(), &p); err != nil {
t.Fatal(err)
}
return p
}
approve := func(p store.LocalityPublication) {
t.Helper()
w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil)
if w.Code != 200 {
t.Fatalf("approve %d %s", w.Code, w.Body.String())
}
}
denied := func(path string) {
t.Helper()
w := call("GET", path, "", nil, nil)
if w.Code == 200 || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") {
t.Fatalf("leaked %s: %d", path, w.Code)
}
}
denied("/public/page")
p := preview()
if !p.Eligible || len(p.Entries) != 2 || len(p.Excluded) != 1 {
t.Fatalf("impact=%+v", p)
}
if w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: true, Confirm: true}, nil); w.Code != 400 {
t.Fatalf("unapproved=%d", w.Code)
}
approve(p)
w := call("GET", "/public/page", "", nil, nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), "Approved page") || !strings.Contains(w.Body.String(), `property="og:title"`) || strings.Contains(w.Body.String(), "window.stolen") || !strings.Contains(w.Body.String(), "data-children-src") || strings.Contains(w.Body.String(), "auth-menu") || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") || strings.Contains(w.Body.String(), " download") {
t.Fatalf("public viewer %d %s", w.Code, w.Body.String())
}
if w.Header().Get("Content-Security-Policy") == "" || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
t.Fatal("public CSP/cache missing")
}
for _, path := range []string{"/public/private/secret", "/outside", "/o/uuid:" + object, "/v/raw/uuid:" + object, "/v/raw/rev:" + rev.UUID, "/_/obj/sha256:" + rev.DocumentHash, "/e/public/page", "/v/search?q=Approved"} {
denied(path)
}
for _, bearer := range []string{"Bearer invalid", "Basic invalid"} {
if w = call("GET", "/public/page", bearer, nil, nil); w.Code == 200 {
t.Fatal("invalid credentials downgraded")
}
}
if w = call("GET", "/public/page", "", nil, map[string]string{"Cookie": authCookieName + "=expired"}); w.Code == 200 {
t.Fatal("expired cookie downgraded")
}
if w = call("GET", "/v/raw/path:/public/movie", "", nil, map[string]string{"Range": "bytes=0-3"}); w.Code != 206 || w.Body.String() != "FAKE" {
t.Fatalf("range=%d %s", w.Code, w.Body.String())
}
if w = call("GET", "/v/raw/path:/public/page", "", nil, nil); w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Disposition"), "attachment") {
t.Fatalf("raw HTML unsafe %d", w.Code)
}
if w = call("HEAD", "/v/raw/path:/public/movie", "", nil, nil); w.Code != 200 || w.Body.Len() != 0 {
t.Fatal("raw HEAD")
}
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
t.Fatal("publication downgraded to legacy")
}
// A new revision invalidates publication before any conditional 304 check.
if _, err = s.store.SaveRevisionContent(object, []byte("<h1>NOT-APPROVED</h1>"), "", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
denied("/public/page")
if w = call("GET", "/v/raw/path:/public/page", "", nil, map[string]string{"If-None-Match": `"` + rev.DocumentHash + `"`}); w.Code == 304 || w.Code == 200 {
t.Fatal("revoked conditional access")
}
approve(preview())
create("/public/new", "UNAPPROVED-NEW", "text/plain")
denied("/public/new")
// Stale impact token cannot approve intervening edits.
p = preview()
if _, err = s.store.SaveRevisionContent(object, []byte("changed again"), "", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 400 {
t.Fatalf("stale impact %d", w.Code)
}
approve(preview())
if _, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,?,?,?,0)`, domain, "/alias", object, 0); err != nil {
t.Fatal(err)
}
denied("/public/page")
denied("/alias")
if _, err = s.store.DB.Exec(`DELETE FROM paths WHERE domain_uuid=? AND path='/alias'`, domain); err != nil {
t.Fatal(err)
}
denied("/public/page")
approve(preview())
setPolicy("domain", domain, "authenticated_acl")
denied("/public/page")
setPolicy("domain", domain, "public")
denied("/public/page")
approve(preview())
// Revocation is explicit and does not change the ceiling/draft.
p = preview()
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: false}, nil); w.Code != 200 {
t.Fatalf("revoke=%d", w.Code)
}
denied("/public/page")
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 409 {
t.Fatal("pre-revocation consent replayed")
}
approve(preview())
a, e := s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, nil)
if e != nil {
t.Fatal(e)
}
a.Mode = "restricted"
a.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: "admin", Permission: "read_write"}}
if _, e = s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, &a); e != nil {
t.Fatal(e)
}
denied("/public/page")
if p = preview(); p.Eligible || len(p.Entries) != 0 {
t.Fatalf("ACL publicly approved %+v", p)
}
}
+62 -24
View File
@@ -2,9 +2,11 @@ package server
import (
"encoding/json"
"errors"
"net/http"
"pageswiki/internal/api"
"pageswiki/internal/store"
)
func (s *Server) handleCreateObjectAPI(
@@ -29,7 +31,7 @@ func (s *Server) handleCreateObjectAPI(
return
}
id, err := s.store.CreateObject(req.Kind)
id, err := s.store.CreateObjectInDomain(s.domainUUIDForRequest(r), req.Kind)
if err != nil {
api.WriteError(
@@ -50,19 +52,7 @@ func (s *Server) handleListObjectsAPI(
w http.ResponseWriter,
r *http.Request,
) {
objs, err := s.store.ListObjects()
if err != nil {
api.WriteError(
w,
500,
"list_failed",
err.Error(),
)
return
}
_ = json.NewEncoder(w).Encode(objs)
s.serveObjectMetadataList(w, r)
}
func (s *Server) handleSaveRevisionAPI(
@@ -71,9 +61,12 @@ func (s *Server) handleSaveRevisionAPI(
) {
var req struct {
ObjectUUID string `json:"object_uuid"`
Body string `json:"body"`
Message string `json:"message"`
ObjectUUID string `json:"object_uuid"`
Body string `json:"body"`
Message string `json:"message"`
Language string `json:"lang"`
ContentType string `json:"content_type"`
Filename string `json:"filename"`
}
err := json.NewDecoder(r.Body).Decode(&req)
@@ -86,17 +79,57 @@ func (s *Server) handleSaveRevisionAPI(
)
return
}
domainUUID := s.domainUUIDForRequest(r)
if mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode"); mode == "enforcing" {
principal, authErr := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if authErr != nil {
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
return
}
var revisionCount int
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
capability := "write"
if revisionCount == 0 {
capability = "create"
}
allowed := false
links, _ := s.store.ListPathLinksInDomain(domainUUID, req.ObjectUUID)
for _, link := range links {
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: link.Path}) == nil {
allowed = true
break
}
}
if !allowed && s.authorizePrincipalObject(domainUUID, principal.UUID, capability, req.ObjectUUID) == nil {
allowed = true
}
if !allowed {
api.WriteError(w, http.StatusForbidden, "forbidden", "forbidden")
return
}
}
_, err = s.store.SaveRevision(
req.ObjectUUID,
req.Body,
req.Message,
)
revision, err :=
s.store.SaveRevisionContent(
req.ObjectUUID,
[]byte(req.Body),
req.Message,
req.Language,
req.ContentType,
req.Filename,
)
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, store.ErrInvalidRevisionLanguage) ||
errors.Is(err, store.ErrPageNotFound) ||
errors.Is(err, store.ErrEmbeddedAssetUnavailable) {
status = http.StatusBadRequest
}
api.WriteError(
w,
500,
status,
"save_failed",
err.Error(),
)
@@ -104,6 +137,11 @@ func (s *Server) handleSaveRevisionAPI(
}
_ = json.NewEncoder(w).Encode(map[string]any{
"status": "ok",
"status": "ok",
"revision_uuid": revision.UUID,
"document_hash": revision.DocumentHash,
"lang": revision.Language,
"content_type": revision.ContentType,
"filename": revision.Filename,
})
}
+10 -61
View File
@@ -9,68 +9,17 @@ type renamePreviewResponse struct {
Targets []string `json:"targets"`
}
func (s *Server) handleRenamePreview(
w http.ResponseWriter,
r *http.Request,
) {
oldPath :=
r.URL.Query().Get(
"old_path",
)
rows, err :=
s.store.DB.Query(`
SELECT path
FROM paths
WHERE path = ?
OR path LIKE ?
ORDER BY LENGTH(path) ASC
`,
oldPath,
oldPath+"/%",
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
func (s *Server) handleRenamePreview(w http.ResponseWriter, r *http.Request) {
if !requirePathMutationPost(w, r) {
return
}
defer rows.Close()
var targets []string
for rows.Next() {
var p string
err := rows.Scan(&p)
if err != nil {
continue
}
targets =
append(
targets,
p,
)
snapshot, err := s.authorizedMutationSnapshot(r, "rename", "", r.URL.Query().Get("old_path"), "", true)
if err != nil {
writePathMutationError(w, err)
return
}
w.Header().Set(
"Content-Type",
"application/json",
)
json.NewEncoder(w).Encode(
renamePreviewResponse{
Targets: targets,
},
)
// Return the checked set, not a fresh query that could reveal a newly added
// descendant for which the requester has never been authorized.
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(renamePreviewResponse{Targets: snapshot.Paths()})
}
+24 -163
View File
@@ -1,7 +1,6 @@
package server
import (
"database/sql"
"encoding/json"
"net/http"
)
@@ -16,175 +15,37 @@ type renamePreview struct {
Count int `json:"count"`
}
func (s *Server) handleRenamePath(
w http.ResponseWriter,
r *http.Request,
) {
func (s *Server) handleRenamePath(w http.ResponseWriter, r *http.Request) {
if !requirePathMutationPost(w, r) {
return
}
var req renamePathRequest
err := json.NewDecoder(
r.Body,
).Decode(&req)
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
req.OldPath = normalizeAuthorizationPath(req.OldPath)
req.NewPath = normalizeAuthorizationPath(req.NewPath)
if req.OldPath == "" || req.NewPath == "" || req.OldPath == "/" || req.NewPath == "/" {
http.Error(w, "invalid path", http.StatusBadRequest)
return
}
snapshot, err := s.authorizedMutationSnapshot(r, "rename", "create", req.OldPath, req.NewPath, req.Recursive)
if err != nil {
http.Error(
w,
err.Error(),
400,
)
writePathMutationError(w, err)
return
}
if req.OldPath == "" ||
req.NewPath == "" {
http.Error(
w,
"invalid path",
400,
)
if r.URL.Query().Get("preview") == "1" {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(renamePreview{Count: len(snapshot.Paths())})
return
}
if req.Recursive {
var count int
err :=
s.store.DB.QueryRow(`
SELECT COUNT(*)
FROM paths
WHERE path = ?
OR path LIKE ?
`,
req.OldPath,
req.OldPath+"/%",
).Scan(&count)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
if r.URL.Query().Get("preview") == "1" {
w.Header().Set(
"Content-Type",
"application/json",
)
json.NewEncoder(w).Encode(
renamePreview{
Count: count,
},
)
return
}
tx, err :=
s.store.DB.BeginTx(
r.Context(),
&sql.TxOptions{},
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
_, err = tx.Exec(`
UPDATE paths
SET path =
CASE
WHEN path = ?
THEN ?
ELSE REPLACE(
path,
? || '/',
? || '/'
)
END
WHERE path = ?
OR path LIKE ?
`,
req.OldPath,
req.NewPath,
req.OldPath,
req.NewPath,
req.OldPath,
req.OldPath+"/%",
)
if err != nil {
tx.Rollback()
http.Error(
w,
err.Error(),
500,
)
return
}
err = tx.Commit()
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
w.WriteHeader(200)
return
}
_, err =
s.store.DB.Exec(`
UPDATE paths
SET path = ?
WHERE path = ?
`,
req.NewPath,
req.OldPath,
)
count, err := s.store.RenamePathsInDomainChecked(s.domainUUIDForRequest(r), req.OldPath, req.NewPath, req.Recursive, snapshot)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
writePathMutationError(w, err)
return
}
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(renamePreview{Count: count})
}
+121
View File
@@ -0,0 +1,121 @@
package server
import (
"archive/zip"
"bytes"
"context"
"io"
"mime"
"net/http"
"os/exec"
"path/filepath"
"strings"
"time"
"pageswiki/internal/store"
)
func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
entry := r.URL.Query().Get("entry")
metadataOnly := r.URL.Query().Get("metadata") == "1"
if path == "" || (!metadataOnly && entry == "") || len(entry) > 4096 || strings.ContainsAny(entry, "\x00\r\n") {
http.Error(w, "path and entry are required", http.StatusBadRequest)
return
}
if !s.requestCanReadPath(r, path) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path)
if err != nil {
http.NotFound(w, r)
return
}
var revision *store.Revision
if revisionUUID := r.URL.Query().Get("rev"); revisionUUID != "" {
revision, err = s.store.GetRevisionMetadata(revisionUUID)
} else {
revision, err = s.latestRevisionMetadataForRequest(r, objectUUID)
}
if err != nil || revision.ObjectUUID != objectUUID {
http.NotFound(w, r)
return
}
if metadataOnly {
s.serveMetadataProjection(w, r, revision.DocumentHash, "archive", path, revision.UUID)
return
}
if strings.EqualFold(filepath.Ext(revision.Filename), ".zip") {
hydrated, err := s.store.GetRevision(revision.UUID)
if err != nil {
http.NotFound(w, r)
return
}
s.serveZIPEntry(w, r, hydrated.Body, entry)
return
}
source, err := s.store.DocumentPath(revision.DocumentHash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
command := exec.CommandContext(ctx, "nice", "-n", "10",
"7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", source, entry)
stdout, err := command.StdoutPipe()
if err != nil || command.Start() != nil {
http.Error(w, "cannot extract archive entry", http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", archiveEntryContentType(entry, nil))
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(entry)}))
w.Header().Set("Cache-Control", "private, max-age=60")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("X-Content-Type-Options", "nosniff")
n, copyErr := io.Copy(w, io.LimitReader(stdout, 128<<20))
if copyErr != nil || n == 128<<20 {
// Never wait for a decoder whose output is no longer being drained.
cancel()
}
waitErr := command.Wait()
if copyErr != nil || waitErr != nil {
return
}
}
func (s *Server) serveZIPEntry(
w http.ResponseWriter,
r *http.Request,
body string,
entry string,
) {
reader, err := zip.NewReader(
bytes.NewReader([]byte(body)),
int64(len(body)),
)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
for _, file := range reader.File {
if file.Name != entry || file.FileInfo().IsDir() {
continue
}
source, err := file.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer source.Close()
w.Header().Set("Content-Type", archiveEntryContentType(file.Name, nil))
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(file.Name)}))
w.Header().Set("Cache-Control", "private, max-age=60")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("X-Content-Type-Options", "nosniff")
_, _ = io.Copy(w, io.LimitReader(source, 128<<20))
return
}
http.NotFound(w, r)
}
+179
View File
@@ -0,0 +1,179 @@
package server
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"pageswiki/internal/store"
)
func (s *Server) handleAssetsAPI(w http.ResponseWriter, r *http.Request) {
domainUUID := s.domainUUIDForRequest(r)
principal := s.requestPrincipal(r)
if principal == nil {
s.authRequired(w, r)
return
}
if !s.canManageAssets(r) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
switch r.Method {
case http.MethodGet:
address := strings.TrimSpace(r.URL.Query().Get("address"))
if address == "" {
assets, err := s.store.ListAssets(domainUUID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"assets": assets})
return
}
asset, err := s.store.GetAsset(domainUUID, address)
if err != nil {
http.NotFound(w, r)
return
}
history, err := s.store.ListAssetHistory(domainUUID, address)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
dependencies, err := s.store.ListAssetDependencies(domainUUID, address)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "history": history, "dependencies": dependencies})
case http.MethodPost:
if strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "application/json") {
var input struct {
Action string `json:"action"`
Address string `json:"address"`
HistoryID int64 `json:"history_id"`
}
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, err.Error(), 400)
return
}
if input.Action != "restore" {
http.Error(w, "unsupported action", 400)
return
}
expected := parseAssetETag(r.Header.Get("If-Match"))
if expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
asset, err := s.store.RestoreAssetHead(domainUUID, input.Address, input.HistoryID, expected, principal.UUID)
if errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
} else if errors.Is(err, store.ErrAssetNotFound) {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), 500)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "restored": true})
return
}
r.Body = http.MaxBytesReader(w, r.Body, 256<<20)
if err := r.ParseMultipartForm(8 << 20); err != nil {
http.Error(w, err.Error(), 400)
return
}
defer r.MultipartForm.RemoveAll()
address := r.FormValue("address")
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "file is required", 400)
return
}
defer file.Close()
payload, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
current, currentErr := s.store.GetAsset(domainUUID, address)
expected := parseAssetETag(r.Header.Get("If-Match"))
if currentErr == nil && expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
if errors.Is(currentErr, store.ErrAssetNotFound) && r.Header.Get("If-None-Match") != "*" {
http.Error(w, "If-None-Match: * is required", http.StatusPreconditionRequired)
return
}
contentType := strings.TrimSpace(r.FormValue("content_type"))
if contentType == "" {
contentType = header.Header.Get("Content-Type")
}
filename := strings.TrimSpace(r.FormValue("filename"))
if filename == "" {
filename = header.Filename
}
asset, changed, err := s.store.PutAsset(domainUUID, address, payload, contentType, filename, expected, principal.UUID)
if errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
}
if err != nil {
http.Error(w, err.Error(), 400)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "changed": changed, "previous": current})
case http.MethodDelete:
address := strings.TrimSpace(r.URL.Query().Get("address"))
expected := parseAssetETag(r.Header.Get("If-Match"))
if expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
if err := s.store.DeleteAsset(domainUUID, address, expected, principal.UUID); errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
} else if errors.Is(err, store.ErrAssetNotFound) {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusNoContent)
default:
w.Header().Set("Allow", "GET, POST, DELETE")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (s *Server) canManageAssets(r *http.Request) bool {
principal := s.requestPrincipal(r)
if principal == nil {
return false
}
domainUUID := s.domainUUIDForRequest(r)
domain := s.domainForRequest(r)
return s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") ||
(domain != nil && s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID))
}
func parseAssetETag(value string) string {
return strings.TrimPrefix(strings.Trim(strings.TrimSpace(value), `"`), "sha256:")
}
func setAssetETag(w http.ResponseWriter, hash string) {
if hash != "" {
w.Header().Set("ETag", `"sha256:`+hash+`"`)
}
}
+99
View File
@@ -0,0 +1,99 @@
package server
import (
"context"
"log"
"net/http"
"pageswiki/internal/store"
"time"
)
func ldapConfigFromForm(r *http.Request) store.AuthProviderConfig {
return store.AuthProviderConfig{
Kind: "ldap", Enabled: r.FormValue("enabled") == "on",
LDAPURL: r.FormValue("ldap_url"), LDAPBindDN: r.FormValue("ldap_bind_dn"), LDAPBindPassword: r.FormValue("ldap_bind_password"),
LDAPSearchBase: r.FormValue("ldap_search_base"), LDAPSearchFilter: r.FormValue("ldap_search_filter"),
LDAPStartTLS: r.FormValue("ldap_start_tls") == "on", LDAPSkipTLSVerify: r.FormValue("ldap_verify_tls") != "on", LDAPCAPEM: r.FormValue("ldap_ca_pem"),
LDAPUIDAttribute: r.FormValue("ldap_uid_attribute"), LDAPUsernameAttribute: r.FormValue("ldap_username_attribute"),
LDAPEmailAttribute: r.FormValue("ldap_email_attribute"), LDAPDisplayAttribute: r.FormValue("ldap_display_attribute"),
LDAPMatchBy: r.FormValue("ldap_match_by"), LDAPAutoProvision: r.FormValue("ldap_auto_provision") == "on",
}
}
func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) {
domainUUID := s.domainUUIDForRequest(r)
principal := s.requestPrincipal(r)
if principal == nil || !s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
http.Error(w, "domain admin required", http.StatusForbidden)
return
}
if r.Method == http.MethodPost {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), 400)
return
}
panel := "auth"
switch r.FormValue("action") {
case "revoke_session":
panel = "auth-sessions"
if err := s.store.RevokeAuthSessionByID(domainUUID, r.FormValue("session_uuid")); err != nil {
http.Error(w, err.Error(), 400)
return
}
case "principal_state":
if err := s.store.SetPrincipalState(domainUUID, r.FormValue("principal_uuid"), r.FormValue("state")); err != nil {
http.Error(w, err.Error(), 400)
return
}
case "create_account":
if _, err := s.store.CreatePasswordAccount(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email")); err != nil {
http.Error(w, err.Error(), 400)
return
}
case "save_oidc":
panel = "auth-providers"
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "oidc", Enabled: r.FormValue("enabled") == "on", Issuer: r.FormValue("issuer"), ClientID: r.FormValue("client_id"), ClientSecret: r.FormValue("client_secret"), OIDCMatchBy: r.FormValue("oidc_match_by")})
if err != nil {
http.Error(w, err.Error(), 400)
return
}
case "save_smtp":
panel = "auth-providers"
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "smtp", Enabled: r.FormValue("enabled") == "on", SMTPAddress: r.FormValue("smtp_address"), SMTPUsername: r.FormValue("smtp_username"), SMTPPassword: r.FormValue("smtp_password"), SMTPFrom: r.FormValue("smtp_from")})
if err != nil {
http.Error(w, err.Error(), 400)
return
}
case "save_ldap":
panel = "auth-providers"
if err := s.store.SetAuthProviderConfig(domainUUID, ldapConfigFromForm(r)); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
case "test_ldap":
panel = "auth-providers"
config, err := s.store.AuthProviderConfig(domainUUID, "ldap")
if err == nil && config.Enabled {
select {
case s.ldapSlots <- struct{}{}:
ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
_, err = s.authenticateLDAPDirectory(ctx, config, r.FormValue("test_username"), r.FormValue("test_password"))
cancel()
<-s.ldapSlots
default:
err = context.DeadlineExceeded
}
}
if err != nil || !config.Enabled {
log.Printf("LDAP connection test failed domain=%s error=%v", domainUUID, err)
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=failed", http.StatusSeeOther)
return
}
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=ok", http.StatusSeeOther)
return
}
http.Redirect(w, r, "/-/sys/config?panel="+panel+"&saved=1", http.StatusSeeOther)
return
}
http.Redirect(w, r, "/-/sys/config?panel=auth", http.StatusFound)
}
+54
View File
@@ -0,0 +1,54 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestBrowserCookieLoginLogoutChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, apiToken, domain := configFixture(t)
s.mux.HandleFunc("/_/auth/", s.handleAuth)
script, err := os.ReadFile("internal/server/testdata/auth-browser.js")
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Real browser cookies and Fetch Metadata: do not inject credentials or
// rewrite Origin/Host. localhost maps to this fixture's default Domain.
if r.Method == "GET" && (strings.HasPrefix(r.URL.Path, "/_/auth/") || r.URL.Path == "/-/sys/config") {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/_/auth/login?return=%2F-%2Fsys%2Fconfig%3Fpanel%3Dauth"
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("cookie browser flow: %v %s", err, out)
}
if _, err := s.store.PrincipalForSession(domain, apiToken); err != nil {
t.Fatalf("browser logout revoked another API session: %v", err)
}
var revoked int
if err := s.store.DB.QueryRow(`SELECT count(*) FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&revoked); err != nil || revoked != 1 {
t.Fatalf("browser revocations=%d %v", revoked, err)
}
}
+301
View File
@@ -0,0 +1,301 @@
package server
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type oidcChallenge struct{ Verifier, Nonce, Return, Issuer, ClientID, BrowserHash string }
func logOIDCFailure(stage, domainUUID, issuer string, err error) {
host := "invalid"
if parsed, parseErr := url.Parse(issuer); parseErr == nil && parsed.Hostname() != "" {
host = parsed.Hostname()
}
cause := err
for errors.Unwrap(cause) != nil {
cause = errors.Unwrap(cause)
}
log.Printf("OIDC %s failed domain=%s issuer_host=%s cause=%T: %v", stage, domainUUID, host, cause, cause)
}
func randomURLToken(size int) string {
raw := make([]byte, size)
_, _ = rand.Read(raw)
return base64.RawURLEncoding.EncodeToString(raw)
}
func externalURL(r *http.Request, path string) string {
scheme := "http"
if requestIsHTTPS(r) {
scheme = "https"
}
return scheme + "://" + r.Host + path
}
func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
domainUUID := s.domainUUIDForRequest(r)
config, err := s.store.AuthProviderConfig(domainUUID, "oidc")
if err != nil || !config.Enabled {
http.NotFound(w, r)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second)
defer cancel()
metadata, err := fetchOIDCMetadata(ctx, config.Issuer)
if err != nil {
logOIDCFailure("discovery", domainUUID, config.Issuer, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
verifier, nonce, browser := randomURLToken(32), randomURLToken(24), randomURLToken(32)
browserDigest := sha256.Sum256([]byte(browser))
state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: safeReturnPath(r.URL.Query().Get("return")), Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:])}, 5*time.Minute)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
digest := sha256.Sum256([]byte(verifier))
query := url.Values{"response_type": {"code"}, "client_id": {config.ClientID}, "redirect_uri": {externalURL(r, "/_/auth/callback/oidc")}, "scope": {"openid email profile"}, "state": {state}, "nonce": {nonce}, "code_challenge_method": {"S256"}, "code_challenge": {base64.RawURLEncoding.EncodeToString(digest[:])}}
authorizationURL, _ := url.Parse(metadata.AuthorizationEndpoint)
parameters := authorizationURL.Query()
for key, value := range query {
parameters[key] = value
}
authorizationURL.RawQuery = parameters.Encode()
setOIDCBrowserCookie(w, r, browser, 300)
w.Header().Set("Cache-Control", "no-store")
http.Redirect(w, r, authorizationURL.String(), http.StatusFound)
}
func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Cache-Control", "no-store")
domainUUID := s.domainUUIDForRequest(r)
var challenge oidcChallenge
if _, err := s.store.ConsumeAuthChallenge(domainUUID, "oidc", r.URL.Query().Get("state"), &challenge); err != nil {
http.Error(w, "invalid OIDC state", http.StatusBadRequest)
return
}
if !validOIDCBrowserChallenge(oidcBrowserCookie(r), challenge.BrowserHash) {
http.Error(w, "OIDC state belongs to a different browser", http.StatusBadRequest)
return
}
setOIDCBrowserCookie(w, r, "", -1)
config, err := s.store.AuthProviderConfig(domainUUID, "oidc")
if err != nil || !config.Enabled {
http.Error(w, "OIDC is disabled", http.StatusBadRequest)
return
}
if config.Issuer != challenge.Issuer || config.ClientID != challenge.ClientID || r.URL.Query().Get("code") == "" || r.URL.Query().Get("error") != "" {
http.Error(w, "OIDC login must be restarted", http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
metadata, err := fetchOIDCMetadata(ctx, config.Issuer)
if err != nil {
logOIDCFailure("callback discovery", domainUUID, config.Issuer, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
form := url.Values{"grant_type": {"authorization_code"}, "code": {r.URL.Query().Get("code")}, "redirect_uri": {externalURL(r, "/_/auth/callback/oidc")}, "client_id": {config.ClientID}, "client_secret": {config.ClientSecret}, "code_verifier": {challenge.Verifier}}
var tokens struct {
AccessToken string `json:"access_token"`
IDToken string `json:"id_token"`
TokenType string `json:"token_type"`
}
if tokenErr := oidcVerifier.requestJSON(ctx, http.MethodPost, metadata.TokenEndpoint, form, "", &tokens); tokenErr != nil {
logOIDCFailure("token exchange", domainUUID, config.Issuer, tokenErr)
http.Error(w, "OIDC token exchange failed", http.StatusBadGateway)
return
}
if tokens.AccessToken == "" || len(tokens.AccessToken) > 16<<10 || !strings.EqualFold(tokens.TokenType, "Bearer") {
http.Error(w, "OIDC token exchange failed", http.StatusBadGateway)
return
}
idClaims, err := oidcVerifier.verify(ctx, metadata, tokens.IDToken, config.ClientID, challenge.Nonce, tokens.AccessToken)
if err != nil {
logOIDCFailure("ID-token verification", domainUUID, config.Issuer, err)
http.Error(w, "OIDC ID-token verification failed", http.StatusBadRequest)
return
}
var claims struct {
Sub, Email string
Name string `json:"name"`
DisplayName string `json:"display_name"`
PreferredName string `json:"preferred_username"`
EmailVerified bool `json:"email_verified"`
}
if userinfoErr := oidcVerifier.requestJSON(ctx, http.MethodGet, metadata.UserinfoEndpoint, nil, tokens.AccessToken, &claims); userinfoErr != nil {
logOIDCFailure("UserInfo", domainUUID, config.Issuer, userinfoErr)
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
return
}
if claims.Sub == "" {
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
return
}
if claims.Sub != idClaims.Sub {
http.Error(w, "OIDC subject mismatch", http.StatusForbidden)
return
}
principal, err := s.store.PrincipalByExternalIdentity(domainUUID, "oidc", "oidc", config.Issuer, claims.Sub)
if err != nil {
switch config.OIDCMatchBy {
case "preferred_username":
principal, err = s.store.PrincipalByExternalUsername(domainUUID, claims.PreferredName)
if err != nil {
log.Printf("OIDC local account match failed domain=%s mode=preferred_username present=%t", domainUUID, strings.TrimSpace(claims.PreferredName) != "")
}
case "display_name":
displayName := strings.TrimSpace(claims.Name)
claimSource := "name"
if displayName == "" {
displayName = strings.TrimSpace(claims.DisplayName)
claimSource = "display_name"
}
principal, err = s.store.PrincipalByDisplayName(domainUUID, displayName)
if err != nil {
log.Printf("OIDC local account match failed domain=%s mode=display_name claim=%s present=%t", domainUUID, claimSource, displayName != "")
}
default:
if claims.Email == "" || !claims.EmailVerified {
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
return
}
principal, err = s.store.PrincipalByVerifiedEmail(domainUUID, claims.Email)
}
if err == nil {
err = s.store.BindExternalIdentity(principal.UUID, "oidc", "oidc", config.Issuer, claims.Sub, claims)
}
}
if err != nil {
http.Error(w, "No local account matches the configured OIDC identity field", http.StatusForbidden)
return
}
token, err := s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
s.setAuthCookie(w, r, token)
http.Redirect(w, r, challenge.Return, http.StatusSeeOther)
}
func (s *Server) requestMagicLink(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Cache-Control", "no-store")
domainUUID := s.domainUUIDForRequest(r)
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid sign-in request", http.StatusBadRequest)
return
}
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
requested, mailboxErr := smtpMailbox(r.FormValue("email"))
if configErr == nil && config.Enabled && mailboxErr == nil {
principal, principalErr := s.store.PrincipalByVerifiedEmail(domainUUID, requested.Address)
if principalErr == nil {
_, _, _, _, principalErr = smtpMessage(config, principal.Email, externalURL(r, "/_/auth/magic-link/verify"))
}
if principalErr == nil {
select {
case s.smtpSlots <- struct{}{}:
token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email}, 15*time.Minute)
if err != nil {
<-s.smtpSlots
break
}
link := externalURL(r, "/_/auth/magic-link/verify?token="+url.QueryEscape(token))
go func() {
defer func() { <-s.smtpSlots }()
_ = sendMagicLinkSMTP(context.Background(), config, principal.Email, link)
}()
default:
}
}
}
s.renderAuthPage(w, r, map[string]any{"MagicSent": true, "Return": safeReturnPath(r.FormValue("return"))})
}
func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", "GET")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Cache-Control", "no-store")
domainUUID := s.domainUUIDForRequest(r)
var payload magicLinkChallenge
principalUUID, err := s.store.ConsumeAuthChallenge(domainUUID, "magic-link", r.URL.Query().Get("token"), &payload)
if err != nil {
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
return
}
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
var valid bool
stateErr := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.state='active' AND p.email=?)`, domainUUID, principalUUID, payload.Email).Scan(&valid)
if configErr != nil || !config.Enabled || stateErr != nil || !valid || payload.Email == "" {
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
return
}
token, err := s.store.CreateBrowserAuthSession(domainUUID, principalUUID, 0)
if err != nil {
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
return
}
s.setAuthCookie(w, r, token)
http.Redirect(w, r, safeReturnPath(payload.Return), http.StatusSeeOther)
}
func (s *Server) renderAuthPage(w http.ResponseWriter, r *http.Request, data map[string]any) {
domainUUID := s.domainUUIDForRequest(r)
if data == nil {
data = map[string]any{}
}
if config, err := s.store.AuthProviderConfig(domainUUID, "oidc"); err == nil {
data["OIDCEnabled"] = config.Enabled
}
if config, err := s.store.AuthProviderConfig(domainUUID, "smtp"); err == nil {
data["MagicEnabled"] = config.Enabled
}
if config, err := s.store.AuthProviderConfig(domainUUID, "ldap"); err == nil {
data["LDAPEnabled"] = config.Enabled
}
data["Locale"] = s.getLocale(domainUUID)
transparency := s.homeGlassTransparency(domainUUID)
data["HomeGlassTransparency"] = transparency
data["HomeGlassOpacity"] = 100 - transparency
configured, opacity := s.homeWallpaperConfig(domainUUID)
data["HomeWallpaperOpacity"] = strconv.FormatFloat(float64(opacity)/100, 'f', 2, 64)
if configured != "" {
// Only the administrator-selected login wallpaper is exposed; the
// request cannot choose an arbitrary document address.
data["HomeWallpaperURL"] = "/_/auth/wallpaper"
w.Header().Set("Cache-Control", "private, no-store")
}
s.renderWithContext(w, r, "auth.gohtml", data)
}
+406
View File
@@ -0,0 +1,406 @@
package server
import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"database/sql"
"encoding/base64"
"errors"
"log"
"net/http"
"net/url"
"strings"
"pageswiki/internal/store"
)
const authCookieName = "pageswiki_session"
const mfaChallengeCookieName = "pageswiki_mfa"
func (s *Server) ensureBootstrapToken(domainUUID string) error {
if value, _ := s.store.GetConfigInDomain(domainUUID, "auth_bootstrap_token_hash"); value != "" {
return nil
}
raw := make([]byte, 24)
if _, err := rand.Read(raw); err != nil {
return err
}
token := base64.RawURLEncoding.EncodeToString(raw)
sum := sha256.Sum256([]byte(token))
if err := s.store.SetConfigInDomain(domainUUID, "auth_bootstrap_token_hash", base64.RawURLEncoding.EncodeToString(sum[:])); err != nil {
return err
}
log.Printf("AUTH BOOTSTRAP TOKEN domain=%s token=%s (one-time; create the owner at /_/auth/login)", domainUUID, token)
return nil
}
func (s *Server) validBootstrapToken(domainUUID, token string) bool {
want, err := s.store.GetConfigInDomain(domainUUID, "auth_bootstrap_token_hash")
if err != nil || want == "" {
return false
}
sum := sha256.Sum256([]byte(strings.TrimSpace(token)))
got := base64.RawURLEncoding.EncodeToString(sum[:])
return subtle.ConstantTimeCompare([]byte(want), []byte(got)) == 1
}
func safeReturnPath(value string) string {
value = strings.TrimSpace(value)
parsed, err := url.Parse(value)
if err != nil || !strings.HasPrefix(parsed.Path, "/") || strings.HasPrefix(parsed.Path, "//") || strings.ContainsAny(parsed.Path, "\\\r\n\x00") || parsed.IsAbs() || parsed.Host != "" {
return "/"
}
return parsed.RequestURI()
}
func requestIsHTTPS(r *http.Request) bool {
if r.TLS != nil {
return true
}
return strings.EqualFold(strings.TrimSpace(strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0]), "https")
}
func (s *Server) setAuthCookie(w http.ResponseWriter, r *http.Request, token string) {
http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: 24 * 60 * 60})
}
func (s *Server) authTokenFromRequest(r *http.Request) string {
if headers := r.Header.Values("Authorization"); len(headers) != 0 {
// An explicitly supplied but invalid credential must never fall back to
// an ambient browser session (including unknown credential schemes).
if len(headers) != 1 {
return ""
}
parts := strings.Fields(headers[0])
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") || len(parts[1]) > 4096 || strings.Contains(parts[1], ",") {
return ""
}
if strings.HasPrefix(parts[1], "pw_browser_") {
return ""
}
return parts[1]
}
var token string
count := 0
for _, cookie := range r.Cookies() {
if cookie.Name == authCookieName {
token = cookie.Value
count++
}
}
if count == 1 {
if strings.HasPrefix(token, "pw_api_") {
return ""
}
return token
}
return ""
}
func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/auth"), "/")
domainUUID := s.domainUUIDForRequest(r)
switch action {
case "wallpaper":
s.handleAuthWallpaper(w, r)
return
case "oidc":
s.beginOIDC(w, r)
return
case "ldap":
s.handleLDAPLogin(w, r)
return
case "callback/oidc":
s.completeOIDC(w, r)
return
case "magic-link":
if r.Method == http.MethodPost {
s.requestMagicLink(w, r)
return
}
case "magic-link/verify":
s.verifyMagicLink(w, r)
return
}
if action == "logout" {
s.handleLogout(w, r)
return
}
if action != "login" && action != "setup" && action != "totp" {
http.NotFound(w, r)
return
}
if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodPost {
w.Header().Set("Allow", "GET, HEAD, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
count, err := s.store.PrincipalCount()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
setup := count == 0
if setup {
if err := s.ensureBootstrapToken(domainUUID); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
if r.Method == http.MethodPost {
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), 400)
return
}
var principal *store.Principal
mfaLevel := 0
if action == "setup" && setup && s.validBootstrapToken(domainUUID, r.FormValue("bootstrap_token")) {
principal, err = s.store.BootstrapOwner(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email"))
if err == nil {
err = s.store.SetConfigInDomain(domainUUID, "authorization_mode", "enforcing")
if err == nil {
_ = s.store.SetConfigInDomain(domainUUID, "auth_bootstrap_token_hash", "")
}
}
} else if action == "login" && !setup {
principal, err = s.store.AuthenticatePassword(r.FormValue("username"), r.FormValue("password"))
if err == nil && s.store.HasTOTP(principal.UUID) {
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
if challengeErr != nil {
err = challengeErr
} else if strings.Contains(r.Header.Get("Accept"), "application/json") {
writeEventJSON(w, http.StatusUnauthorized, map[string]any{"mfa_required": true, "challenge_token": challenge, "method": "totp"})
return
} else {
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Value: challenge, Path: "/_/auth/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode, MaxAge: 300})
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": safeReturnPath(r.FormValue("return"))})
return
}
}
} else if action == "totp" && !setup {
challenge := r.FormValue("challenge_token")
if challenge == "" {
if cookie, cookieErr := r.Cookie(mfaChallengeCookieName); cookieErr == nil {
challenge = cookie.Value
}
}
var principalUUID string
principalUUID, err = s.store.CompleteTOTPLoginChallenge(domainUUID, challenge, r.FormValue("code"))
if err == nil {
principal = &store.Principal{UUID: principalUUID}
mfaLevel = 1
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Path: "/_/auth/", MaxAge: -1, HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode})
}
} else {
err = store.ErrAccessDenied
}
if err == nil {
var token string
if strings.Contains(r.Header.Get("Accept"), "application/json") {
token, err = s.store.CreateAuthSessionWithMFA(domainUUID, principal.UUID, mfaLevel)
} else {
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, mfaLevel)
}
if err == nil {
if strings.Contains(r.Header.Get("Accept"), "application/json") {
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": 86400})
return
}
s.setAuthCookie(w, r, token)
http.Redirect(w, r, safeReturnPath(r.FormValue("return")), http.StatusSeeOther)
return
}
}
if action == "totp" {
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Error": translate(s.getLocale(domainUUID), "auth.totp_invalid"), "Return": safeReturnPath(r.FormValue("return"))})
return
}
s.renderAuthPage(w, r, map[string]any{"Setup": setup, "Error": translate(s.getLocale(domainUUID), "auth.invalid_credentials"), "Return": safeReturnPath(r.FormValue("return"))})
return
}
s.renderAuthPage(w, r, map[string]any{"Setup": setup, "Return": safeReturnPath(r.URL.Query().Get("return"))})
}
func authorizationTargetForRequest(r *http.Request, domainUUID string) (string, string) {
policy := policyForRequest(r)
if policy.Target == targetDomain {
return string(targetDomain), domainUUID
}
path := r.URL.Path
if strings.HasPrefix(path, "/e/") {
return "path", semanticEditorPath(path)
}
return string(targetPath), path
}
func (s *Server) authorizePathRequest(r *http.Request, capability, path string) error {
domainUUID := s.domainUUIDForRequest(r)
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
if err != nil {
return err
}
if mode != "enforcing" {
return nil
}
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if err != nil {
return err
}
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: path})
}
func ownUserEditorPath(path, username string) bool {
for _, prefix := range []string{"/e/-/u/", "/e/u/"} {
if strings.HasPrefix(path, prefix) {
owner := strings.Split(strings.TrimPrefix(path, prefix), "/")[0]
return owner == username
}
}
return false
}
func (s *Server) withAuthorization(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// HTML and dynamic listings contain per-principal data. They must not
// enter shared caches even in legacy mode; immutable projections can
// choose a more specific private cache policy in their own handlers.
if r.URL.Path != "/health" && !strings.HasPrefix(r.URL.Path, "/static/") && !strings.HasPrefix(r.URL.Path, "/o/path:/system/static/") {
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Add("Vary", "Cookie, Authorization")
}
// Runs before handler-owned auth and legacy-mode bypasses, protecting
// login, groups, configuration and upload forms as well as JSON APIs.
if !validBrowserMutationOrigin(r) {
rejectBrowserMutation(w, r)
return
}
// No process-wide principal/policy cache: revocation is observed by the
// next request. Read snapshots never authorize mutating store operations.
r = cacheRequestDomain(withReadPolicyContext(r))
if r.URL.Path == "/g" || strings.HasPrefix(r.URL.Path, "/g/") {
next.ServeHTTP(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/_/auth/") || r.URL.Path == "/health" || strings.HasPrefix(r.URL.Path, "/static/") || strings.HasPrefix(r.URL.Path, adapterGatewayPrefix+"/") || strings.HasPrefix(r.URL.Path, "/_/api/v1/adapter/session/") || r.URL.Path == "/_/api/v1/federation/enroll" {
next.ServeHTTP(w, r)
return
}
policy := policyForRequest(r)
// Calendar handlers enforce per-calendar ACLs, including public reads,
// regardless of the domain's legacy/enforcing switch.
if taskRoute(r.URL.Path) || calendarRoute(r.URL.Path) {
next.ServeHTTP(w, r)
return
}
// Compatibility URL for public application assets, not document objects.
// Serve from a confined static FileServer, never from object resolution.
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && strings.HasPrefix(r.URL.Path, "/o/path:/system/static/") {
http.StripPrefix("/o/path:/system/static/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
return
}
if policy.Capability == "deny" {
http.Error(w, "forbidden: endpoint authorization policy is not defined", http.StatusForbidden)
return
}
domainUUID := s.domainUUIDForRequest(r)
mode, configErr := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
if configErr != nil {
http.Error(w, "authorization policy unavailable", http.StatusServiceUnavailable)
return
}
userSpacePath := ""
if store.IsUserSpacePath(r.URL.Path) {
userSpacePath = r.URL.Path
} else if strings.HasPrefix(r.URL.Path, "/v/raw/path:/u/") {
userSpacePath = strings.TrimPrefix(r.URL.Path, "/v/raw/path:")
} else if username := publicUserNavigationUsername(r); username != "" {
if r.URL.Path == "/v/children" {
userSpacePath = r.URL.Query().Get("path")
} else {
userSpacePath = "/u/" + username
}
}
if mode != "enforcing" && userSpacePath == "" {
next.ServeHTTP(w, r)
return
}
token := s.authTokenFromRequest(r)
if token == "" {
if s.tryAnonymousProjection(w, r) {
return
}
s.authRequired(w, r)
return
}
principal, err := s.store.PrincipalForSession(domainUUID, token)
if err != nil {
s.authRequired(w, r)
return
}
if userSpacePath != "" {
allowed, audienceErr := s.store.UserSpaceCanReadAuthenticated(r.Context(), domainUUID, principal.UUID, userSpacePath)
if audienceErr != nil || !allowed {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
}
if mode != "enforcing" {
next.ServeHTTP(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/-/sys/config") {
domain := s.domainForRequest(r)
if s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") || (domain != nil && s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID)) {
next.ServeHTTP(w, r)
return
}
}
if strings.HasPrefix(r.URL.Path, "/_/api/v1/city/") && r.Method != http.MethodGet && r.Method != http.MethodHead && s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
next.ServeHTTP(w, r)
return
}
capability := policy.Capability
if capability == "write" && principal.State == "active" && ownUserEditorPath(r.URL.Path, principal.Username) && s.store.CheckLocalityAccessCaps(domainUUID, principal.UUID, "write", semanticEditorPath(r.URL.Path)) == nil {
next.ServeHTTP(w, r)
return
}
if handled, resolvedErr := s.authorizeResolvedEndpoint(r, principal, policy); handled {
if resolvedErr != nil {
if errors.Is(resolvedErr, store.ErrAccessDenied) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
http.Error(w, resolvedErr.Error(), http.StatusInternalServerError)
return
}
next.ServeHTTP(w, r)
return
}
targetType, targetID := authorizationTargetForRequest(r, domainUUID)
if capability == "write" && targetType == "path" {
if _, resolveErr := s.store.ResolvePathInDomain(domainUUID, targetID); errors.Is(resolveErr, sql.ErrNoRows) {
capability = "create"
}
}
err = s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: targetType, TargetID: targetID})
if err != nil {
if errors.Is(err, store.ErrAccessDenied) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
http.Error(w, err.Error(), 500)
return
}
next.ServeHTTP(w, r)
})
}
func (s *Server) authRequired(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/_/api/") {
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
return
}
http.Redirect(w, r, "/_/auth/login?return="+url.QueryEscape(r.URL.RequestURI()), http.StatusSeeOther)
}
+253
View File
@@ -0,0 +1,253 @@
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/base64"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
"unicode/utf8"
ldap "github.com/go-ldap/ldap/v3"
"pageswiki/internal/store"
)
type ldapDirectory interface {
Bind(string, string) error
Search(*ldap.SearchRequest) (*ldap.SearchResult, error)
Close() error
}
type ldapDialFunc func(context.Context, store.AuthProviderConfig) (ldapDirectory, error)
type ldapIdentity struct {
Subject, Username, Email, DisplayName, DN string
}
func ldapTLSConfig(config store.AuthProviderConfig) (*tls.Config, error) {
parsed, err := url.Parse(config.LDAPURL)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{ // #nosec G402 -- opt-out is an explicit administrator setting.
MinVersion: tls.VersionTLS12,
ServerName: parsed.Hostname(),
InsecureSkipVerify: config.LDAPSkipTLSVerify,
}
if config.LDAPCAPEM != "" {
pool, poolErr := x509.SystemCertPool()
if poolErr != nil || pool == nil {
pool = x509.NewCertPool()
}
if !pool.AppendCertsFromPEM([]byte(config.LDAPCAPEM)) {
return nil, errors.New("LDAP CA PEM contains no certificate")
}
tlsConfig.RootCAs = pool
}
return tlsConfig, nil
}
func dialLDAPDirectory(_ context.Context, config store.AuthProviderConfig) (ldapDirectory, error) {
if err := store.ValidateLDAPProviderConfig(config); err != nil {
return nil, err
}
tlsConfig, err := ldapTLSConfig(config)
if err != nil {
return nil, err
}
conn, err := ldap.DialURL(config.LDAPURL,
ldap.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}),
ldap.DialWithTLSConfig(tlsConfig),
)
if err != nil {
return nil, err
}
conn.SetTimeout(8 * time.Second)
if config.LDAPStartTLS {
if err = conn.StartTLS(tlsConfig); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
}
func ldapStableSubject(raw []byte) (string, error) {
if len(raw) == 0 || len(raw) > 4096 {
return "", errors.New("LDAP immutable UID is missing or too large")
}
return "attr:" + base64.RawURLEncoding.EncodeToString(raw), nil
}
func (s *Server) authenticateLDAPDirectory(ctx context.Context, config store.AuthProviderConfig, login, password string) (ldapIdentity, error) {
login = strings.TrimSpace(login)
if login == "" || len(login) > 512 || password == "" || len(password) > 4096 || config.UUID == "" {
return ldapIdentity{}, store.ErrAccessDenied
}
if err := store.ValidateLDAPProviderConfig(config); err != nil || !config.Enabled {
return ldapIdentity{}, store.ErrAccessDenied
}
dial := s.ldapDial
if dial == nil {
dial = dialLDAPDirectory
}
directory, err := dial(ctx, config)
if err != nil {
return ldapIdentity{}, err
}
defer func() { _ = directory.Close() }()
if config.LDAPBindDN != "" {
if config.LDAPBindPassword == "" {
return ldapIdentity{}, errors.New("LDAP service bind password is unavailable")
}
if err = directory.Bind(config.LDAPBindDN, config.LDAPBindPassword); err != nil {
return ldapIdentity{}, fmt.Errorf("LDAP service bind: %w", err)
}
}
attributes := []string{config.LDAPUIDAttribute, config.LDAPUsernameAttribute, config.LDAPEmailAttribute, config.LDAPDisplayAttribute}
unique := make([]string, 0, len(attributes))
seen := map[string]bool{}
for _, attribute := range attributes {
key := strings.ToLower(attribute)
if !seen[key] {
seen[key] = true
unique = append(unique, attribute)
}
}
filter := strings.ReplaceAll(config.LDAPSearchFilter, "{{username}}", ldap.EscapeFilter(login))
request := ldap.NewSearchRequest(config.LDAPSearchBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 2, 6, false, filter, unique, nil)
result, err := directory.Search(request)
if err != nil {
return ldapIdentity{}, fmt.Errorf("LDAP user search: %w", err)
}
if len(result.Entries) != 1 || result.Entries[0].DN == "" {
return ldapIdentity{}, store.ErrAccessDenied
}
entry := result.Entries[0]
subject, err := ldapStableSubject(entry.GetEqualFoldRawAttributeValue(config.LDAPUIDAttribute))
if err != nil {
return ldapIdentity{}, err
}
identity := ldapIdentity{
Subject: subject, DN: entry.DN,
Username: strings.ToLower(strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPUsernameAttribute))),
Email: strings.ToLower(strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPEmailAttribute))),
DisplayName: strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPDisplayAttribute)),
}
if identity.Username == "" || !utf8.ValidString(identity.Username) || !utf8.ValidString(identity.DisplayName) || !utf8.ValidString(identity.Email) {
return ldapIdentity{}, errors.New("LDAP identity attributes are invalid")
}
// The successful user bind is the password proof. Never retain or log the
// supplied password, and do not accept a search result alone as login.
if err = directory.Bind(entry.DN, password); err != nil {
return ldapIdentity{}, store.ErrAccessDenied
}
return identity, nil
}
func (s *Server) authenticateLDAPPrincipal(ctx context.Context, domainUUID, login, password string) (*store.Principal, error) {
config, err := s.store.AuthProviderConfig(domainUUID, "ldap")
if err != nil || !config.Enabled {
return nil, store.ErrAccessDenied
}
identity, err := s.authenticateLDAPDirectory(ctx, config, login, password)
if err != nil {
return nil, err
}
attributes := map[string]string{"dn": identity.DN, "username": identity.Username, "email": identity.Email, "display_name": identity.DisplayName}
principal, err := s.store.PrincipalByExternalIdentity(domainUUID, "ldap", config.UUID, "", identity.Subject)
if err == nil {
if bindErr := s.store.BindExternalIdentity(principal.UUID, "ldap", config.UUID, "", identity.Subject, attributes); bindErr != nil {
return nil, bindErr
}
return principal, nil
}
if !errors.Is(err, sql.ErrNoRows) {
return nil, err
}
switch config.LDAPMatchBy {
case "email":
principal, err = s.store.PrincipalByVerifiedEmail(domainUUID, identity.Email)
default:
principal, err = s.store.PrincipalByExternalUsername(domainUUID, identity.Username)
}
if err == nil {
if err = s.store.BindExternalIdentityIfProviderUnbound(domainUUID, principal.UUID, "ldap", config.UUID, "", identity.Subject, attributes); err != nil {
return nil, err
}
return principal, nil
}
if !errors.Is(err, store.ErrAccessDenied) || !config.LDAPAutoProvision {
return nil, store.ErrAccessDenied
}
return s.store.ProvisionExternalAccount(domainUUID, identity.Username, identity.DisplayName, identity.Email, "ldap", config.UUID, "", identity.Subject, attributes)
}
func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Cache-Control", "no-store")
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid sign-in request", http.StatusBadRequest)
return
}
returnPath := safeReturnPath(r.FormValue("return"))
select {
case s.ldapSlots <- struct{}{}:
defer func() { <-s.ldapSlots }()
default:
http.Error(w, "directory authentication is busy", http.StatusServiceUnavailable)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
defer cancel()
domainUUID := s.domainUUIDForRequest(r)
principal, err := s.authenticateLDAPPrincipal(ctx, domainUUID, r.FormValue("username"), r.FormValue("password"))
if err != nil {
log.Printf("LDAP login failed domain=%s error=%v", domainUUID, err)
s.renderAuthPage(w, r, map[string]any{"Error": translate(s.getLocale(domainUUID), "auth.invalid_credentials"), "Return": returnPath})
return
}
if s.store.HasTOTP(principal.UUID) {
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
if challengeErr != nil {
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
return
}
if strings.Contains(r.Header.Get("Accept"), "application/json") {
writeEventJSON(w, http.StatusUnauthorized, map[string]any{"mfa_required": true, "challenge_token": challenge, "method": "totp"})
return
}
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Value: challenge, Path: "/_/auth/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode, MaxAge: 300})
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": returnPath})
return
}
var token string
if strings.Contains(r.Header.Get("Accept"), "application/json") {
token, err = s.store.CreateAuthSession(domainUUID, principal.UUID)
} else {
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
}
if err != nil {
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
return
}
if strings.Contains(r.Header.Get("Accept"), "application/json") {
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": 86400})
return
}
s.setAuthCookie(w, r, token)
http.Redirect(w, r, returnPath, http.StatusSeeOther)
}
+165
View File
@@ -0,0 +1,165 @@
package server
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
ldap "github.com/go-ldap/ldap/v3"
"pageswiki/internal/store"
)
type fakeLDAPDirectory struct {
result *ldap.SearchResult
searchErr error
userBindErr error
binds [][2]string
searchFilter string
}
func (f *fakeLDAPDirectory) Bind(dn, password string) error {
f.binds = append(f.binds, [2]string{dn, password})
if len(f.binds) > 1 {
return f.userBindErr
}
return nil
}
func (f *fakeLDAPDirectory) Search(request *ldap.SearchRequest) (*ldap.SearchResult, error) {
f.searchFilter = request.Filter
return f.result, f.searchErr
}
func (f *fakeLDAPDirectory) Close() error { return nil }
func ldapEntry(uid, username, email, displayName string) *ldap.Entry {
return ldap.NewEntry("uid="+username+",ou=people,dc=example,dc=test", map[string][]string{
"entryUUID": {uid},
"uid": {username},
"mail": {email},
"displayName": {displayName},
})
}
func configuredLDAPServer(t *testing.T) (*Server, string, store.AuthProviderConfig) {
t.Helper()
s, _, domain := configFixture(t)
config := store.AuthProviderConfig{
Kind: "ldap", Enabled: true, LDAPURL: "ldaps://directory.example.test:636",
LDAPBindDN: "cn=pageswiki,ou=services,dc=example,dc=test", LDAPBindPassword: "service-secret",
LDAPSearchBase: "ou=people,dc=example,dc=test", LDAPSearchFilter: "(&(objectClass=person)(uid={{username}}))",
LDAPUIDAttribute: "entryUUID", LDAPUsernameAttribute: "uid", LDAPEmailAttribute: "mail", LDAPDisplayAttribute: "displayName",
LDAPMatchBy: "username",
}
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
t.Fatal(err)
}
config, err := s.store.AuthProviderConfig(domain, "ldap")
if err != nil {
t.Fatal(err)
}
s.ldapSlots = make(chan struct{}, 4)
return s, domain, config
}
func TestLDAPAuthenticationSearchesSafelyAndBindsAsUser(t *testing.T) {
s, domain, _ := configuredLDAPServer(t)
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Directory Admin")}}}
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "admin", "user-secret")
if err != nil || principal.Username != "admin" {
t.Fatalf("LDAP principal = %#v, %v", principal, err)
}
if len(directory.binds) != 2 || directory.binds[0] != [2]string{"cn=pageswiki,ou=services,dc=example,dc=test", "service-secret"} || directory.binds[1][1] != "user-secret" {
t.Fatalf("LDAP binds = %#v", directory.binds)
}
if directory.searchFilter != "(&(objectClass=person)(uid=admin))" {
t.Fatalf("search filter = %q", directory.searchFilter)
}
loaded, err := s.store.AuthProviderConfig(domain, "ldap")
if err != nil {
t.Fatal(err)
}
subject, _ := ldapStableSubject([]byte("stable-id"))
bound, err := s.store.PrincipalByExternalIdentity(domain, "ldap", loaded.UUID, "", subject)
if err != nil || bound.UUID != principal.UUID {
t.Fatalf("immutable LDAP binding = %#v, %v", bound, err)
}
}
func TestLDAPAuthenticationEscapesFilterAndRejectsAmbiguousOrBadPassword(t *testing.T) {
s, _, config := configuredLDAPServer(t)
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}}
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
login := `admin*)(uid=*)`
if _, err := s.authenticateLDAPDirectory(context.Background(), config, login, "user-secret"); err != nil {
t.Fatal(err)
}
if strings.Contains(directory.searchFilter, login) || !strings.Contains(directory.searchFilter, ldap.EscapeFilter(login)) {
t.Fatalf("LDAP search input was not escaped: %q", directory.searchFilter)
}
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("one", "admin", "", "Admin"), ldapEntry("two", "admin", "", "Admin")}}
directory.binds = nil
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "user-secret"); !errors.Is(err, store.ErrAccessDenied) {
t.Fatalf("ambiguous directory result accepted: %v", err)
}
if len(directory.binds) != 1 {
t.Fatalf("ambiguous result attempted a user bind: %#v", directory.binds)
}
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}
directory.binds = nil
directory.userBindErr = errors.New("invalid credentials")
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "wrong"); !errors.Is(err, store.ErrAccessDenied) {
t.Fatalf("bad LDAP password accepted: %v", err)
}
}
func TestLDAPAutoProvisionUsesImmutableSubjectOnFollowingLogins(t *testing.T) {
s, domain, config := configuredLDAPServer(t)
config.LDAPAutoProvision = true
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
t.Fatal(err)
}
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "directory-user", "directory@example.test", "Directory User")}}}
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "directory-user", "user-secret")
if err != nil || principal.Username != "directory-user" {
t.Fatalf("provisioned LDAP principal = %#v, %v", principal, err)
}
// Mutable directory attributes may change, but the immutable configured UID
// remains the authoritative identity after first login.
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "renamed-user", "renamed@example.test", "Renamed User")}}
directory.binds = nil
again, err := s.authenticateLDAPPrincipal(context.Background(), domain, "renamed-user", "user-secret")
if err != nil || again.UUID != principal.UUID {
t.Fatalf("immutable LDAP re-login = %#v, %v", again, err)
}
}
func TestLDAPLoginHandlerCreatesTypedBrowserSession(t *testing.T) {
s, domain, _ := configuredLDAPServer(t)
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Admin")}}}
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
form := url.Values{"username": {"admin"}, "password": {"user-secret"}, "return": {"/v/tree/docs"}}
request := httptest.NewRequest(http.MethodPost, "https://localhost/_/auth/ldap", strings.NewReader(form.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := httptest.NewRecorder()
s.handleLDAPLogin(response, request)
if response.Code != http.StatusSeeOther || response.Header().Get("Location") != "/v/tree/docs" {
t.Fatalf("LDAP browser login: %d %s", response.Code, response.Body.String())
}
cookies := response.Result().Cookies()
if len(cookies) != 1 || cookies[0].Name != authCookieName || !cookies[0].HttpOnly || !cookies[0].Secure || !strings.HasPrefix(cookies[0].Value, "pw_browser_") {
t.Fatalf("LDAP browser cookie: %#v", cookies)
}
principal, err := s.store.PrincipalForSession(domain, cookies[0].Value)
if err != nil || principal.Username != "admin" {
t.Fatalf("LDAP browser session: %#v, %v", principal, err)
}
}
@@ -0,0 +1,225 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestAutomaticPublication(t *testing.T) {
s, token, domain := configFixture(t)
ctx := context.Background()
s.mux.HandleFunc("/", s.handleHome)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
actor, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
check := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
q, err := s.store.CreateZoneAtPath(domain, actor.UUID, "quartier", "", "auto", "Auto", "", "/auto")
check(err)
street, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "street", "Street", "", "/auto/street")
check(err)
spot, err := s.store.CreateSpotAtPath(domain, actor.UUID, street, "spot", "Spot", "", "/auto/street/spot")
check(err)
setAudience := func(kind, id, value string) {
t.Helper()
g, e := s.store.CityGovernance(ctx, domain, actor.UUID, kind, id)
check(e)
_, e = s.store.ChangeCityGovernance(ctx, domain, actor.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false)
check(e)
}
setAudience("domain", domain, "public")
setAudience("quartier", q, "public")
change := func(kind, id, mode string) {
t.Helper()
p, e := s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, nil)
check(e)
_, e = s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, &store.AutoPublicationChange{Mode: mode, Version: p.Version, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Confirm: true, ImpactToken: p.Impact.ImpactToken})
check(e)
}
create := func(path, body string) string {
t.Helper()
id, e := s.store.CreatePageAtPathInDomain(domain, path)
check(e)
_, e = s.store.SaveRevisionContent(id, []byte(body), "", "und", "text/html", "")
check(e)
return id
}
get := func(path string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest("GET", "http://localhost"+path, nil)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
page := create("/auto/street/first", "<p>Original public text</p>")
if w := get("/auto/street/first"); w.Code == 200 {
t.Fatal("default published")
}
preview, err := s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, nil)
check(err)
_, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, ImpactToken: preview.Impact.ImpactToken})
if err == nil {
t.Fatal("missing confirmation")
}
create("/auto/street/stale-preview", "<p>Changed impact</p>")
_, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, Confirm: true, ImpactToken: preview.Impact.ImpactToken})
if err == nil {
t.Fatal("stale impact accepted")
}
change("street", street, "automatic")
if err := s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
t.Fatal("automatic scope allowed disabling authorization")
}
if w := get("/auto/street/first"); w.Code != 200 || !strings.Contains(w.Body.String(), "Original public text") {
t.Fatal("existing", w.Code, w.Body.String())
}
create("/auto/street/second", "<p>Future public text</p>")
_, err = s.store.SaveRevisionContent(page, []byte("<p>Updated public text</p>"), "", "und", "text/html", "")
check(err)
for _, path := range []string{"/auto/street/first", "/auto/street/second"} {
if w := get(path); w.Code != 200 {
t.Fatal("auto head", path, w.Code, w.Body.String())
}
}
if w := get("/auto/street/first"); !strings.Contains(w.Body.String(), "Updated public text") {
t.Fatal("stale head")
}
create("/auto/streets/not-in-scope", "PRIVATE-BOUNDARY")
if w := get("/auto/streets/not-in-scope"); w.Code == 200 {
t.Fatal("scope escaped")
}
create("/auto/street/spot/child", "<p>Spot body</p>")
create("/auto/street/spot", "<p>Spot portal</p>")
if w := get("/auto/street/spot"); w.Code != 200 {
t.Fatal("scope root excluded", w.Code)
}
if w := get("/auto/street/spot/child"); w.Code != 200 {
t.Fatal("inherited auto", w.Code)
}
change("spot", spot, "required")
if w := get("/auto/street/spot"); w.Code == 200 {
t.Fatal("scope root escaped manual floor")
}
if w := get("/auto/street/spot/child"); w.Code == 200 {
t.Fatal("manual child did not narrow")
}
change("spot", spot, "inherit")
if w := get("/auto/street/spot/child"); w.Code != 200 {
t.Fatal("inherit did not restore")
}
change("quartier", q, "required")
if w := get("/auto/street/first"); w.Code == 200 {
t.Fatal("superior floor ignored")
}
p, err := s.store.AutoPublication(ctx, domain, actor.UUID, "spot", spot, nil)
check(err)
if p.CanAutomatic {
t.Fatal("subordinate bypass offered")
}
change("quartier", q, "inherit")
setAudience("spot", spot, "authenticated_acl")
if w := get("/auto/street/spot/child"); w.Code == 200 {
t.Fatal("private audience leaked")
}
setAudience("spot", spot, "inherit")
acl, err := s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, nil)
check(err)
acl.Mode = "restricted"
acl.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: actor.Username, Permission: "read_write"}}
acl, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl)
check(err)
if w := get("/auto/street/spot/child"); w.Code == 200 {
t.Fatal("restricted ACL leaked")
}
acl.Mode = "inherit"
acl.Entries = nil
_, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl)
check(err)
aliasPage := create("/auto/street/aliased", "PRIVATE-ALIAS")
_, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,'/private-alias',?,0,0)`, domain, aliasPage)
check(err)
if w := get("/auto/street/aliased"); w.Code == 200 {
t.Fatal("aliased object published")
}
// The existing revoke action must stop inherited automatic publication too.
publish, err := s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, nil)
check(err)
_, err = s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, &store.PublicationChange{Epoch: publish.Epoch, TopologyVersion: publish.TopologyVersion, PublicationVersion: publish.PublicationVersion, Enabled: false})
check(err)
if w := get("/auto/street/spot/child"); w.Code == 200 {
t.Fatal("revoke left automatic publication active")
}
endpoint := "/_/api/v1/city/zones/" + street + "/governance/auto-publication"
if w := get(endpoint); w.Code == 200 {
t.Fatal("anonymous policy API")
}
r := httptest.NewRequest("GET", "http://localhost"+endpoint, nil)
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
var state store.AutoPublication
check(json.Unmarshal(w.Body.Bytes(), &state))
if w.Code != 200 || state.Mode != "automatic" {
t.Fatal("API", w.Code, w.Body.String())
}
empty, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "empty", "Empty", "", "/auto/empty")
check(err)
change("street", empty, "automatic")
create("/auto/empty/new", "<p>First article in empty scope</p>")
if w := get("/auto/empty/new"); w.Code != 200 {
t.Fatal("empty scope cannot auto publish", w.Code)
}
if os.Getenv("PAGESWIKI_BROWSER_TEST") == "1" {
s.mux.HandleFunc("/v/city/", s.handleCityMap)
script, e := os.ReadFile("internal/server/testdata/automatic-publication.js")
check(e)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if r.Header.Get("Origin") != "" {
r.Header.Set("Origin", "http://localhost")
}
if r.URL.Path == "/v/city/s/auto/empty" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "390"} {
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/city/s/auto/empty", t.TempDir(), "", width).CombinedOutput()
cancel()
if e != nil {
t.Fatalf("automatic publication UI %s: %v %s", width, e, out)
}
}
}
// Revoking the enabling principal also invalidates the public-navigation cache.
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor.UUID)
check(err)
if w := get("/auto/street/first"); w.Code == 200 {
t.Fatal("revoked enabling authority")
}
}
+85
View File
@@ -0,0 +1,85 @@
package server
import (
"net/http"
"net/url"
"strconv"
"strings"
)
type bookmarkSection struct {
Label string
Source string
Links []NavItem
}
func (s *Server) bookmarkSections(r *http.Request) []bookmarkSection {
sections := []bookmarkSection{}
for i, item := range s.homeBookmarks(s.domainUUIDForRequest(r)) {
if item.Kind == "section" {
section := bookmarkSection{Label: item.Label}
if item.Children {
section.Source = "/v/bookmarks?section=" + strconv.Itoa(i)
}
sections = append(sections, section)
continue
}
if !validHomeBookmarkURL(item.Href) {
continue
}
u, _ := url.Parse(item.Href)
if u.Host == "" && !s.requestCanReadPath(r, u.Path) {
continue
}
if len(sections) == 0 {
sections = append(sections, bookmarkSection{Label: translate(s.getLocale(s.domainUUIDForRequest(r)), "home.bookmarks")})
}
section := &sections[len(sections)-1]
if section.Source == "" {
section.Links = append(section.Links, item)
}
}
return sections
}
// A small, keyset-paged listing: no revision bodies, stats, or preview work.
// The section index is resolved against domain config, not a caller's URL.
func (s *Server) handleBookmarkChildren(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", 405)
return
}
items := s.homeBookmarks(s.domainUUIDForRequest(r))
i, err := strconv.Atoi(r.URL.Query().Get("section"))
if err != nil || i < 0 || i >= len(items) || items[i].Kind != "section" || !items[i].Children {
http.NotFound(w, r)
return
}
u, err := url.Parse(items[i].Href)
if err != nil || u.Host != "" || u.Scheme != "" || !strings.HasPrefix(u.Path, "/") || !s.requestCanReadPath(r, u.Path) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
names, more, err := s.store.ListBookmarkChildren(r.Context(), s.domainUUIDForRequest(r), u.Path, r.URL.Query().Get("after"))
if err != nil {
http.Error(w, "could not load bookmarks", 500)
return
}
links := []NavItem{}
prefix := strings.TrimRight(u.Path, "/") + "/"
for _, name := range names {
p := prefix + name
if prefix == "/" && (name == "u" || name == "_" || name == "-") {
continue
}
if s.requestCanReadPath(r, p) {
links = append(links, NavItem{Label: name, Href: (&url.URL{Path: p}).String()})
}
}
next := ""
if more && len(names) > 0 {
next = names[len(names)-1]
}
w.Header().Set("Cache-Control", "no-store")
writeEventJSON(w, 200, map[string]any{"links": links, "next": next})
}
+91
View File
@@ -0,0 +1,91 @@
package server
import (
"errors"
"net/http"
"net/url"
"path"
"strings"
)
type homeBookmark struct {
Label string `json:"label"`
Href string `json:"href"`
Kind string `json:"kind,omitempty"`
Children bool `json:"children,omitempty"`
}
func bookmarksFromForm(r *http.Request) ([]homeBookmark, error) {
labels, targets, kinds := r.Form["bookmark_label"], r.Form["bookmark_target"], r.Form["bookmark_kind"]
if len(labels) > 64 || len(labels) != len(targets) || len(labels) != len(kinds) {
return nil, errors.New("invalid bookmark rows")
}
if children := r.Form["bookmark_children"]; len(children) != 0 && len(children) != len(labels) {
return nil, errors.New("invalid bookmark toggle rows")
}
bookmarks := []homeBookmark{}
sections := 0
for i, label := range labels {
label, target := strings.TrimSpace(label), strings.TrimSpace(targets[i])
if label == "" && target == "" {
continue
}
if label == "" || len(label) > 200 || len(target) > 4096 {
return nil, errors.New("invalid bookmark")
}
switch kinds[i] {
case "section":
sections++
if sections > 16 || strings.ContainsAny(target, "?#\\\r\n") {
return nil, errors.New("invalid bookmark section")
}
children := len(r.Form["bookmark_children"]) > i && r.Form["bookmark_children"][i] == "true"
if children && target == "" {
return nil, errors.New("missing section parent path")
}
if target != "" {
target = (&url.URL{Path: path.Clean("/" + strings.TrimPrefix(target, "/"))}).String()
}
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: "section", Children: children})
continue
case "path":
if target == "" || strings.ContainsAny(target, "?#\\\r\n") {
return nil, errors.New("invalid bookmark path")
}
target = path.Clean("/" + strings.TrimPrefix(target, "/"))
// Store an escaped local URL so spaces, % and Unicode retain their
// literal semantic Path meaning when the link is opened.
target = (&url.URL{Path: target}).String()
case "url":
if !validHomeBookmarkURL(target) {
return nil, errors.New("invalid bookmark URL")
}
default:
return nil, errors.New("invalid bookmark kind")
}
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: kinds[i]})
}
return bookmarks, nil
}
func (s *Server) managedBookmarks(domainUUID string) []homeBookmark {
items := s.homeBookmarks(domainUUID)
var out []homeBookmark
for _, item := range items {
kind := item.Kind
if kind == "" {
kind = "url"
if strings.HasPrefix(item.Href, "/") {
kind = "path"
}
}
target := item.Href
if kind == "path" || kind == "section" {
if u, err := url.Parse(target); err == nil {
target = u.Path
}
}
out = append(out, homeBookmark{Label: item.Label, Href: target, Kind: kind, Children: item.Children})
}
return out
}
+62 -6
View File
@@ -2,9 +2,44 @@ package server
import (
"net/http"
"path/filepath"
"strings"
"pageswiki/internal/store"
)
type browserEntryView struct {
store.BrowserEntry
ThumbnailURL string
}
func (s *Server) browserEntryViews(entries []store.BrowserEntry) []browserEntryView {
views := make([]browserEntryView, 0, len(entries))
for _, entry := range entries {
view := browserEntryView{BrowserEntry: entry}
if !entry.IsDir && entry.RevisionUUID != "" {
filename := entry.Filename
if filename == "" {
filename = filepath.Base(entry.Path)
}
adapter := selectProjectionAdapter(contentDescriptor{
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
Filename: filename,
SizeBytes: int64(entry.Size),
})
if adapter.ID == "image" {
if cachedURL, cached := s.entryPreviewState(entry, "image", false); cached {
view.ThumbnailURL = cachedURL
} else {
view.ThumbnailURL = rawProjectionURL("rev:"+entry.RevisionUUID) + "?preview=1"
}
}
}
views = append(views, view)
}
return views
}
func (s *Server) handleBrowser(
w http.ResponseWriter,
r *http.Request,
@@ -25,7 +60,8 @@ func (s *Server) handleBrowser(
}
entries, err :=
s.store.ListBrowserEntries(
s.store.ListBrowserEntriesInDomain(
s.domainUUIDForRequest(r),
currentPath,
)
@@ -33,20 +69,40 @@ func (s *Server) handleBrowser(
http.Error(w, err.Error(), 500)
return
}
visibleEntries := entries[:0]
for _, entry := range entries {
if s.requestCanReadPath(r, entry.Path) {
visibleEntries = append(visibleEntries, entry)
}
}
entries = visibleEntries
var navigatorLinks []store.BrowserNavigationLink
if principal := s.requestPrincipal(r); principal != nil {
if links, linkErr := s.store.ListBrowserNavigationLinks(s.domainUUIDForRequest(r), principal.UUID); linkErr == nil {
for _, link := range links {
if s.requestCanReadPath(r, link.Path) {
navigatorLinks = append(navigatorLinks, link)
}
}
}
}
s.render(
s.renderWithContext(
w,
r,
"browser.gohtml",
map[string]any{
"NavItems": resolveNavItems(
s.getSystemNavItems(),
s.getSystemNavItems(s.domainUUIDForRequest(r)),
NavContext{
Path: currentPath,
},
),
"Path": currentPath,
"Breadcrumbs": buildBreadcrumbs(currentPath),
"Entries": entries,
"Path": currentPath,
"Breadcrumbs": buildBreadcrumbs(currentPath),
"Entries": s.browserEntryViews(entries),
"NavigatorLinks": navigatorLinks,
"CanEditNavigator": s.requestPrincipal(r) != nil,
},
)
}
+108
View File
@@ -0,0 +1,108 @@
package server
import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"strings"
"pageswiki/internal/store"
)
type browserNavigationInput struct {
UUID string `json:"uuid"`
Label string `json:"label"`
Path string `json:"path"`
IsDir bool `json:"is_directory"`
}
func (s *Server) handleBrowserNavigationAPI(w http.ResponseWriter, r *http.Request) {
domainUUID := s.domainUUIDForRequest(r)
principal := s.requestPrincipal(r)
if principal == nil || principal.State != "active" {
http.Error(w, "authentication required", http.StatusUnauthorized)
return
}
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
links, err := s.store.ListBrowserNavigationLinks(domainUUID, principal.UUID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
visible := links[:0]
for _, link := range links {
if s.requestCanReadPath(r, link.Path) {
visible = append(visible, link)
}
}
_ = json.NewEncoder(w).Encode(map[string]any{"links": visible})
case http.MethodPost:
var input browserNavigationInput
if !decodeBrowserNavigationInput(w, r, &input) {
return
}
input.Path = normalizeAuthorizationPath(input.Path)
if input.Path == "" || input.Path == "/" || !s.requestCanReadPath(r, input.Path) {
http.Error(w, "invalid or forbidden path", http.StatusForbidden)
return
}
link, err := s.store.AddBrowserNavigationLink(domainUUID, principal.UUID, input.Label, input.Path, input.IsDir)
if err != nil {
status := http.StatusBadRequest
if errors.Is(err, store.ErrBrowserNavigationLimit) {
status = http.StatusConflict
}
http.Error(w, err.Error(), status)
return
}
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(link)
case http.MethodPatch:
var input browserNavigationInput
if !decodeBrowserNavigationInput(w, r, &input) {
return
}
if err := s.store.RenameBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID), input.Label); err != nil {
writeBrowserNavigationError(w, err)
return
}
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
case http.MethodDelete:
var input browserNavigationInput
if !decodeBrowserNavigationInput(w, r, &input) {
return
}
if err := s.store.DeleteBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID)); err != nil {
writeBrowserNavigationError(w, err)
return
}
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
default:
w.Header().Set("Allow", "GET, POST, PATCH, DELETE")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func decodeBrowserNavigationInput(w http.ResponseWriter, r *http.Request, input *browserNavigationInput) bool {
r.Body = http.MaxBytesReader(w, r.Body, 16<<10)
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(input); err != nil {
http.Error(w, "invalid request", http.StatusBadRequest)
return false
}
return true
}
func writeBrowserNavigationError(w http.ResponseWriter, err error) {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, "navigator link not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
}
+125
View File
@@ -0,0 +1,125 @@
package server
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func browserNavigationRequest(s *Server, token, method string, body any) *httptest.ResponseRecorder {
var encoded bytes.Buffer
if body != nil {
_ = json.NewEncoder(&encoded).Encode(body)
}
request := httptest.NewRequest(method, "http://localhost/_/api/v1/browser/navigation", &encoded)
request.Header.Set("Authorization", "Bearer "+token)
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(response, request)
return response
}
func TestBrowserMarqueeAndNavigatorDragChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
for _, path := range []string{"/drag/alpha", "/drag/beta"} {
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
t.Fatal(err)
}
}
testScript := `<script>(async()=>{
await new Promise(resolve=>setTimeout(resolve,50));
const rows=[...document.querySelectorAll('.browser-row')],grid=document.getElementById('browser-grid');
const first=rows[0].getBoundingClientRect(),last=rows.at(-1).getBoundingClientRect(),pointerId=9;
grid.dispatchEvent(new PointerEvent('pointerdown',{bubbles:true,button:0,pointerId,clientX:first.left+1,clientY:first.top+1}));
grid.dispatchEvent(new PointerEvent('pointermove',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
grid.dispatchEvent(new PointerEvent('pointerup',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
const transfer=new DataTransfer();
transfer.setData('application/x-pageswiki-paths',JSON.stringify(rows.map(row=>({path:row.dataset.path,name:row.dataset.name,is_directory:false}))));
await navigatorDrop({preventDefault(){},stopPropagation(){},dataTransfer:transfer});
document.body.dataset.browserInteractions=selectedEntries().length===2&&document.querySelectorAll('.navigator-link').length===2?'ready':'failed';
})()</script>`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if strings.HasPrefix(r.URL.Path, "/static/") {
s.mux.ServeHTTP(w, r)
return
}
if r.URL.Path == "/v/browser/drag" {
recorded := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(recorded, r)
for key, values := range recorded.Header() {
w.Header()[key] = values
}
w.WriteHeader(recorded.Code)
_, _ = w.Write([]byte(strings.Replace(recorded.Body.String(), "</body>", testScript+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
command := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=3000", server.URL+"/v/browser/drag")
output, err := command.CombinedOutput()
if body := string(output); err != nil || !strings.Contains(body, `data-browser-interactions="ready"`) {
t.Fatalf("browser interactions: err=%v body=%s", err, body)
}
}
func TestBrowserNavigatorAPIAndUI(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
if _, err := s.store.CreatePageAtPathInDomain(domain, "/docs/page"); err != nil {
t.Fatal(err)
}
response := browserNavigationRequest(s, token, http.MethodPost, browserNavigationInput{Label: "Docs", Path: "/docs", IsDir: true})
if response.Code != http.StatusCreated {
t.Fatalf("add navigator link: %d %s", response.Code, response.Body.String())
}
var link struct {
UUID string `json:"uuid"`
}
if err := json.Unmarshal(response.Body.Bytes(), &link); err != nil || link.UUID == "" {
t.Fatalf("add response: %#v %v", link, err)
}
response = browserNavigationRequest(s, token, http.MethodPatch, browserNavigationInput{UUID: link.UUID, Label: "Documents"})
if response.Code != http.StatusOK {
t.Fatalf("rename navigator link: %d %s", response.Code, response.Body.String())
}
page := configRequest(s, token, http.MethodGet, "/v/browser/", nil)
body := page.Body.String()
for _, expected := range []string{
`data-navigation-id="` + link.UUID + `"`,
`href="/v/browser/docs"`,
`>Documents</a>`,
`/static/js/browser-interactions.js?v=1`,
`id="selection-marquee"`,
`id="browser-operation-stack"`,
} {
if page.Code != http.StatusOK || !strings.Contains(body, expected) {
t.Fatalf("browser UI missing %q: status=%d body=%s", expected, page.Code, body)
}
}
response = browserNavigationRequest(s, token, http.MethodDelete, browserNavigationInput{UUID: link.UUID})
if response.Code != http.StatusOK {
t.Fatalf("delete navigator link: %d %s", response.Code, response.Body.String())
}
}
+99
View File
@@ -0,0 +1,99 @@
package server
import (
"context"
"encoding/base64"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
const browserThumbnailPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
func TestBrowserImageThumbnailUsesLazyImmutableRevision(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/v/raw/", s.handleRawView)
object, err := s.store.CreatePageAtPathInDomain(domain, "/pictures/sample.png")
if err != nil {
t.Fatal(err)
}
png, err := base64.StdEncoding.DecodeString(browserThumbnailPNG)
if err != nil {
t.Fatal(err)
}
revision, err := s.store.SaveRevisionContent(object, png, "test", "und", "image/png", "sample.png")
if err != nil {
t.Fatal(err)
}
s.previewRoot = t.TempDir()
w := configRequest(s, token, http.MethodGet, "/v/browser/pictures", nil)
body := w.Body.String()
want := `data-preview-src="/v/raw/rev:` + revision.UUID + `?preview=1"`
if w.Code != http.StatusOK || !strings.Contains(body, want) ||
!strings.Contains(body, `class="browser-thumbnail children-preview children-preview-pending"`) ||
!strings.Contains(body, `/static/js/browser-thumbnails.js?v=1`) {
t.Fatalf("browser thumbnail: status=%d want=%q body=%s", w.Code, want, body)
}
raw := configRequest(s, token, http.MethodGet, "/v/raw/rev:"+revision.UUID+"?preview=1", nil)
if raw.Code != http.StatusOK || raw.Header().Get("Content-Type") != "image/png" {
t.Fatalf("thumbnail source: status=%d type=%q body=%s", raw.Code, raw.Header().Get("Content-Type"), raw.Body.String())
}
if err = os.WriteFile(filepath.Join(s.previewRoot, revision.DocumentHash+".jpg"), []byte("cached thumbnail"), 0o644); err != nil {
t.Fatal(err)
}
w = configRequest(s, token, http.MethodGet, "/v/browser/pictures", nil)
if cached := `/v/preview-cache/` + revision.DocumentHash + `.jpg`; w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `data-preview-src="`+cached+`"`) {
t.Fatalf("cached browser thumbnail: status=%d cache=%q body=%s", w.Code, cached, w.Body.String())
}
}
func TestBrowserImageThumbnailChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/v/raw/", s.handleRawView)
object, err := s.store.CreatePageAtPathInDomain(domain, "/pictures/browser.png")
if err != nil {
t.Fatal(err)
}
png, err := base64.StdEncoding.DecodeString(browserThumbnailPNG)
if err != nil {
t.Fatal(err)
}
revision, err := s.store.SaveRevisionContent(object, png, "test", "und", "image/png", "browser.png")
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 strings.HasPrefix(r.URL.Path, "/static/") {
s.mux.ServeHTTP(w, r)
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=3000", server.URL+"/v/browser/pictures")
out, err := cmd.CombinedOutput()
body := string(out)
scriptReady := strings.Contains(body, `data-browser-thumbnails="ready"`)
previewLoaded := strings.Contains(body, `data-preview-loaded="true"`)
sourceLoaded := strings.Contains(body, `src="/v/raw/rev:`+revision.UUID+`?preview=1"`)
if err != nil || !scriptReady || !previewLoaded || !sourceLoaded {
t.Fatalf("chromium thumbnail: err=%v script=%t preview=%t source=%t", err, scriptReady, previewLoaded, sourceLoaded)
}
}
+215
View File
@@ -0,0 +1,215 @@
package server
import (
"bytes"
"context"
"encoding/xml"
"errors"
"io"
"net/http"
"net/url"
"pageswiki/internal/calendar"
"path"
"strings"
"time"
)
type calendarAuthTransport struct {
base http.RoundTripper
origin, user, password string
}
func (t calendarAuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
origin, e := url.Parse(t.origin)
if e != nil || !strings.EqualFold(origin.Host, r.URL.Host) || origin.Scheme != r.URL.Scheme {
return nil, errors.New("calendar credential origin mismatch")
}
clone := r.Clone(r.Context())
clone.SetBasicAuth(t.user, t.password)
return t.base.RoundTrip(clone)
}
type remoteCalendarObject struct{ href, etag, data string }
func remoteCalendarReport(ctx context.Context, client *http.Client, address, body string) ([]remoteCalendarObject, error) {
req, e := http.NewRequestWithContext(ctx, "REPORT", address, strings.NewReader(body))
if e != nil {
return nil, errors.New("invalid CalDAV URL")
}
req.Header.Set("Content-Type", "application/xml; charset=utf-8")
req.Header.Set("Depth", "1")
response, e := client.Do(req)
if e != nil {
return nil, calendarSourceRequestError(e)
}
defer response.Body.Close()
if response.StatusCode != 207 {
return nil, errors.New("external CalDAV REPORT returned " + http.StatusText(response.StatusCode))
}
b, e := io.ReadAll(io.LimitReader(response.Body, calendar.MaxFeed+1))
if e != nil || len(b) > calendar.MaxFeed {
return nil, calendar.ErrLimit
}
dec := xml.NewDecoder(bytes.NewReader(b))
depth, count := 0, 0
for {
token, e := dec.Token()
if e == io.EOF {
break
}
if e != nil {
return nil, errors.New("invalid CalDAV XML")
}
switch token.(type) {
case xml.Directive:
return nil, errors.New("CalDAV DTD rejected")
case xml.StartElement:
depth++
count++
if depth > 16 || count > 100000 {
return nil, calendar.ErrLimit
}
case xml.EndElement:
depth--
}
}
var root davNode
if xml.Unmarshal(b, &root) != nil || root.XMLName.Space != davNS || root.XMLName.Local != "multistatus" {
return nil, errors.New("invalid CalDAV multistatus")
}
origin, _ := url.Parse(address)
prefix := strings.TrimSuffix(path.Clean(origin.Path), "/") + "/"
seen := map[string]bool{}
out := []remoteCalendarObject{}
for _, row := range root.Children {
if row.XMLName.Space != davNS || row.XMLName.Local != "response" {
return nil, errors.New("unsupported CalDAV report response")
}
href := row.child(davNS, "href")
if href == nil {
return nil, errors.New("CalDAV href missing")
}
u, e := url.Parse(strings.TrimSpace(href.Text))
if e != nil {
return nil, errors.New("invalid CalDAV href")
}
u = origin.ResolveReference(u)
if !strings.EqualFold(u.Host, origin.Host) || u.Scheme != origin.Scheme || u.User != nil || u.RawQuery != "" || u.Fragment != "" || !strings.HasPrefix(path.Clean(u.Path), prefix) {
return nil, errors.New("CalDAV href outside collection")
}
key := u.EscapedPath()
if seen[key] {
return nil, errors.New("duplicate CalDAV href")
}
seen[key] = true
item := remoteCalendarObject{href: key}
for _, ps := range row.Children {
if ps.XMLName.Space == davNS && ps.XMLName.Local == "status" {
return nil, errors.New("incomplete CalDAV report")
}
if ps.XMLName.Space != davNS || ps.XMLName.Local != "propstat" {
continue
}
status := ps.child(davNS, "status")
props := ps.child(davNS, "prop")
if status == nil || props == nil || !strings.Contains(status.Text, " 200 ") {
return nil, errors.New("CalDAV property unavailable")
}
if tag := props.child(davNS, "getetag"); tag != nil {
item.etag = tag.Text
}
if data := props.child(calNS, "calendar-data"); data != nil {
item.data = data.Text
}
}
if item.etag == "" {
return nil, errors.New("CalDAV ETag missing")
}
out = append(out, item)
if len(out) > 5000 {
return nil, calendar.ErrLimit
}
}
return out, nil
}
// Materialize a bounded read-only window: 90 days back, 275 days forward.
// REPORT fetches validators; calendar-multiget retrieves only changed resources.
func fetchCalDAVSource(ctx context.Context, client *http.Client, repo *calendar.Repository, domain string, c calendar.Calendar) ([]calendar.Event, map[string]calendar.RemoteResource, error) {
start := time.Now().UTC().Truncate(24*time.Hour).AddDate(0, 0, -90)
end := start.AddDate(0, 0, 365)
query := `<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:getetag/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT"><c:time-range start="` + start.Format("20060102T150405Z") + `" end="` + end.Format("20060102T150405Z") + `"/></c:comp-filter></c:comp-filter></c:filter></c:calendar-query>`
rows, e := remoteCalendarReport(ctx, client, c.Source.URL, query)
if e != nil {
return nil, nil, e
}
result := []calendar.Event{}
resources := map[string]calendar.RemoteResource{}
missing := []remoteCalendarObject{}
uids := map[string]bool{}
totalBytes := 0
add := func(row remoteCalendarObject, event calendar.Event) error {
totalBytes += len(event.Data)
if totalBytes > calendar.MaxFeed {
return calendar.ErrLimit
}
if uids[event.UID] {
return errors.New("CalDAV contains duplicate UID or recurrence exceptions")
}
uids[event.UID] = true
result = append(result, event)
resources[row.href] = calendar.RemoteResource{UID: event.UID, ETag: row.etag}
return nil
}
for _, row := range rows {
old, ok := c.Source.Resources[row.href]
if ok && old.ETag == row.etag {
event, e := repo.Event(ctx, domain, c.ID, c.Owner, old.UID)
if e == nil {
if e = add(row, event); e != nil {
return nil, nil, e
}
continue
}
if !errors.Is(e, calendar.ErrMissing) {
return nil, nil, e
}
}
missing = append(missing, row)
}
for offset := 0; offset < len(missing); offset += 100 {
limit := offset + 100
if limit > len(missing) {
limit = len(missing)
}
wanted := map[string]string{}
var body strings.Builder
body.WriteString(`<c:calendar-multiget xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:getetag/><c:calendar-data/></d:prop>`)
for _, row := range missing[offset:limit] {
wanted[row.href] = row.etag
body.WriteString("<d:href>" + xmlText(row.href) + "</d:href>")
}
body.WriteString("</c:calendar-multiget>")
batch, e := remoteCalendarReport(ctx, client, c.Source.URL, body.String())
if e != nil {
return nil, nil, e
}
if len(batch) != len(wanted) {
return nil, nil, errors.New("incomplete CalDAV multiget")
}
for _, row := range batch {
tag, ok := wanted[row.href]
if !ok || tag != row.etag {
return nil, nil, errors.New("CalDAV changed during synchronization; retry required")
}
event, e := calendar.Parse(row.data)
if e != nil {
return nil, nil, e
}
if e = add(row, event); e != nil {
return nil, nil, e
}
}
}
return result, resources, nil
}
+489
View File
@@ -0,0 +1,489 @@
package server
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"pageswiki/internal/calendar"
)
const davNS = "DAV:"
const calNS = "urn:ietf:params:xml:ns:caldav"
type davNode struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Text string `xml:",chardata"`
Children []davNode `xml:",any"`
}
func (n davNode) child(ns, name string) *davNode {
for i := range n.Children {
if n.Children[i].XMLName.Space == ns && n.Children[i].XMLName.Local == name {
return &n.Children[i]
}
}
return nil
}
func (n davNode) attr(name string) string {
for _, a := range n.Attrs {
if a.Name.Space == "" && a.Name.Local == name {
return a.Value
}
}
return ""
}
func readDAV(w http.ResponseWriter, r *http.Request) (davNode, error) {
b, e := io.ReadAll(http.MaxBytesReader(w, r.Body, 64<<10))
if e != nil {
return davNode{}, e
}
if len(bytes.TrimSpace(b)) == 0 {
return davNode{}, nil
}
// Reject DTDs and cap nesting before recursive decoding.
d := xml.NewDecoder(bytes.NewReader(b))
depth, count := 0, 0
for {
t, e := d.Token()
if e == io.EOF {
break
}
if e != nil {
return davNode{}, e
}
switch t.(type) {
case xml.Directive:
return davNode{}, fmt.Errorf("DTD not allowed")
case xml.StartElement:
depth++
count++
if depth > 12 || count > 1000 {
return davNode{}, fmt.Errorf("XML complexity limit")
}
case xml.EndElement:
depth--
}
}
var n davNode
e = xml.Unmarshal(b, &n)
return n, e
}
func xmlText(s string) string { var b bytes.Buffer; xml.EscapeText(&b, []byte(s)); return b.String() }
type davResponse struct {
href string
props map[xml.Name]string
status int
}
func davProperty(ns, key, value string) (xml.Name, string) {
return xml.Name{Space: ns, Local: key}, value
}
func writeDAV(w http.ResponseWriter, rows []davResponse, requested []davNode) {
var b strings.Builder
b.WriteString(`<?xml version="1.0" encoding="UTF-8"?><d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">`)
for _, row := range rows {
b.WriteString("<d:response><d:href>" + xmlText(row.href) + "</d:href>")
if row.status != 0 {
fmt.Fprintf(&b, "<d:status>HTTP/1.1 %d %s</d:status>", row.status, http.StatusText(row.status))
} else {
present, missing := strings.Builder{}, strings.Builder{}
props := requested
if len(props) == 0 {
for n := range row.props {
props = append(props, davNode{XMLName: n})
}
}
for _, prop := range props {
name := prop.XMLName
value, ok := row.props[name]
tag := "p:" + name.Local
piece := "<" + tag + " xmlns:p=\"" + xmlText(name.Space) + "\">" + value + "</" + tag + ">"
if ok {
present.WriteString(piece)
} else {
missing.WriteString(piece)
}
}
if present.Len() > 0 {
b.WriteString("<d:propstat><d:prop>" + present.String() + "</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat>")
}
if missing.Len() > 0 {
b.WriteString("<d:propstat><d:prop>" + missing.String() + "</d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat>")
}
}
b.WriteString("</d:response>")
}
b.WriteString("</d:multistatus>")
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.WriteHeader(207)
io.WriteString(w, b.String())
}
func eventDAVRow(c calendar.Calendar, e calendar.Event) davResponse {
return davResponse{href: calendarDAV + c.ID + "/" + calendar.DAVFilename(c, e.UID), props: map[xml.Name]string{{Space: davNS, Local: "getetag"}: xmlText(e.ETag), {Space: davNS, Local: "getcontenttype"}: "text/calendar; charset=utf-8", {Space: davNS, Local: "resourcetype"}: "", {Space: calNS, Local: "calendar-data"}: xmlText(e.Data)}}
}
func calendarDAVRow(c calendar.Calendar) davResponse {
return davResponse{href: calendarDAV + c.ID + "/", props: map[xml.Name]string{
{Space: davNS, Local: "displayname"}: xmlText(c.PrettyName), {Space: davNS, Local: "resourcetype"}: "<d:collection/><c:calendar/>",
{Space: calNS, Local: "supported-calendar-component-set"}: `<c:comp name="VEVENT"/>`,
{Space: calNS, Local: "supported-calendar-data"}: `<c:calendar-data content-type="text/calendar" version="2.0"/>`,
{Space: davNS, Local: "supported-report-set"}: `<d:supported-report><d:report><c:calendar-query/></d:report></d:supported-report><d:supported-report><d:report><c:calendar-multiget/></d:report></d:supported-report>`,
}}
}
func (s *Server) handleCalendarDAV(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second)
defer cancel()
r = r.WithContext(ctx)
w.Header().Set("Cache-Control", "private, no-store")
if strings.TrimSuffix(r.URL.Path, "/") == strings.TrimSuffix(calendarDAV, "/") {
s.handleDAVHome(w, r)
return
}
if r.Method == "MKCALENDAR" {
s.handleTaskMKCalendar(w, r)
return
}
segment := strings.Split(strings.TrimPrefix(r.URL.Path, calendarDAV), "/")[0]
if found, err := s.store.IsTaskDAVCollection(r.Context(), s.domainUUIDForRequest(r), segment); err != nil {
taskError(w, err)
return
} else if found {
s.handleTaskDAVAt(w, r, segment)
return
}
repo, ok := s.calendarReady(w, r)
if !ok {
return
}
domain := s.domainUUIDForRequest(r)
actor, active, _, credential, authenticated := s.davActor(w, r, "calendar", true)
if !authenticated {
return
}
parts := strings.Split(strings.Trim(strings.TrimPrefix(r.URL.Path, calendarDAV), "/"), "/")
root := len(parts) == 1 && parts[0] == ""
s.setDAVCapabilities(w, r, actor, credential)
w.Header().Set("Allow", "OPTIONS, PROPFIND, REPORT, GET, HEAD, PUT, DELETE")
if r.Method == "OPTIONS" {
w.WriteHeader(204)
return
}
var c calendar.Calendar
if !root {
var e error
c, e = repo.Get(domain, parts[0], actor)
if e != nil {
calendarError(w, e)
return
}
}
if len(parts) > 2 {
http.NotFound(w, r)
return
}
if r.Method == "PROPFIND" {
depth := r.Header.Get("Depth")
if depth != "0" && depth != "1" {
http.Error(w, "Depth 0 or 1 required", 403)
return
}
n, e := readDAV(w, r)
if e != nil {
http.Error(w, "invalid XML", 400)
return
}
if n.XMLName.Local != "" && (n.XMLName.Local != "propfind" || n.XMLName.Space != davNS) {
http.Error(w, "invalid PROPFIND", 400)
return
}
var req []davNode
if p := n.child(davNS, "prop"); p != nil {
req = p.Children
}
rows := []davResponse{}
if root {
rows = append(rows, davResponse{href: calendarDAV, props: map[xml.Name]string{{Space: davNS, Local: "resourcetype"}: "<d:collection/>", {Space: davNS, Local: "displayname"}: "PagesWiki calendars", {Space: calNS, Local: "calendar-home-set"}: "<d:href>" + calendarDAV + "</d:href>"}})
if depth == "1" {
cs, e := repo.List(domain, actor)
if e != nil {
calendarError(w, e)
return
}
for _, v := range cs {
rows = append(rows, calendarDAVRow(v))
}
}
} else if len(parts) == 1 {
row := calendarDAVRow(c)
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(active && c.CanWrite(actor))
rows = append(rows, row)
if depth == "1" {
var items []calendar.Event
withData := false
for _, p := range req {
if !taskDAVFullProperty(p) {
http.Error(w, "unsupported property projection", 403)
return
}
if p.XMLName == (xml.Name{Space: calNS, Local: "calendar-data"}) {
withData = true
}
}
c, items, e = repo.DAVSnapshot(r.Context(), domain, c.ID, actor, withData)
if e != nil {
calendarError(w, e)
return
}
for _, item := range items {
rows = append(rows, eventDAVRow(c, item))
}
}
} else {
uid, e := calendar.DAVUID(c, parts[1])
if e != nil {
calendarError(w, e)
return
}
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
if e != nil {
calendarError(w, e)
return
}
row := eventDAVRow(c, v)
row.href = r.URL.Path
rows = append(rows, row)
}
writeDAV(w, rows, req)
return
}
if root {
w.WriteHeader(405)
return
}
if r.Method == "REPORT" && len(parts) == 1 {
n, e := readDAV(w, r)
if e != nil {
http.Error(w, "invalid XML", 400)
return
}
if n.XMLName.Space != calNS {
http.Error(w, "unsupported report", 405)
return
}
seen := map[string]int{}
for _, child := range n.Children {
key := child.XMLName.Space + "|" + child.XMLName.Local
seen[key]++
allowed := child.XMLName.Space == davNS && child.XMLName.Local == "prop" || n.XMLName.Local == "calendar-query" && child.XMLName.Space == calNS && child.XMLName.Local == "filter" || n.XMLName.Local == "calendar-multiget" && child.XMLName.Space == davNS && child.XMLName.Local == "href"
if !allowed || child.XMLName.Local != "href" && seen[key] > 1 {
http.Error(w, "unsupported report element", 403)
return
}
}
p := n.child(davNS, "prop")
if p == nil || len(p.Children) == 0 {
http.Error(w, "explicit properties required", 400)
return
}
for _, prop := range p.Children {
if !taskDAVFullProperty(prop) {
http.Error(w, "calendar-data projection/expansion unsupported", 403)
return
}
}
rows := []davResponse{}
switch n.XMLName.Local {
case "calendar-query":
filter := n.child(calNS, "filter")
if filter == nil || len(filter.Children) != 1 {
http.Error(w, "time-range filter required", 403)
return
}
outer := filter.Children[0]
if outer.XMLName.Space != calNS || outer.XMLName.Local != "comp-filter" || outer.attr("name") != "VCALENDAR" || len(outer.Children) > 1 || taskDAVAttrCount(outer) != 1 || taskDAVAttrCount(*filter) != 0 {
http.Error(w, "unsupported filter", 403)
return
}
inner := davNode{XMLName: xml.Name{Space: calNS, Local: "comp-filter"}}
if len(outer.Children) == 1 {
inner = outer.Children[0]
}
if len(outer.Children) == 1 && (inner.XMLName.Space != calNS || inner.XMLName.Local != "comp-filter" || inner.attr("name") != "VEVENT" || len(inner.Children) > 1 || taskDAVAttrCount(inner) != 1) {
http.Error(w, "unsupported filter", 403)
return
}
if len(inner.Children) == 0 {
withData := false
for _, prop := range p.Children {
if prop.XMLName == (xml.Name{Space: calNS, Local: "calendar-data"}) {
withData = true
}
}
var items []calendar.Event
c, items, e = repo.DAVSnapshot(r.Context(), domain, c.ID, actor, withData)
if e != nil {
calendarError(w, e)
return
}
for _, item := range items {
rows = append(rows, eventDAVRow(c, item))
}
break
}
tr := inner.Children[0]
if tr.XMLName.Space != calNS || tr.XMLName.Local != "time-range" || len(tr.Children) != 0 || taskDAVAttrCount(tr) != 2 {
http.Error(w, "unsupported filter", 403)
return
}
start, e1 := time.Parse("20060102T150405Z", tr.attr("start"))
end, e2 := time.Parse("20060102T150405Z", tr.attr("end"))
if e1 != nil || e2 != nil {
http.Error(w, "UTC start and end required", 400)
return
}
query := repo.QueryETags
for _, prop := range p.Children {
if prop.XMLName.Space == calNS && prop.XMLName.Local == "calendar-data" {
query = repo.Query
}
}
events, e := query(r.Context(), domain, c.ID, actor, start, end)
if e != nil {
calendarError(w, e)
return
}
for _, event := range events {
rows = append(rows, eventDAVRow(c, event))
}
case "calendar-multiget":
count := 0
for _, node := range n.Children {
if node.XMLName.Space != davNS || node.XMLName.Local != "href" {
continue
}
count++
if count > 200 {
http.Error(w, "too many hrefs", 413)
return
}
u, e := url.Parse(strings.TrimSpace(node.Text))
prefix := calendarDAV + c.ID + "/"
if e != nil || u.IsAbs() || u.RawQuery != "" || u.Fragment != "" || !strings.HasPrefix(u.Path, prefix) {
http.Error(w, "foreign href", 400)
return
}
uid, e := calendar.DAVUID(c, strings.TrimPrefix(u.Path, prefix))
if e != nil {
http.Error(w, "invalid href", 400)
return
}
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
if e == calendar.ErrMissing {
rows = append(rows, davResponse{href: u.Path, status: 404})
continue
}
if e != nil {
calendarError(w, e)
return
}
row := eventDAVRow(c, v)
row.href = u.Path
rows = append(rows, row)
}
default:
http.Error(w, "unsupported report", 403)
return
}
writeDAV(w, rows, p.Children)
return
}
if len(parts) != 2 {
w.WriteHeader(405)
return
}
uid, e := calendar.DAVUID(c, parts[1])
if e != nil && r.Method != "PUT" {
calendarError(w, e)
return
}
switch r.Method {
case "GET", "HEAD":
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
if e != nil {
calendarError(w, e)
return
}
w.Header().Set("ETag", v.ETag)
w.Header().Set("Content-Type", "text/calendar; charset=utf-8")
if r.Header.Get("If-None-Match") == v.ETag {
w.WriteHeader(304)
return
}
if r.Method == "GET" {
io.WriteString(w, v.Data)
}
case "PUT", "DELETE":
if !active {
calendarError(w, calendar.ErrForbidden)
return
}
match := r.Header.Get("If-Match")
if match == "" && r.Header.Get("If-None-Match") == "*" && r.Method == "PUT" {
match = "*"
}
if match == "" && r.Method != "PUT" {
http.Error(w, "If-Match or If-None-Match: * required", 428)
return
}
var event *calendar.Event
unchangedRepresentation := false
if r.Method == "PUT" {
if !strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "text/calendar") {
http.Error(w, "text/calendar required", 415)
return
}
data, e := io.ReadAll(http.MaxBytesReader(w, r.Body, calendar.MaxResource))
if e != nil {
http.Error(w, "resource too large", 413)
return
}
v, e := calendar.Parse(string(data))
if e != nil {
calendarError(w, e)
return
}
event = &v
unchangedRepresentation = string(data) == v.Data
}
var authorize func() error
if credential != "" {
authorize = func() error {
return s.store.CheckDAVCredential(r.Context(), domain, actor, credential, true, "calendar")
}
}
v, created, e := repo.ChangeDAV(r.Context(), domain, c.ID, actor, parts[1], match, event, authorize)
if e != nil {
calendarError(w, e)
return
}
if event != nil && unchangedRepresentation {
w.Header().Set("ETag", v.ETag)
}
if created {
w.Header().Set("Location", r.URL.Path)
w.WriteHeader(201)
} else {
w.WriteHeader(204)
}
default:
w.WriteHeader(405)
}
}
+484
View File
@@ -0,0 +1,484 @@
package server
import (
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"pageswiki/internal/calendar"
)
const calendarAPI = "/_/api/v1/extension/organizer.calendar/"
const calendarDAV = "/_/api/v1/caldav/"
const davCredentialsAPI = "/_/api/v1/caldav-credentials"
func calendarRoute(path string) bool {
return path == "/v/plan" || strings.HasPrefix(path, "/v/plan/") || strings.HasPrefix(path, "/v/event/") || path == "/v/calendar" || strings.HasPrefix(path, calendarAPI) || strings.HasPrefix(path, calendarDAV) || path == strings.TrimSuffix(calendarDAV, "/") || path == davCredentialsAPI || strings.HasPrefix(path, davCredentialsAPI+"/")
}
func (s *Server) registerCalendarRoutes() {
for _, p := range []string{"/v/plan", "/v/plan/", "/v/event/", "/v/calendar"} {
s.mux.HandleFunc(p, s.handleCalendarPage)
}
s.mux.HandleFunc(calendarAPI, s.handleCalendarAPI)
s.mux.HandleFunc(calendarDAV, s.handleCalendarDAV)
s.mux.HandleFunc(strings.TrimSuffix(calendarDAV, "/"), s.handleCalendarDAV)
s.mux.HandleFunc(davCredentialsAPI, s.handleDAVCredentials)
s.mux.HandleFunc(davCredentialsAPI+"/", s.handleDAVCredentials)
}
func (s *Server) calendars() (*calendar.Repository, error) {
s.calendarOnce.Do(func() {
if s.store.DocumentRoot == "" {
s.calendarErr = errors.New("calendar storage unavailable")
return
}
s.calendarRepo, s.calendarErr = calendar.New(filepath.Join(s.store.DocumentRoot, "calendars"), s.store.DB)
if s.calendarErr == nil {
s.calendarRepo.PublicAllowed = func(domain string) bool {
var allowed bool
err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM locality_publication_policies p JOIN domains d ON d.uuid=p.domain_uuid AND d.enabled=1 JOIN config c ON c.domain_uuid=p.domain_uuid AND c.key='authorization_mode' AND c.value='enforcing' WHERE p.domain_uuid=? AND p.node_kind='domain' AND p.node_uuid=p.domain_uuid AND p.policy='public')`, domain).Scan(&allowed)
return err == nil && allowed
}
}
})
return s.calendarRepo, s.calendarErr
}
func (s *Server) calendarActor(r *http.Request) (string, bool) {
p, e := s.store.PrincipalForSession(s.domainUUIDForRequest(r), s.authTokenFromRequest(r))
if e != nil {
return "", false
}
return p.UUID, p.State == "active"
}
func (s *Server) calendarReady(w http.ResponseWriter, r *http.Request) (*calendar.Repository, bool) {
if !s.systemExtensionEnabled(s.domainUUIDForRequest(r), "organizer.calendar") {
http.NotFound(w, r)
return nil, false
}
repo, e := s.calendars()
if e != nil {
http.Error(w, "calendar storage unavailable", 503)
return nil, false
}
return repo, true
}
func calendarError(w http.ResponseWriter, e error) {
status := 500
message := "calendar operation failed"
switch {
case errors.Is(e, calendar.ErrMissing):
status = 404
message = "calendar or event not found"
case errors.Is(e, calendar.ErrForbidden):
status = 403
message = e.Error()
case errors.Is(e, calendar.ErrConflict):
status = 412
message = "The calendar changed. Reload before saving."
case errors.Is(e, calendar.ErrPreconditionRequired):
status = 428
message = "If-Match required"
case errors.Is(e, calendar.ErrNameTaken):
status = 409
message = e.Error()
case errors.Is(e, calendar.ErrInvalid):
status = 422
message = e.Error()
case errors.Is(e, calendar.ErrLimit):
status = 507
message = e.Error()
}
writeEventJSON(w, status, map[string]string{"error": message})
}
func calendarHref(id, uid string) string {
return calendarDAV + id + "/" + base64.RawURLEncoding.EncodeToString([]byte(uid)) + ".ics"
}
func calendarUID(filename string) (string, error) {
if !strings.HasSuffix(filename, ".ics") {
return "", calendar.ErrMissing
}
b, e := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(filename, ".ics"))
if e != nil || len(b) == 0 || len(b) > 512 {
return "", calendar.ErrMissing
}
return string(b), nil
}
func decodeCalendarJSON(w http.ResponseWriter, r *http.Request, dst any) bool {
d := json.NewDecoder(http.MaxBytesReader(w, r.Body, calendar.MaxResource))
d.DisallowUnknownFields()
if e := d.Decode(dst); e != nil {
http.Error(w, "invalid JSON", 400)
return false
}
return true
}
func (s *Server) handleCalendarAPI(w http.ResponseWriter, r *http.Request) {
repo, ok := s.calendarReady(w, r)
if !ok {
return
}
domain := s.domainUUIDForRequest(r)
actor, active := s.calendarActor(r)
path := strings.Trim(strings.TrimPrefix(r.URL.Path, calendarAPI), "/")
parts := strings.Split(path, "/")
if path == "upcoming" {
s.handleCalendarUpcoming(w, r, repo, domain, actor)
return
}
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "source" {
s.handleCalendarSource(w, r, repo, domain, parts[1], actor, active)
return
}
if path == "preferences" {
if actor == "" {
calendarError(w, calendar.ErrForbidden)
return
}
if r.Method != "PUT" {
w.WriteHeader(405)
return
}
var body struct {
Calendars []string `json:"calendars"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
if err := repo.SaveDefaults(r.Context(), domain, actor, body.Calendars); err != nil {
calendarError(w, err)
return
}
writeEventJSON(w, 200, map[string]any{"calendars": body.Calendars})
return
}
if path == "event" && r.Method == "GET" {
cs, e := repo.List(domain, actor)
if e != nil {
calendarError(w, e)
return
}
if len(cs) > 256 {
calendarError(w, calendar.ErrLimit)
return
}
var found *calendar.Event
id := ""
for _, c := range cs {
v, e := repo.Event(r.Context(), domain, c.ID, actor, r.URL.Query().Get("uid"))
if errors.Is(e, calendar.ErrMissing) {
continue
}
if e != nil {
calendarError(w, e)
return
}
if found != nil {
writeEventJSON(w, 409, map[string]string{"error": "UID exists in multiple calendars; select a calendar"})
return
}
found = &v
id = c.ID
}
if found == nil {
calendarError(w, calendar.ErrMissing)
return
}
writeEventJSON(w, 200, map[string]any{"calendar": id, "event": found})
return
}
if path == "calendars" {
switch r.Method {
case "GET":
cs, e := repo.List(domain, actor)
if e != nil {
calendarError(w, e)
return
}
for i := range cs {
cs[i].Months = nil
if cs[i].Source != nil {
source := *cs[i].Source
source.HasCredential = source.Credential != ""
source.Credential = ""
source.Resources = nil
cs[i].Source = &source
}
if cs[i].Owner != actor {
cs[i].Source = nil // source URLs may contain private subscription tokens
role := cs[i].Members[actor]
cs[i].Members = map[string]string{}
if actor != "" && role != "" {
cs[i].Members[actor] = role
}
}
}
defaults, e := repo.Defaults(domain, actor)
if e != nil {
calendarError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"calendars": cs, "actor": actor, "active": active, "public_allowed": repo.PublicAllowed(domain), "defaults": defaults})
case "POST":
if !active {
calendarError(w, calendar.ErrForbidden)
return
}
var body struct {
Name string `json:"name"`
PrettyName *string `json:"pretty_name"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
var pretty []string
if body.PrettyName != nil {
if !calendar.ValidPrettyName(*body.PrettyName) {
writeEventJSON(w, 422, map[string]string{"error": "pretty_name: use letters, numbers, - or _ (1–120 UTF-8 bytes; no commas, spaces or UUIDs)"})
return
}
pretty = []string{*body.PrettyName}
}
c, e := repo.Create(r.Context(), domain, actor, body.Name, pretty...)
if e != nil {
calendarError(w, e)
return
}
writeEventJSON(w, 201, c)
default:
w.Header().Set("Allow", "GET, POST")
w.WriteHeader(405)
}
return
}
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "settings" {
if r.Method != "PUT" && r.Method != "DELETE" {
w.Header().Set("Allow", "PUT, DELETE")
w.WriteHeader(405)
return
}
if !active {
calendarError(w, calendar.ErrForbidden)
return
}
c, err := repo.Get(domain, parts[1], actor)
if err != nil {
calendarError(w, err)
return
}
if c.Owner != actor {
calendarError(w, calendar.ErrForbidden)
return
}
if r.Method == "DELETE" {
var body struct {
Version string `json:"version"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
if body.Version == "" || body.Version != c.Version {
calendarError(w, calendar.ErrConflict)
return
}
if err := s.store.CancelEventJobsByTypeKey(calendarSyncType, domain+":"+c.ID); err != nil {
calendarError(w, err)
return
}
if err := repo.Delete(r.Context(), domain, c.ID, actor, body.Version); err != nil {
calendarError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
return
}
var body struct {
Name string `json:"name"`
Public bool `json:"public"`
Members map[string]string `json:"members"`
Version string `json:"version"`
PrettyName *string `json:"pretty_name"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
// Only existing local-domain users may be collaborators.
members := map[string]string{}
for id, role := range body.Members {
if _, e := uuid.Parse(id); e != nil {
p, e := s.store.PrincipalByUsername(domain, id)
if e != nil {
http.Error(w, "unknown collaborator", 400)
return
}
id = p.UUID
}
var count int
e := s.store.DB.QueryRowContext(r.Context(), `SELECT COUNT(*) FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=? AND state='active'`, domain, id).Scan(&count)
if e != nil || count != 1 {
http.Error(w, "unknown collaborator", 400)
return
}
members[id] = role
}
body.Members = members
var pretty []string
if body.PrettyName != nil {
if !calendar.ValidPrettyName(*body.PrettyName) {
writeEventJSON(w, 422, map[string]string{"error": "pretty_name: use letters, numbers, - or _ (1–120 UTF-8 bytes; no commas, spaces or UUIDs)"})
return
}
pretty = []string{*body.PrettyName}
}
c, e := repo.Settings(r.Context(), domain, parts[1], actor, body.Version, body.Name, body.Public, body.Members, pretty...)
if e != nil {
calendarError(w, e)
return
}
c.Months = nil
if c.Source != nil {
c.Source.HasCredential = c.Source.Credential != ""
c.Source.Credential = ""
c.Source.Resources = nil
}
writeEventJSON(w, 200, c)
return
}
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "events" {
if r.Method == "GET" {
uid := r.URL.Query().Get("uid")
if resource := r.URL.Query().Get("resource"); resource != "" {
c, err := repo.Get(domain, parts[1], actor)
if err != nil {
calendarError(w, err)
return
}
uid, err = calendar.DAVUID(c, resource)
if err != nil {
calendarError(w, err)
return
}
}
v, e := repo.Event(r.Context(), domain, parts[1], actor, uid)
if e != nil {
calendarError(w, e)
return
}
writeEventJSON(w, 200, v)
return
}
if r.Method != "POST" {
w.WriteHeader(405)
return
}
if !active {
calendarError(w, calendar.ErrForbidden)
return
}
var body struct {
UID string `json:"uid"`
Summary string `json:"summary"`
Description string `json:"description"`
Location string `json:"location"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
AllDay bool `json:"all_day"`
ETag string `json:"etag"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
old := ""
match := "*"
status := 201
if body.UID != "" {
v, e := repo.Event(r.Context(), domain, parts[1], actor, body.UID)
if e != nil {
calendarError(w, e)
return
}
old = v.Data
match = body.ETag
status = 200
} else {
body.UID = uuid.NewString()
}
v, e := calendar.Build(old, calendar.Event{UID: body.UID, Summary: body.Summary, Description: body.Description, Location: body.Location, Start: body.Start, End: body.End, AllDay: body.AllDay})
if e != nil {
calendarError(w, e)
return
}
v, e = repo.Change(r.Context(), domain, parts[1], actor, body.UID, match, &v)
if e != nil {
calendarError(w, e)
return
}
w.Header().Set("Location", calendarHref(parts[1], v.UID))
w.Header().Set("ETag", v.ETag)
writeEventJSON(w, status, v)
return
}
http.NotFound(w, r)
}
func (s *Server) handleCalendarPage(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
w.WriteHeader(405)
return
}
if _, ok := s.calendarReady(w, r); !ok {
return
}
if r.URL.Path == "/v/calendar" {
http.Redirect(w, r, "/v/plan", 302)
return
}
actor, _ := s.calendarActor(r)
edit := strings.HasPrefix(r.URL.Path, "/v/event/") || strings.HasSuffix(r.URL.Path, "/new")
if edit && actor == "" {
s.authRequired(w, r)
return
}
// The client parses only these URI shapes; malformed dates must be real 404s.
if !validCalendarViewPath(r.URL.Path) {
http.NotFound(w, r)
return
}
s.renderWithContext(w, r, "calendar.gohtml", map[string]any{"Title": "Calendar", "CalendarActor": actor, "CalendarTimezone": s.getTimezoneName(s.domainUUIDForRequest(r)), "Path": r.URL.Path})
}
func validCalendarViewPath(path string) bool {
if path == "/v/plan" || path == "/v/plan/" || path == "/v/event/new" {
return true
}
if strings.HasPrefix(path, "/v/plan/event/") {
return len(strings.TrimPrefix(path, "/v/plan/event/")) > 0
}
if strings.HasPrefix(path, "/v/event/") {
return strings.HasSuffix(path, "/edit") && len(strings.TrimSuffix(strings.TrimPrefix(path, "/v/event/"), "/edit")) > 0
}
if !strings.HasPrefix(path, "/v/plan/cal/") {
return false
}
p := strings.Split(strings.TrimPrefix(path, "/v/plan/cal/"), "/")
if len(p) == 5 && p[3] == "week" && p[4] == "list" {
p = p[:3]
}
if len(p) > 4 || len(p) < 1 {
return false
}
if len(p) == 4 {
switch p[3] {
case "week", "timeline", "events", "new":
default:
return false
}
p = p[:3]
}
layout := []string{"2006", "2006/01", "2006/01/02"}[len(p)-1]
v := strings.Join(p, "/")
t, e := time.Parse(layout, v)
return e == nil && t.Year() > 0 && t.Year() < 9999 && t.Format(layout) == v
}
+50
View File
@@ -0,0 +1,50 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"pageswiki/internal/calendar"
"testing"
"time"
)
func TestCalendarMobileChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium mobile fixture")
}
s, token, domain := configFixture(t)
s.registerCalendarRoutes()
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
t.Fatal(err)
}
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Mobile fixture calendar","pretty_name":"mobile"}`, "")
if w.Code != 201 {
t.Fatal(w.Body.String())
}
var c calendar.Calendar
json.Unmarshal(w.Body.Bytes(), &c)
for _, title := range []string{"Mobile meeting with a readable title", "Overlapping meeting"} {
raw, _ := json.Marshal(map[string]string{"summary": title, "start": "2026-09-22T09:00:00Z", "end": "2026-09-22T10:00:00Z"})
if w = calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", string(raw), ""); w.Code != 201 {
t.Fatal(w.Body.String())
}
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/calendar-mobile-smoke.mjs", srv.URL, t.TempDir())
if out, e := cmd.CombinedOutput(); e != nil {
t.Fatalf("mobile calendar: %v %s", e, out)
} else {
t.Log(string(out))
}
}
+348
View File
@@ -0,0 +1,348 @@
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/netip"
"net/url"
"strings"
"time"
"pageswiki/internal/calendar"
"pageswiki/internal/store"
)
const calendarSyncType = "calendar.source.sync"
const calendarSweepType = "calendar.source.sweep"
var errCalendarSourceBlocked = errors.New("calendar source address blocked: only public HTTPS endpoints are allowed")
var errCalendarSourceDNS = errors.New("calendar source DNS lookup failed")
// Return only a classified cause, never URL.Error (which can contain tokens).
func calendarSourceRequestError(err error) error {
if errors.Is(err, errCalendarSourceBlocked) {
return errCalendarSourceBlocked
}
if errors.Is(err, errCalendarSourceDNS) {
return errCalendarSourceDNS
}
var cert *tls.CertificateVerificationError
var unknown x509.UnknownAuthorityError
if errors.As(err, &cert) || errors.As(err, &unknown) {
return errors.New("external calendar TLS certificate verification failed")
}
var network net.Error
if errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &network) && network.Timeout()) {
return errors.New("external calendar request timed out")
}
return errors.New("external calendar connection failed (no reachable address, TLS handshake or redirect failure)")
}
type calendarSyncPayload struct {
Domain string `json:"domain"`
Calendar string `json:"calendar"`
Force bool `json:"force,omitempty"`
}
func validCalendarSourceURL(raw string) bool {
u, e := url.Parse(raw)
if e != nil || len(raw) > 4096 || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.Fragment != "" || (u.Port() != "" && u.Port() != "443") {
return false
}
host := strings.TrimSuffix(strings.ToLower(u.Hostname()), ".")
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
return false
}
if ip, err := netip.ParseAddr(host); err == nil && !publicCalendarIP(ip) {
return false
}
return true
}
func publicCalendarIP(ip netip.Addr) bool {
ip = ip.Unmap()
if !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
return false
}
for _, cidr := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "64:ff9b::/96", "64:ff9b:1::/48", "2002::/16"} {
if netip.MustParsePrefix(cidr).Contains(ip) {
return false
}
}
return true
}
// DNS is checked at dial time and the approved address is pinned for this
// connection. No proxy environment or alternate-port bypass is permitted.
func calendarSourceClient() *http.Client {
tr := &http.Transport{TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 25 * time.Second, DisableKeepAlives: true}
tr.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
host, port, e := net.SplitHostPort(address)
if e != nil || port != "443" {
return nil, errCalendarSourceBlocked
}
ips, e := net.DefaultResolver.LookupNetIP(ctx, "ip", host)
if e != nil || len(ips) == 0 {
return nil, errCalendarSourceDNS
}
for _, ip := range ips {
if !publicCalendarIP(ip) {
return nil, errCalendarSourceBlocked
}
}
// Try all validated addresses rather than failing on an unreachable
// first IPv6/IPv4 address. Never re-resolve after the safety check.
for _, ip := range ips {
var conn net.Conn
conn, e = (&net.Dialer{Timeout: 2 * time.Second}).DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
if e == nil {
return conn, nil
}
if ctx.Err() != nil {
return nil, ctx.Err()
}
}
return nil, e
}
return &http.Client{Transport: tr, Timeout: 45 * time.Second, CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 3 || !validCalendarSourceURL(req.URL.String()) {
return errors.New("calendar source redirect blocked")
}
req.Header.Del("Referer")
return nil
}}
}
func fetchCalendarSource(ctx context.Context, client *http.Client, c calendar.Calendar) ([]calendar.Event, string, string, bool, error) {
if c.Source == nil || !validCalendarSourceURL(c.Source.URL) {
return nil, "", "", false, errors.New("invalid calendar source URL")
}
req, e := http.NewRequestWithContext(ctx, "GET", c.Source.URL, nil)
if e != nil {
return nil, "", "", false, errors.New("invalid calendar source URL")
}
req.Header.Set("Accept", "text/calendar")
req.Header.Set("User-Agent", "PagesWiki-Calendar/1")
if c.Source.ETag != "" {
req.Header.Set("If-None-Match", c.Source.ETag)
}
if c.Source.Modified != "" {
req.Header.Set("If-Modified-Since", c.Source.Modified)
}
response, e := client.Do(req)
if e != nil {
return nil, "", "", false, calendarSourceRequestError(e)
}
defer response.Body.Close()
if response.StatusCode == 304 {
if c.Source.Success == 0 {
return nil, "", "", false, errors.New("unexpected 304 before initial synchronization")
}
return nil, "", "", true, nil
}
if response.StatusCode != 200 {
return nil, "", "", false, errors.New("external calendar returned HTTP " + http.StatusText(response.StatusCode))
}
body, e := io.ReadAll(io.LimitReader(response.Body, calendar.MaxFeed+1))
if e != nil {
return nil, "", "", false, errors.New("external calendar body could not be read")
}
now := time.Now().UTC().Truncate(24 * time.Hour)
events, e := calendar.ParseExternalFeed(body, now.AddDate(0, 0, -90), now.AddDate(0, 0, 275))
if e != nil {
e = fmt.Errorf("ICS validation stage failed: %w", e)
}
return events, response.Header.Get("ETag"), response.Header.Get("Last-Modified"), false, e
}
func (s *Server) ensureCalendarSweep() error {
var exists bool
if e := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM event_definitions WHERE uuid=?)`, calendarSweepType).Scan(&exists); e != nil {
return e
}
if exists {
return nil
}
return s.store.SaveEventDefinition(&store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
}
func (s *Server) enqueueCalendarSource(domain, id string, force bool) (bool, error) {
b, _ := json.Marshal(calendarSyncPayload{Domain: domain, Calendar: id, Force: force})
return s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+id, string(b), time.Now(), -10, 1)
}
func (s *Server) sweepCalendarSources() error {
repo, e := s.calendars()
if e != nil {
return e
}
domains, e := s.store.ListDomains()
if e != nil {
return e
}
for _, d := range domains {
if !d.Enabled || !s.systemExtensionEnabled(d.UUID, "organizer.calendar") {
continue
}
cs, e := repo.Sources(d.UUID)
if e != nil {
return e
}
for _, c := range cs {
if c.Source.Enabled && c.Source.Next <= time.Now().UnixMilli() {
if _, e = s.enqueueCalendarSource(d.UUID, c.ID, false); e != nil {
return e
}
}
}
}
return nil
}
func (s *Server) runCalendarSource(job *store.EventJob) error {
var p calendarSyncPayload
if e := json.Unmarshal([]byte(job.Payload), &p); e != nil {
return e
}
if !s.systemExtensionEnabled(p.Domain, "organizer.calendar") {
return nil
}
repo, e := s.calendars()
if e != nil {
return e
}
c, e := repo.SourceCalendar(p.Domain, p.Calendar)
if e != nil {
return e
}
if !c.Source.Enabled || (!p.Force && c.Source.Next > time.Now().UnixMilli()) {
return nil
}
var active bool
e = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? AND m.state='active' AND p.state='active' AND d.enabled=1)`, p.Domain, c.Owner).Scan(&active)
if e != nil {
return e
}
if !active {
return errors.New("calendar source owner is inactive")
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
defer cancel()
client := calendarSourceClient()
if c.Source.Credential != "" {
user, password, err := s.store.OpenCalendarCredential(p.Domain, c.ID, c.Source.Credential)
if err != nil {
return errors.New("calendar credential unavailable")
}
client.Transport = calendarAuthTransport{base: client.Transport, origin: c.Source.URL, user: user, password: password}
}
var events []calendar.Event
var etag, modified string
var unchanged bool
var syncErr error
var resources map[string]calendar.RemoteResource
if c.Source.Kind == "caldav" {
events, resources, syncErr = fetchCalDAVSource(ctx, client, repo, p.Domain, c)
} else {
events, etag, modified, unchanged, syncErr = fetchCalendarSource(ctx, client, c)
}
if syncErr != nil {
// Deliberately omit source URL, credentials, UID, summary and body.
log.Printf("calendar source synchronization failed domain=%s calendar=%s protocol=%s stage=fetch-validate: %v", p.Domain, p.Calendar, c.Source.Kind, syncErr)
}
if s.store.EventCancellationRequested(job.UUID) {
return errors.New("calendar synchronization cancelled")
}
if e = repo.ApplySource(ctx, p.Domain, c, events, etag, modified, unchanged, syncErr, resources); e != nil {
return e
}
return syncErr
}
func (s *Server) handleCalendarSource(w http.ResponseWriter, r *http.Request, repo *calendar.Repository, domain, id, actor string, active bool) {
if !active {
calendarError(w, calendar.ErrForbidden)
return
}
c, e := repo.Get(domain, id, actor)
if e != nil {
calendarError(w, e)
return
}
if c.Owner != actor {
calendarError(w, calendar.ErrForbidden)
return
}
switch r.Method {
case "PUT":
var body struct {
Kind string `json:"kind"`
Username string `json:"username"`
Password string `json:"password"`
ClearCredential bool `json:"clear_credential"`
URL string `json:"url"`
IntervalSeconds int64 `json:"interval_seconds"`
Enabled bool `json:"enabled"`
Version string `json:"version"`
}
if !decodeCalendarJSON(w, r, &body) {
return
}
body.URL = strings.TrimSpace(body.URL)
if !validCalendarSourceURL(body.URL) {
writeEventJSON(w, 422, map[string]string{"error": "Use a public HTTPS ICS URL (port 443, no URL username/password)."})
return
}
if body.Kind == "" {
body.Kind = "ics"
}
credential := ""
if c.Source != nil && c.Source.URL == body.URL && !body.ClearCredential {
credential = c.Source.Credential
}
if body.Username != "" || body.Password != "" {
if body.Username == "" || body.Password == "" || strings.Contains(body.Username, ":") || len(body.Username) > 256 || len(body.Password) > 4096 {
writeEventJSON(w, 422, map[string]string{"error": "Supply both username and password (username must not contain a colon)."})
return
}
credential, e = s.store.SealCalendarCredential(domain, id, body.Username, body.Password)
if e != nil {
calendarError(w, e)
return
}
}
c, e = repo.SetSource(r.Context(), domain, id, actor, body.Version, calendar.Source{Kind: body.Kind, Credential: credential, URL: body.URL, IntervalSeconds: body.IntervalSeconds, Enabled: body.Enabled})
if e != nil {
calendarError(w, e)
return
}
if body.Enabled {
if _, e = s.enqueueCalendarSource(domain, id, true); e != nil {
calendarError(w, e)
return
}
s.wakeEventScheduler()
}
c.Months = nil
c.Source.HasCredential = c.Source.Credential != ""
c.Source.Credential = ""
c.Source.Resources = nil
writeEventJSON(w, 200, c)
case "POST":
if c.Source == nil || !c.Source.Enabled {
writeEventJSON(w, 409, map[string]string{"error": "Enable a source first"})
return
}
queued, e := s.enqueueCalendarSource(domain, id, true)
if e != nil {
calendarError(w, e)
return
}
s.wakeEventScheduler()
writeEventJSON(w, 202, map[string]bool{"queued": queued})
default:
w.Header().Set("Allow", "PUT, POST")
w.WriteHeader(405)
}
}
+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)
}
}
+235
View File
@@ -0,0 +1,235 @@
package server
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/calendar"
)
func TestCalendarChromiumSmoke(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
t.Fatal(err)
}
s.registerCalendarRoutes()
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar Smoke"}`, "")
if w.Code != 201 {
t.Fatal(w.Body.String())
}
var c calendar.Calendar
json.Unmarshal(w.Body.Bytes(), &c)
w = calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", `{"summary":"Browser meeting","start":"2026-09-22T09:00:00Z","end":"2026-09-22T10:00:00Z"}`, "")
if w.Code != 201 {
t.Fatal(w.Body.String())
}
var event calendar.Event
json.Unmarshal(w.Body.Bytes(), &event)
// Exercise the Web viewer with a DAV-client filename, not base64url(UID).
native, err := s.calendarRepo.Event(context.Background(), domain, c.ID, c.Owner, event.UID)
if err != nil {
t.Fatal(err)
}
if _, err = s.calendarRepo.Change(context.Background(), domain, c.ID, c.Owner, event.UID, native.ETag, nil); err != nil {
t.Fatal(err)
}
if _, _, err = s.calendarRepo.ChangeDAV(context.Background(), domain, c.ID, c.Owner, "browser-client-event.ics", "", &native, nil); 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)
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, route := range []string{"/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline", "/v/plan/event/" + event.UID} {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=4000", server.URL+route+"?calendar="+c.ID)
out, e := cmd.CombinedOutput()
cancel()
if e != nil || strings.Contains(string(out), "Uncaught ") || !strings.Contains(string(out), "Browser meeting") {
t.Fatalf("%s: %v %s", route, e, out)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/calendar-browser-smoke.mjs", server.URL+"/v/plan/cal/2026/09?calendar="+c.ID, t.TempDir())
if out, e := cmd.CombinedOutput(); e != nil {
t.Fatalf("browser interactions: %v %s", e, out)
} else {
t.Log(string(out))
}
}
func calendarRequest(s *Server, token, method, path, body, etag string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
if token != "" {
r.Header.Set("Authorization", "Bearer "+token)
}
r.Header.Set("Content-Type", "application/json")
if method == "REPORT" || method == "PROPFIND" {
r.Header.Set("Content-Type", "application/xml")
r.Header.Set("Depth", "1")
}
if method == "PUT" && !strings.Contains(path, "settings") {
r.Header.Set("Content-Type", "text/calendar")
}
if etag == "*" {
r.Header.Set("If-None-Match", "*")
} else if etag != "" {
r.Header.Set("If-Match", etag)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
func TestCalendarEndToEndDAVAndPublicRevocation(t *testing.T) {
s, token, domain := configFixture(t)
s.registerCalendarRoutes()
if _, err := s.store.DB.Exec(`INSERT INTO locality_publication_policies(domain_uuid,node_kind,node_uuid,policy,actor_uuid,updated_at_ms) VALUES(?,'domain',?,'public','test',0)`, domain, domain); err != nil {
t.Fatal(err)
}
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar"}`, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body.String())
}
var c calendar.Calendar
if e := json.Unmarshal(w.Body.Bytes(), &c); e != nil {
t.Fatal(e)
}
path := calendarAPI + "calendars/" + c.ID + "/events"
w = calendarRequest(s, token, "POST", path, `{"summary":"Meeting","start":"2026-09-30T23:00:00Z","end":"2026-10-01T02:00:00Z"}`, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body.String())
}
var event calendar.Event
json.Unmarshal(w.Body.Bytes(), &event)
href := w.Header().Get("Location")
report := `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT"><c:time-range start="20261001T000000Z" end="20261002T000000Z"/></c:comp-filter></c:comp-filter></c:filter></c:calendar-query>`
w = calendarRequest(s, token, "REPORT", calendarDAV+c.ID+"/", report, "")
if w.Code != 207 || !strings.Contains(w.Body.String(), "Meeting") {
t.Fatal(w.Code, w.Body.String())
}
w = calendarRequest(s, "", "GET", href, "", "")
if w.Code != 404 {
t.Fatal("private leaked", w.Code)
}
w = calendarRequest(s, token, "GET", href, "", "")
ics := w.Body.String()
if w.Code != 200 {
t.Fatal(w.Code, ics)
}
w = calendarRequest(s, token, "PUT", href, ics, "")
if w.Code != 428 {
t.Fatal(w.Code, w.Body.String())
}
w = calendarRequest(s, token, "PUT", href, ics, "*")
if w.Code != 412 {
t.Fatal(w.Code, w.Body.String())
}
c, _ = s.calendarRepo.Get(s.domainUUIDForRequest(httptest.NewRequest("GET", "http://localhost/", nil)), c.ID, c.Owner)
settings := calendarAPI + "calendars/" + c.ID + "/settings"
body, _ := json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": c.Version})
w = calendarRequest(s, token, "PUT", settings, string(body), "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
json.Unmarshal(w.Body.Bytes(), &c)
w = calendarRequest(s, "", "REPORT", calendarDAV+c.ID+"/", report, "")
if w.Code != 207 {
t.Fatal(w.Code, w.Body.String())
}
w = calendarRequest(s, "", "DELETE", href, "", event.ETag)
if w.Code != 403 {
t.Fatal(w.Code, w.Body.String())
}
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": false, "members": map[string]string{}, "version": c.Version})
w = calendarRequest(s, token, "PUT", settings, string(body), "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
w = calendarRequest(s, "", "GET", href, "", "")
if w.Code != 404 {
t.Fatal("revocation failed", w.Code)
}
// Calendar-level publication cannot override a newly restricted Domain.
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": func() string { fresh, _ := s.calendarRepo.Get(domain, c.ID, c.Owner); return fresh.Version }()})
w = calendarRequest(s, token, "PUT", settings, string(body), "")
if w.Code != 200 {
t.Fatal(w.Body.String())
}
if _, e := s.store.DB.Exec(`UPDATE locality_publication_policies SET policy='authenticated_acl' WHERE domain_uuid=? AND node_kind='domain'`, domain); e != nil {
t.Fatal(e)
}
w = calendarRequest(s, "", "GET", href, "", "")
if w.Code != 404 {
t.Fatal("Domain ceiling bypass", w.Code)
}
// A disabled extension closes both protocols, even for its owner.
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", false); e != nil {
t.Fatal(e)
}
w = calendarRequest(s, token, "GET", href, "", "")
if w.Code != 404 {
t.Fatal(w.Code)
}
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", true); e != nil {
t.Fatal(e)
}
for _, p := range []string{"/v/plan", "/v/plan/cal/2026", "/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline"} {
w = calendarRequest(s, token, "GET", p, "", "")
if w.Code != 200 || !strings.Contains(w.Body.String(), "calendar-app") {
t.Fatal(p, w.Code, w.Body.String())
}
}
for _, p := range []string{"/v/plan/cal/2026/02/30", "/v/plan/cal/2026/13", "/v/plan/bogus"} {
w = calendarRequest(s, token, "GET", p, "", "")
if w.Code != 404 {
t.Fatal(p, w.Code)
}
}
}
func TestCalendarDeleteAPIUsesVersionAndCancelsQueuedSync(t *testing.T) {
s, token, domain := configFixture(t)
s.registerCalendarRoutes()
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Disposable","pretty_name":"disposable"}`, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body.String())
}
var c calendar.Calendar
if err := json.Unmarshal(w.Body.Bytes(), &c); err != nil {
t.Fatal(err)
}
if _, err := s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+c.ID, `{}`, time.Now().Add(time.Hour), -10, 1); err != nil {
t.Fatal(err)
}
settings := calendarAPI + "calendars/" + c.ID + "/settings"
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+uuid.NewString()+`"}`, "")
if w.Code != 412 {
t.Fatal("stale delete", w.Code, w.Body.String())
}
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+c.Version+`"}`, "")
if w.Code != 204 || w.Body.Len() != 0 {
t.Fatal(w.Code, w.Body.String())
}
if _, err := s.calendarRepo.Get(domain, c.ID, c.Owner); !errors.Is(err, calendar.ErrMissing) {
t.Fatal(err)
}
var status string
if err := s.store.DB.QueryRow(`SELECT status FROM event_jobs WHERE event_type=? AND event_key=? ORDER BY created_at_ms DESC LIMIT 1`, calendarSyncType, domain+":"+c.ID).Scan(&status); err != nil || status != "cancelled" {
t.Fatal(status, err)
}
}
+81
View File
@@ -0,0 +1,81 @@
package server
import (
"context"
"net/http"
"net/url"
"pageswiki/internal/calendar"
"sort"
"time"
)
func (s *Server) handleCalendarUpcoming(w http.ResponseWriter, r *http.Request, repo *calendar.Repository, domain, actor string) {
w.Header().Set("Cache-Control", "private, no-store")
if r.Method != http.MethodGet {
w.Header().Set("Allow", "GET")
w.WriteHeader(405)
return
}
if actor == "" {
calendarError(w, calendar.ErrForbidden)
return
}
cs, err := repo.List(domain, actor)
if err != nil {
calendarError(w, err)
return
}
defaults, err := repo.Defaults(domain, actor)
if err != nil {
calendarError(w, err)
return
}
if len(defaults) == 0 && len(cs) > 0 {
defaults = []string{cs[0].ID}
}
selected := map[string]bool{}
for _, id := range defaults {
selected[id] = true
}
type entry struct {
calendar.Event
Calendar string `json:"calendar"`
Href string `json:"href"`
}
events := []entry{}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
// Calendar day boundaries use the configured Domain timezone, including
// 23/25-hour DST days. The browser sends a civil date only as a hint.
location := s.getLocation(domain)
now := time.Now().In(location)
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
start := today
if raw := r.URL.Query().Get("date"); raw != "" {
day, e := time.ParseInLocation("2006-01-02", raw, location)
if e != nil || day.Before(today.AddDate(0, 0, -2)) || day.After(today.AddDate(0, 0, 1)) {
calendarError(w, calendar.ErrInvalid)
return
}
start = day
}
end := start.AddDate(0, 0, 31)
for _, c := range cs {
if !selected[c.ID] {
continue
}
items, e := repo.Upcoming(ctx, domain, c.ID, actor, start, end)
if e != nil {
calendarError(w, e)
return
}
for _, event := range items {
events = append(events, entry{Event: event, Calendar: c.Name, Href: "/v/plan/event/" + url.PathEscape(event.UID) + "?calendar=" + url.QueryEscape(c.PrettyName)})
}
}
sort.SliceStable(events, func(i, j int) bool { return events[i].Start.Before(events[j].Start) })
if len(events) > 20 {
events = events[:20]
}
writeEventJSON(w, 200, map[string]any{"events": events})
}
+70
View File
@@ -0,0 +1,70 @@
package server
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"pageswiki/internal/calendar"
)
func TestCalendarUpcomingDefaultsAndBounds(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, "Default", "default")
if e != nil {
t.Fatal(e)
}
other, e := repo.Create(context.Background(), domain, owner.UUID, "Other", "other")
if e != nil {
t.Fatal(e)
}
if e = repo.SaveDefaults(context.Background(), domain, owner.UUID, []string{c.ID}); e != nil {
t.Fatal(e)
}
for i := 0; i < 25; i++ {
start := time.Now().UTC().Add(time.Duration(i+1) * time.Hour).Truncate(time.Second)
b, _ := json.Marshal(map[string]any{"summary": fmt.Sprintf("Event %02d", i), "start": start, "end": start.Add(time.Hour)})
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", string(b), ""); w.Code != 201 {
t.Fatal(w.Code, w.Body.String())
}
}
start := time.Now().UTC().Add(time.Hour).Truncate(time.Second)
b, _ := json.Marshal(map[string]any{"summary": "Not selected", "start": start, "end": start.Add(time.Hour)})
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+other.ID+"/events", string(b), ""); w.Code != 201 {
t.Fatal(w.Code, w.Body.String())
}
w := calendarRequest(s, token, "GET", calendarAPI+"upcoming", "", "")
var result struct {
Events []struct {
calendar.Event
Href string
}
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Events) != 20 {
t.Fatal(w.Code, w.Body.String())
}
if !strings.Contains(result.Events[0].Href, "calendar=default") || result.Events[0].Summary != "Event 00" || result.Events[19].Summary != "Event 19" {
t.Fatal(result.Events)
}
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
t.Fatal("private response cached")
}
if w = calendarRequest(s, "", "GET", calendarAPI+"upcoming", "", ""); w.Code != 403 {
t.Fatal("anonymous access", w.Code)
}
if w = calendarRequest(s, token, "GET", calendarAPI+"upcoming?date=1900-01-01", "", ""); w.Code != 422 {
t.Fatal("unbounded dates", w.Code)
}
}
@@ -0,0 +1,48 @@
package server
import (
"net/url"
"strings"
"testing"
"golang.org/x/net/html"
)
func TestChildrenPathsKeepLiteralURLCharacters(t *testing.T) {
s, _, domain := configFixture(t)
path := "/encoding/file ?#%_日本語.txt"
object, err := s.store.CreatePageAtPathInDomain(domain, path)
if err != nil {
t.Fatal(err)
}
if _, err := s.store.SaveRevisionContent(object, []byte("example"), "", "und", "text/plain", "file.txt"); err != nil {
t.Fatal(err)
}
for _, view := range []string{"list", "detail", "icon"} {
rendered, err := s.renderChildrenProjectionFiltered(domain, childrenMacroArgs{TargetPath: "/encoding", View: view, Limit: 100}, func(string) bool { return true })
if err != nil {
t.Fatal(err)
}
z := html.NewTokenizer(strings.NewReader(rendered))
found := false
for z.Next() != html.ErrorToken {
token := z.Token()
if token.Data != "a" {
continue
}
for _, attr := range token.Attr {
if attr.Key != "href" {
continue
}
parsed, err := url.Parse(attr.Val)
if err != nil || parsed.Path != path || parsed.RawQuery != "" || parsed.Fragment != "" {
t.Fatalf("%s link=%q (%v)", view, attr.Val, err)
}
found = true
}
}
if !found {
t.Fatalf("%s link missing: %s", view, rendered)
}
}
}
+335
View File
@@ -0,0 +1,335 @@
package server
import (
"fmt"
"html"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"pageswiki/internal/store"
)
func (s *Server) handleChildrenProjection(w http.ResponseWriter, r *http.Request) {
args := parseChildrenMacroArgs("", r.URL.Query().Get("path"))
args.View = r.URL.Query().Get("view")
if args.View == "compact" {
args.View = "icon"
}
if args.View != "icon" && args.View != "detail" {
args.View = "list"
}
args.Excludes = splitExcludePatterns(r.URL.Query().Get("exclude"))
args.Offset, _ = strconv.Atoi(r.URL.Query().Get("offset"))
args.Limit, _ = strconv.Atoi(r.URL.Query().Get("limit"))
if args.Limit <= 0 || args.Limit > 400 {
args.Limit = 120
}
rendered, err := s.renderReadableChildren(r, args)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
_, _ = w.Write([]byte(rendered))
}
func (s *Server) renderChildrenProjection(
domainUUID string,
args childrenMacroArgs,
) (string, error) {
return s.renderChildrenProjectionFiltered(domainUUID, args, nil)
}
func (s *Server) renderChildrenProjectionFiltered(
domainUUID string,
args childrenMacroArgs,
allowed func(string) bool,
) (string, error) {
// /u is a profile/user-Wiki namespace, not a root directory entry. Keep it
// reachable by URL while hiding it from every root children projection,
// including asynchronous view switches and cached continuation requests.
if args.TargetPath == "/" && !isExcludedPath("/u", args.Excludes) {
args.Excludes = append(args.Excludes, "/u")
}
if args.View == "list" {
entries, total, err := s.store.ListChildEntriesPageInDomain(domainUUID, args.TargetPath, args.Offset, args.Limit)
if err != nil {
return "", err
}
var body strings.Builder
body.WriteString(`<ul class="children-view-list">`)
for _, entry := range entries {
child := entry.Path
if isExcludedPath(child, args.Excludes) || (allowed != nil && !allowed(child)) {
continue
}
displayName := entry.Title
if displayName == "" {
displayName = childName(args.TargetPath, child)
}
body.WriteString(fmt.Sprintf(
`<li class="children-entry" data-child-path="%s"><a href="%s">%s</a></li>`,
html.EscapeString(child),
html.EscapeString((&url.URL{Path: child}).EscapedPath()),
html.EscapeString(displayName),
))
}
body.WriteString("</ul>")
writeChildrenMore(
&body,
args,
args.Offset+len(entries),
total,
)
return body.String(), nil
}
entries, total, err := s.store.ListChildEntriesPageInDomain(
domainUUID,
args.TargetPath,
args.Offset,
args.Limit,
)
if err != nil {
return "", err
}
var body strings.Builder
if args.View == "icon" {
body.WriteString(`<div class="children-icon-grid">`)
} else {
body.WriteString(`<table class="children-detail"><thead><tr>` +
`<th>Name</th><th>Type</th><th>Size</th><th>Updated</th>` +
`</tr></thead><tbody>`)
}
for _, entry := range entries {
if isExcludedPath(entry.Path, args.Excludes) || (allowed != nil && !allowed(entry.Path)) {
continue
}
if args.View == "icon" {
body.WriteString(s.renderChildIcon(args.TargetPath, entry))
continue
}
contentType := entry.ContentType
if entry.IsDir {
contentType = "folder"
}
updated := "—"
if entry.UpdatedAt > 0 {
updated = time.Unix(entry.UpdatedAt, 0).Format("2006-01-02 15:04")
}
displayName := entry.Title
if displayName == "" {
displayName = childName(args.TargetPath, entry.Path)
}
body.WriteString(fmt.Sprintf(
`<tr class="children-entry" data-child-path="%s"><td><a href="%s">%s</a></td><td>%s</td><td>%d</td><td>%s</td></tr>`,
html.EscapeString(entry.Path),
html.EscapeString((&url.URL{Path: entry.Path}).EscapedPath()),
html.EscapeString(displayName),
html.EscapeString(contentType),
entry.Size,
updated,
))
}
if args.View == "icon" {
body.WriteString("</div>")
} else {
body.WriteString("</tbody></table>")
}
writeChildrenMore(&body, args, args.Offset+len(entries), total)
return body.String(), nil
}
func writeChildrenMore(
body *strings.Builder,
args childrenMacroArgs,
nextOffset, total int,
) {
if nextOffset >= total {
return
}
query := url.Values{
"path": {args.TargetPath},
"view": {args.View},
"offset": {strconv.Itoa(nextOffset)},
"limit": {strconv.Itoa(args.Limit)},
}
if len(args.Excludes) > 0 {
query.Set("exclude", strings.Join(args.Excludes, ","))
}
body.WriteString(fmt.Sprintf(
`<div class="children-next" data-children-next="%s" aria-label="Load more items"></div>`,
html.EscapeString("/v/children?"+query.Encode()),
))
}
func childName(parent, child string) string {
return strings.TrimPrefix(strings.TrimPrefix(child, parent), "/")
}
func (s *Server) renderChildIcon(parent string, entry store.BrowserEntry) string {
path := html.EscapeString(entry.Path)
name := entry.Title
if name == "" {
name = childName(parent, entry.Path)
}
name = html.EscapeString(name)
preview := s.entryPreviewHTML(entry, true)
return fmt.Sprintf(
`<a class="children-icon-item children-entry" data-child-path="%s" href="%s">%s<span class="children-icon-name">%s</span></a>`,
path,
html.EscapeString((&url.URL{Path: entry.Path}).EscapedPath()),
preview,
name,
)
}
// entryPreviewHTML is shared by directory listings and search results. Callers
// that render broad result sets can disable enqueueing and rely on the periodic
// preview scheduler, avoiding a burst of database writes during navigation.
func (s *Server) entryPreviewHTML(
entry store.BrowserEntry,
enqueueMissing bool,
) string {
if entry.IsDir {
return childTypeIcon("folder")
}
filename := entry.Filename
if filename == "" {
filename = filepath.Base(entry.Path)
}
adapter := selectProjectionAdapter(contentDescriptor{
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
Filename: filename,
SizeBytes: int64(entry.Size),
})
previewURL, cached := s.entryPreviewState(entry, adapter.ID, enqueueMissing)
if cached {
badge := ""
switch adapter.ID {
case "pdf":
badge = "PDF"
case "video":
badge = "▶"
case "excalidraw":
badge = "EX"
case "archive":
badge = "ZIP"
}
badgeHTML := ""
if badge != "" {
badgeHTML = `<span class="children-preview-badge">` + badge + `</span>`
}
return fmt.Sprintf(
`<span class="children-preview children-preview-pending"><img data-preview-kind="image" data-preview-src="%s" alt="">%s</span>`,
html.EscapeString(previewURL),
badgeHTML,
)
}
switch adapter.ID {
case "image":
return pendingPreviewIcon("image", previewURL, "")
case "video":
return pendingPreviewIcon("video", previewURL, "▶")
case "pdf":
return pendingPreviewIcon("pdf", previewURL, "PDF")
case "excalidraw":
return pendingPreviewIcon("drawing", previewURL, "EX")
case "archive":
return childTypeIcon("archive")
case "debian-package":
return childTypeIcon("package")
case "audio":
return childTypeIcon("audio")
case "text", "csv":
if isProgramFilename(filename) {
return childTypeIcon("code")
}
return childTypeIcon("text")
default:
if isProgramFilename(filename) {
return childTypeIcon("code")
}
switch strings.ToLower(filepath.Ext(filename)) {
case ".txt", ".md", ".log", ".rst":
return childTypeIcon("text")
}
return childTypeIcon("file")
}
}
func (s *Server) entryPreviewState(
entry store.BrowserEntry,
kind string,
enqueueMissing bool,
) (string, bool) {
if enqueueMissing {
return s.enqueuePreview(entry)
}
switch kind {
case "image", "pdf", "video", "excalidraw", "archive":
default:
return "", false
}
if s.previewRoot == "" || entry.DocumentHash == "" {
return "", false
}
cacheURL := s.previewCacheURL(entry.DocumentHash, kind)
info, err := os.Stat(s.previewCachePath(entry.DocumentHash, kind))
return cacheURL, err == nil && info.Size() > 0
}
func pendingPreviewIcon(kind, cacheURL, badge string) string {
if cacheURL == "" {
return childTypeIcon(kind)
}
badgeHTML := ""
if badge != "" {
badgeHTML = `<span class="children-preview-badge">` + badge + `</span>`
}
return `<span class="children-preview children-preview-placeholder" ` +
`data-preview-cache="` + html.EscapeString(cacheURL) + `" ` +
`data-preview-badge="` + html.EscapeString(badge) + `">` +
childTypeIcon(kind) + badgeHTML + `</span>`
}
func isProgramFilename(filename string) bool {
switch strings.ToLower(filepath.Ext(filename)) {
case ".py", ".sh", ".bash", ".zsh", ".js", ".jsx", ".ts", ".tsx",
".go", ".rs", ".c", ".h", ".cc", ".cpp", ".java", ".rb", ".php",
".sql", ".css", ".html", ".yaml", ".yml", ".toml":
return true
default:
return false
}
}
func childTypeIcon(kind string) string {
paths := map[string]string{
"folder": `<path d="M3 6h7l2 2h9v11H3z"/>`,
"archive": `<path d="M5 3h14v18H5zM9 3v4h3v4H9v4h3v4H9"/>`,
"package": `<path d="m4 8 8-4 8 4v9l-8 4-8-4zM4 8l8 4 8-4M12 12v9"/>`,
"audio": `<path d="M9 18V6l10-2v12M9 10l10-2M6 21a3 3 0 1 0 0-6 3 3 0 0 0 0 6M16 19a3 3 0 1 0 0-6 3 3 0 0 0 0 6"/>`,
"image": `<path d="M4 5h16v14H4zM7 15l4-4 3 3 2-2 4 4M8 9h.01"/>`,
"video": `<path d="M4 6h12v12H4zM16 10l4-2v8l-4-2z"/>`,
"pdf": `<path d="M6 3h9l4 4v14H6zM15 3v5h4M8 16h2a2 2 0 0 0 0-4H8v6M13 18v-6h3M13 15h3"/>`,
"drawing": `<path d="M4 19l4-1 10-10-3-3L5 15zM13 7l3 3M4 21h16"/>`,
"code": `<path d="m9 7-5 5 5 5M15 7l5 5-5 5M14 4l-4 16"/>`,
"text": `<path d="M6 3h9l4 4v14H6zM15 3v5h4M9 12h7M9 16h7"/>`,
"file": `<path d="M6 3h9l4 4v14H6zM15 3v5h4"/>`,
}
path := paths[kind]
if path == "" {
path = paths["file"]
}
return `<span class="children-type-icon children-type-` + kind +
`" aria-hidden="true"><svg viewBox="0 0 24 24">` + path +
`</svg></span>`
}
+184
View File
@@ -0,0 +1,184 @@
package server
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"net/url"
"path"
"strings"
)
type cityAutoPlanItem struct {
Path string `json:"path"`
PageUUID string `json:"page_uuid"`
Quartier, Street, Spot string
QuartierPath, StreetPath, SpotPath string `json:",omitempty"`
Undefined bool `json:"undefined_spot"`
Action string `json:"action"`
}
func cityAutoName(raw, prefix string) string {
decoded, err := url.PathUnescape(raw)
if err == nil {
raw = decoded
}
name := citySuggestionName(raw)
if len(name) >= 2 {
return name
}
sum := sha256.Sum256([]byte(raw))
return prefix + "-" + hex.EncodeToString(sum[:4])
}
// Names are identifiers, not ownership. In particular Spot names are unique
// across the entire Domain, even when their Streets differ.
func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary string) (string, error) {
for attempt := 0; attempt < 32; attempt++ {
name := base
if attempt > 0 {
sum := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%d", kind, boundary, attempt)))
stem := base
if len(stem) > 47 {
stem = stem[:47]
}
name = stem + "-" + hex.EncodeToString(sum[:8])
}
var exists bool
var err error
if kind == "spot" {
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM spots WHERE domain_uuid=? AND name=? AND state!='archived')`, domain, name).Scan(&exists)
} else if kind == "quartier" {
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND kind='quartier' AND name=? AND state!='archived')`, domain, name).Scan(&exists)
} else {
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND parent_uuid=? AND name=? AND state!='archived')`, domain, parent, name).Scan(&exists)
}
if err != nil {
return "", err
}
if !exists {
return name, nil
}
}
return "", errors.New("unable to allocate unique generated City name")
}
func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error) {
rows, err := s.store.DB.Query(`SELECT p.path,p.object_uuid FROM paths p WHERE p.domain_uuid=? AND p.path LIKE '/%' AND p.path!='/u' AND p.path NOT LIKE '/u/%'
AND NOT EXISTS(SELECT 1 FROM city_page_overrides ov JOIN spots dest ON dest.uuid=ov.spot_uuid AND dest.domain_uuid=ov.domain_uuid AND dest.state!='archived' WHERE ov.domain_uuid=p.domain_uuid AND ov.page_uuid=p.object_uuid)
ORDER BY p.path`, domainUUID)
if err != nil {
return nil, err
}
defer rows.Close()
var out []cityAutoPlanItem
for rows.Next() {
var semanticPath, pageUUID string
if err := rows.Scan(&semanticPath, &pageUUID); err != nil {
return nil, err
}
parts := strings.Split(strings.Trim(semanticPath, "/"), "/")
if len(parts) < 3 || parts[0] == "system" {
continue
}
item := cityAutoPlanItem{Path: semanticPath, PageUUID: pageUUID, Quartier: cityAutoName(parts[0], "quartier"), Street: cityAutoName(parts[1], "street"), QuartierPath: "/" + parts[0], StreetPath: "/" + parts[0] + "/" + parts[1]}
if len(parts) >= 4 {
item.Spot = cityAutoName(parts[2], "spot")
item.SpotPath = item.StreetPath + "/" + parts[2]
} else {
item.Undefined = true
item.Spot = item.Street + "-undefined"
item.SpotPath = item.StreetPath + "/_undefined"
}
item.Action = "create"
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
item.Quartier = q.Name
item.Action = "merge"
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", q.UUID, item.StreetPath); e == nil {
item.Street = st.Name
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", st.UUID, item.SpotPath); e == nil {
item.Spot = spot.Name
}
}
}
out = append(out, item)
}
return out, rows.Err()
}
func (s *Server) applyCityAutoPlan(domainUUID, actor string) (int, error) {
plan, err := s.buildCityAutoPlan(domainUUID)
if err != nil {
return 0, err
}
quartiers, streets, spots := map[string]string{}, map[string]string{}, map[string]string{}
applied := 0
for _, item := range plan {
qID := quartiers[item.QuartierPath]
if qID == "" {
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
qID = q.UUID
} else {
if !errors.Is(e, sql.ErrNoRows) {
return applied, e
}
name, err := s.availableCityAutoName(domainUUID, "quartier", "", item.Quartier, item.QuartierPath)
if err != nil {
return applied, err
}
qID, e = s.store.CreateZoneAtPath(domainUUID, actor, "quartier", "", name, item.Quartier, "Generated from URL Paths", item.QuartierPath)
if e != nil {
return applied, e
}
}
quartiers[item.QuartierPath] = qID
}
streetKey := qID + item.StreetPath
streetID := streets[streetKey]
if streetID == "" {
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", qID, item.StreetPath); e == nil {
streetID = st.UUID
} else {
if !errors.Is(e, sql.ErrNoRows) {
return applied, e
}
name, err := s.availableCityAutoName(domainUUID, "street", qID, item.Street, item.StreetPath)
if err != nil {
return applied, err
}
streetID, e = s.store.CreateZoneAtPath(domainUUID, actor, "street", qID, name, item.Street, "", item.StreetPath)
if e != nil {
return applied, e
}
}
streets[streetKey] = streetID
}
spotKey := streetID + item.SpotPath
spotID := spots[spotKey]
if spotID == "" {
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", streetID, item.SpotPath); e == nil {
spotID = spot.UUID
} else {
if !errors.Is(e, sql.ErrNoRows) {
return applied, e
}
name, err := s.availableCityAutoName(domainUUID, "spot", streetID, item.Spot, item.SpotPath)
if err != nil {
return applied, err
}
spotID, e = s.store.CreateSpotAtPath(domainUUID, actor, streetID, name, item.Spot, "Generated from URL Paths", item.SpotPath)
if e != nil {
return applied, e
}
}
spots[spotKey] = spotID
}
if err := s.store.AddSpotPage(domainUUID, actor, spotID, item.PageUUID, item.Path, "primary", path.Base(item.Path)); err != nil && !errors.Is(err, sql.ErrNoRows) {
return applied, err
}
applied++
}
return applied, nil
}
+77
View File
@@ -0,0 +1,77 @@
package server
import (
"pageswiki/internal/db"
"pageswiki/internal/store"
"path/filepath"
"testing"
)
func TestCityAutoPlanUsesFixedURLDepthAndMergesDescendants(t *testing.T) {
database, err := db.Open(filepath.Join(t.TempDir(), "pages.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { database.Close() })
if err = db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.New(database)
domain, err := st.GetDefaultDomain()
if err != nil {
t.Fatal(err)
}
for _, p := range []string{"/tech/dev/readme", "/tech/dev/lang/python/page", "/tech/dev/lang/py3/page2"} {
if _, err = st.CreatePageAtPathInDomain(domain.UUID, p); err != nil {
t.Fatal(err)
}
}
srv := &Server{store: st}
plan, err := srv.buildCityAutoPlan(domain.UUID)
if err != nil || len(plan) != 3 {
t.Fatalf("plan=%#v, %v", plan, err)
}
if count, err := srv.applyCityAutoPlan(domain.UUID, "actor"); err != nil || count != 3 {
t.Fatalf("apply count=%d err=%v", count, err)
}
city, err := st.GetCityMap(domain.UUID)
if err != nil {
t.Fatal(err)
}
if len(city.Quartiers) != 1 || len(city.Quartiers[0].Streets) != 1 || len(city.Quartiers[0].Streets[0].Spots) != 2 {
t.Fatalf("city=%#v", city)
}
for _, spot := range city.Quartiers[0].Streets[0].Spots {
if spot.Name == "lang" && len(spot.Pages) != 2 {
t.Fatalf("lang pages=%#v", spot.Pages)
}
}
if err := srv.reconcileCityActivity(domain.UUID); err != nil {
t.Fatal("event-driven reconcile:", err)
}
checkpoint, err := st.ActivityOutboxCheckpoint("city", domain.UUID)
if err != nil || checkpoint.LastSequence == 0 {
t.Fatalf("city checkpoint=%+v err=%v", checkpoint, err)
}
}
func TestCityAutoPlanSameSpotNameInDifferentStreets(t *testing.T) {
s, _, domain := configFixture(t)
for _, p := range []string{"/alpha/dev/lang/one", "/beta/dev/lang/two", "/alpha/dev/readme", "/beta/dev/readme"} {
if _, e := s.store.CreatePageAtPathInDomain(domain, p); e != nil {
t.Fatal(e)
}
}
for i := 0; i < 2; i++ {
if _, e := s.applyCityAutoPlan(domain, "system:city"); e != nil {
t.Fatal("reconcile", i, e)
}
var spots, names int
if e := s.store.DB.QueryRow(`SELECT count(*),count(DISTINCT name) FROM spots WHERE domain_uuid=? AND state!='archived'`, domain).Scan(&spots, &names); e != nil {
t.Fatal(e)
}
if spots != 4 || names != 4 {
t.Fatal("colliding names merged or duplicated", spots, names)
}
}
}
+129
View File
@@ -0,0 +1,129 @@
package server
import (
"database/sql"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"pageswiki/internal/store"
)
func cityGovernanceRoute(path string) bool {
if !strings.HasPrefix(path, "/_/api/v1/city/") {
return false
}
p := strings.Split(strings.TrimPrefix(path, "/_/api/v1/city/"), "/")
return (len(p) == 3 || (len(p) == 4 && (p[3] == "preview" || p[3] == "members" || p[3] == "access" || p[3] == "publication" || p[3] == "auto-publication"))) && (p[0] == "zones" || p[0] == "spots" || p[0] == "domains") && p[1] != "" && p[2] == "governance"
}
func (s *Server) handleCityGovernance(w http.ResponseWriter, r *http.Request) {
privateCityResponse(w)
p := strings.Split(strings.TrimPrefix(r.URL.Path, "/_/api/v1/city/"), "/")
preview := len(p) == 4 && p[3] == "preview"
members := len(p) == 4 && p[3] == "members"
access := len(p) == 4 && p[3] == "access"
publication := len(p) == 4 && p[3] == "publication"
automatic := len(p) == 4 && p[3] == "auto-publication"
if !((len(p) == 3 || access || publication || automatic) && (r.Method == "GET" || r.Method == "PATCH")) && !(preview && r.Method == "POST") && !(members && r.Method == "GET") {
writeEventJSON(w, 405, map[string]string{"error": "Method not allowed"})
return
}
principal := s.requestPrincipal(r)
if principal == nil {
writeEventJSON(w, 403, map[string]string{"error": translate(s.getLocale(s.domainUUIDForRequest(r)), "city.governance.denied")})
return
}
domain := s.domainUUIDForRequest(r)
kind := map[string]string{"spots": "spot", "domains": "domain"}[p[0]]
var err error
if p[0] == "zones" {
err = s.store.DB.QueryRowContext(r.Context(), `SELECT kind FROM zones WHERE uuid=? AND domain_uuid=?`, p[1], domain).Scan(&kind)
}
var result any
if err == nil && automatic {
var change *store.AutoPublicationChange
if r.Method == "PATCH" {
change = &store.AutoPublicationChange{}
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
decoder.DisallowUnknownFields()
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
err = store.ErrGovernanceInvalid
}
}
if err == nil {
result, err = s.store.AutoPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
}
} else if err == nil && publication {
var change *store.PublicationChange
if r.Method == "PATCH" {
change = &store.PublicationChange{}
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
decoder.DisallowUnknownFields()
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
err = store.ErrGovernanceInvalid
}
}
if err == nil {
result, err = s.store.LocalityPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
}
} else if err == nil && access {
var change *store.LocalityAccess
if r.Method == "PATCH" {
change = &store.LocalityAccess{}
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 32<<10))
decoder.DisallowUnknownFields()
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
err = store.ErrGovernanceInvalid
}
}
if err == nil {
result, err = s.store.LocalityAccess(r.Context(), domain, principal.UUID, kind, p[1], change)
}
} else if err == nil && members {
result, err = s.store.CityGovernanceMembers(r.Context(), domain, principal.UUID, kind, p[1], r.URL.Query().Get("q"))
} else if err == nil && r.Method == "GET" {
result, err = s.store.CityGovernance(r.Context(), domain, principal.UUID, kind, p[1])
} else if err == nil {
var change store.CityGovernanceChange
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10))
decoder.DisallowUnknownFields()
if decoder.Decode(&change) != nil || decoder.Decode(new(any)) != io.EOF {
err = store.ErrGovernanceInvalid
} else {
result, err = s.store.ChangeCityGovernance(r.Context(), domain, principal.UUID, kind, p[1], change, preview)
}
}
if err != nil {
status, key := 503, "city.governance.unavailable"
switch {
case errors.Is(err, sql.ErrNoRows), errors.Is(err, store.ErrAccessDenied):
status, key = 403, "city.governance.denied"
case errors.Is(err, store.ErrGovernanceConflict):
status, key = 409, err.Error()
case errors.Is(err, store.ErrLocalityAccessEnforcing), errors.Is(err, store.ErrLocalityAccessAlias), errors.Is(err, store.ErrPublicationImpact), errors.Is(err, store.ErrGovernanceCeiling), errors.Is(err, store.ErrGovernanceConfirm), errors.Is(err, store.ErrGovernanceInvalid):
status, key = 400, err.Error()
}
writeEventJSON(w, status, map[string]string{"error": translate(s.getLocale(domain), key)})
return
}
writeEventJSON(w, 200, result)
}
func governanceEndpoint(kind, id string) string {
collection := map[string]string{"quartier": "zones", "street": "zones", "spot": "spots", "domain": "domains"}[kind]
return "/_/api/v1/city/" + collection + "/" + id + "/governance"
}
func (s *Server) governanceForUI(r *http.Request, kind, id string) string {
p := s.requestPrincipal(r)
if p == nil {
return ""
}
if _, err := s.store.CityGovernance(r.Context(), s.domainUUIDForRequest(r), p.UUID, kind, id); err != nil {
return ""
}
return governanceEndpoint(kind, id)
}
@@ -0,0 +1,70 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestCityGovernanceChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
s.mux.HandleFunc("/v/city", s.handleCityMap)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
q, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "life", "Life", "", "/life")
if err != nil {
t.Fatal(err)
}
p, err := s.store.CreatePasswordAccount(domain, "delegate", "a long test password", "Delegate", "")
if err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/city-governance.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.Header.Get("Origin") != "" {
r.Header.Set("Origin", "http://localhost")
}
if r.Method == "GET" && (r.URL.Path == "/v/city/q/life" || r.URL.Path == "/v/city") {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, path := range []string{"/v/city/q/life", "/v/city"} {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+path, t.TempDir())
out, e := cmd.CombinedOutput()
cancel()
if e != nil {
t.Fatalf("%s: %v %s", path, e, out)
}
}
g, err := s.store.CityGovernance(context.Background(), domain, p.UUID, "quartier", q)
if err != nil || len(g.Owners) != 1 || g.Policy != "authenticated_acl" || g.PublicationEnabled {
t.Fatalf("UI persisted %+v %v", g, err)
}
a, err := s.store.LocalityAccess(context.Background(), domain, p.UUID, "quartier", q, nil)
if err != nil || len(a.Entries) != 1 || a.Entries[0].Permission != "read_write" || a.Mode != "restricted" {
t.Fatalf("ACL UI persisted %+v %v", a, err)
}
}
+238
View File
@@ -0,0 +1,238 @@
package server
import (
"context"
"encoding/json"
"net/http/httptest"
"strings"
"sync"
"testing"
"pageswiki/internal/store"
)
func TestCityGovernanceDelegationAndCaps(t *testing.T) {
s, admin, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
q, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "life", "Life", "", "/life")
if err != nil {
t.Fatal(err)
}
street, err := s.store.CreateZoneAtPath(domain, "test", "street", q, "home", "Home", "", "/life/home")
if err != nil {
t.Fatal(err)
}
spot, err := s.store.CreateSpotAtPath(domain, "test", street, "desk", "Desk", "", "/life/home/desk")
if err != nil {
t.Fatal(err)
}
q2, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "work", "Work", "", "/work")
if err != nil {
t.Fatal(err)
}
tokens, members, principals := map[string]string{}, map[string]string{}, map[string]string{}
for _, name := range []string{"quartierowner", "spotowner", "coowner", "outsider"} {
p, e := s.store.CreatePasswordAccount(domain, name, "a long test password", name, "")
if e != nil {
t.Fatal(e)
}
principals[name] = p.UUID
tokens[name], e = s.store.CreateAuthSession(domain, p.UUID)
if e != nil {
t.Fatal(e)
}
var membership string
if e = s.store.DB.QueryRow(`SELECT uuid FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=?`, domain, p.UUID).Scan(&membership); e != nil {
t.Fatal(e)
}
members[name] = membership
// Do not let default author grants accidentally admit the owner API.
if _, e = s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid IN (SELECT role_uuid FROM auth_role_bindings WHERE membership_uuid=?)`, members[name]); e != nil {
t.Fatal(e)
}
}
call := func(token, method, url string, body any) *httptest.ResponseRecorder {
b, _ := json.Marshal(body)
r := httptest.NewRequest(method, "http://localhost"+url, strings.NewReader(string(b)))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
dURL, qURL, sURL := governanceEndpoint("domain", domain), governanceEndpoint("quartier", q), governanceEndpoint("spot", spot)
read := func(token, url string) store.CityGovernance {
t.Helper()
w := call(token, "GET", url, nil)
if w.Code != 200 {
t.Fatalf("GET %s=%d %s", url, w.Code, w.Body.String())
}
var g store.CityGovernance
if e := json.Unmarshal(w.Body.Bytes(), &g); e != nil {
t.Fatal(e)
}
return g
}
change := func(token, url, policy string, owners *[]string, confirm bool, code int) store.CityGovernance {
t.Helper()
g := read(token, url)
c := store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Owners: owners, ConfirmRelaxation: confirm}
if policy != "" {
c.Policy = &policy
}
w := call(token, "PATCH", url, c)
if w.Code != code {
t.Fatalf("PATCH %s=%d want %d: %s", url, w.Code, code, w.Body.String())
}
if code == 200 {
_ = json.Unmarshal(w.Body.Bytes(), &g)
}
return g
}
if g := read(admin, dURL); g.Policy != "authenticated_acl" || g.DraftEffectivePublic || g.PublicationEnabled {
t.Fatalf("unsafe default: %+v", g)
}
change(admin, qURL, "public", nil, true, 400)
change(admin, dURL, "public", nil, false, 400)
if g := change(admin, dURL, "public", nil, true, 200); g.DraftEffectivePublic {
t.Fatal("Domain ceiling published root")
}
qs := []string{members["quartierowner"]}
change(admin, qURL, "", &qs, false, 200)
qg := read(tokens["quartierowner"], qURL)
if qg.CanAppoint {
t.Fatal("Quartier owner can appoint peers")
}
w := call(tokens["quartierowner"], "PATCH", qURL, store.CityGovernanceChange{Epoch: qg.Epoch, TopologyVersion: qg.TopologyVersion, Owners: &qs})
if w.Code != 403 {
t.Fatalf("peer appointment=%d", w.Code)
}
if w = call(tokens["quartierowner"], "GET", governanceEndpoint("quartier", q2), nil); w.Code != 403 {
t.Fatal("foreign Quartier accessible")
}
ss := []string{members["spotowner"], members["coowner"]}
change(tokens["quartierowner"], sURL, "", &ss, false, 200)
if g := read(tokens["spotowner"], sURL); len(g.Owners) != 2 || g.CanAppoint {
t.Fatalf("co-owners: %+v", g)
}
if w = call(tokens["spotowner"], "GET", sURL+"/members", nil); w.Code != 403 {
t.Fatal("Spot owner enumerated appointment candidates")
}
if w = call(tokens["outsider"], "GET", sURL, nil); w.Code != 403 {
t.Fatal("outsider governance")
}
if w = call(tokens["spotowner"], "POST", "/_/api/v1/city/spots/"+spot+"/title", map[string]any{"title": "changed"}); w.Code != 403 {
t.Fatal("ownership granted topology write")
}
change(tokens["spotowner"], sURL, "public", nil, true, 200)
change(tokens["quartierowner"], qURL, "authenticated_acl", nil, false, 200)
if g := read(tokens["spotowner"], sURL); g.DraftEffectivePublic || g.SuperiorAllowsPublic || g.Policy != "public" {
t.Fatal("ancestor narrowing did not clamp preserved intent")
}
change(tokens["spotowner"], sURL, "public", nil, true, 400)
change(tokens["quartierowner"], qURL, "inherit", nil, false, 400)
change(tokens["quartierowner"], qURL, "inherit", nil, true, 200)
if !read(tokens["spotowner"], sURL).DraftEffectivePublic {
t.Fatal("intent lost after relaxation")
}
change(tokens["spotowner"], sURL, "authenticated_acl", nil, false, 200)
change(tokens["quartierowner"], qURL, "public", nil, true, 200)
if read(tokens["spotowner"], sURL).DraftEffectivePublic {
t.Fatal("parent bypassed child restriction")
}
// Policy preview changes neither epoch nor persistence, even on relaxation.
g := read(tokens["spotowner"], sURL)
policy := "inherit"
patch := store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy}
if w = call(tokens["spotowner"], "POST", sURL+"/preview", patch); w.Code != 200 || !strings.Contains(w.Body.String(), `"draft_effective_public":true`) {
t.Fatalf("preview=%d %s", w.Code, w.Body.String())
}
if after := read(tokens["spotowner"], sURL); after.Epoch != g.Epoch || after.Policy != g.Policy {
t.Fatal("preview persisted")
}
change(tokens["spotowner"], sURL, "inherit", nil, true, 200)
if w = call(tokens["spotowner"], "PATCH", sURL, patch); w.Code != 409 {
t.Fatal("stale epoch accepted")
}
// Same-version writers cannot both commit, even when both are legitimate co-owners.
g = read(tokens["spotowner"], sURL)
policy = "authenticated_acl"
patch = store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy}
var wg sync.WaitGroup
codes := make(chan int, 2)
for _, name := range []string{"spotowner", "coowner"} {
wg.Add(1)
go func(token string) { defer wg.Done(); codes <- call(token, "PATCH", sURL, patch).Code }(tokens[name])
}
wg.Wait()
close(codes)
success, conflict := 0, 0
for c := range codes {
if c == 200 {
success++
}
if c == 409 {
conflict++
}
}
if success != 1 || conflict != 1 {
t.Fatalf("concurrent saves: %d success %d conflicts", success, conflict)
}
g = read(tokens["spotowner"], sURL)
patch.Epoch = g.Epoch
patch.TopologyVersion = g.TopologyVersion
if _, err = s.store.DB.Exec(`UPDATE domain_topology_versions SET version=version+1 WHERE domain_uuid=?`, domain); err != nil {
t.Fatal(err)
}
if w = call(tokens["spotowner"], "PATCH", sURL, patch); w.Code != 409 {
t.Fatal("stale topology accepted")
}
// Browser-origin fencing remains ahead of the handler-owned permission path.
r := httptest.NewRequest("PATCH", "http://localhost"+sURL, strings.NewReader(`{}`))
r.Header.Set("Authorization", "Bearer "+admin)
r.Header.Set("Origin", "https://untrusted.invalid")
r.Header.Set("Sec-Fetch-Site", "cross-site")
w = httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 403 {
t.Fatal("governance bypassed browser-origin fencing")
}
// Revocation is live and does not depend on an in-memory principal cache.
if _, err = s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, principals["spotowner"]); err != nil {
t.Fatal(err)
}
if _, err = s.store.CityGovernance(context.Background(), domain, principals["spotowner"], "spot", spot); err != store.ErrAccessDenied {
t.Fatalf("suspended owner=%v", err)
}
if _, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE uuid=?`, members["coowner"]); err != nil {
t.Fatal(err)
}
if _, err = s.store.CityGovernance(context.Background(), domain, principals["coowner"], "spot", spot); err != store.ErrAccessDenied {
t.Fatalf("revoked member=%v", err)
}
bad := []string{members["coowner"]}
change(admin, sURL, "", &bad, false, 400)
bad = []string{"foreign-membership"}
change(admin, sURL, "", &bad, false, 400)
if w = call(admin, "DELETE", sURL, nil); w.Code != 405 {
t.Fatal("unsupported method")
}
if w = call(admin, "GET", governanceEndpoint("domain", "foreign"), nil); w.Code != 403 {
t.Fatal("foreign Domain")
}
var events int
if err = s.store.DB.QueryRow(`SELECT COUNT(*) FROM city_governance_events WHERE domain_uuid=?`, domain).Scan(&events); err != nil || events < 8 {
t.Fatalf("audit events=%d %v", events, err)
}
none := []string{}
change(admin, qURL, "", &none, false, 200)
if w = call(tokens["quartierowner"], "GET", qURL, nil); w.Code != 403 {
t.Fatal("removed ownership remained effective")
}
if _, err = s.store.DB.Exec(`UPDATE groups SET state='suspended' WHERE domain_uuid=? AND builtin_key='domain_admin'`, domain); err != nil {
t.Fatal(err)
}
if w = call(admin, "GET", dURL, nil); w.Code != 403 {
t.Fatal("revoked admin remained authoritative")
}
}
+409
View File
@@ -0,0 +1,409 @@
package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"pageswiki/internal/store"
)
func cityReadErrorStatus(err error) int {
switch {
case errors.Is(err, store.ErrAccessDenied), errors.Is(err, sql.ErrNoRows), errors.Is(err, store.ErrCityNameAmbiguous):
return http.StatusNotFound
case errors.Is(err, store.ErrCityMapBudget), errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled):
return http.StatusServiceUnavailable
default:
return http.StatusInternalServerError
}
}
func (s *Server) handleCityMap(w http.ResponseWriter, r *http.Request) {
privateCityResponse(w)
domainUUID := s.domainUUIDForRequest(r)
principal := s.requestPrincipal(r)
canPlan := principal != nil && (s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") || s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) == nil)
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/v/city"), "/")
if path != "" {
parts := strings.Split(path, "/")
kinds := map[string]string{"q": "quartier", "s": "street", "p": "spot"}
kind := ""
if len(parts) == 2 || (len(parts) == 3 && parts[0] == "s") {
kind = kinds[parts[0]]
}
if kind == "" {
http.NotFound(w, r)
return
}
detail, err := s.store.CityNodeContext(r.Context(), domainUUID, kind, strings.Join(parts[1:], "/"))
if err != nil {
status := cityReadErrorStatus(err)
if status == http.StatusNotFound {
http.NotFound(w, r)
return
}
if status == http.StatusServiceUnavailable {
w.Header().Set("Retry-After", "5")
}
http.Error(w, http.StatusText(status), status)
return
}
if !canReadCityNode(detail, s.pathReadFilter(r)) {
http.NotFound(w, r)
return
}
pages := []store.CityPagePlacement{}
if kind == "spot" {
pages, err = s.spotPagesForRequest(r, detail.UUID)
if err != nil {
status := cityReadErrorStatus(err)
if status == http.StatusServiceUnavailable {
w.Header().Set("Retry-After", "5")
}
http.Error(w, http.StatusText(status), status)
return
}
}
pageViews := make([]cityPageView, 0, len(pages))
for _, page := range pages {
pageViews = append(pageViews, cityPageView{page, cityPageHref(page.Path)})
}
quartierHref, streetHref := "", ""
if detail.Quartier != nil {
quartierHref = cityDetailHref("quartier", detail.Quartier.Name, "", detail.Quartier.UUID)
}
if detail.Street != nil {
streetHref = cityDetailHref("street", detail.Street.Name, detail.Quartier.Name, detail.Street.UUID)
}
s.renderWithContext(w, r, "city_node.gohtml", map[string]any{"Title": detail.Title, "Node": detail, "Pages": pageViews, "QuartierHref": quartierHref, "StreetHref": streetHref, "MapHref": "/v/city?" + kind + "=" + detail.UUID, "CanPlan": canPlan, "Locale": s.getLocale(domainUUID), "GovernanceEndpoint": s.governanceForUI(r, kind, detail.UUID)})
return
}
s.renderWithContext(w, r, "city_map.gohtml", map[string]any{"Title": translate(s.getLocale(domainUUID), "city.map"), "CanPlan": canPlan, "Locale": s.getLocale(domainUUID), "GovernanceEndpoint": s.governanceForUI(r, "domain", domainUUID)})
}
func (s *Server) handleCityAPI(w http.ResponseWriter, r *http.Request) {
if cityGovernanceRoute(r.URL.Path) {
s.handleCityGovernance(w, r)
return
}
domainUUID := s.domainUUIDForRequest(r)
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/city/"), "/")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if action == "map" && r.Method == http.MethodGet {
privateCityResponse(w)
city, err := s.cityProjectionForRequest(r)
if err != nil {
status := cityReadErrorStatus(err)
if status == http.StatusServiceUnavailable {
w.Header().Set("Retry-After", "5")
}
writeEventJSON(w, status, map[string]string{"error": err.Error()})
return
}
_ = json.NewEncoder(w).Encode(city)
return
}
parts := strings.Split(action, "/")
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "pages" && r.Method == http.MethodGet {
privateCityResponse(w)
pages, err := s.spotPagesForRequest(r, parts[1])
if err != nil {
status := cityReadErrorStatus(err)
if status == http.StatusServiceUnavailable {
w.Header().Set("Retry-After", "5")
}
message := http.StatusText(status)
if status == http.StatusNotFound {
message = "Spot not found"
}
writeEventJSON(w, status, map[string]string{"error": message})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"pages": pages})
return
}
principal := s.requestPrincipal(r)
if principal == nil || (!s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") && s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) != nil) {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "domain admin required"})
return
}
if action == "organize" && r.Method == http.MethodPost {
var request store.CityOrganizeRequest
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 128<<10)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_selection"})
return
}
result, err := s.store.OrganizeCity(domainUUID, principal.UUID, request)
if err != nil {
writeCityOrganizeError(w, err)
return
}
writeEventJSON(w, http.StatusOK, result)
return
}
if len(parts) == 3 && (parts[0] == "spots" || parts[0] == "zones") && parts[2] == "title" && r.Method == http.MethodPost {
var request struct {
Kind string `json:"kind"`
Title string `json:"title"`
BaseVersion int64 `json:"base_version"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_title"})
return
}
if parts[0] == "spots" {
request.Kind = "spot"
} else if request.Kind != "quartier" && request.Kind != "street" {
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_selection"})
return
}
if err := s.store.RenameCityTitle(domainUUID, principal.UUID, request.Kind, parts[1], request.Title, request.BaseVersion); err != nil {
writeCityOrganizeError(w, err)
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if action == "health" && r.Method == http.MethodGet {
health, err := s.store.GetCityHealth(domainUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
_ = json.NewEncoder(w).Encode(health)
return
}
if action == "events" && r.Method == http.MethodGet {
afterVersion, _ := strconv.ParseInt(r.URL.Query().Get("after_version"), 10, 64)
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
events, err := s.store.ListTopologyEvents(domainUUID, afterVersion, limit)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"events": events, "count": len(events)})
return
}
if (action == "plans/preview" || action == "plans/apply") && r.Method == http.MethodPost {
var plan store.CityPlan
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&plan) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid City plan JSON"})
return
}
var preview store.CityPlanPreview
var err error
if action == "plans/apply" {
preview, err = s.store.ApplyCityPlan(domainUUID, principal.UUID, plan)
} else {
preview, err = s.store.PreviewCityPlan(domainUUID, plan)
}
if err != nil {
writeEventJSON(w, 409, map[string]any{"error": err.Error(), "preview": preview})
return
}
_ = json.NewEncoder(w).Encode(preview)
return
}
if action == "autoplan" && r.Method == http.MethodGet {
plan, err := s.buildCityAutoPlan(domainUUID)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"items": plan, "count": len(plan)})
return
}
if action == "autoplan" && r.Method == http.MethodPost {
count, err := s.applyCityAutoPlan(domainUUID, principal.UUID)
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"status": "ok", "applied": count})
return
}
if action == "zones" && r.Method == http.MethodPost {
var request struct {
Kind string `json:"kind"`
ParentUUID string `json:"parent_uuid"`
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
PathPrefix string `json:"path_prefix"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
id, err := s.store.CreateZoneAtPath(domainUUID, principal.UUID, request.Kind, request.ParentUUID, request.Name, request.Title, request.Description, request.PathPrefix)
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusCreated, map[string]string{"uuid": id})
return
}
if action == "spots" && r.Method == http.MethodPost {
var request struct {
StreetUUID string `json:"street_uuid"`
Name string `json:"name"`
Title string `json:"title"`
Summary string `json:"summary"`
PathPrefix string `json:"path_prefix"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
id, err := s.store.CreateSpotAtPath(domainUUID, principal.UUID, request.StreetUUID, request.Name, request.Title, request.Summary, request.PathPrefix)
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusCreated, map[string]string{"uuid": id})
return
}
if len(parts) == 2 && (parts[0] == "zones" || parts[0] == "spots") && r.Method == http.MethodDelete {
kind := "spot"
if parts[0] == "zones" {
var request struct {
Kind string `json:"kind"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
kind = request.Kind
}
if err := s.store.DeleteCityNode(domainUUID, principal.UUID, kind, parts[1]); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if len(parts) == 2 && (parts[0] == "zones" || parts[0] == "spots") && r.Method == http.MethodPatch {
var request struct {
Kind string `json:"kind"`
PathPrefix string `json:"path_prefix"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil || strings.TrimSpace(request.PathPrefix) == "" {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON or Path"})
return
}
kind := "spot"
if parts[0] == "zones" {
kind = request.Kind
}
if err := s.store.UpdateCityNodePath(domainUUID, principal.UUID, kind, parts[1], request.PathPrefix); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "pages" && (r.Method == http.MethodPost || r.Method == http.MethodDelete) {
var request struct {
PageUUID string `json:"page_uuid"`
Path string `json:"path"`
EdgeType string `json:"edge_type"`
Label string `json:"label"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
var err error
if r.Method == http.MethodDelete {
err = s.store.RemoveSpotPage(domainUUID, principal.UUID, parts[1], request.PageUUID, request.EdgeType)
} else {
err = s.store.AddSpotPage(domainUUID, principal.UUID, parts[1], request.PageUUID, request.Path, request.EdgeType, request.Label)
}
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "relations" && r.Method == http.MethodPost {
var request struct {
TargetSpotUUID string `json:"target_spot_uuid"`
RelationType string `json:"relation_type"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
if err := s.store.AddSpotRelation(domainUUID, principal.UUID, parts[1], request.TargetSpotUUID, request.RelationType); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "relations" && r.Method == http.MethodDelete {
var request struct {
TargetSpotUUID string `json:"target_spot_uuid"`
RelationType string `json:"relation_type"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
if err := s.store.RemoveSpotRelation(domainUUID, principal.UUID, parts[1], request.TargetSpotUUID, request.RelationType); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
if len(parts) == 3 && parts[2] == "move" && r.Method == http.MethodPost {
var request struct {
ParentUUID string `json:"parent_uuid"`
StreetUUID string `json:"street_uuid"`
SortKey int `json:"sort_key"`
}
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
return
}
var err error
if parts[0] == "zones" {
err = s.store.MoveStreet(domainUUID, principal.UUID, parts[1], request.ParentUUID, request.SortKey)
} else if parts[0] == "spots" {
err = s.store.MoveSpot(domainUUID, principal.UUID, parts[1], request.StreetUUID, request.SortKey)
} else {
err = store.ErrAccessDenied
}
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
return
}
w.Header().Set("Allow", "GET, POST")
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
func jsonNumber(value int64) string {
return strconv.FormatInt(value, 10)
}
func writeCityOrganizeError(w http.ResponseWriter, err error) {
status := http.StatusBadRequest
if errors.Is(err, store.ErrCityChanged) {
status = http.StatusConflict
}
if errors.Is(err, sql.ErrNoRows) {
writeEventJSON(w, http.StatusNotFound, map[string]string{"error": "city_invalid_selection"})
return
}
writeEventJSON(w, status, map[string]string{"error": err.Error()})
}
+135
View File
@@ -0,0 +1,135 @@
package server
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestCityLabelsChromium(t *testing.T) {
runCityBrowserFixture(t, "city-labels.js", []string{"/life/home/readme", "/life/work/tools/guide"}, []string{"normal", "popup"})
}
func TestCityOrganizerChromium(t *testing.T) {
for _, mode := range []string{"normal", "popup"} {
t.Run(mode, func(t *testing.T) {
runCityBrowserFixture(t, "city-organizer.js", []string{"/life/home/readme", "/life/home/notes", "/life/home/alpha/page", "/life/home/beta/page", "/life/work/tools/guide"}, []string{mode})
})
}
}
func TestCityImmersiveChromium(t *testing.T) {
paths := []string{"/life/home/readme", "/life/home/alpha"}
for i := 0; i < 27; i++ {
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%02d", i))
}
for i := 0; i < 7; i++ {
paths = append(paths, fmt.Sprintf("/life/home/spot-%02d/page", i))
}
runCityBrowserFixture(t, "city-immersive.js", paths, []string{"normal", "popup", "menu", "mobile-light"})
}
func TestCityKeyboardChromium(t *testing.T) {
paths := []string{"/life/home/alpha", "/life/home/beta/page", "/life/work/tools/page", "/work/dev/code/page"}
for i := 0; i < 6; i++ {
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%d", i))
}
runCityBrowserFixture(t, "city-keyboard.js", paths, []string{"normal", "popup"})
}
func TestCityWorkerChromium(t *testing.T) {
runCityBrowserFixture(t, "city-worker.js", []string{"/life/home/alpha/page", "/life/work/tools/page"}, []string{"normal", "popup"})
}
func TestCityNamedNavigationChromium(t *testing.T) {
runCityBrowserFixture(t, "city-links.js", []string{"/life/home/alpha/page?#%", "/work/home/beta/page"}, []string{"normal", "popup", "quartier", "street", "spot"})
}
func runCityBrowserFixture(t *testing.T, scriptName string, paths, modes []string) {
t.Helper()
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
for _, path := range paths {
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
t.Fatal(err)
}
}
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/v/city", s.handleCityMap)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
s.mux.HandleFunc("/v/search", s.handleSearch)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
script, err := os.ReadFile("internal/server/testdata/" + scriptName)
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 == "/static/vendor/city-generator/gen-city.bundle.js" && scriptName != "city-worker.js" {
// Deliberately vertical/diagonal roads: labels must remain horizontal.
// Real template, renderer, styles, data and authorization are retained.
w.Header().Set("Content-Type", "text/javascript")
_, _ = io.WriteString(w, `globalThis.PagesWikiGenCity={generateFabric:async(key,width,height)=>({version:'test',width,height,roads:Array.from({length:24},(_,i)=>[20+(i%6)*12,15+Math.floor(i/6)*18,20+(i%6)*12+(i%2?4:0),30+Math.floor(i/6)*18].map((v,j)=>v*(j%2?height:width)/100)),buildings:[[[25,30],[28,30],[28,34],[25,34]],[[32,50],[35,50],[35,54],[32,54]],[[62,32],[65,32],[65,36],[62,36]],[[78,50],[81,50],[81,54],[78,54]]].map(b=>b.map(p=>[p[0]*width/100,p[1]*height/100]))})};`)
return
}
if r.URL.Path == "/v/city" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
_, _ = io.WriteString(w, strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, mode := range modes {
url := server.URL + "/v/city"
if mode == "popup" {
url += "?popup=1"
} else if mode == "menu" || mode == "mobile-light" {
url += "?fixture=" + mode
} else if mode == "quartier" || mode == "street" || mode == "spot" {
id := map[string]string{"quartier": "life", "street": "life/home", "spot": "alpha"}[mode]
node, err := s.store.CityNode(domain, mode, id)
if err != nil {
t.Fatal(err)
}
url += "?" + mode + "=" + node.UUID
}
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", url, t.TempDir())
screenshot := ""
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
cancel()
t.Fatal(err)
}
screenshot = filepath.Join(dir, strings.TrimSuffix(scriptName, ".js")+"-"+mode+".png")
}
cmd.Args = append(cmd.Args, screenshot)
if mode == "mobile-light" {
cmd.Args = append(cmd.Args, "390", "844")
}
out, err := cmd.CombinedOutput()
cancel()
if err != nil {
t.Fatalf("%s: %v %s", mode, err, out)
}
}
}
+40
View File
@@ -0,0 +1,40 @@
package server
import (
"net/url"
"strings"
"pageswiki/internal/store"
)
// Names identify Streets only within their parent Quartier. UUID references
// remain available for stable identity across renames and moves.
func cityDetailHref(kind, name, quartierName, uuid string) string {
prefix := map[string]string{"quartier": "q", "street": "s", "spot": "p"}[strings.ToLower(kind)]
if prefix == "" {
return "/v/city"
}
ref := "uuid:" + uuid
if name != "" && (prefix != "s" || quartierName != "") {
ref = url.PathEscape(name)
if prefix == "s" {
ref = url.PathEscape(quartierName) + "/" + ref
}
} else {
ref = url.PathEscape(ref)
}
return "/v/city/" + prefix + "/" + ref
}
func canReadCityNode(node store.CityNodeDetail, allowed func(string) bool) bool {
return allowed(node.PathPrefix) && (node.Quartier == nil || allowed(node.Quartier.PathPrefix)) && (node.Street == nil || allowed(node.Street.PathPrefix))
}
func cityPageHref(path string) string {
return (&url.URL{Path: path}).EscapedPath()
}
type cityPageView struct {
store.CityPagePlacement
Href string
}
+87
View File
@@ -0,0 +1,87 @@
package server
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCityNamedLinksAndLightweightDetails(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
s.mux.HandleFunc("/v/search", s.handleSearch)
for _, path := range []string{"/life/home/alpha/page?#%", "/work/home/beta/page"} {
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
t.Fatal(err)
}
}
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
t.Fatal(err)
}
call := func(route string) *httptest.ResponseRecorder {
r := httptest.NewRequest(http.MethodGet, "http://localhost"+route, nil)
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
street, err := s.store.CityNode(domain, "street", "life/home")
if err != nil {
t.Fatal(err)
}
for _, route := range []string{"/v/city/s/life/home", "/v/city/s/work/home", "/v/city/s/uuid:" + street.UUID} {
if w := call(route); w.Code != 200 {
t.Fatalf("%s=%d %s", route, w.Code, w.Body.String())
}
}
for _, route := range []string{"/v/city/s/home", "/v/city/s/missing/home", "/v/city/q/life/extra"} {
if w := call(route); w.Code != 404 {
t.Fatalf("%s=%d", route, w.Code)
}
}
w := call("/v/city/p/alpha")
for _, wanted := range []string{`href="/v/city/q/life"`, `href="/v/city/s/life/home"`, `href="/life/home/alpha/page%3F%23%25"`, `href="/v/city?spot=`} {
if w.Code != 200 || !strings.Contains(w.Body.String(), wanted) {
t.Fatalf("detail missing %s (%d): %s", wanted, w.Code, w.Body.String())
}
}
for _, selector := range []string{"life/home", "uuid:" + street.UUID} {
if w := call("/v/search?q=page&locality=street&locality_id=" + selector); w.Code != 200 {
t.Fatalf("locality search %s=%d: %s", selector, w.Code, w.Body.String())
}
}
r := httptest.NewRequest(http.MethodGet, "http://localhost/v/city", nil)
r.Header.Set("Authorization", "Bearer "+token)
city, err := s.cityProjectionForRequest(cacheRequestDomain(withReadPolicyContext(r)))
if err != nil {
t.Fatal(err)
}
for _, q := range city.Quartiers {
for _, st := range q.Streets {
if st.DetailHref != "/v/city/s/"+q.Name+"/"+st.Name {
t.Fatalf("projection link=%s", st.DetailHref)
}
}
}
macro := s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(string) bool { return true })
if !strings.Contains(macro, `href="/v/city/s/life/home"`) || !strings.Contains(macro, `href="/life/home/alpha/page%3F%23%25"`) {
t.Fatalf("macro=%s", macro)
}
macro = s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(p string) bool { return p != "/life" })
if strings.Contains(macro, "alpha") || strings.Contains(macro, "home") {
t.Fatalf("hidden ancestry leaked=%s", macro)
}
// Detail and breadcrumbs must not enumerate the entire City/Page graph.
if _, err := s.store.DB.Exec(`DROP TABLE spot_pages`); err != nil {
t.Fatal(err)
}
w = call("/v/city/s/life/home")
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/v/city?street=`+street.UUID+`"`) {
t.Fatalf("lightweight detail=%d %s", w.Code, w.Body.String())
}
macro = s.expandCityMacros("{{street}}", "/life/home/page", domain, func(string) bool { return true })
if !strings.Contains(macro, `href="/v/city/s/life/home"`) {
t.Fatalf("lightweight macro=%s", macro)
}
}
+65
View File
@@ -0,0 +1,65 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCityOrganizerAPIRequiresAdminAndVersion(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
for _, p := range []string{"/life/home/one/page", "/life/home/two/page"} {
if _, err := s.store.CreatePageAtPathInDomain(domain, p); err != nil {
t.Fatal(err)
}
}
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
t.Fatal(err)
}
city, err := s.store.GetCityMap(domain)
if err != nil {
t.Fatal(err)
}
one, two := city.Quartiers[0].Streets[0].Spots[0], city.Quartiers[0].Streets[0].Spots[1]
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
if err != nil {
t.Fatal(err)
}
readerToken, err := s.store.CreateAuthSession(domain, user.UUID)
if err != nil {
t.Fatal(err)
}
call := func(token, url string, body any) *httptest.ResponseRecorder {
b, _ := json.Marshal(body)
r := httptest.NewRequest(http.MethodPost, "http://localhost"+url, strings.NewReader(string(b)))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
rename := map[string]any{"title": "Changed", "base_version": city.Version}
merge := map[string]any{"action": "merge", "base_version": city.Version, "spots": []string{one.UUID}, "target_spot_uuid": two.UUID}
for url, body := range map[string]any{"/_/api/v1/city/spots/" + one.UUID + "/title": rename, "/_/api/v1/city/organize": merge} {
if w := call(readerToken, url, body); w.Code != http.StatusForbidden {
t.Fatalf("non-admin: %d %s", w.Code, w.Body.String())
}
}
if w := call(token, "/_/api/v1/city/spots/"+one.UUID+"/title", rename); w.Code != 200 {
t.Fatalf("rename: %d %s", w.Code, w.Body.String())
}
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != http.StatusConflict {
t.Fatalf("stale merge: %d %s", w.Code, w.Body.String())
}
merge["base_version"] = city.Version + 1
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != 200 {
t.Fatalf("merge: %d %s", w.Code, w.Body.String())
}
plan, err := s.buildCityAutoPlan(domain)
if err != nil || len(plan) != 1 {
t.Fatalf("manual membership re-inferred: %+v %v", plan, err)
}
}
+121
View File
@@ -0,0 +1,121 @@
package server
import (
"net/http"
"pageswiki/internal/store"
)
// City locality metadata requires read permission on its boundary; Page edges
// have their own checks. Never expose a hidden Page via a portal, count, nearby
// relation or editable destination list even when its Spot is visible.
func filterCityProjection(city store.CityMap, allowed func(string) bool) store.CityMap {
result := store.CityMap{DomainUUID: city.DomainUUID, Version: city.Version, Quartiers: []store.CityQuartier{}, Relations: []store.CitySpotRelation{}}
visible := map[string]bool{}
for _, quartier := range city.Quartiers {
if !allowed(quartier.PathPrefix) {
continue
}
quartier.DetailHref = cityDetailHref("quartier", quartier.Name, "", quartier.UUID)
streets := []store.CityStreet{}
for _, street := range quartier.Streets {
if !allowed(street.PathPrefix) {
continue
}
street.DetailHref = cityDetailHref("street", street.Name, quartier.Name, street.UUID)
spots := []store.CitySpot{}
for _, spot := range street.Spots {
if !allowed(spot.PathPrefix) {
continue
}
spot.DetailHref = cityDetailHref("spot", spot.Name, quartier.Name, spot.UUID)
pages := []store.CityPagePlacement{}
unique := map[string]bool{}
spot.PortalPath, spot.PortalPageUUID = "", ""
portalPriority := -1
for _, page := range spot.Pages {
if page.Path == "" || !allowed(page.Path) {
continue
}
pages = append(pages, page)
unique[page.PageUUID] = true
priority := 0
if page.EdgeType == "primary" {
priority = 1
}
if page.Path == spot.PathPrefix {
priority = 2
}
if priority > portalPriority {
spot.PortalPath, spot.PortalPageUUID = page.Path, page.PageUUID
portalPriority = priority
}
}
spot.Pages, spot.PageCount = pages, len(unique)
visible[spot.UUID] = true
spots = append(spots, spot)
}
street.Spots = spots
streets = append(streets, street)
}
quartier.Streets = streets
result.Quartiers = append(result.Quartiers, quartier)
}
for _, relation := range city.Relations {
if visible[relation.SourceSpotUUID] && visible[relation.TargetSpotUUID] {
result.Relations = append(result.Relations, relation)
}
}
return result
}
func (s *Server) cityProjectionForRequest(r *http.Request) (store.CityMap, error) {
city, err := s.navigationCity(r.Context(), s.domainUUIDForRequest(r))
if err != nil {
return city, err
}
return filterCityProjection(city, s.pathReadFilter(r)), nil
}
func (s *Server) spotPagesForRequest(r *http.Request, spotUUID string) ([]store.CityPagePlacement, error) {
domain := s.domainUUIDForRequest(r)
spot, err := s.store.CityNodeContext(r.Context(), domain, "spot", "uuid:"+spotUUID)
if err != nil {
return nil, err
}
if !canReadCityNode(spot, s.pathReadFilter(r)) {
return nil, store.ErrAccessDenied
}
pages, err := s.store.ListSpotPagesContext(r.Context(), domain, spotUUID)
if err != nil {
return nil, err
}
out := []store.CityPagePlacement{}
allowed := s.pathReadFilter(r)
for _, page := range pages {
if page.Path != "" && allowed(page.Path) {
out = append(out, page)
}
}
return out, nil
}
func privateCityResponse(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Add("Vary", "Cookie, Authorization")
}
func (s *Server) pageSpotAppearancesForRequest(r *http.Request, objectUUID string) ([]store.CitySpotAppearance, error) {
spots, err := s.store.ListPageSpotAppearances(s.domainUUIDForRequest(r), objectUUID)
if err != nil {
return nil, err
}
out := []store.CitySpotAppearance{}
allowed := s.pathReadFilter(r)
for _, spot := range spots {
if allowed(spot.PathPrefix) {
out = append(out, spot)
}
}
return out, nil
}
+156
View File
@@ -0,0 +1,156 @@
package server
import (
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"pageswiki/internal/store"
)
func TestCityReadErrorsDistinguishMissingFromResourceLimits(t *testing.T) {
for _, test := range []struct {
err error
want int
}{
{store.ErrAccessDenied, http.StatusNotFound}, {sql.ErrNoRows, http.StatusNotFound},
{store.ErrCityMapBudget, http.StatusServiceUnavailable},
{context.DeadlineExceeded, http.StatusServiceUnavailable}, {context.Canceled, http.StatusServiceUnavailable},
} {
if got := cityReadErrorStatus(test.err); got != test.want {
t.Fatalf("status(%v)=%d want=%d", test.err, got, test.want)
}
}
}
func TestCityReadPortalPriorityAfterFiltering(t *testing.T) {
spot := store.CitySpot{UUID: "spot", PathPrefix: "/q/s/p", Pages: []store.CityPagePlacement{
{PageUUID: "guide", Path: "/q/s/p/guide", EdgeType: "guide"},
{PageUUID: "primary", Path: "/q/s/p/primary", EdgeType: "primary"},
{PageUUID: "boundary", Path: "/q/s/p", EdgeType: "path"},
}}
street := store.CityStreet{PathPrefix: "/q/s", Spots: []store.CitySpot{spot}}
quartier := store.CityQuartier{PathPrefix: "/q", Streets: []store.CityStreet{street}}
city := store.CityMap{Quartiers: []store.CityQuartier{quartier}}
for _, test := range []struct{ excluded, want string }{{"", "/q/s/p"}, {"/q/s/p/primary", "/q/s/p"}} {
projected := filterCityProjection(city, func(path string) bool { return path != test.excluded })
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != test.want {
t.Fatalf("portal=%s want=%s", got, test.want)
}
}
city.Quartiers[0].Streets[0].Spots[0].Pages = city.Quartiers[0].Streets[0].Spots[0].Pages[:2]
projected := filterCityProjection(city, func(string) bool { return true })
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != "/q/s/p/primary" {
t.Fatalf("primary Page lost to first visible guide: %s", got)
}
}
func TestCityReadProjectionHidesPagesLocalitiesPortalsAndRelations(t *testing.T) {
s, _, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
for _, path := range []string{"/life/home/alpha/good", "/life/home/alpha/aaa-secret", "/life/home/beta/private", "/secret/vault/lock/private"} {
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
t.Fatal(err)
}
}
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
t.Fatal(err)
}
user, err := s.store.CreatePasswordAccount(domain, "limited", "a long test password", "Limited", "")
if err != nil {
t.Fatal(err)
}
token, err := s.store.CreateAuthSession(domain, user.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.domain_uuid=? AND m.principal_uuid=?`, domain, user.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 _, p := range []string{"/life", "/life/home", "/life/home/alpha", "/life/home/alpha/good"} {
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)`, "grant:"+p, role, p); err != nil {
t.Fatal(err)
}
}
raw, err := s.store.GetCityMap(domain)
if err != nil {
t.Fatal(err)
}
var alpha, beta store.CitySpot
for _, q := range raw.Quartiers {
for _, street := range q.Streets {
for _, p := range street.Spots {
if p.Title == "alpha" {
alpha = p
}
if p.Title == "beta" {
beta = p
}
}
}
}
if err := s.store.AddSpotRelation(domain, "test", alpha.UUID, beta.UUID, "related"); err != nil {
t.Fatal(err)
}
request := func(route string) *http.Request {
r := httptest.NewRequest("GET", "http://localhost"+route, nil)
r.Header.Set("Authorization", "Bearer "+token)
return r
}
call := func(route string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, request(route))
return w
}
w := call("/_/api/v1/city/map")
if w.Code != 200 {
t.Fatalf("map=%d %s", w.Code, w.Body.String())
}
for _, secret := range []string{"aaa-secret", "private", "beta", "vault", "secret"} {
if strings.Contains(w.Body.String(), secret) {
t.Fatalf("hidden metadata %q leaked: %s", secret, w.Body.String())
}
}
var projected store.CityMap
if err := json.Unmarshal(w.Body.Bytes(), &projected); err != nil {
t.Fatal(err)
}
spot := projected.Quartiers[0].Streets[0].Spots[0]
if spot.PageCount != 1 || len(spot.Pages) != 1 || spot.PortalPath != "/life/home/alpha/good" || len(projected.Relations) != 0 {
t.Fatalf("projection leaked count/portal/relation: %+v", spot)
}
if w.Header().Get("ETag") != "" || w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatal("unscoped City caching")
}
if w := call("/_/api/v1/city/spots/" + beta.UUID + "/pages"); w.Code != 404 {
t.Fatalf("hidden Spot pages=%d", w.Code)
}
if w := call("/v/city/p/beta"); w.Code != 404 {
t.Fatalf("hidden detail=%d", w.Code)
}
if w := call("/v/city/p/alpha"); w.Code != 200 || strings.Contains(w.Body.String(), "aaa-secret") {
t.Fatalf("visible detail=%d %s", w.Code, w.Body.String())
}
r := cacheRequestDomain(withReadPolicyContext(request("/life/home/alpha/good")))
macro := s.expandMacrosForRequest(r, `{{spot-pages}} {{nearby}}`, "/life/home/alpha/good", domain)
if strings.Contains(macro, "aaa-secret") || strings.Contains(macro, "beta") || !strings.Contains(macro, "/good") {
t.Fatalf("macro projection=%s", macro)
}
// A new request re-evaluates revoked grants; there is no cross-request cache.
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
t.Fatal(err)
}
w = call("/_/api/v1/city/map")
if w.Code != 200 || strings.Contains(w.Body.String(), "alpha") {
t.Fatalf("revoked projection=%d %s", w.Code, w.Body.String())
}
}
+21 -7
View File
@@ -8,11 +8,24 @@ import (
)
func (s *Server) compileMarkdownFromPath(
r *http.Request,
path string,
) (string, error) {
return s.transpileMarkdownFromPath(r, path, markdownAuthoring)
}
type markdownTranspileMode string
const (
markdownAuthoring markdownTranspileMode = "authoring"
markdownProjection markdownTranspileMode = "projection"
)
func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode markdownTranspileMode) (string, error) {
objectUUID, err :=
s.store.ResolvePath(
s.store.ResolvePathInDomain(
s.domainUUIDForRequest(r),
path,
)
@@ -21,7 +34,8 @@ func (s *Server) compileMarkdownFromPath(
}
latest, err :=
s.store.GetLatestRevision(
s.latestRevisionForRequest(
r,
objectUUID,
)
@@ -29,11 +43,10 @@ func (s *Server) compileMarkdownFromPath(
return "", err
}
html :=
s.expandMacros(
latest.Body,
path,
)
html := latest.Body
if mode == markdownProjection {
html = s.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r))
}
return compiler.HTMLToMarkdown(
html,
@@ -61,6 +74,7 @@ func (s *Server) handleCompileMarkdown(
out, err :=
s.compileMarkdownFromPath(
r,
path,
)
+64
View File
@@ -0,0 +1,64 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestConfigTableDensityChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain := configFixture(t)
if _, _, err := s.store.PutAsset(domain, "asset:text:density", []byte("test"), "text/plain", strings.Repeat("long-filename-", 30)+".txt", "", ""); err != nil {
t.Fatal(err)
}
event := store.EventDefinition{Name: `A "quoted" event`, EventType: "preview.sweep", EventKey: "density", ScheduleKind: "manual", Enabled: true, MaxAttempts: 1}
if err := s.store.SaveEventDefinition(&event); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/config-density.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.Method == "GET" && r.URL.Path == "/-/sys/config" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for key, values := range rec.Header() {
w.Header()[key] = values
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, panel := range []string{"auth", "campuses", "extensions"} {
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/-/sys/config?panel=auth&capture="+panel, t.TempDir())
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
cmd.Args = append(cmd.Args, filepath.Join(dir, "dense-"+panel+".png"))
}
out, err := cmd.CombinedOutput()
cancel()
if err != nil {
t.Fatalf("%s: %v %s", panel, err, out)
}
}
}
File diff suppressed because it is too large. Load diff
+26
View File
@@ -0,0 +1,26 @@
package server
type configPanelLink struct{ ID, Label string }
type configPanelGroup struct {
ID, Label string
Panels []configPanelLink
}
// The registry is owned by Pages Wiki; adapter data supplies names and IDs,
// never arbitrary templates or executable settings HTML.
func configPanelGroups(locale string, plugins []adapterPlugin) []configPanelGroup {
link := func(id, key string) configPanelLink { return configPanelLink{id, translate(locale, key)} }
groups := []configPanelGroup{
{"appearance", translate(locale, "config.group_appearance"), []configPanelLink{link("home", "config.home"), link("editor", "config.editor"), link("bookmarks", "home.bookmarks"), link("nav-home", "config.nav_home"), link("nav-system", "config.nav_system")}},
{"content", translate(locale, "config.group_content"), []configPanelLink{link("objects", "config.objects"), link("assets", "config.assets"), link("external-resources", "config.external_resources"), link("broadcasts", "config.broadcasts")}},
{"identity", translate(locale, "config.group_identity"), []configPanelLink{link("auth", "auth.accounts"), link("auth-providers", "config.auth_providers"), link("auth-sessions", "auth.sessions"), link("groups", "auth.groups")}},
{"campus", translate(locale, "config.group_campus"), []configPanelLink{link("campuses", "config.campuses"), link("domains", "config.domains")}},
{"adapters", translate(locale, "config.group_adapters"), []configPanelLink{link("adapters", "config.adapter_catalog"), link("adapters-builtin", "config.builtin_adapters")}},
{"operations", translate(locale, "config.group_operations"), []configPanelLink{link("scheduler", "config.execution_status"), link("logs", "config.execution_log"), link("events", "config.event_definitions"), link("queue", "config.queue_settings")}},
}
for _, plugin := range plugins {
groups[4].Panels = append(groups[4].Panels, configPanelLink{"adapter-" + plugin.ID, plugin.Name})
}
groups = append(groups, configPanelGroup{"extensions", translate(locale, "extension.title"), []configPanelLink{link("extensions", "extension.catalog"), link("diary", "periodical.diary"), link("blog", "periodical.blog")}})
return groups
}
+347
View File
@@ -0,0 +1,347 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"golang.org/x/net/html"
"pageswiki/internal/db"
"pageswiki/internal/store"
)
func configFixture(t *testing.T) (*Server, string, string) {
t.Helper()
t.Chdir("../..")
database, err := db.Open(t.TempDir() + "/config.db")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { database.Close() })
if err = db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.NewWithDocumentRoot(database, t.TempDir()+"/documents")
if err = st.EnsureConfigTable(); err != nil {
t.Fatal(err)
}
domain, err := st.GetDefaultDomain()
if err != nil {
t.Fatal(err)
}
owner, err := st.BootstrapOwner(domain.UUID, "admin", "a long test password", "Test Admin", "")
if err != nil {
t.Fatal(err)
}
token, err := st.CreateAuthSession(domain.UUID, owner.UUID)
if err != nil {
t.Fatal(err)
}
if err = st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
t.Fatal(err)
}
if err = st.SetConfigInDomain(domain.UUID, "locale", "ja"); err != nil {
t.Fatal(err)
}
s := &Server{store: st, mux: http.NewServeMux()}
s.mustLoadTemplates()
s.registerStaticRoutes()
s.mux.HandleFunc("/-/sys/config", s.handleConfig)
s.mux.HandleFunc("/-/sys/config/auth", s.handleAuthAdmin)
s.mux.HandleFunc("/-/sys/config/adapters", s.handleAdapterPluginConfig)
s.mux.HandleFunc("/_/api/v1/assets", s.handleAssetsAPI)
s.mux.HandleFunc("/_/api/v1/adapter", s.handleAdapterAPI)
s.mux.HandleFunc("/_/api/v1/queue", s.handleQueueAPI)
s.mux.HandleFunc("/_/api/v1/queue/", s.handleQueueAPI)
s.mux.HandleFunc("/_/api/v1/event", s.handleEventAPI)
s.mux.HandleFunc("/_/api/v1/event/", s.handleEventAPI)
s.mux.HandleFunc("/o/path:/system/static/css/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join("static/css", filepath.Base(r.URL.Path)))
})
if err = s.saveAdapterPlugins(domain.UUID, []adapterPlugin{{ID: "hosted.test", Name: "Test Editor", Mode: "editor", Enabled: true, URLTemplate: "https://editor.example/open?ticket={ticket}"}}); err != nil {
t.Fatal(err)
}
if _, _, err = st.PutAsset(domain.UUID, "asset:image:test", []byte("test"), "image/png", "test.png", "", owner.UUID); err != nil {
t.Fatal(err)
}
return s, token, domain.UUID
}
func configRequest(s *Server, token, method, path string, form url.Values) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(form.Encode()))
r.Header.Set("Authorization", "Bearer "+token)
if form != nil {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
func TestConfigOIDCAccountMatchingMode(t *testing.T) {
s, token, domain := configFixture(t)
w := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `name="oidc_match_by"`) || !strings.Contains(w.Body.String(), `value="email" selected`) {
t.Fatalf("default OIDC matching selector: %d", w.Code)
}
form := url.Values{
"action": {"save_oidc"},
"enabled": {"on"},
"issuer": {"https://id.example.test"},
"client_id": {"pages"},
"client_secret": {"secret"},
"oidc_match_by": {"display_name"},
}
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
if w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "panel=auth-providers") {
t.Fatalf("save OIDC matching mode: %d %s", w.Code, w.Body.String())
}
config, err := s.store.AuthProviderConfig(domain, "oidc")
if err != nil || config.OIDCMatchBy != "display_name" {
t.Fatalf("stored OIDC matching mode: %#v %v", config, err)
}
w = configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `value="display_name" selected`) {
t.Fatalf("selected OIDC matching mode: %d", w.Code)
}
form.Set("oidc_match_by", "preferred_username")
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
if w.Code != http.StatusSeeOther {
t.Fatalf("save preferred_username matching mode: %d %s", w.Code, w.Body.String())
}
config, err = s.store.AuthProviderConfig(domain, "oidc")
if err != nil || config.OIDCMatchBy != "preferred_username" {
t.Fatalf("stored preferred_username matching mode: %#v %v", config, err)
}
w = configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `value="preferred_username" selected`) {
t.Fatalf("selected preferred_username matching mode: %d", w.Code)
}
form.Set("oidc_match_by", "username")
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
if w.Code != http.StatusBadRequest {
t.Fatalf("invalid OIDC matching mode accepted: %d", w.Code)
}
}
func TestConfigLDAPProviderPanelAndSecretPreservation(t *testing.T) {
s, token, domain := configFixture(t)
w := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `name="ldap_search_filter"`) || !strings.Contains(w.Body.String(), `{{username}}`) {
t.Fatalf("LDAP provider panel: %d %s", w.Code, w.Body.String())
}
form := url.Values{
"action": {"save_ldap"},
"enabled": {"on"},
"ldap_url": {"ldaps://directory.example.test:636"},
"ldap_bind_dn": {"cn=pageswiki,ou=services,dc=example,dc=test"},
"ldap_bind_password": {"directory-secret"},
"ldap_search_base": {"ou=people,dc=example,dc=test"},
"ldap_search_filter": {"(&(objectClass=person)(uid={{username}}))"},
"ldap_verify_tls": {"on"},
"ldap_uid_attribute": {"entryUUID"},
"ldap_username_attribute": {"uid"},
"ldap_email_attribute": {"mail"},
"ldap_display_attribute": {"displayName"},
"ldap_match_by": {"username"},
"ldap_auto_provision": {"on"},
}
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
if w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "panel=auth-providers") {
t.Fatalf("save LDAP provider: %d %s", w.Code, w.Body.String())
}
config, err := s.store.AuthProviderConfig(domain, "ldap")
if err != nil || config.LDAPBindPassword != "directory-secret" || config.LDAPSkipTLSVerify || !config.LDAPAutoProvision || config.UUID == "" {
t.Fatalf("stored LDAP provider: %#v %v", config, err)
}
form.Set("ldap_bind_password", "")
form.Set("ldap_search_filter", "(mail={{username}})")
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
if w.Code != http.StatusSeeOther {
t.Fatalf("update LDAP provider: %d %s", w.Code, w.Body.String())
}
config, err = s.store.AuthProviderConfig(domain, "ldap")
if err != nil || config.LDAPBindPassword != "directory-secret" || config.LDAPSearchFilter != "(mail={{username}})" {
t.Fatalf("LDAP secret preservation: %#v %v", config, err)
}
}
func TestAssetsListingAdminAllowedAndMemberDenied(t *testing.T) {
s, token, domain := configFixture(t)
w := configRequest(s, token, "GET", "/_/api/v1/assets", nil)
var data map[string]json.RawMessage
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil || data["assets"] == nil {
t.Fatalf("asset list: %d %s", w.Code, w.Body.String())
}
member, err := s.store.CreatePasswordAccount(domain, "reader", "another long password", "Reader", "")
if err != nil {
t.Fatal(err)
}
memberToken, err := s.store.CreateAuthSession(domain, member.UUID)
if err != nil {
t.Fatal(err)
}
w = configRequest(s, memberToken, "GET", "/_/api/v1/assets", nil)
if w.Code != http.StatusForbidden {
t.Fatalf("member can list assets: %d", w.Code)
}
}
func TestConfigBookmarksSaveAndPanelRegistry(t *testing.T) {
s, token, domain := configFixture(t)
form := url.Values{"section": {"bookmarks"}, "bookmark_label": {"Docs", "External"}, "bookmark_kind": {"path", "url"}, "bookmark_target": {"/docs/100% complete", "https://example.com/help"}}
w := configRequest(s, token, "POST", "/-/sys/config", form)
if w.Code != 302 {
t.Fatalf("save bookmarks: %d %s", w.Code, w.Body.String())
}
items := s.homeBookmarks(domain)
if len(items) != 2 || items[0].Href != "/docs/100%25%20complete" {
t.Fatalf("bookmarks: %#v", items)
}
form["bookmark_target"][1] = "javascript:alert(1)"
w = configRequest(s, token, "POST", "/-/sys/config", form)
if w.Code != 400 || len(s.homeBookmarks(domain)) != 2 {
t.Fatal("unsafe bookmark accepted or prior state lost")
}
w = configRequest(s, token, "GET", "/-/sys/config?panel=bookmarks", nil)
if w.Code != 200 {
t.Fatalf("config: %d %s", w.Code, w.Body.String())
}
root, err := html.Parse(strings.NewReader(w.Body.String()))
if err != nil {
t.Fatal(err)
}
ids := map[string]bool{}
var panels []string
var scripts []string
var walk func(*html.Node)
walk = func(n *html.Node) {
if n.Type == html.ElementNode {
if n.Data == "table" {
var class, parentClass string
for _, attr := range n.Attr {
if attr.Key == "class" {
class = attr.Val
}
}
if n.Parent != nil {
for _, attr := range n.Parent.Attr {
if attr.Key == "class" {
parentClass = attr.Val
}
}
}
if (strings.Contains(class, "recent-table") || strings.Contains(class, "config-form-table")) && !strings.Contains(parentClass, "table-scroll") {
t.Errorf("configuration table has no horizontal scroll container: %s", class)
}
}
for _, a := range n.Attr {
if a.Key == "id" {
if ids[a.Val] {
t.Errorf("duplicate ID %s", a.Val)
}
ids[a.Val] = true
}
if a.Key == "data-panel" {
panels = append(panels, a.Val)
}
}
}
if n.Type == html.ElementNode && n.Data == "script" && n.FirstChild != nil {
scripts = append(scripts, n.FirstChild.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(root)
for _, panel := range panels {
if !ids["panel-"+panel] {
t.Errorf("missing panel %s", panel)
}
}
if len(panels) < 18 || !ids["panel-adapter-hosted.test"] {
t.Fatalf("incomplete panel registry: %v", panels)
}
if node, err := exec.LookPath("node"); err == nil {
for _, script := range scripts {
cmd := exec.Command(node, "--check")
cmd.Stdin = strings.NewReader(script)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("invalid inline JS: %s", out)
}
}
}
}
func TestIntegratedAdapterSettingsPersistAndRedirect(t *testing.T) {
s, token, domain := configFixture(t)
form := url.Values{"id": {"hosted.test"}, "connection_fields": {"1"}, "name": {"Edited"}, "mode": {"editor"},
"url_template": {"https://editor.example/open?ticket={ticket}"}, "enabled": {"on"}, "new_tab": {"on"}, "html_markdown": {"on"}, "default_editor": {"on"}, "extensions": {".html, .txt"}}
w := configRequest(s, token, "POST", "/-/sys/config/adapters?id=hosted.test", form)
if w.Code != 302 || !strings.Contains(w.Header().Get("Location"), "panel=adapter-hosted.test") {
t.Fatalf("save adapter: %d %s", w.Code, w.Body.String())
}
plugins, err := s.adapterPlugins(domain)
if err != nil || len(plugins) != 1 || !plugins[0].NewTab || !plugins[0].HTMLMarkdown || !plugins[0].DefaultEditor || plugins[0].Name != "Edited" {
t.Fatalf("plugin=%#v %v", plugins, err)
}
}
func TestConfigChromiumSmoke(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium smoke test")
}
s, token, _ := configFixture(t)
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
t.Fatal(err)
}
if err := s.runEvent(&store.EventJob{UUID: "resource-browser", Type: store.ResourceEventType}); 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)
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, panel := range []string{"assets", "events", "queue", "scheduler", "logs", "resources", "bookmarks", "adapter-hosted.test", "extensions"} {
t.Run(panel, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=1500", server.URL+"/-/sys/config?panel="+panel)
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
cmd.Args = append(cmd.Args, "--window-size=1440,1000", "--screenshot="+filepath.Join(dir, panel+".png"))
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("browser: %v %s", err, out)
}
body := string(out)
target, classes := panel, "config-section config-panel active"
if panel == "resources" || panel == "scheduler" {
target, classes = "scheduler", "config-section config-panel execution-dashboard active"
}
if strings.Contains(body, "Uncaught ") || !strings.Contains(body, `id="panel-`+target+`" class="`+classes+`"`) {
t.Fatalf("browser panel failed: %s", body)
}
if panel == "assets" && !strings.Contains(body, `data-asset-copy-hash=`) {
t.Fatalf("assets did not load: %s", body)
}
if panel == "resources" && !strings.Contains(body, `class="chart-line"`) {
t.Fatalf("resource chart did not load: %s", body)
}
})
}
}
+225
View File
@@ -0,0 +1,225 @@
package server
import (
"path/filepath"
"strings"
)
type projectionDelivery string
const (
deliveryHTML projectionDelivery = "html"
deliveryText projectionDelivery = "text"
deliveryRawMedia projectionDelivery = "raw-media"
deliveryRawFrame projectionDelivery = "raw-frame"
deliveryDownload projectionDelivery = "download"
)
type contentDescriptor struct {
MediaType string
Filename string
SizeBytes int64
}
type projectionAdapter struct {
ID string
Kind string
Delivery projectionDelivery
NeedsText bool
WindowableText bool
Match func(contentDescriptor) bool
}
const inlineTextWindowThreshold = int64(2 << 20)
var projectionAdapters = []projectionAdapter{
{
ID: "html",
Kind: "html",
Delivery: deliveryHTML,
Match: mediaTypes("text/html"),
},
{
ID: "excalidraw",
Kind: "excalidraw",
Delivery: deliveryText,
NeedsText: true,
WindowableText: true,
Match: extensions(".excalidraw"),
},
{
ID: "csv",
Kind: "csv",
Delivery: deliveryText,
NeedsText: true,
WindowableText: true,
Match: matchAny(
mediaTypes("text/csv", "application/csv"),
extensions(".csv"),
),
},
{
ID: "archive",
Kind: "archive",
Delivery: deliveryDownload,
Match: matchAny(
mediaTypes(
"application/zip", "application/x-zip-compressed",
"application/x-rar-compressed", "application/vnd.rar",
"application/x-7z-compressed", "application/x-tar",
"application/gzip", "application/x-xz",
),
extensions(".zip", ".rar", ".7z", ".tar", ".gz", ".tgz", ".xz", ".txz"),
),
},
{
ID: "debian-package",
Kind: "metadata",
Delivery: deliveryDownload,
Match: matchAny(
mediaTypes(
"application/vnd.debian.binary-package",
"application/x-debian-package",
),
extensions(".deb"),
),
},
{
ID: "eml",
Kind: "email",
Delivery: deliveryText,
Match: matchAny(
mediaTypes("message/rfc822"),
extensions(".eml"),
),
},
{
ID: "pe",
Kind: "metadata",
Delivery: deliveryDownload,
Match: extensions(".exe", ".dll", ".sys"),
},
{
ID: "json",
Kind: "json",
Delivery: deliveryText,
WindowableText: true,
Match: matchAny(mediaTypes("application/json", "application/ld+json"), extensions(".json", ".jsonl")),
},
{
ID: "text",
Kind: "code",
Delivery: deliveryText,
NeedsText: true,
WindowableText: true,
Match: textMediaType,
},
{
ID: "image",
Kind: "image",
Delivery: deliveryRawMedia,
Match: matchAny(
mediaTypePrefix("image/"),
extensions(".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif", ".svg"),
),
},
{
ID: "audio",
Kind: "audio",
Delivery: deliveryRawMedia,
Match: matchAny(
mediaTypePrefix("audio/"),
extensions(".mp3", ".flac", ".wav", ".ogg", ".m4a", ".aac", ".opus"),
),
},
{
ID: "video",
Kind: "video",
Delivery: deliveryRawMedia,
Match: matchAny(
mediaTypePrefix("video/"),
extensions(".mp4", ".webm", ".mkv", ".mov", ".m4v", ".ogv"),
),
},
{
ID: "pdf",
Kind: "pdf",
Delivery: deliveryRawFrame,
Match: matchAny(
mediaTypes("application/pdf"),
extensions(".pdf"),
),
},
{
ID: "download",
Kind: "download",
Delivery: deliveryDownload,
Match: func(contentDescriptor) bool { return true },
},
}
func selectProjectionAdapter(descriptor contentDescriptor) projectionAdapter {
for _, adapter := range projectionAdapters {
if adapter.Match(descriptor) {
return adapter
}
}
return projectionAdapters[len(projectionAdapters)-1]
}
func mediaTypes(types ...string) func(contentDescriptor) bool {
return func(descriptor contentDescriptor) bool {
for _, candidate := range types {
if descriptor.MediaType == candidate {
return true
}
}
return false
}
}
func mediaTypePrefix(prefix string) func(contentDescriptor) bool {
return func(descriptor contentDescriptor) bool {
return strings.HasPrefix(descriptor.MediaType, prefix)
}
}
func extensions(values ...string) func(contentDescriptor) bool {
return func(descriptor contentDescriptor) bool {
extension := strings.ToLower(filepath.Ext(descriptor.Filename))
for _, candidate := range values {
if extension == candidate {
return true
}
}
return false
}
}
func matchAny(matchers ...func(contentDescriptor) bool) func(contentDescriptor) bool {
return func(descriptor contentDescriptor) bool {
for _, matcher := range matchers {
if matcher(descriptor) {
return true
}
}
return false
}
}
func textMediaType(descriptor contentDescriptor) bool {
switch descriptor.MediaType {
case "application/json",
"application/xml",
"application/javascript",
"application/x-javascript",
"application/x-sh",
"application/x-shellscript",
"application/x-yaml",
"application/yaml",
"application/toml":
return true
default:
return strings.HasPrefix(descriptor.MediaType, "text/")
}
}
+188
View File
@@ -0,0 +1,188 @@
package server
import (
"bytes"
"encoding/json"
"mime"
"net/http"
"path/filepath"
"strings"
"unicode"
"unicode/utf8"
"golang.org/x/text/encoding/japanese"
textunicode "golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
type detectedText struct {
ContentType string
Text string
IsText bool
}
func detectRevisionText(data []byte, filename string) detectedText {
if len(data) == 0 {
return detectedText{"text/plain; charset=utf-8", "", true}
}
magicType := http.DetectContentType(data)
if magicType != "application/octet-stream" &&
!strings.HasPrefix(magicType, "text/plain") {
return detectedText{ContentType: magicType}
}
decoded, charset, ok := decodeTextBytes(data)
if !ok {
return detectedText{ContentType: http.DetectContentType(data)}
}
contentType := detectTextMediaType(decoded, filename)
if strings.HasPrefix(contentType, "text/") ||
contentType == "application/json" ||
contentType == "application/xml" ||
contentType == "application/javascript" ||
contentType == "application/x-sh" {
contentType += "; charset=" + charset
}
return detectedText{
ContentType: contentType,
Text: decoded,
IsText: true,
}
}
func decodeTextBytes(data []byte) (string, string, bool) {
if bytes.HasPrefix(data, []byte{0xef, 0xbb, 0xbf}) {
return string(data[3:]), "utf-8", true
}
if bytes.HasPrefix(data, []byte{0xff, 0xfe}) {
decoded, _, err := transform.Bytes(
textunicode.UTF16(textunicode.LittleEndian, textunicode.ExpectBOM).NewDecoder(),
data,
)
return string(decoded), "utf-16le", err == nil
}
if bytes.HasPrefix(data, []byte{0xfe, 0xff}) {
decoded, _, err := transform.Bytes(
textunicode.UTF16(textunicode.BigEndian, textunicode.ExpectBOM).NewDecoder(),
data,
)
return string(decoded), "utf-16be", err == nil
}
if utf8.Valid(data) {
text := string(data)
return text, "utf-8", looksLikeText(text)
}
if bytes.Contains(data, []byte{0x1b, '$'}) {
if decoded, _, err := transform.Bytes(japanese.ISO2022JP.NewDecoder(), data); err == nil {
text := string(decoded)
return text, "iso-2022-jp", looksLikeText(text)
}
}
for _, candidate := range []struct {
name string
enc transform.Transformer
}{
{"shift_jis", japanese.ShiftJIS.NewDecoder()},
{"euc-jp", japanese.EUCJP.NewDecoder()},
} {
decoded, _, err := transform.Bytes(candidate.enc, data)
if err == nil && looksLikeText(string(decoded)) {
return string(decoded), candidate.name, true
}
}
return "", "", false
}
func looksLikeText(text string) bool {
if text == "" {
return true
}
var controls, total int
for _, r := range text {
total++
if r == '\n' || r == '\r' || r == '\t' || r == '\f' {
continue
}
if unicode.IsControl(r) || r == utf8.RuneError {
controls++
}
}
return controls*100 <= total*2
}
func detectTextMediaType(text, filename string) string {
firstLine := text
if newline := strings.IndexByte(firstLine, '\n'); newline >= 0 {
firstLine = firstLine[:newline]
}
lower := strings.ToLower(firstLine)
switch {
case strings.HasPrefix(lower, "#!") &&
strings.Contains(lower, "python"):
return "text/x-python"
case strings.HasPrefix(lower, "#!") &&
(strings.Contains(lower, "/sh") ||
strings.Contains(lower, "bash") ||
strings.Contains(lower, "zsh")):
return "application/x-sh"
case strings.HasPrefix(lower, "#!") &&
(strings.Contains(lower, "node") || strings.Contains(lower, "deno")):
return "application/javascript"
}
if json.Valid([]byte(text)) {
return "application/json"
}
if strings.HasPrefix(strings.TrimSpace(text), "<?xml") {
return "application/xml"
}
if extType := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))); extType != "" {
if mediaType, _, err := mime.ParseMediaType(extType); err == nil {
return mediaType
}
}
return "text/plain"
}
func codeLanguage(contentType, filename, text string) string {
mediaType, _, _ := mime.ParseMediaType(contentType)
ext := strings.ToLower(filepath.Ext(filename))
switch {
case mediaType == "text/x-python" || ext == ".py":
return "python"
case mediaType == "application/x-sh" || mediaType == "application/x-shellscript" ||
ext == ".sh" || ext == ".bash" || ext == ".zsh":
return "shell"
case mediaType == "application/json" || ext == ".json":
return "json"
case mediaType == "application/javascript" || mediaType == "application/x-javascript" ||
ext == ".js" || ext == ".jsx" || ext == ".mjs" || ext == ".cjs":
return "javascript"
case ext == ".ts" || ext == ".tsx":
return "typescript"
case mediaType == "text/html" || ext == ".html" || ext == ".htm":
return "html"
case ext == ".css":
return "css"
case ext == ".go":
return "go"
case ext == ".rs":
return "rust"
case ext == ".java":
return "java"
case ext == ".c" || ext == ".h" || ext == ".cc" || ext == ".cpp" || ext == ".hpp":
return "cpp"
case mediaType == "application/x-yaml" || mediaType == "application/yaml" ||
ext == ".yaml" || ext == ".yml":
return "yaml"
case mediaType == "application/toml" || ext == ".toml":
return "toml"
case ext == ".sql":
return "sql"
}
detected := detectTextMediaType(text, filename)
if detected != "text/plain" && detected != mediaType {
return codeLanguage(detected, filename, text)
}
return "text"
}
+82
View File
@@ -0,0 +1,82 @@
package server
import (
"strings"
"testing"
"golang.org/x/text/encoding/japanese"
)
func TestDetectRevisionTextWithoutExtension(t *testing.T) {
tests := []struct {
name string
data []byte
contentType string
isText bool
language string
}{
{
name: "shell shebang",
data: []byte("#!/bin/sh\necho hello\n"),
contentType: "application/x-sh",
isText: true,
language: "shell",
},
{
name: "json",
data: []byte("{\"enabled\":true}\n"),
contentType: "application/json",
isText: true,
language: "json",
},
{
name: "PDF magic",
data: []byte("%PDF-1.7\n%\x80\x81\x82\n"),
contentType: "application/pdf",
isText: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
detected := detectRevisionText(test.data, "")
if detected.IsText != test.isText {
t.Fatalf("IsText = %v, want %v", detected.IsText, test.isText)
}
if !strings.HasPrefix(detected.ContentType, test.contentType) {
t.Fatalf(
"ContentType = %q, want prefix %q",
detected.ContentType,
test.contentType,
)
}
if test.language != "" {
if got := codeLanguage(
detected.ContentType,
"",
detected.Text,
); got != test.language {
t.Fatalf("language = %q, want %q", got, test.language)
}
}
})
}
}
func TestDetectRevisionTextDecodesShiftJIS(t *testing.T) {
data, err := japanese.ShiftJIS.NewEncoder().Bytes(
[]byte("日本語のテキストです\n"),
)
if err != nil {
t.Fatal(err)
}
detected := detectRevisionText(data, "")
if !detected.IsText {
t.Fatal("Shift_JIS text was classified as binary")
}
if detected.Text != "日本語のテキストです\n" {
t.Fatalf("decoded text = %q", detected.Text)
}
if !strings.Contains(detected.ContentType, "charset=shift_jis") {
t.Fatalf("ContentType = %q", detected.ContentType)
}
}
+173
View File
@@ -0,0 +1,173 @@
package server
import (
"mime"
"net/http"
"path/filepath"
"strings"
"pageswiki/internal/store"
)
func (s *Server) windowedContentViewForRequest(r *http.Request, objectUUID, semanticPath string) (contentView, bool, error) {
metadata, err := s.latestRevisionMetadataForRequest(r, objectUUID)
if err != nil || metadata.UUID == "" {
return contentView{}, false, err
}
size, err := s.store.DocumentSize(metadata.DocumentHash)
if err != nil {
return contentView{}, false, nil
}
mediaType := strings.ToLower(strings.TrimSpace(strings.Split(metadata.ContentType, ";")[0]))
adapter := selectProjectionAdapter(contentDescriptor{MediaType: mediaType, Filename: metadata.Filename, SizeBytes: size})
if adapter.ID == "pdf" || (adapter.ID == "archive" && !strings.EqualFold(filepath.Ext(metadata.Filename), ".zip")) {
view := revisionContentView(metadata, semanticPath)
view.SizeBytes = size
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
s.applyMetadataProjection(&view, semanticPath)
return view, true, nil
}
if size <= inlineTextWindowThreshold || !adapter.WindowableText {
return contentView{}, false, nil
}
window, err := s.store.ReadDocumentWindow(metadata.DocumentHash, 0, 128<<10)
if err != nil {
return contentView{}, false, err
}
view := revisionWindowedContentView(metadata, semanticPath, size, window.Data, window.NextOffset)
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
return view, true, nil
}
type contentView struct {
Kind string
AdapterID string
Delivery projectionDelivery
ContentType string
Filename string
Text string
RawURL string
Language string
SizeBytes int64
Windowed bool
ArchiveEntries []archiveEntryView
ArchiveTruncated bool
MetadataStatus string
MetadataURL string
Metadata []metadataView
ParseError string
EmailHTML bool
PDFPages int
CSVRows [][]string
CSVTruncated bool
ExcalidrawValid bool
ExcalidrawElements int
ObjectUUID string
RevisionUUID string
DocumentHash string
WindowURL string
NextOffset int64
JSONURL string
}
func revisionWindowedContentView(revision *store.Revision, semanticPath string, totalBytes int64, initial []byte, nextOffset int64) contentView {
contentType := revision.ContentType
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
mediaType, contentType = "application/octet-stream", "application/octet-stream"
}
filename := strings.TrimSpace(revision.Filename)
if filename == "" {
filename = filepath.Base(semanticPath)
}
if mediaType == "application/octet-stream" {
detected := detectRevisionText(initial, filename)
if detected.ContentType != "" {
contentType = detected.ContentType
mediaType, _, _ = mime.ParseMediaType(contentType)
}
}
adapter := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(mediaType), Filename: filename, SizeBytes: totalBytes})
text, _, ok := decodeTextBytes(initial)
if !ok {
text = string(initial)
}
view := contentView{Kind: adapter.Kind, AdapterID: adapter.ID, Delivery: adapter.Delivery, ContentType: contentType, Filename: filename, Text: text, Language: codeLanguage(contentType, filename, text), SizeBytes: totalBytes, Windowed: true, ObjectUUID: revision.ObjectUUID, RevisionUUID: revision.UUID, DocumentHash: revision.DocumentHash, WindowURL: "/v/text/rev:" + revision.UUID, NextOffset: nextOffset}
if adapter.ID == "json" {
view.JSONURL = "/v/json/rev:" + revision.UUID
view.Text = ""
}
if adapter.ID == "csv" {
parseCSVView(&view, initial)
view.CSVTruncated = view.CSVTruncated || nextOffset < totalBytes
}
if adapter.ID == "excalidraw" {
// Large scenes keep bounded source delivery rather than parsing an
// incomplete or unbounded graph during navigation.
view.Kind = "code"
view.Language = "json"
}
return view
}
func revisionContentView(revision *store.Revision, semanticPath string) contentView {
contentType := revision.ContentType
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
mediaType = "application/octet-stream"
contentType = mediaType
}
mediaType = strings.ToLower(mediaType)
filename := strings.TrimSpace(revision.Filename)
if filename == "" {
filename = filepath.Base(semanticPath)
}
view := contentView{
ContentType: contentType,
Filename: filename,
SizeBytes: int64(len(revision.Body)),
ObjectUUID: revision.ObjectUUID,
RevisionUUID: revision.UUID,
DocumentHash: revision.DocumentHash,
}
if mediaType == "application/octet-stream" {
detected := detectRevisionText([]byte(revision.Body), filename)
if detected.ContentType != "" {
contentType = detected.ContentType
mediaType, _, _ = mime.ParseMediaType(contentType)
view.ContentType = contentType
}
if detected.IsText {
view.Text = detected.Text
}
}
adapter := selectProjectionAdapter(contentDescriptor{
MediaType: mediaType,
Filename: filename,
SizeBytes: view.SizeBytes,
})
view.Kind = adapter.Kind
view.AdapterID = adapter.ID
view.Delivery = adapter.Delivery
view.Windowed = adapter.WindowableText &&
view.SizeBytes > inlineTextWindowThreshold
if adapter.ID == "json" {
view.JSONURL = "/v/json/rev:" + revision.UUID
}
if adapter.NeedsText {
if view.Text == "" {
if decoded, _, ok := decodeTextBytes([]byte(revision.Body)); ok {
view.Text = decoded
} else {
view.Text = revision.Body
}
}
view.Language = codeLanguage(contentType, filename, view.Text)
}
structuredContentView(&view, []byte(revision.Body), semanticPath)
if adapter.ID == "eml" {
view.EmailHTML = extractEmailHTML([]byte(revision.Body)) != ""
}
return view
}
+201
View File
@@ -0,0 +1,201 @@
package server
import (
"strings"
"testing"
"pageswiki/internal/store"
)
func TestRevisionContentViewSelectsSafeAdapters(t *testing.T) {
tests := []struct {
contentType string
kind string
}{
{"text/html; charset=utf-8", "html"},
{"text/x-python; charset=utf-8", "code"},
{"application/x-sh", "code"},
{"application/json", "json"},
{"image/png", "image"},
{"audio/mpeg", "audio"},
{"video/mp4", "video"},
{"application/pdf", "pdf"},
{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"download",
},
}
for _, test := range tests {
view := revisionContentView(
&store.Revision{
ContentType: test.contentType,
Filename: "sample",
Body: "<script>alert(1)</script>",
},
"/sample",
)
if view.Kind != test.kind {
t.Errorf(
"Content-Type %q kind = %q, want %q",
test.contentType,
view.Kind,
test.kind,
)
}
if test.kind == "code" && view.Text == "" {
t.Errorf("code content was not passed to escaped template rendering")
}
}
}
func TestRevisionContentViewSniffsExtensionlessOctetStream(t *testing.T) {
script := revisionContentView(
&store.Revision{
ContentType: "application/octet-stream",
Body: "#!/usr/bin/env bash\necho hello\n",
},
"/run",
)
if script.Kind != "code" || script.Language != "shell" {
t.Fatalf("script view = %#v", script)
}
if script.Text == "" {
t.Fatal("extensionless script did not expose decoded text")
}
pdf := revisionContentView(
&store.Revision{
ContentType: "application/octet-stream",
Body: "%PDF-1.7\n%\x80\x81\x82\n",
},
"/manual",
)
if pdf.Kind != "pdf" {
t.Fatalf("PDF kind = %q, want pdf", pdf.Kind)
}
}
func TestRevisionContentViewSelectsSpecializedAdapterBoundaries(t *testing.T) {
tests := []struct {
name string
contentType string
filename string
adapterID string
kind string
}{
{"CSV", "text/csv", "report.csv", "csv", "csv"},
{"ZIP", "application/zip", "bundle.zip", "archive", "archive"},
{
"DEB",
"application/octet-stream",
"pageswiki.deb",
"debian-package",
"metadata",
},
{
"Excalidraw",
"application/json",
"diagram.excalidraw",
"excalidraw",
"excalidraw",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
view := revisionContentView(
&store.Revision{
ContentType: test.contentType,
Filename: test.filename,
Body: "{}",
},
"/"+test.filename,
)
if view.AdapterID != test.adapterID || view.Kind != test.kind {
t.Fatalf(
"view adapter/kind = %q/%q, want %q/%q",
view.AdapterID,
view.Kind,
test.adapterID,
test.kind,
)
}
})
}
}
func TestCSVProjectionIsBounded(t *testing.T) {
body := "name,value\n" + strings.Repeat("row,1\n", maxCSVRows+10)
view := revisionContentView(&store.Revision{ContentType: "text/csv", Filename: "report.csv", Body: body}, "/report.csv")
if view.Kind != "csv" || len(view.CSVRows) != maxCSVRows {
t.Fatalf("CSV projection kind/rows = %q/%d", view.Kind, len(view.CSVRows))
}
if !view.CSVTruncated {
t.Fatal("bounded CSV projection did not report truncation")
}
}
func TestCodeLanguageStopsWhenDetectionReturnsCurrentMediaType(t *testing.T) {
if language := codeLanguage("text/csv; charset=utf-8", "report.csv", "name,value\na,1\n"); language != "text" {
t.Fatalf("CSV fallback language = %q, want text", language)
}
}
func TestExcalidrawProjectionValidatesScene(t *testing.T) {
valid := revisionContentView(&store.Revision{
ContentType: "application/json", Filename: "map.excalidraw",
Body: `{"type":"excalidraw","elements":[{"type":"rectangle","x":1,"y":2,"width":30,"height":20}]}`,
}, "/map.excalidraw")
if valid.Kind != "excalidraw" || !valid.ExcalidrawValid || valid.ExcalidrawElements != 1 {
t.Fatalf("valid scene projection = %#v", valid)
}
invalid := revisionContentView(&store.Revision{
ContentType: "application/json", Filename: "map.excalidraw", Body: `{"elements":[]}`,
}, "/map.excalidraw")
if invalid.ExcalidrawValid || invalid.ParseError == "" {
t.Fatalf("invalid scene was accepted: %#v", invalid)
}
}
func TestExcalidrawProjectionRejectsExcessivePointComplexity(t *testing.T) {
points := strings.Repeat("[0,0],", maxExcalidrawPoints+1)
points = strings.TrimSuffix(points, ",")
view := contentView{}
parseExcalidrawView(&view, []byte(`{"type":"excalidraw","elements":[{"type":"line","x":0,"y":0,"width":1,"height":1,"points":[`+points+`]}]}`))
if view.ExcalidrawValid || view.ParseError == "" {
t.Fatal("excessive Excalidraw point complexity was accepted")
}
}
func TestTextAdapterSignalsWindowingForLargeDocuments(t *testing.T) {
view := revisionContentView(
&store.Revision{
ContentType: "text/plain; charset=utf-8",
Filename: "large.txt",
Body: strings.Repeat("x", int(inlineTextWindowThreshold+1)),
},
"/large.txt",
)
if view.AdapterID != "text" {
t.Fatalf("AdapterID = %q, want text", view.AdapterID)
}
if !view.Windowed {
t.Fatal("large text adapter did not request windowed delivery")
}
}
func TestPreviewUsesViewerAdapterForNonHTMLRevision(t *testing.T) {
view := previewContentView(&store.Revision{
UUID: "revision-1",
ContentType: "text/plain; charset=utf-8",
Filename: "payload.txt",
Body: "<script>alert('preview')</script>",
}, "/payload.txt")
if view.Kind != "code" {
t.Fatalf("preview kind = %q, want code", view.Kind)
}
if view.Text != "<script>alert('preview')</script>" {
t.Fatalf("preview text = %q", view.Text)
}
}
+94
View File
@@ -0,0 +1,94 @@
package server
import (
"net/http"
"net/url"
"strings"
)
// No cross-origin trust/bypass list: adapters and federation use explicit
// server-to-server credentials, while browser editors are same-origin proxies.
var browserOriginProtection http.CrossOriginProtection
func validBrowserMutationOrigin(r *http.Request) bool {
// Legacy semantic editor entry may allocate an empty Page. Until creation
// is moved entirely into save, subject that navigation to the mutation gate.
navigation := (r.Method == http.MethodGet || r.Method == http.MethodHead) &&
(strings.HasPrefix(r.URL.Path, "/e/") || strings.HasPrefix(r.URL.Path, "/o/e/") || r.URL.Path == "/:/new")
if navigation {
copy := r.Clone(r.Context())
copy.Method = http.MethodPost
r = copy
}
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
return true
}
if browserOriginProtection.Check(r) != nil {
return false
}
// The standard guard compares hosts for older clients without Fetch
// Metadata. Also require the same scheme, especially an HTTP-origin form
// targeting an HTTPS session. Trusted same-origin Fetch Metadata already
// accounts for TLS termination at the reverse proxy.
if origin := r.Header.Get("Origin"); origin != "" && r.Header.Get("Sec-Fetch-Site") != "same-origin" {
u, err := url.Parse(origin)
scheme := "http"
if requestIsHTTPS(r) {
scheme = "https"
}
if err != nil || u.User != nil || u.Scheme != scheme || u.Host == "" || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return false
}
}
// Check legacy clients too when ambient browser credentials are present.
// A CLI must send an explicit Bearer credential, not reuse a browser cookie
// without supplying an Origin. Do not trust X-Forwarded-Host for this test.
_, session := r.Cookie(authCookieName)
_, challenge := r.Cookie(mfaChallengeCookieName)
if session != nil && challenge != nil || len(r.Header.Values("Authorization")) != 0 {
return true
}
if r.Header.Get("Sec-Fetch-Site") == "same-origin" || r.Header.Get("Origin") != "" {
return true // CrossOriginProtection already validated the supplied origin.
}
if navigation && r.Header.Get("Sec-Fetch-Site") == "none" && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
return true // address-bar/bookmark navigation, not an external embedding
}
u, err := url.Parse(r.Referer())
return err == nil && u.User == nil && (u.Scheme == "http" || u.Scheme == "https") && strings.EqualFold(u.Host, r.Host) && (!requestIsHTTPS(r) || u.Scheme == "https")
}
func rejectBrowserMutation(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if strings.HasPrefix(r.URL.Path, "/_/api/") {
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "cross-origin or unverified browser mutation rejected"})
return
}
http.Error(w, "forbidden: cross-origin or unverified browser mutation", http.StatusForbidden)
}
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if r.Method == http.MethodGet || r.Method == http.MethodHead {
s.renderAuthPage(w, r, map[string]any{"Logout": true})
return
}
if r.Method != http.MethodPost {
w.Header().Set("Allow", "GET, HEAD, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if token := s.authTokenFromRequest(r); token != "" {
if err := s.store.RevokeAuthSession(token); err != nil {
http.Error(w, "session revocation failed", http.StatusInternalServerError)
return
}
}
if len(r.Header.Values("Authorization")) == 0 {
http.SetCookie(w, &http.Cookie{Name: authCookieName, Path: "/", MaxAge: -1, HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode})
} else {
writeEventJSON(w, http.StatusOK, map[string]string{"status": "signed_out"})
return
}
http.Redirect(w, r, "/_/auth/login", http.StatusSeeOther)
}
+184
View File
@@ -0,0 +1,184 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestBrowserMutationOrigins(t *testing.T) {
for _, tc := range []struct {
name, method, origin, site, referer, authorization string
cookie, allow bool
}{
{"same_origin", "POST", "https://wiki.example", "same-origin", "", "", true, true},
{"legacy_same_origin", "POST", "https://wiki.example", "", "", "", true, true},
{"legacy_cross_scheme", "POST", "http://wiki.example", "", "", "", true, false},
{"legacy_cross_scheme_login", "POST", "http://wiki.example", "", "", "", false, false},
{"origin_is_not_url_path", "POST", "https://wiki.example/path", "", "", "", true, false},
{"foreign_origin", "POST", "https://evil.example", "cross-site", "", "", true, false},
{"sibling_is_not_trusted", "POST", "https://sibling.wiki.example", "same-site", "", "", true, false},
{"null_origin", "POST", "null", "", "", "", true, false},
{"no_origin_cookie", "POST", "", "", "", "", true, false},
{"same_origin_metadata", "POST", "", "same-origin", "", "", true, true},
{"old_same_origin_referrer", "POST", "", "", "https://wiki.example/form", "", true, true},
{"foreign_referrer", "POST", "", "", "https://evil.example/form", "", true, false},
{"cli_bearer", "POST", "", "", "", "Bearer explicit", false, true},
{"explicit_bearer_with_cookie", "POST", "", "", "", "Bearer explicit", true, true},
{"cross_origin_login", "POST", "https://evil.example", "cross-site", "", "", false, false},
{"machine_enrollment", "POST", "", "", "", "", false, true},
{"safe_read", "GET", "https://evil.example", "cross-site", "", "", true, true},
{"cross_origin_delete", "DELETE", "https://evil.example", "cross-site", "", "", true, false},
} {
t.Run(tc.name, func(t *testing.T) {
r := httptest.NewRequest(tc.method, "https://wiki.example/_/auth/login", nil)
r.Header.Set("Origin", tc.origin)
r.Header.Set("Sec-Fetch-Site", tc.site)
r.Header.Set("Referer", tc.referer)
if tc.authorization != "" {
r.Header.Set("Authorization", tc.authorization)
}
if tc.cookie {
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "browser"})
}
called := false
w := httptest.NewRecorder()
(&Server{}).withAuthorization(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { called = true })).ServeHTTP(w, r)
if called != tc.allow || (!tc.allow && w.Code != http.StatusForbidden) {
t.Fatalf("allowed=%v status=%d, want %v", called, w.Code, tc.allow)
}
})
}
// Handler-owned JSON routes receive a parseable denial, before DB access.
r := httptest.NewRequest("POST", "https://wiki.example/_/api/v1/assets", nil)
r.Header.Set("Origin", "https://evil.example")
w := httptest.NewRecorder()
(&Server{}).withAuthorization(http.NotFoundHandler()).ServeHTTP(w, r)
var result map[string]string
if w.Code != 403 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result["error"] == "" {
t.Fatalf("invalid CSRF JSON: %d %s", w.Code, w.Body.String())
}
}
func TestExplicitCredentialNeverFallsBackToCookie(t *testing.T) {
s := &Server{}
for _, tc := range []struct{ header, want string }{
{"Basic secret", ""}, {"Bearer", ""}, {"Bearer a b", ""}, {"Bearer a,b", ""}, {"bearer api-token", "api-token"},
} {
r := httptest.NewRequest("GET", "http://localhost/", nil)
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "cookie-token"})
r.Header.Set("Authorization", tc.header)
if got := s.authTokenFromRequest(r); got != tc.want {
t.Fatalf("header=%q got=%q want=%q", tc.header, got, tc.want)
}
}
r := httptest.NewRequest("GET", "http://localhost/", nil)
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "one"})
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "two"})
if s.authTokenFromRequest(r) != "" {
t.Fatal("ambiguous cookies accepted")
}
}
func TestLogoutRequiresPostAndRevokesSelectedCredential(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/auth/", s.handleAuth)
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
cookieToken, err := s.store.CreateBrowserAuthSession(domain, principal.UUID, 0)
if err != nil {
t.Fatal(err)
}
request := func(method, origin, bearer string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost/_/auth/logout", nil)
r.AddCookie(&http.Cookie{Name: authCookieName, Value: cookieToken})
if origin != "" {
r.Header.Set("Origin", origin)
}
if bearer != "" {
r.Header.Set("Authorization", "Bearer "+bearer)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
if w := request("GET", "", ""); w.Code != 200 {
t.Fatalf("confirmation: %d %s", w.Code, w.Body.String())
}
if _, err := s.store.PrincipalForSession(domain, cookieToken); err != nil {
t.Fatal("GET revoked session")
}
if w := request("POST", "http://evil.example", ""); w.Code != 403 {
t.Fatalf("CSRF logout accepted: %d", w.Code)
}
if w := request("POST", "", token); w.Code != 200 {
t.Fatalf("bearer logout: %d", w.Code)
}
if _, err := s.store.PrincipalForSession(domain, token); err == nil {
t.Fatal("Bearer token survived logout")
}
if _, err := s.store.PrincipalForSession(domain, cookieToken); err != nil {
t.Fatal("Bearer logout revoked unrelated cookie credential")
}
if w := request("POST", "http://localhost", ""); w.Code != 303 {
t.Fatalf("cookie logout: %d", w.Code)
}
if _, err := s.store.PrincipalForSession(domain, cookieToken); err == nil {
t.Fatal("cookie token survived logout")
}
}
func TestReturnPathStaysLocal(t *testing.T) {
for _, value := range []string{"https://evil.example", "//evil.example", "/%2fevil.example", "/\\evil.example", "/%00bad"} {
if got := safeReturnPath(value); got != "/" {
t.Fatalf("unsafe return %q -> %q", value, got)
}
}
if got := safeReturnPath("/life/home?q=1"); got != "/life/home?q=1" {
t.Fatal(got)
}
}
func TestSessionPurposeAndEditorNavigation(t *testing.T) {
s, apiToken, domain := configFixture(t)
principal, err := s.store.PrincipalForSession(domain, apiToken)
if err != nil {
t.Fatal(err)
}
browserToken, err := s.store.CreateBrowserAuthSession(domain, principal.UUID, 0)
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
token, transport string
allowed bool
}{
{apiToken, "bearer", true}, {apiToken, "cookie", false},
{browserToken, "bearer", false}, {browserToken, "cookie", true},
} {
r := httptest.NewRequest("GET", "http://localhost/", nil)
if tc.transport == "bearer" {
r.Header.Set("Authorization", "Bearer "+tc.token)
} else {
r.AddCookie(&http.Cookie{Name: authCookieName, Value: tc.token})
}
_, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
if (err == nil) != tc.allowed {
t.Fatalf("%s purpose mismatch: allowed=%v err=%v", tc.transport, tc.allowed, err)
}
}
r := httptest.NewRequest("GET", "https://wiki.example/e/new-page", nil)
r.AddCookie(&http.Cookie{Name: authCookieName, Value: browserToken})
r.Header.Set("Sec-Fetch-Site", "cross-site")
if validBrowserMutationOrigin(r) {
t.Fatal("legacy editor GET accepted cross-site")
}
r.Header.Set("Sec-Fetch-Site", "none")
r.Header.Set("Sec-Fetch-Mode", "navigate")
if !validBrowserMutationOrigin(r) {
t.Fatal("address-bar editor navigation rejected")
}
}
+17
View File
@@ -0,0 +1,17 @@
package server
import "net/http"
// calendar-access normally asserts RFC 4791 conformance, which our limited
// implementation does not provide. Only advertise it for an explicitly opted-in
// compatibility credential (e.g. OneTask's OPTIONS gate); never infer from UA.
func (s *Server) setDAVCapabilities(w http.ResponseWriter, r *http.Request, actor, credential string) {
w.Header().Set("DAV", "1")
if credential == "" {
return
}
if enabled, err := s.store.DAVCalendarAccessCompat(r.Context(), s.domainUUIDForRequest(r), actor, credential); err == nil && enabled {
w.Header().Set("DAV", "1, calendar-access")
w.Header().Set("X-PagesWiki-CalDAV-Profile", "limited-compatibility")
}
}
+137
View File
@@ -0,0 +1,137 @@
package server
import (
"context"
"encoding/base64"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
// Reproduce the reported OPTIONS gate, not a claim to execute the OneTask app.
// Never use real device credentials or imported calendar data in this fixture.
func TestDAVCalendarAccessCompatibility(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
c, err := repo.Create(ctx, domain, actor, "Synthetic calendar")
if err != nil {
t.Fatal(err)
}
request := func(auth, method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
if auth != "" {
r.Header.Set("Authorization", auth)
}
r.Header.Set("Depth", "1")
r.Header.Set("Content-Type", "text/calendar")
for k, v := range h {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
if w.Code != want {
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
}
return w
}
issue := func(scope string, write, compat bool) (string, string) {
t.Helper()
b, _ := json.Marshal(map[string]any{"label": "Compatibility fixture", "scope": scope, "write": write, "calendar_access_compat": compat})
w := request("Bearer "+token, "POST", davCredentialsAPI, string(b), nil, 201)
var result struct {
Username, Password string
Credential store.TaskDAVCredential
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Credential.CalendarAccessCompat != compat {
t.Fatal("compatibility not persisted")
}
return "Basic " + base64.StdEncoding.EncodeToString([]byte(result.Username+":"+result.Password)), result.Username
}
strict, _ := issue("all", true, false)
compat, id := issue("all", true, true)
readOnly, _ := issue("tasks", false, true)
paths := []string{calendarDAV, strings.TrimSuffix(calendarDAV, "/"), taskDAVRoot, strings.TrimSuffix(taskDAVRoot, "/"), taskDAVCollection, strings.TrimSuffix(taskDAVCollection, "/"), calendarDAV + c.ID + "/", calendarDAV + c.ID}
for _, p := range paths {
for _, auth := range []string{strict, "Bearer " + token} {
w := request(auth, "OPTIONS", p, "", map[string]string{"User-Agent": "OneTask"}, 204)
if w.Header().Get("DAV") != "1" || w.Header().Get("X-PagesWiki-CalDAV-Profile") != "" {
t.Fatal("UA or session enabled compatibility", w.Header())
}
}
w := request(compat, "OPTIONS", p, "", nil, 204)
if w.Header().Get("DAV") != "1, calendar-access" || w.Header().Get("X-PagesWiki-CalDAV-Profile") != "limited-compatibility" || w.Header().Get("Location") != "" {
t.Fatal(p, w.Header())
}
if strings.Contains(w.Header().Get("DAV"), "calendar-schedule") || strings.Contains(w.Header().Get("Allow"), "MKCALENDAR") {
t.Fatal("unimplemented capability advertised")
}
}
// Follow the accepted gate through authenticated discovery, fetch and writes.
home := request(compat, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
if !strings.Contains(home.Body.String(), "current-user-principal") || !strings.Contains(home.Body.String(), taskDAVCollection) {
t.Fatal(home.Body)
}
col := request(readOnly, "PROPFIND", taskDAVCollection, commonDAVProps, map[string]string{"Depth": "0"}, 207)
if !strings.Contains(col.Body.String(), "current-user-privilege-set") || strings.Contains(col.Body.String(), "<d:write/>") {
t.Fatal("readonly privileges", col.Body)
}
col = request(compat, "PROPFIND", taskDAVCollection, commonDAVProps, map[string]string{"Depth": "0"}, 207)
if !strings.Contains(col.Body.String(), "<d:write/>") {
t.Fatal("missing write privilege", col.Body)
}
p := taskDAVCollection + "compat.ics"
request(compat, "PUT", p, taskICALFixture, nil, 201)
get := request(compat, "GET", p, "", nil, 200)
request(compat, "PUT", p, taskICALFixture, nil, 204)
get = request(compat, "GET", p, "", nil, 200)
request(readOnly, "PUT", p, taskICALFixture, map[string]string{"If-Match": get.Header().Get("ETag")}, 403)
changed := strings.Replace(taskICALFixture, "SUMMARY:CLI task", "SUMMARY:Compatibility update", 1)
request(compat, "PUT", p, changed, map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
query := strings.Replace(commonDAVQuery, `<c:comp-filter name="VCALENDAR"/>`, `<c:comp-filter name="VCALENDAR"><c:comp-filter name="VTODO"/></c:comp-filter>`, 1)
result := request(compat, "REPORT", taskDAVCollection, query, nil, 207)
if !strings.Contains(result.Body.String(), "Compatibility update") {
t.Fatal(result.Body)
}
// Compatibility never strips unsupported recurrence to make a write succeed.
bad := strings.Replace(changed, "END:VTODO", "RRULE:FREQ=DAILY\r\nEND:VTODO", 1)
get = request(compat, "GET", p, "", nil, 200)
request(compat, "PUT", p, bad, map[string]string{"If-Match": get.Header().Get("ETag")}, 403)
request(compat, "DELETE", p, "", map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
request(readOnly, "OPTIONS", calendarDAV+c.ID+"/", "", nil, 401)
request(compat, "POST", davCredentialsAPI, `{"label":"escalate","calendar_access_compat":true}`, nil, 401)
// Another principal/domain cannot pick up this credential's compatibility.
if enabled, _ := s.store.DAVCalendarAccessCompat(ctx, domain, "someone-else", id); enabled {
t.Fatal("cross-owner flag leaked")
}
if enabled, _ := s.store.DAVCalendarAccessCompat(ctx, "other-domain", actor, id); enabled {
t.Fatal("cross-domain flag leaked")
}
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, id); err != nil {
t.Fatal(err)
}
w := request(compat, "OPTIONS", calendarDAV, "", nil, 401)
if w.Header().Get("DAV") != "" {
t.Fatal("revoked credential advertised support")
}
var count int
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_dav_compat WHERE credential_id=?`, id).Scan(&count); err != nil || count != 0 {
t.Fatal("orphan compatibility row", count, err)
}
expired, expiredID := issue("all", true, true)
if _, err = s.store.DB.Exec(`UPDATE organizer_dav_credentials SET expires_ms=? WHERE id=?`, time.Now().Add(-time.Minute).UnixMilli(), expiredID); err != nil {
t.Fatal(err)
}
request(expired, "OPTIONS", calendarDAV, "", nil, 401)
}
+153
View File
@@ -0,0 +1,153 @@
package server
import (
"context"
"encoding/base64"
"net/http/httptest"
"strings"
"testing"
"time"
"pageswiki/internal/calendar"
)
const commonDAVProps = `<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:current-user-principal/><c:calendar-home-set/><d:resourcetype/><d:getetag/><d:displayname/><d:current-user-privilege-set/></d:prop></d:propfind>`
const commonDAVQuery = `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data><c:comp name="VCALENDAR"><c:allprop/><c:allcomp/></c:comp></c:calendar-data></d:prop><c:filter><c:comp-filter name="VCALENDAR"/></c:filter></c:calendar-query>`
func TestCommonDAVScopesAndCalendarWrites(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
c, err := repo.Create(ctx, domain, actor, "Common fixture")
if err != nil {
t.Fatal(err)
}
col := calendarDAV + c.ID + "/"
item, err := calendar.Build("", calendar.Event{UID: "common@example.test", Summary: "Synthetic event", Start: time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)})
if err != nil {
t.Fatal(err)
}
makeAuth := func(scope string, write bool) (string, string) {
t.Helper()
c, p, e := s.store.CreateDAVCredential(ctx, domain, actor, scope, write, 1, scope)
if e != nil {
t.Fatal(e)
}
return "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+p)), c.ID
}
taskAuth, legacyID := makeAuth("tasks", true)
// Simulate a credential from before scope storage existed.
if _, err = s.store.DB.Exec(`DELETE FROM organizer_dav_scopes WHERE credential_id=?`, legacyID); err != nil {
t.Fatal(err)
}
calAuth, _ := makeAuth("calendar", true)
allAuth, allID := makeAuth("all", true)
readAuth, _ := makeAuth("all", false)
req := func(auth, method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
r.Header.Set("Authorization", auth)
r.Header.Set("Depth", "1")
r.Header.Set("Content-Type", "text/calendar")
for k, v := range h {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
if w.Code != want {
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
}
return w
}
for _, p := range []string{calendarDAV, strings.TrimSuffix(calendarDAV, "/")} {
for _, test := range []struct {
auth string
tasks, cal bool
}{{taskAuth, true, false}, {calAuth, false, true}, {allAuth, true, true}} {
w := req(test.auth, "PROPFIND", p, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), taskDAVCollection) != test.tasks || strings.Contains(w.Body.String(), col) != test.cal || w.Header().Get("Location") != "" {
t.Fatal(w.Body)
}
if !strings.Contains(w.Body.String(), "current-user-principal") {
t.Fatal(w.Body)
}
}
}
req(taskAuth, "PROPFIND", col, commonDAVProps, nil, 401)
req(calAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 401)
req(allAuth, "GET", davCredentialsAPI, "", nil, 401)
req(allAuth, "POST", davCredentialsAPI, `{"label":"escalate","scope":"all"}`, nil, 401)
unauth := req(allAuth, "GET", calendarAPI+"calendars", "", nil, 200)
if !strings.Contains(unauth.Body.String(), `"actor":""`) || strings.Contains(unauth.Body.String(), c.ID) {
t.Fatal("DAV credential escaped to JSON API", unauth.Body)
}
path := col + "arbitrary-client-name.ics"
req(readAuth, "PUT", path, item.Data, nil, 403)
req(calAuth, "PUT", path, item.Data, nil, 201)
w := req(allAuth, "GET", path, "", nil, 200)
tag := w.Header().Get("ETag")
req(allAuth, "GET", path, "", map[string]string{"If-None-Match": tag}, 304)
req(allAuth, "PUT", path, item.Data, nil, 428)
req(allAuth, "PUT", col+"duplicate.ics", item.Data, nil, 412)
req(allAuth, "PUT", path, item.Data, map[string]string{"If-Match": `"stale"`}, 412)
updated, err := calendar.Build(item.Data, calendar.Event{UID: item.UID, Summary: "Edited through CLI", Start: item.Start, End: item.End})
if err != nil {
t.Fatal(err)
}
req(allAuth, "PUT", path, updated.Data, map[string]string{"If-Match": tag}, 204)
for _, method := range []string{"PROPFIND", "REPORT"} {
body := commonDAVProps
if method == "REPORT" {
body = commonDAVQuery
}
w = req(allAuth, method, col, body, nil, 207)
if !strings.Contains(w.Body.String(), path) {
t.Fatal("lost client filename", w.Body)
}
}
// Reject a partial filter instead of silently returning the entire collection.
bad := strings.Replace(commonDAVQuery, `name="VCALENDAR"`, `name="VCALENDAR" test="anyof"`, 1)
req(allAuth, "REPORT", col, bad, nil, 403)
// Canonical URI used by the existing Web UI still resolves after CLI creation.
req("Bearer "+token, "GET", calendarHref(c.ID, item.UID), "", nil, 200)
projected := req("Bearer "+token, "GET", calendarAPI+"calendars/"+c.ID+"/events?resource=arbitrary-client-name.ics", "", nil, 200)
if !strings.Contains(projected.Body.String(), "Edited through CLI") {
t.Fatal("Web viewer projection failed", projected.Body)
}
// ACL revocation and membership checks are independent of the secret's scope.
if _, err = s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, actor); err != nil {
t.Fatal(err)
}
req(allAuth, "GET", path, "", nil, 401)
s.store.DB.Exec(`UPDATE principals SET state='active' WHERE uuid=?`, actor)
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, allID); err != nil {
t.Fatal(err)
}
req(allAuth, "GET", path, "", nil, 401)
// Permission recheck inside the calendar's locked write path.
_, _, err = repo.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary-client-name.ics", updated.ETag, &updated, func() error { return s.store.CheckDAVCredential(ctx, domain, actor, allID, true, "calendar") })
if err != calendar.ErrForbidden {
t.Fatal("revoked credential wrote", err)
}
req(calAuth, "DELETE", path, "", map[string]string{"If-Match": updated.ETag}, 204)
req(calAuth, "GET", path, "", nil, 404)
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.calendar":false}`); err != nil {
t.Fatal(err)
}
w = req(taskAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), col) || !strings.Contains(w.Body.String(), taskDAVCollection) {
t.Fatal("disabled calendar exposed", w.Body)
}
req(calAuth, "PROPFIND", col, commonDAVProps, nil, 404)
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
t.Fatal(err)
}
w = req(calAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), taskDAVCollection) || !strings.Contains(w.Body.String(), col) {
t.Fatal("disabled tasks exposed", w.Body)
}
req(taskAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 404)
}
+154
View File
@@ -0,0 +1,154 @@
package server
import (
"encoding/xml"
"net/http"
"strings"
mw "pageswiki/internal/server/middleware"
)
func (s *Server) davActor(w http.ResponseWriter, r *http.Request, requested string, anonymous bool) (actor string, write bool, scope, credential string, ok bool) {
scope = "all"
if len(r.Header.Values("Authorization")) > 1 {
mw.SetDAVAuthOutcome(r, "multiple_authorization")
w.WriteHeader(401)
return
}
if id, secret, basic := r.BasicAuth(); basic {
var err error
actor, write, scope, err = s.store.AuthenticateDAV(r.Context(), s.domainUUIDForRequest(r), id, secret, requested)
if err == nil {
mw.SetDAVAuthOutcome(r, "basic_accepted")
return actor, write, scope, id, true
}
mw.SetDAVAuthOutcome(r, "basic_rejected")
} else {
actor, write = s.calendarActor(r)
if actor != "" || anonymous && r.Header.Get("Authorization") == "" {
if actor != "" {
mw.SetDAVAuthOutcome(r, "session_accepted")
} else {
mw.SetDAVAuthOutcome(r, "anonymous")
}
return actor, write, scope, "", true
}
if r.Header.Get("Authorization") == "" {
mw.SetDAVAuthOutcome(r, "missing")
} else {
mw.SetDAVAuthOutcome(r, "invalid_authorization")
}
}
w.Header().Set("WWW-Authenticate", `Basic realm="PagesWiki CalDAV", charset="UTF-8"`)
w.WriteHeader(401)
return
}
func (s *Server) handleDAVHome(w http.ResponseWriter, r *http.Request) {
domain := s.domainUUIDForRequest(r)
tasks := s.systemExtensionEnabled(domain, "organizer.tasks")
calendars := s.systemExtensionEnabled(domain, "organizer.calendar")
if !tasks && !calendars {
http.NotFound(w, r)
return
}
actor, writable, scope, credential, ok := s.davActor(w, r, "", false)
if !ok {
return
}
s.setDAVCapabilities(w, r, actor, credential)
w.Header().Set("Allow", "OPTIONS, PROPFIND")
if r.Method == "OPTIONS" {
w.WriteHeader(204)
return
}
if r.Method != "PROPFIND" {
w.WriteHeader(405)
return
}
if depth := r.Header.Get("Depth"); depth != "0" && depth != "1" {
http.Error(w, "Depth 0 or 1 required", 403)
return
}
n, err := readDAV(w, r)
if err != nil || n.XMLName.Local != "" && n.XMLName != (xml.Name{Space: davNS, Local: "propfind"}) {
http.Error(w, "invalid PROPFIND", 400)
return
}
var req []davNode
if p := n.child(davNS, "prop"); p != nil {
req = p.Children
}
rows := []davResponse{{href: calendarDAV, props: map[xml.Name]string{
{Space: davNS, Local: "resourcetype"}: "<d:collection/><d:principal/>",
{Space: davNS, Local: "displayname"}: "PagesWiki CalDAV",
{Space: davNS, Local: "current-user-principal"}: "<d:href>" + calendarDAV + "</d:href>",
{Space: davNS, Local: "principal-URL"}: "<d:href>" + calendarDAV + "</d:href>",
{Space: calNS, Local: "calendar-home-set"}: "<d:href>" + calendarDAV + "</d:href>",
}}}
rows[0].props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(false)
if writable && tasks && scope != "calendar" {
rows[0].props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] += "<d:privilege><d:bind/></d:privilege>"
}
if r.Header.Get("Depth") == "1" {
if calendars && scope != "tasks" {
repo, err := s.calendars()
if err != nil {
calendarError(w, err)
return
}
items, err := repo.List(domain, actor)
if err != nil {
calendarError(w, err)
return
}
for _, c := range items {
row := calendarDAVRow(c)
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(writable && c.CanWrite(actor))
rows = append(rows, row)
}
}
if tasks && scope != "calendar" {
row := taskDAVCollectionRow()
title, err := s.store.TaskDAVDefaultTitle(r.Context(), domain, actor)
if err != nil {
taskError(w, err)
return
}
row.props[xml.Name{Space: davNS, Local: "displayname"}] = xmlText(title)
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(writable)
rows = append(rows, row)
if !s.appendTaskDAVCollections(w, r, domain, actor, writable, &rows) {
return
}
}
}
writeDAV(w, rows, req)
}
func davPrivileges(write bool) string {
value := "<d:privilege><d:read/></d:privilege>"
if write {
value += "<d:privilege><d:write/></d:privilege>"
}
return value
}
func (s *Server) handleDAVCredentials(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-store")
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "organizer.tasks") && !s.systemExtensionEnabled(domain, "organizer.calendar") {
http.NotFound(w, r)
return
}
actor, _ := s.calendarActor(r) // Basic DAV secrets cannot issue/revoke credentials.
if actor == "" {
s.authRequired(w, r)
return
}
parts := []string{"dav-credentials"}
if r.URL.Path != davCredentialsAPI {
parts = append(parts, strings.Split(strings.TrimPrefix(r.URL.Path, davCredentialsAPI+"/"), "/")...)
}
s.handleTaskDAVCredentials(w, r, actor, parts)
}
+71 -31
View File
@@ -4,21 +4,47 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
var diagramSourceCache = map[string]diagramCacheEntry{}
var diagramSourceMu sync.Mutex
type diagramCacheEntry struct {
Type string
Source string
At time.Time
Domain string
Path string
}
func (s *Server) diagramCachePath(kind, hash string) string {
root := s.diagramRoot
if root == "" {
root = "/data/cache/diagram"
}
return filepath.Join(root, kind, hash+".svg")
}
func readDiagramCache(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
b, err := io.ReadAll(io.LimitReader(f, (4<<20)+1))
if len(b) > 4<<20 {
return nil, errors.New("diagram cache exceeds limit")
}
return b, err
}
func diagramHash(
@@ -81,6 +107,8 @@ func krokiURL() string {
}
func (s *Server) renderDiagramsInHTML(
r *http.Request,
path string,
html string,
) string {
@@ -109,20 +137,35 @@ func (s *Server) renderDiagramsInHTML(
htmlUnescapeString(
m[2],
)
if len(source) > 32<<10 || path == "" {
return block
}
hash :=
diagramHash(
diagramType,
source,
htmlProjectionVersion+"\x00"+s.domainUUIDForRequest(r)+"\x00"+path+"\x00"+source,
"svg",
)
diagramSourceMu.Lock()
if len(diagramSourceCache) >= 128 {
oldest, key := time.Now(), ""
for k, v := range diagramSourceCache {
if v.At.Before(oldest) {
oldest, key = v.At, k
}
}
delete(diagramSourceCache, key)
}
diagramSourceCache[hash] =
diagramCacheEntry{
Type: diagramType,
Source: source,
At: time.Now(),
Domain: s.domainUUIDForRequest(r), Path: path,
}
diagramSourceMu.Unlock()
return `<figure class="diagram diagram-` + diagramType + `">` +
`<img src="/:/compile/diagram/` + diagramType + `/` + hash + `.svg" loading="lazy" alt="` + diagramType + ` diagram">` +
@@ -153,6 +196,10 @@ func (s *Server) handleCompileDiagram(
w http.ResponseWriter,
r *http.Request,
) {
// Apply also to disk-cache hits and direct navigation, not only new renders.
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Cache-Control", "private, no-store")
if r.Method == http.MethodPost {
s.handleCompileDiagramPost(
@@ -208,14 +255,16 @@ func (s *Server) handleCompileDiagram(
return
}
cachePath :=
filepath.Join(
"/data/cache/diagram",
diagramType,
hash+".svg",
)
cachePath := s.diagramCachePath(diagramType, hash)
if b, err := os.ReadFile(cachePath); err == nil {
diagramSourceMu.Lock()
entry, ok := diagramSourceCache[hash]
diagramSourceMu.Unlock()
if !ok || entry.Type != diagramType || entry.Domain != s.domainUUIDForRequest(r) || time.Since(entry.At) > 30*time.Minute || !s.pathReadFilter(r)(entry.Path) {
http.NotFound(w, r)
return
}
if b, err := readDiagramCache(cachePath); err == nil {
w.Header().Set(
"Content-Type",
@@ -226,16 +275,9 @@ func (s *Server) handleCompileDiagram(
return
}
entry, ok :=
diagramSourceCache[hash]
if !ok || entry.Type != diagramType {
http.NotFound(w, r)
return
}
s.renderDiagramSVG(
w,
r,
diagramType,
entry.Source,
cachePath,
@@ -269,12 +311,12 @@ func (s *Server) handleCompileDiagramPost(
io.ReadAll(
io.LimitReader(
r.Body,
1024*1024,
(1<<20)+1,
),
)
if err != nil {
http.Error(w, err.Error(), 500)
if err != nil || len(sourceBytes) > 1<<20 {
http.Error(w, "diagram source exceeds limit", http.StatusRequestEntityTooLarge)
return
}
@@ -288,14 +330,9 @@ func (s *Server) handleCompileDiagramPost(
"svg",
)
cachePath :=
filepath.Join(
"/data/cache/diagram",
diagramType,
hash+".svg",
)
cachePath := s.diagramCachePath(diagramType, hash)
if b, err := os.ReadFile(cachePath); err == nil {
if b, err := readDiagramCache(cachePath); err == nil {
w.Header().Set(
"Content-Type",
@@ -308,6 +345,7 @@ func (s *Server) handleCompileDiagramPost(
s.renderDiagramSVG(
w,
r,
diagramType,
source,
cachePath,
@@ -316,13 +354,15 @@ func (s *Server) handleCompileDiagramPost(
func (s *Server) renderDiagramSVG(
w http.ResponseWriter,
r *http.Request,
diagramType string,
source string,
cachePath string,
) {
req, err :=
http.NewRequest(
http.NewRequestWithContext(
r.Context(),
http.MethodPost,
krokiURL()+"/"+diagramType+"/svg",
bytes.NewBufferString(source),
@@ -364,11 +404,11 @@ func (s *Server) renderDiagramSVG(
b, err :=
io.ReadAll(
resp.Body,
io.LimitReader(resp.Body, (4<<20)+1),
)
if err != nil {
http.Error(w, err.Error(), 500)
if err != nil || len(b) > 4<<20 {
http.Error(w, "diagram output unavailable or exceeds limit", http.StatusBadGateway)
return
}
@@ -392,7 +432,7 @@ func (s *Server) renderDiagramSVG(
w.Header().Set(
"Content-Security-Policy",
"default-src 'none'; style-src 'unsafe-inline'; img-src data:",
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
)
_, _ =
@@ -0,0 +1,91 @@
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, `<svg xmlns="http://www.w3.org/2000/svg"><script>SVG_SENTINEL</script><text>diagram</text></svg>`)
}))
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 := `<pre><code class="language-mermaid">graph LR; A--&gt;B;</code></pre>`
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, `<pre><code class="language-mermaid">`+strings.Repeat("A", 33<<10)+`</code></pre>`, "/guide", true), "/:/compile/diagram/") {
t.Fatal("oversized diagram enqueued")
}
}
-44
View File
@@ -1,44 +0,0 @@
package server
import (
"net/http"
"pageswiki/internal/store"
)
func (s *Server) currentDistrict() *store.District {
sp, err :=
s.store.GetDefaultDistrict()
if err != nil {
return &store.District{
ID: 1,
Name: "default",
Title: "Default District",
Enabled: true,
}
}
return sp
}
func (s *Server) districtForRequest(
r *http.Request,
) *store.District {
if r == nil {
return s.currentDistrict()
}
sp, err :=
s.store.GetDistrictByHost(
r.Host,
)
if err != nil {
return s.currentDistrict()
}
return sp
}
+17
View File
@@ -0,0 +1,17 @@
package server
func (s *Server) getDomainConfig(key string, domainUUID ...string) string {
var (
value string
err error
)
if len(domainUUID) > 0 && domainUUID[0] != "" {
value, err = s.store.GetConfigInDomain(domainUUID[0], key)
} else {
value, err = s.store.GetConfig(key)
}
if err != nil {
return ""
}
return value
}
+121
View File
@@ -0,0 +1,121 @@
package server
import (
"context"
"log"
"net/http"
"sync"
"pageswiki/internal/store"
)
type requestDomainKey struct{}
type requestDomainLookup struct {
once sync.Once
host string
domain *store.Domain
err error
}
func cacheRequestDomain(r *http.Request) *http.Request {
return r.WithContext(context.WithValue(r.Context(), requestDomainKey{}, &requestDomainLookup{host: r.Host}))
}
func (s *Server) currentDomain() *store.Domain {
sp, err :=
s.store.GetDefaultDomain()
if err != nil {
return &store.Domain{
ID: 1,
Name: "default",
Title: "Default Domain",
Enabled: true,
}
}
return sp
}
func (s *Server) currentCampus() *store.Campus {
campus, err :=
s.store.GetDefaultCampus()
if err != nil {
return &store.Campus{
ID: 1,
Name: "default",
Title: "Default Campus",
DefaultLang: "en",
Enabled: true,
}
}
return campus
}
func (s *Server) campusForDomain(
domain *store.Domain,
) *store.Campus {
if domain == nil {
return s.currentCampus()
}
campus, err :=
s.store.GetCampusByUUID(
domain.CampusUUID,
)
if err != nil {
return s.currentCampus()
}
return campus
}
func (s *Server) domainForRequest(
r *http.Request,
) *store.Domain {
domain, _ := s.domainForRequestWithError(r)
return domain
}
func (s *Server) domainForRequestWithError(
r *http.Request,
) (*store.Domain, error) {
if r == nil {
return s.currentDomain(), nil
}
if cached, ok := r.Context().Value(requestDomainKey{}).(*requestDomainLookup); ok && cached.host == r.Host {
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHost(r.Host) })
return cached.domain, cached.err
}
sp, err :=
s.store.GetDomainByHost(
r.Host,
)
if err != nil {
return nil, err
}
return sp, nil
}
func logDomainLookupError(r *http.Request, err error) {
log.Printf(
"domain lookup failed host=%q: %v",
r.Host,
err,
)
}
func (s *Server) domainUUIDForRequest(r *http.Request) string {
domain := s.domainForRequest(r)
if domain == nil {
return ""
}
return domain.UUID
}
@@ -0,0 +1,62 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestDomainEntryDialogChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
for _, theme := range []string{"light", "nanoha-dark"} {
for _, width := range []string{"1440", "390"} {
t.Run(theme+"-"+width, func(t *testing.T) {
s, token, domain := configFixture(t)
if err := s.store.SetConfigInDomain(domain, "theme", theme); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/domain-entry-dialog.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 == "/-/sys/config" {
rec := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
screenshot := ""
if dir := os.Getenv("PAGESWIKI_PERIODICAL_SCREENSHOT_DIR"); dir != "" {
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
screenshot = filepath.Join(dir, "domain-entry-"+theme+"-"+width+".png")
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/-/sys/config?panel=domains", t.TempDir(), screenshot, width).CombinedOutput()
if err != nil {
t.Fatalf("browser %v %s", err, out)
}
})
}
}
}
+54
View File
@@ -0,0 +1,54 @@
package server
import (
"context"
"net/url"
"pageswiki/internal/store"
"strconv"
"strings"
"testing"
)
func TestDomainEntryConfig(t *testing.T) {
s, token, domain, actor := periodicalFixture(t)
var id int64
if err := s.store.DB.QueryRow(`SELECT id FROM domains WHERE uuid=?`, domain).Scan(&id); err != nil {
t.Fatal(err)
}
form := url.Values{"section": {"domain-entry"}, "domain_id": {strconv.FormatInt(id, 10)}, "entry_layout": {"diary"}, "diary_enabled": {"1"}, "diary_prefix": {"/"}, "blog_prefix": {"/blog"}}
w := configRequest(s, token, "POST", "/-/sys/config", form)
if w.Code != 302 {
t.Fatal(w.Code, w.Body.String())
}
if _, err := s.store.DB.Exec(`INSERT INTO federation_instances(uuid,campus_uuid,fqdn,fingerprint,status,last_seen_at,created_at) SELECT 'old-peer',campus_uuid,'old.example.test','sha256:fixture','approved',0,0 FROM domains WHERE uuid=?`, domain); err != nil {
t.Fatal(err)
}
if blocked := configRequest(s, token, "POST", "/-/sys/config", form); blocked.Code != 409 {
t.Fatal("legacy peer not blocked", blocked.Code)
}
if _, err := s.store.DB.Exec(`INSERT INTO federation_instance_capabilities VALUES('old-peer',2)`); err != nil {
t.Fatal(err)
}
if ready := configRequest(s, token, "POST", "/-/sys/config", form); ready.Code != 302 {
t.Fatal("supported peer blocked", ready.Code)
}
if got, _ := s.store.GetConfigInDomain(domain, "home_layout"); got != "diary" {
t.Fatal(got)
}
if w = periodicalRequest(s, token, "POST", "/_/api/v1/extension/content.diary/settings", `{"enabled":false}`); w.Code != 403 {
t.Fatal("override allowed", w.Code)
}
w = configRequest(s, token, "GET", "/-/sys/config?panel=domains", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), "data-domain-entry-open") {
t.Fatal("missing dialog", w.Code)
}
if err := s.store.SetDomainEntry(context.Background(), id, "unrelated", store.DomainEntrySettings{Layout: "classic", DiaryPrefix: "/diary", BlogPrefix: "/blog"}); err == nil {
t.Fatal("unauthorized settings")
}
if _, err := s.store.DB.Exec(`UPDATE campuses SET authority_mode='managed' WHERE uuid=(SELECT campus_uuid FROM domains WHERE uuid=?)`, domain); err != nil {
t.Fatal(err)
}
if err := s.store.SetDomainEntry(context.Background(), id, actor, store.DomainEntrySettings{Layout: "classic", DiaryPrefix: "/diary", BlogPrefix: "/blog"}); err == nil {
t.Fatal("managed settings override")
}
}
+24 -21
View File
@@ -1,13 +1,9 @@
package server
func (s *Server) getEditorKeymap() string {
func (s *Server) getEditorKeymap(domainUUID ...string) string {
v, err :=
s.store.GetConfig(
"editor_keymap",
)
if err != nil || v == "" {
v := s.getDomainConfig("editor_keymap", domainUUID...)
if v == "" {
return "default"
}
@@ -19,30 +15,37 @@ func (s *Server) getEditorKeymap() string {
}
}
func (s *Server) getEditorSyntaxHighlight() bool {
func (s *Server) getEditorSyntaxHighlight(domainUUID ...string) bool {
v, err :=
s.store.GetConfig(
"editor_syntax_highlight",
)
if err != nil || v == "" {
v := s.getDomainConfig("editor_syntax_highlight", domainUUID...)
if v == "" {
return true
}
return v != "false"
}
func (s *Server) getEditorAutocomplete() bool {
func (s *Server) getEditorAutocomplete(domainUUID ...string) bool {
v, err :=
s.store.GetConfig(
"editor_autocomplete",
)
if err != nil || v == "" {
v := s.getDomainConfig("editor_autocomplete", domainUUID...)
if v == "" {
return true
}
return v != "false"
}
func (s *Server) getEditorFormat(domainUUID ...string) string {
v := s.getDomainConfig("editor_format_default", domainUUID...)
if v == "markdown" {
return "markdown"
}
return "html"
}
func (s *Server) getEditorPreviewDefault(domainUUID ...string) string {
if s.getDomainConfig("editor_preview_default", domainUUID...) == "hidden" {
return "hidden"
}
return "visible"
}
+14
View File
@@ -0,0 +1,14 @@
package server
import "strings"
// Both the compact /e/docs and the historic /e/-/docs edit /docs.
func semanticEditorPath(route string) string {
if route == "/e/-" || route == "/e/-/" {
return "/"
}
if strings.HasPrefix(route, "/e/-/") {
return "/" + strings.TrimPrefix(route, "/e/-/")
}
return "/" + strings.TrimPrefix(route, "/e/")
}
+60
View File
@@ -0,0 +1,60 @@
package server
import (
"bytes"
"io"
"mime"
"mime/multipart"
"net/http"
"net/mail"
"strings"
)
func (s *Server) handleEmailHTML(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path)
if err != nil {
http.NotFound(w, r)
return
}
revision, err := s.latestRevisionForRequest(r, objectUUID)
if err != nil {
http.NotFound(w, r)
return
}
htmlBody := extractEmailHTML([]byte(revision.Body))
if htmlBody == "" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Security-Policy",
"default-src 'none'; img-src data: cid: https: http:; style-src 'unsafe-inline'")
w.Header().Set("X-Content-Type-Options", "nosniff")
_, _ = io.WriteString(w, htmlBody)
}
func extractEmailHTML(body []byte) string {
message, err := mail.ReadMessage(bytes.NewReader(body))
if err != nil {
return ""
}
mediaType, params, _ := mime.ParseMediaType(message.Header.Get("Content-Type"))
if mediaType == "text/html" {
return readMailBody(message.Header.Get("Content-Transfer-Encoding"), message.Body)
}
if !strings.HasPrefix(mediaType, "multipart/") || params["boundary"] == "" {
return ""
}
reader := multipart.NewReader(message.Body, params["boundary"])
for {
part, err := reader.NextPart()
if err != nil {
return ""
}
partType, _, _ := mime.ParseMediaType(part.Header.Get("Content-Type"))
if partType == "text/html" {
return readMailBody(part.Header.Get("Content-Transfer-Encoding"), part)
}
}
}
+217
View File
@@ -0,0 +1,217 @@
package server
import (
"net/http"
"strings"
)
type authorizationTargetKind string
const (
targetPath authorizationTargetKind = "path"
targetDomain authorizationTargetKind = "domain"
)
type endpointPolicy struct {
Name string
Capability string
Target authorizationTargetKind
Resolver endpointTargetResolver
}
type endpointTargetResolver string
const (
resolveNone endpointTargetResolver = ""
resolveHandler endpointTargetResolver = "handler"
resolveObjectURL endpointTargetResolver = "object_url"
resolveRevision endpointTargetResolver = "revision_url"
resolveRaw endpointTargetResolver = "raw_address"
resolveText endpointTargetResolver = "text_revision"
resolvePDF endpointTargetResolver = "pdf_revision"
resolvePreview endpointTargetResolver = "preview_document"
resolveQueryPath endpointTargetResolver = "query_path"
resolveViewPath endpointTargetResolver = "view_path"
resolvePrint endpointTargetResolver = "print_address"
resolveDiff endpointTargetResolver = "revision_diff"
)
var endpointPolicies = map[string]endpointPolicy{
"GET /_/api/v1/extension": {Name: "extension.catalog", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"POST /-/sys/config/vim": {Name: "config.vim", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"GET /:/new": {Name: "page.create_form", Capability: "create", Target: targetDomain},
"GET /v/upload": {Name: "upload.form", Capability: "read", Target: targetDomain},
"GET /_/api/v1/object/list": {Name: "object.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/path-picker": {Name: "path_picker.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/search/suggest": {Name: "search.suggest", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/browser/navigation": {Name: "browser.navigation.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"HEAD /_/api/v1/object/list": {Name: "object.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/revisions/list": {Name: "revision.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"HEAD /_/api/v1/revisions/list": {Name: "revision.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/gc/zombies/list": {Name: "gc.list", Capability: "manage", Target: targetDomain},
"POST /:/new": {Name: "page.create", Capability: "create", Target: targetDomain},
"POST /:/compile/markdown": {Name: "render.markdown", Capability: "read", Target: targetDomain},
"POST /_/api/v1/object/create": {Name: "object.create", Capability: "create", Target: targetDomain},
"POST /_/api/v1/object/delete": {Name: "object.delete", Capability: "delete", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/revisions/save": {Name: "revision.save", Capability: "write", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/revisions/restore": {Name: "revision.restore", Capability: "write", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/render/markdown": {Name: "render.markdown", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/tag/add": {Name: "tag.add", Capability: "write", Target: targetDomain},
"POST /_/api/v1/path/bind": {Name: "path.bind", Capability: "link", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/path/rename": {Name: "path.rename", Capability: "rename", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/path/rename-preview": {Name: "path.rename_preview", Capability: "rename", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/path/remove": {Name: "path.remove", Capability: "delete", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/path/copy": {Name: "path.copy", Capability: "create", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/browser/navigation": {Name: "browser.navigation.add", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"PATCH /_/api/v1/browser/navigation": {Name: "browser.navigation.rename", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"DELETE /_/api/v1/browser/navigation": {Name: "browser.navigation.delete", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/gc/zombies/purge": {Name: "gc.purge", Capability: "delete", Target: targetDomain},
"POST /v/upload": {Name: "document.upload", Capability: "upload", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/assets": {Name: "asset.list", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/assets": {Name: "asset.create", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"DELETE /_/api/v1/assets": {Name: "asset.delete", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/notifications": {Name: "notifications.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"GET /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"DELETE /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/notifications/read": {Name: "notifications.read", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"PATCH /_/api/v1/notifications/read": {Name: "notifications.read", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/publication/requests": {Name: "publication.request", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/publication/requests/": {Name: "publication.decide", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
}
var mutationPrefixPolicies = []struct {
Prefix string
Policy endpointPolicy
}{
{Prefix: "/e/", Policy: endpointPolicy{Name: "document.edit", Capability: "write", Target: targetPath}},
{Prefix: "/-/sys/", Policy: endpointPolicy{Name: "system.manage", Capability: "manage", Target: targetDomain}},
{Prefix: "/_/api/v1/event", Policy: endpointPolicy{Name: "event.manage", Capability: "manage", Target: targetDomain}},
{Prefix: "/_/api/v1/queue", Policy: endpointPolicy{Name: "queue.manage", Capability: "manage", Target: targetDomain}},
{Prefix: "/_/api/v1/adapter", Policy: endpointPolicy{Name: "adapter.manage", Capability: "manage", Target: targetDomain}},
{Prefix: "/_/api/v1/publication/requests/", Policy: endpointPolicy{Name: "publication.decide", Capability: "manage", Target: targetDomain, Resolver: resolveHandler}},
{Prefix: "/_/api/v1/city/", Policy: endpointPolicy{Name: "city.mutate", Capability: "write", Target: targetDomain}},
{Prefix: "/_/api/v1/import/", Policy: endpointPolicy{Name: "import.mutate", Capability: "write", Target: targetDomain}},
{Prefix: "/v/import", Policy: endpointPolicy{Name: "import.mutate", Capability: "write", Target: targetDomain}},
{Prefix: "/:/compile/diagram/", Policy: endpointPolicy{Name: "render.diagram", Capability: "read", Target: targetDomain}},
{Prefix: "/l/", Policy: endpointPolicy{Name: "path.link", Capability: "link", Target: targetDomain, Resolver: resolveHandler}},
}
func policyForRequest(r *http.Request) endpointPolicy {
if taskRoute(r.URL.Path) {
return endpointPolicy{Name: "tasks.handler", Capability: "read", Target: targetDomain, Resolver: resolveHandler}
}
if calendarRoute(r.URL.Path) {
return endpointPolicy{Name: "calendar.handler", Capability: "read", Target: targetDomain, Resolver: resolveHandler}
}
if kind, operation := periodicalRoute(r.URL.Path); kind != "" {
if (r.Method == "GET" && operation != "posts") || (r.Method == "POST" && operation != "view" && operation != "plan" && operation != "new") {
capability := "read"
if r.Method == "POST" {
capability = "write"
if operation == "settings" {
capability = "manage"
}
}
return endpointPolicy{Name: "extension." + kind + "." + operation, Capability: capability, Target: targetDomain, Resolver: resolveHandler}
}
return endpointPolicy{Name: "extension.method.undefined", Capability: "deny", Target: targetDomain}
}
if cityGovernanceRoute(r.URL.Path) {
return endpointPolicy{Name: "city.governance", Capability: "manage", Target: targetDomain, Resolver: resolveHandler}
}
cleanPath := strings.TrimSuffix(r.URL.Path, "/")
if policy, ok := endpointPolicies[r.Method+" "+cleanPath]; ok {
return policy
}
// Exact routes are method-closed. This also protects older handlers that do
// not yet carry their own explicit method guard.
for route := range endpointPolicies {
_, declaredPath, ok := strings.Cut(route, " ")
if ok && declaredPath == cleanPath {
return endpointPolicy{Name: "method.undefined", Capability: "deny", Target: targetDomain}
}
}
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
if resolver, capability := readEndpointResolver(r.URL.Path); resolver != resolveNone {
return endpointPolicy{Name: "derived.read", Capability: capability, Target: targetPath, Resolver: resolver}
}
if strings.HasPrefix(r.URL.Path, "/e/") {
return endpointPolicy{Name: "document.editor", Capability: "write", Target: targetPath}
}
if strings.HasPrefix(r.URL.Path, "/-/sys/") ||
strings.HasPrefix(r.URL.Path, "/_/api/v1/event") ||
strings.HasPrefix(r.URL.Path, "/_/api/v1/queue") {
return endpointPolicy{Name: "system.read", Capability: "manage", Target: targetDomain}
}
return endpointPolicy{Name: "content.read", Capability: "read", Target: endpointTargetKind(r.URL.Path)}
}
for _, rule := range mutationPrefixPolicies {
if strings.HasPrefix(r.URL.Path, rule.Prefix) {
return rule.Policy
}
}
if isReservedEndpoint(r.URL.Path) {
return endpointPolicy{Name: "reserved.undefined", Capability: "deny", Target: targetDomain}
}
return endpointPolicy{Name: "document.mutate", Capability: "write", Target: targetPath}
}
func readEndpointResolver(path string) (endpointTargetResolver, string) {
switch {
case path == "/v/children", path == "/v/tree", strings.HasPrefix(path, "/v/tree/"):
return resolveHandler, "read"
case path == "/v/city", strings.HasPrefix(path, "/v/city/"), strings.HasPrefix(path, "/_/api/v1/city/"):
return resolveHandler, "read"
case path == "/v/bookmarks":
return resolveHandler, "read"
case strings.HasPrefix(path, "/_/obj/"):
return resolveHandler, "read"
case strings.HasPrefix(path, "/v/raw/"):
return resolveRaw, "read"
case strings.HasPrefix(path, "/v/text/"):
return resolveText, "read"
case path == "/v/pdf-page":
return resolvePDF, "read"
case strings.HasPrefix(path, "/v/preview-cache/"):
return resolvePreview, "read"
case path == "/v/archive", path == "/v/email-html":
return resolveQueryPath, "read"
case path == "/v/search", path == "/v/tree", strings.HasPrefix(path, "/v/tree/"):
return resolveHandler, "read"
case strings.HasPrefix(path, "/_/view/print/"):
return resolvePrint, "read"
case strings.HasPrefix(path, "/v/preview"), strings.HasPrefix(path, "/v/plain"), strings.HasPrefix(path, "/v/markdown"):
return resolveViewPath, "read"
case strings.HasPrefix(path, "/o/e/"):
return resolveObjectURL, "write"
case strings.HasPrefix(path, "/l/uuid:"):
return resolveObjectURL, "read"
case strings.HasPrefix(path, "/o/r/"), strings.HasPrefix(path, "/o/"):
return resolveObjectURL, "read"
case strings.HasPrefix(path, "/r/history/"):
return resolveObjectURL, "read"
case strings.HasPrefix(path, "/r/view/"):
return resolveRevision, "read"
case strings.HasPrefix(path, "/r/diff/"):
return resolveDiff, "read"
}
return resolveNone, ""
}
func endpointTargetKind(path string) authorizationTargetKind {
if isReservedEndpoint(path) {
return targetDomain
}
return targetPath
}
func isReservedEndpoint(path string) bool {
for _, prefix := range []string{"/_/", "/-/", "/:/", "/o/", "/r/", "/v/", "/l/", "/static/"} {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
+77
View File
@@ -0,0 +1,77 @@
package server
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestPolicyForRequest(t *testing.T) {
tests := []struct {
method, path, capability string
target authorizationTargetKind
}{
{http.MethodGet, "/guide/delete-is-a-title", "read", targetPath},
{http.MethodPost, "/guide/delete-is-a-title", "write", targetPath},
{http.MethodGet, "/e/-/guide", "write", targetPath},
{http.MethodGet, "/:/new", "create", targetDomain},
{http.MethodPost, "/:/new", "create", targetDomain},
{http.MethodGet, "/_/api/v1/object/create", "deny", targetDomain},
{http.MethodPost, "/_/api/v1/path/rename", "rename", targetDomain},
{http.MethodPost, "/_/api/v1/render/markdown", "read", targetDomain},
{http.MethodDelete, "/_/api/v1/assets", "manage", targetDomain},
{http.MethodGet, "/_/api/v1/assets", "manage", targetDomain},
{http.MethodGet, "/_/api/v1/notifications", "read", targetDomain},
{http.MethodPost, "/_/api/v1/notifications/read", "read", targetDomain},
{http.MethodPatch, "/_/api/v1/notifications/read", "read", targetDomain},
{http.MethodPost, "/_/api/v1/publication/requests", "manage", targetDomain},
{http.MethodPost, "/_/api/v1/publication/requests/request-1/decisions", "manage", targetDomain},
{http.MethodPut, "/_/api/v1/assets", "deny", targetDomain},
{http.MethodGet, "/_/api/v1/event/jobs", "manage", targetDomain},
{http.MethodGet, "/_/api/v1/queue", "manage", targetDomain},
{http.MethodPost, "/_/api/v1/event/run", "manage", targetDomain},
{http.MethodPost, "/_/api/v1/not-registered/delete", "deny", targetDomain},
}
for _, tt := range tests {
t.Run(tt.method+" "+tt.path, func(t *testing.T) {
r := httptest.NewRequest(tt.method, tt.path, nil)
got := policyForRequest(r)
if got.Capability != tt.capability || got.Target != tt.target {
t.Fatalf("policy = capability %q target %q; want %q %q", got.Capability, got.Target, tt.capability, tt.target)
}
})
}
}
func TestReadEndpointResolvers(t *testing.T) {
tests := []struct {
path string
resolver endpointTargetResolver
capability string
}{
{"/v/raw/rev:123", resolveRaw, "read"},
{"/v/text/rev:123", resolveText, "read"},
{"/v/pdf-page", resolvePDF, "read"},
{"/v/preview-cache/hash.jpg", resolvePreview, "read"},
{"/v/preview/docs/page", resolveViewPath, "read"},
{"/o/e/uuid:123", resolveObjectURL, "write"},
{"/r/view/revision", resolveRevision, "read"},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, tt.path, nil)
policy := policyForRequest(request)
if policy.Resolver != tt.resolver || policy.Capability != tt.capability {
t.Fatalf("resolver=%q capability=%q; want %q %q", policy.Resolver, policy.Capability, tt.resolver, tt.capability)
}
})
}
}
func TestEndpointPoliciesAreNamed(t *testing.T) {
for route, policy := range endpointPolicies {
if policy.Name == "" || policy.Capability == "" || policy.Target == "" {
t.Errorf("incomplete policy for %s: %#v", route, policy)
}
}
}
+284
View File
@@ -0,0 +1,284 @@
package server
import (
"context"
"errors"
"net/http"
"path/filepath"
"strings"
"pageswiki/internal/store"
)
func (s *Server) authorizeResolvedEndpoint(r *http.Request, principal *store.Principal, policy endpointPolicy) (bool, error) {
domainUUID := s.domainUUIDForRequest(r)
switch policy.Resolver {
case resolveNone:
return false, nil
case resolveHandler:
return true, nil
case resolveRaw:
address := strings.TrimPrefix(r.URL.Path, "/v/raw/")
switch {
case strings.HasPrefix(address, "path:"):
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "path:"))
case strings.HasPrefix(address, "uuid:"):
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "uuid:"))
case strings.HasPrefix(address, "rev:"):
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
case strings.HasPrefix(address, "asset:"):
// Asset HEAD authorization is address-aware and remains in its handler.
return true, nil
default:
return true, store.ErrAccessDenied
}
case resolveText:
address := strings.TrimPrefix(r.URL.Path, "/v/text/")
if !strings.HasPrefix(address, "rev:") {
return true, store.ErrAccessDenied
}
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
case resolvePDF:
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("rev"))
case resolvePreview:
name := filepath.Base(strings.TrimPrefix(r.URL.Path, "/v/preview-cache/"))
hash := strings.TrimSuffix(strings.TrimSuffix(name, ".jpg"), ".svg")
return true, s.authorizePrincipalDocument(domainUUID, principal.UUID, policy.Capability, hash)
case resolveQueryPath:
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("path"))
case resolveViewPath:
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, semanticViewPath(r.URL.Path))
case resolvePrint:
address := strings.TrimPrefix(r.URL.Path, "/_/view/print/")
return true, s.authorizePrincipalAddress(domainUUID, principal.UUID, policy.Capability, address)
case resolveObjectURL:
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, objectIDFromRoute(r.URL.Path))
case resolveRevision:
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(r.URL.Path, "/r/view/"))
case resolveDiff:
parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/r/diff/"), "/", 2)
if len(parts) != 2 {
return true, store.ErrAccessDenied
}
if err := s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[0]); err != nil {
return true, err
}
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[1])
default:
return true, store.ErrAccessDenied
}
}
func (s *Server) authorizePrincipalAddress(domainUUID, principalUUID, capability, address string) error {
switch {
case strings.HasPrefix(address, "path:"):
return s.authorizePrincipalPath(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "path:"))
case strings.HasPrefix(address, "uuid:"):
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "uuid:"))
case strings.HasPrefix(address, "rev:"):
return s.authorizePrincipalRevision(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "rev:"))
default:
return store.ErrAccessDenied
}
}
func (s *Server) requestCanReadPath(r *http.Request, path string) bool {
return s.pathReadFilter(r)(path)
}
func (s *Server) authorizeObjectForRequest(r *http.Request, capability, objectUUID string) error {
domainUUID := s.domainUUIDForRequest(r)
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
if err != nil {
return err
}
if mode != "enforcing" {
return nil
}
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if err != nil {
return store.ErrAccessDenied
}
return s.authorizePrincipalObject(domainUUID, principal.UUID, capability, objectUUID)
}
func normalizeAuthorizationPath(value string) string {
value = strings.TrimSpace(value)
if value == "" || !strings.HasPrefix(value, "/") {
return ""
}
if value != "/" {
value = strings.TrimRight(value, "/")
}
return value
}
// authorizedMutationSnapshot returns exactly the bindings whose source and
// destination grants were checked. The mutation must pass this opaque snapshot
// to the checked store method, never enumerate the tree for a second time.
func (s *Server) authorizedMutationSnapshot(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) (*store.PathMutationSnapshot, error) {
source = normalizeAuthorizationPath(source)
if source == "" {
return nil, store.ErrAccessDenied
}
if destinationCapability != "" {
destination = normalizeAuthorizationPath(destination)
if destination == "" {
return nil, store.ErrAccessDenied
}
}
domainUUID := s.domainUUIDForRequest(r)
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
if err != nil {
return nil, err
}
var principal *store.Principal
if mode == "enforcing" {
principal, err = s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if err != nil {
return nil, store.ErrAccessDenied
}
}
snapshot, err := s.store.SnapshotPathsInDomain(domainUUID, source, recursive)
if err != nil {
// Do not expose the existence/size of a scope whose root is unreadable.
if principal != nil && (errors.Is(err, store.ErrPathNotFound) || errors.Is(err, store.ErrPathMutationTooLarge)) && s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, source) != nil {
return nil, store.ErrAccessDenied
}
return nil, err
}
if principal == nil {
return snapshot, nil
}
for _, sourcePath := range snapshot.Paths() {
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, sourcePath); err != nil {
return nil, err
}
if destinationCapability != "" {
destinationPath := destination + strings.TrimPrefix(sourcePath, snapshot.Root())
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, destinationCapability, destinationPath); err != nil {
return nil, err
}
}
}
return snapshot, nil
}
func (s *Server) authorizePathTreeForRequest(r *http.Request, capability, root string, recursive bool) error {
_, err := s.authorizedMutationSnapshot(r, capability, "", root, "", recursive)
return err
}
func (s *Server) authorizePathMoveForRequest(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) error {
_, err := s.authorizedMutationSnapshot(r, sourceCapability, destinationCapability, source, destination, recursive)
return err
}
func (s *Server) authorizePrincipalDocument(domainUUID, principalUUID, capability, hash string) error {
if hash == "" {
return store.ErrAccessDenied
}
references, err := s.store.DocumentReferencesInDomain(domainUUID, hash)
if err != nil {
return err
}
for _, reference := range references {
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, reference.Path) == nil {
return nil
}
}
return store.ErrAccessDenied
}
func (s *Server) authorizePrincipalPath(domainUUID, principalUUID, capability, path string) error {
if path == "" || !strings.HasPrefix(path, "/") {
return store.ErrAccessDenied
}
if store.IsUserSpacePath(path) {
allowed, err := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, path)
if err != nil || !allowed {
return store.ErrAccessDenied
}
}
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "path", TargetID: path})
}
func (s *Server) authorizePrincipalObject(domainUUID, principalUUID, capability, objectUUID string) error {
if objectUUID == "" {
return store.ErrAccessDenied
}
request := store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "object", TargetID: objectUUID}
inScope, err := s.store.ObjectInDomainAuthorityScope(domainUUID, objectUUID)
if err != nil {
return err
}
if !inScope {
return s.store.AuthorizeExactObject(request)
}
links, err := s.store.ListPathLinksInDomain(domainUUID, objectUUID)
if err != nil {
return err
}
for _, link := range links {
if !store.IsUserSpacePath(link.Path) {
continue
}
allowed, audienceErr := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, link.Path)
if audienceErr != nil || !allowed {
return store.ErrAccessDenied
}
}
if err := s.store.CheckObjectLocalityAccessCaps(domainUUID, principalUUID, capability, objectUUID); err != nil {
return err
}
if err := s.store.Authorize(request); err == nil {
return nil
} else if !errors.Is(err, store.ErrAccessDenied) {
return err
}
if !s.store.ObjectBoundInDomain(domainUUID, objectUUID) {
return store.ErrAccessDenied
}
for _, link := range links {
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, link.Path) == nil {
return nil
}
}
return store.ErrAccessDenied
}
func (s *Server) authorizePrincipalRevision(domainUUID, principalUUID, capability, revisionUUID string) error {
if revisionUUID == "" {
return store.ErrAccessDenied
}
revision, err := s.store.GetRevisionMetadata(revisionUUID)
if err != nil {
return store.ErrAccessDenied
}
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, revision.ObjectUUID)
}
func objectIDFromRoute(path string) string {
for _, prefix := range []string{"/o/e/uuid:", "/o/r/uuid:", "/o/uuid:", "/l/uuid:", "/r/history/"} {
if strings.HasPrefix(path, prefix) {
return strings.TrimPrefix(path, prefix)
}
}
return ""
}
func semanticViewPath(route string) string {
for _, prefix := range []string{"/v/preview", "/v/plain", "/v/markdown", "/v/browser"} {
if strings.HasPrefix(route, prefix) {
path := strings.TrimPrefix(route, prefix)
if path == "" {
return "/"
}
if !strings.HasPrefix(path, "/") {
return "/" + path
}
return path
}
}
return ""
}
+88
View File
@@ -0,0 +1,88 @@
package server
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"pageswiki/internal/db"
"pageswiki/internal/store"
)
func TestObjectAndRevisionAuthorizationFollowReadableBindings(t *testing.T) {
database, err := db.Open(t.TempDir() + "/endpoint-target.db")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = database.Close() })
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.New(database)
if err := st.EnsureConfigTable(); err != nil {
t.Fatal(err)
}
domain, err := st.GetDefaultDomain()
if err != nil {
t.Fatal(err)
}
principal, err := st.BootstrapOwner(domain.UUID, "scoped", "correct horse battery staple", "Scoped", "")
if err != nil {
t.Fatal(err)
}
var roleUUID string
if err := database.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.UUID, principal.UUID).Scan(&roleUUID); err != nil {
t.Fatal(err)
}
if _, err := database.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, roleUUID); err != nil {
t.Fatal(err)
}
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-read',?,'read','path','/allowed',1)`, roleUUID); err != nil {
t.Fatal(err)
}
allowedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed")
if err != nil {
t.Fatal(err)
}
allowedRevision, err := st.SaveRevision(allowedObject, "allowed", "test")
if err != nil {
t.Fatal(err)
}
deniedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/denied")
if err != nil {
t.Fatal(err)
}
server := &Server{store: st}
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", allowedObject); err != nil {
t.Fatalf("object through readable binding: %v", err)
}
if err := server.authorizePrincipalRevision(domain.UUID, principal.UUID, "read", allowedRevision); err != nil {
t.Fatalf("revision through readable binding: %v", err)
}
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", deniedObject); !errors.Is(err, store.ErrAccessDenied) {
t.Fatalf("unreadable object authorization = %v", err)
}
if _, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed/child"); err != nil {
t.Fatal(err)
}
token, err := st.CreateAuthSession(domain.UUID, principal.UUID)
if err != nil {
t.Fatal(err)
}
if err := st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodPost, "http://localhost/_/api/v1/path/copy", nil)
request.Header.Set("Authorization", "Bearer "+token)
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); !errors.Is(err, store.ErrAccessDenied) {
t.Fatalf("recursive operation crossed an ungranted child: %v", err)
}
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-child-read',?,'read','path','/allowed/child',1)`, roleUUID); err != nil {
t.Fatal(err)
}
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); err != nil {
t.Fatalf("fully granted recursive operation: %v", err)
}
}
+296
View File
@@ -0,0 +1,296 @@
package server
import (
"encoding/json"
"errors"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"pageswiki/internal/store"
)
func writeEventJSON(w http.ResponseWriter, status int, value any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(value)
}
func decodeEventJSON(w http.ResponseWriter, r *http.Request, value any) bool {
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
if err := json.NewDecoder(r.Body).Decode(value); err != nil {
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return false
}
return true
}
func (s *Server) handleEventAPI(w http.ResponseWriter, r *http.Request) {
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/event"), "/")
switch action {
case "":
if r.Method == http.MethodGet {
definitions, err := s.store.ListEventDefinitions()
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"events": definitions})
return
}
if r.Method == http.MethodPost {
var definition store.EventDefinition
if !decodeEventJSON(w, r, &definition) {
return
}
if err := s.store.SaveEventDefinition(&definition); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
s.wakeEventScheduler()
writeEventJSON(w, http.StatusCreated, definition)
return
}
case "run":
if r.Method == http.MethodPost {
var input struct {
UUID string `json:"uuid"`
}
if !decodeEventJSON(w, r, &input) {
return
}
queued, err := s.store.RunEventDefinition(input.UUID)
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
s.wakeEventScheduler()
writeEventJSON(w, 202, map[string]any{"queued": queued})
return
}
case "delete":
if r.Method == http.MethodPost || r.Method == http.MethodDelete {
var input struct {
UUID string `json:"uuid"`
}
if !decodeEventJSON(w, r, &input) {
return
}
if err := s.store.DeleteEventDefinition(input.UUID); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]bool{"deleted": true})
return
}
case "logs":
if r.Method == http.MethodGet {
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
before, err := strconv.ParseInt(r.URL.Query().Get("before_id"), 10, 64)
if r.URL.Query().Has("before_id") && (err != nil || before < 0) {
writeEventJSON(w, 400, map[string]string{"error": "invalid log cursor"})
return
}
logs, err := s.store.ListEventLogsBefore(r.URL.Query().Get("job_uuid"), limit, before)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"logs": logs})
return
}
case "logs/trends":
if r.Method == http.MethodGet {
hours, _ := strconv.Atoi(r.URL.Query().Get("hours"))
if hours < 1 || hours > 24*365 {
hours = 24
}
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
trends, err := s.store.ListEventLogTrends(r.Context(), r.URL.Query().Get("event_type"), time.Now().Add(-time.Duration(hours)*time.Hour).UnixMilli(), limit)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"trends": trends, "hours": hours})
return
}
case "policies":
if r.Method == http.MethodGet {
policies, err := s.store.ListEventLogPolicies()
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"policies": policies})
return
}
if r.Method == http.MethodPost || r.Method == http.MethodPut {
var policy store.EventLogPolicy
if !decodeEventJSON(w, r, &policy) {
return
}
if err := s.store.SetEventLogPolicy(policy); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, policy)
return
}
}
w.Header().Set("Allow", "GET, POST, DELETE")
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported event operation"})
}
func (s *Server) handleQueueAPI(w http.ResponseWriter, r *http.Request) {
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/queue"), "/")
switch action {
case "jobs":
if r.Method == http.MethodGet {
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
jobs, next, err := s.store.EventJobPage(r.Context(), r.URL.Query().Get("status"), r.URL.Query().Get("cursor"), limit)
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, store.ErrEventJobFilter) {
code = http.StatusBadRequest
}
writeEventJSON(w, code, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{"jobs": jobs, "next_cursor": next})
return
}
case "":
if r.Method == http.MethodGet {
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
jobs, err := s.store.ListEventJobs(limit)
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
stats, err := s.store.EventStats()
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
settings, err := s.store.GetQueueSettings()
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, map[string]any{
"stats": stats, "settings": settings, "jobs": jobs,
"active_workers": s.schedulerRunning.Load(),
})
return
}
case "settings":
if r.Method == http.MethodGet {
settings, err := s.store.GetQueueSettings()
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
return
}
writeEventJSON(w, 200, settings)
return
}
if r.Method == http.MethodPost || r.Method == http.MethodPut {
var settings store.QueueSettings
if !decodeEventJSON(w, r, &settings) {
return
}
if err := s.store.SetQueueSettings(settings); err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
s.wakeEventScheduler()
writeEventJSON(w, 200, settings)
return
}
case "cancel", "retry":
if r.Method == http.MethodPost {
var input struct {
UUID string `json:"uuid"`
}
if !decodeEventJSON(w, r, &input) {
return
}
var err error
if action == "cancel" {
err = s.store.CancelEventJob(input.UUID)
} else {
err = s.store.RetryEventJob(input.UUID)
}
if err != nil {
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
return
}
s.wakeEventScheduler()
writeEventJSON(w, 200, map[string]bool{"ok": true})
return
}
case "resources":
if r.Method == http.MethodGet {
writeEventJSON(w, 200, s.resourceStatus())
return
}
case "resources/history":
if r.Method == http.MethodGet {
hours, _ := strconv.Atoi(r.URL.Query().Get("hours"))
if hours != 1 && hours != 24 && hours != 168 {
hours = 24
}
samples, err := s.store.ResourceHistory(r.Context(), hours, time.Now())
if err != nil {
writeEventJSON(w, 500, map[string]string{"error": "could not load resource history"})
return
}
writeEventJSON(w, 200, map[string]any{"samples": samples, "hours": hours, "retention_days": 7})
return
}
}
w.Header().Set("Allow", "GET, POST, PUT")
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported queue operation"})
}
func (s *Server) resourceStatus() map[string]any {
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
root := s.store.DocumentRoot
if root == "" {
root = "."
}
root, _ = filepath.Abs(root)
var fs syscall.Statfs_t
_ = syscall.Statfs(root, &fs)
total := fs.Blocks * uint64(fs.Bsize)
available := fs.Bavail * uint64(fs.Bsize)
loadAverage := ""
if data, err := os.ReadFile("/proc/loadavg"); err == nil {
fields := strings.Fields(string(data))
if len(fields) >= 3 {
loadAverage = strings.Join(fields[:3], " ")
}
}
stats, _ := s.store.ActiveEventStats()
settings, _ := s.store.GetQueueSettings()
return map[string]any{
"timestamp_ms": time.Now().UnixMilli(),
"io": readProcessIO(),
"cpu": map[string]any{"logical_cpus": runtime.NumCPU(), "load_average": loadAverage},
"memory": map[string]any{
"heap_bytes": memory.HeapAlloc, "go_system_bytes": memory.Sys,
"gc_cycles": memory.NumGC, "goroutines": runtime.NumGoroutine(),
},
"storage": map[string]any{
"path": root, "total_bytes": total, "used_bytes": total - available,
"available_bytes": available,
},
"queue": map[string]any{"stats": map[string]int{"queued": stats.Queued, "running": stats.Running}, "active_workers": s.schedulerRunning.Load(), "paused": settings.Paused},
}
}
+641
View File
@@ -0,0 +1,641 @@
package server
import (
"archive/zip"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"html"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"pageswiki/internal/store"
)
const previewEventType = "preview.generate"
const previewSweepEventType = "preview.sweep"
const cityReconcileEventType = "city.reconcile"
var cityActivitySubscription = store.ActivitySubscription{ConsumerID: "city", EventTypes: []string{"page.created", "revision.created", "path.renamed", "path.unlinked", "object.deleted"}}
type previewEventPayload struct {
RevisionUUID string `json:"revision_uuid"`
DocumentHash string `json:"document_hash"`
ContentType string `json:"content_type"`
Filename string `json:"filename"`
Kind string `json:"kind"`
Page int `json:"page,omitempty"`
}
type cityReconcilePayload struct {
DomainUUID string `json:"domain_uuid"`
}
func (s *Server) initializePreviewScheduler() {
if s.store == nil || s.store.DocumentRoot == "" {
return
}
s.previewRoot = filepath.Join(filepath.Dir(s.store.DocumentRoot), "previews")
if err := os.MkdirAll(s.previewRoot, 0755); err != nil {
log.Printf("preview cache initialization failed: %v", err)
s.previewRoot = ""
}
}
func (s *Server) startEventScheduler() {
if s.store == nil {
return
}
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
log.Printf("resource monitor initialization failed: %v", err)
}
if err := s.ensureCalendarSweep(); err != nil {
log.Printf("calendar scheduler initialization failed: %v", err)
}
if err := s.store.EnsureTaskActivityBridgeEvent(); err != nil {
log.Printf("task journal bridge initialization failed: %v", err)
}
if err := s.store.EnsureTaskReminderEvent(); err != nil {
log.Printf("task reminder initialization failed: %v", err)
}
if err := s.store.EnsureTaskFocusEvent(); err != nil {
log.Printf("focus timer initialization failed: %v", err)
}
if err := s.store.EnsureNotificationPushEvent(); err != nil {
log.Printf("notification push initialization failed: %v", err)
}
go func() {
initialPreviewSweep := time.NewTimer(5 * time.Second)
defer initialPreviewSweep.Stop()
previewSweep := time.NewTicker(15 * time.Minute)
defer previewSweep.Stop()
dispatch := time.NewTicker(time.Second)
defer dispatch.Stop()
cleanup := time.NewTicker(time.Hour)
defer cleanup.Stop()
activitySweep := time.NewTicker(10 * time.Second)
defer activitySweep.Stop()
for {
select {
case <-initialPreviewSweep.C:
s.enqueueAllPreviewEvents()
case <-previewSweep.C:
s.enqueueAllPreviewEvents()
case <-dispatch.C:
case <-cleanup.C:
if settings, err := s.store.GetQueueSettings(); err == nil {
if err := s.store.PurgeEventHistory(settings.RetentionHours); err != nil {
log.Printf("event history cleanup failed: %v", err)
}
}
case <-activitySweep.C:
if _, err := s.store.ExpireApprovalRequests(context.Background(), time.Now().UnixMilli(), 100); err != nil {
log.Printf("approval request expiry failed: %v", err)
}
s.enqueueCityActivityEvents()
case <-s.schedulerWake:
if _, err := s.store.ExpireApprovalRequests(context.Background(), time.Now().UnixMilli(), 100); err != nil {
log.Printf("approval request expiry failed: %v", err)
}
s.enqueueCityActivityEvents()
}
if _, err := s.store.EnqueueDueDefinitions(time.Now()); err != nil {
log.Printf("event schedule evaluation failed: %v", err)
}
s.runDueEvents()
}
}()
}
func (s *Server) wakeEventScheduler() {
if s.schedulerWake == nil {
return
}
select {
case s.schedulerWake <- struct{}{}:
default:
}
}
func (s *Server) runDueEvents() {
for {
settings, err := s.store.GetQueueSettings()
if err != nil {
log.Printf("event scheduler settings failed: %v", err)
return
}
if settings.Paused || s.schedulerRunning.Load() >= int64(settings.Concurrency) {
return
}
// Keep one reserved background slot while foreground work is active.
// A global stop lets long-lived editor/WebSocket requests starve jobs.
if s.foregroundRequests.Load() > 0 && s.schedulerRunning.Load() > 0 {
return
}
job, err := s.store.ClaimDueEvent()
if errors.Is(err, sql.ErrNoRows) || errors.Is(err, context.Canceled) {
return
}
if err != nil {
log.Printf("event scheduler claim failed: %v", err)
return
}
s.schedulerRunning.Add(1)
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", "execution started")
go func(job *store.EventJob) {
defer func() {
s.schedulerRunning.Add(-1)
s.wakeEventScheduler()
}()
var runErr error
if s.store.EventCancellationRequested(job.UUID) {
runErr = errors.New("cancelled before execution")
} else {
runErr = s.runEvent(job)
if runErr == nil && s.store.EventCancellationRequested(job.UUID) {
runErr = errors.New("cancelled during execution")
}
}
if finishErr := s.store.FinishEvent(job.UUID, runErr); finishErr != nil {
log.Printf("event scheduler finish failed job=%s: %v", job.UUID, finishErr)
}
if runErr != nil {
log.Printf("event job failed type=%s key=%s: %v", job.Type, job.Key, runErr)
}
}(job)
}
}
func (s *Server) runEvent(job *store.EventJob) error {
switch job.Type {
case store.NotificationPushEventType:
return s.deliverNotificationPush(job)
case store.TaskFocusEventType:
processed, err := s.store.CompleteTaskFocus(context.Background(), time.Now(), 100)
if err == nil && processed > 0 {
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("focus timers completed=%d", processed))
}
return err
case store.TaskReminderEventType:
checked, sent, err := s.store.DeliverTaskReminders(context.Background(), time.Now(), 100)
if err == nil && checked > 0 {
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("task reminders checked=%d delivered=%d", checked, sent))
}
return err
case store.TaskActivityBridgeEventType:
processed, published, err := s.store.PublishTaskActivity(context.Background(), 100)
if err == nil && processed > 0 {
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("journal entries checked=%d published=%d", processed, published))
}
return err
case calendarSweepType:
return s.sweepCalendarSources()
case calendarSyncType:
return s.runCalendarSource(job)
case metadataProjectionEventType:
var payload metadataProjectionPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generateMetadataProjection(payload)
case store.ResourceEventType:
snapshot := s.resourceStatus()
payload, err := json.Marshal(snapshot)
if err != nil {
return err
}
return s.store.SaveResourceSample(job.UUID, snapshot["timestamp_ms"].(int64), payload)
case previewSweepEventType:
s.enqueueAllPreviewEvents()
return nil
case previewEventType:
var payload previewEventPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generatePreview(payload)
case cityReconcileEventType:
var payload cityReconcilePayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.reconcileCityActivity(payload.DomainUUID)
case "pdf.page.generate":
var payload previewEventPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generatePDFPage(payload)
case importAnalyzeEventType:
var payload importAnalyzePayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.analyzeImport(payload)
case importCommitEventType:
var payload importCommitPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.commitImport(payload)
default:
return fmt.Errorf("unknown event type %q", job.Type)
}
}
func (s *Server) enqueueCityActivityEvents() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
domains, err := s.store.PendingActivityConsumerDomains(ctx, cityActivitySubscription, 100)
if err != nil {
log.Printf("activity outbox domain scan failed: %v", err)
return
}
for _, domain := range domains {
payload, err := json.Marshal(cityReconcilePayload{DomainUUID: domain})
if err != nil {
continue
}
_, err = s.store.EnqueueEventWithOptions("", cityReconcileEventType, "city:"+domain, string(payload), time.Now(), -2, 3)
if err != nil {
log.Printf("city activity enqueue failed domain=%s: %v", domain, err)
}
}
}
func (s *Server) reconcileCityActivity(domainUUID string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
events, err := s.store.ActivityOutboxForConsumer(ctx, cityActivitySubscription, domainUUID, 500)
if err != nil {
return err
}
if len(events) == 0 {
return nil
}
if _, err := s.applyCityAutoPlan(domainUUID, "system:city"); err != nil {
return err
}
return s.store.CompleteActivityOutboxThrough("city", domainUUID, events[len(events)-1].Sequence)
}
func (s *Server) generatePDFPage(payload previewEventPayload) error {
if payload.Page < 1 || payload.DocumentHash == "" {
return fmt.Errorf("invalid PDF page job")
}
target := filepath.Join(s.previewRoot, fmt.Sprintf("%s-p%d.jpg", payload.DocumentHash, payload.Page))
if info, err := os.Stat(target); err == nil && info.Size() > 0 {
return nil
}
source, err := s.store.DocumentPath(payload.DocumentHash)
if err != nil {
return err
}
workDir, err := os.MkdirTemp(s.previewRoot, ".pdf-page-*")
if err != nil {
return err
}
defer os.RemoveAll(workDir)
base := filepath.Join(workDir, "page")
if err := runPreviewCommand("pdftoppm", "-f", strconv.Itoa(payload.Page), "-l", strconv.Itoa(payload.Page), "-singlefile", "-jpeg", "-scale-to", "1600", source, base); err != nil {
return err
}
return os.Rename(base+".jpg", target)
}
func (s *Server) previewCachePath(hash, kind string) string {
extension := ".jpg"
if kind == "excalidraw" {
extension = ".svg"
}
return filepath.Join(s.previewRoot, hash+extension)
}
func (s *Server) previewCacheURL(hash, kind string) string {
extension := ".jpg"
if kind == "excalidraw" {
extension = ".svg"
}
return "/v/preview-cache/" + hash + extension
}
func (s *Server) handlePreviewCache(w http.ResponseWriter, r *http.Request) {
if s.previewRoot == "" {
http.NotFound(w, r)
return
}
name := filepath.Base(strings.TrimPrefix(r.URL.Path, "/v/preview-cache/"))
if name == "." || name == "" ||
(!strings.HasSuffix(name, ".jpg") && !strings.HasSuffix(name, ".svg")) {
http.NotFound(w, r)
return
}
path := filepath.Join(s.previewRoot, name)
if _, err := os.Stat(path); err != nil {
http.NotFound(w, r)
return
}
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("X-Content-Type-Options", "nosniff")
if strings.HasSuffix(name, ".svg") {
w.Header().Set("Content-Security-Policy", "sandbox")
}
http.ServeFile(w, r, path)
}
func (s *Server) enqueuePreview(entry store.BrowserEntry) (string, bool) {
if s.previewRoot == "" || entry.DocumentHash == "" {
return "", false
}
filename := entry.Filename
if filename == "" {
filename = filepath.Base(entry.Path)
}
adapter := selectProjectionAdapter(contentDescriptor{
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
Filename: filename,
})
kind := adapter.ID
if kind != "image" && kind != "pdf" &&
kind != "video" && kind != "excalidraw" && kind != "archive" {
return "", false
}
cachePath := s.previewCachePath(entry.DocumentHash, kind)
cacheURL := s.previewCacheURL(entry.DocumentHash, kind)
if info, err := os.Stat(cachePath); err == nil && info.Size() > 0 {
return cacheURL, true
}
payload, err := json.Marshal(previewEventPayload{
RevisionUUID: entry.RevisionUUID,
DocumentHash: entry.DocumentHash,
ContentType: entry.ContentType,
Filename: filename,
Kind: kind,
})
if err == nil {
if queued, queueErr := s.store.EnqueueEvent(
previewEventType,
entry.DocumentHash,
string(payload),
time.Now(),
); queueErr == nil && queued {
s.wakeEventScheduler()
}
}
return cacheURL, false
}
func (s *Server) enqueueAllPreviewEvents() int {
domains, err := s.store.ListDomains()
if err != nil {
return 0
}
count := 0
for _, domain := range domains {
if !domain.Enabled || domain.Placement != "local" {
continue
}
entries, err := s.store.ListPreviewCandidatesInDomain(domain.UUID)
if err != nil {
continue
}
for _, entry := range entries {
if _, cached := s.enqueuePreview(entry); !cached {
count++
}
}
}
s.wakeEventScheduler()
return count
}
func (s *Server) generatePreview(payload previewEventPayload) error {
target := s.previewCachePath(payload.DocumentHash, payload.Kind)
if info, err := os.Stat(target); err == nil && info.Size() > 0 {
return nil
}
source, err := s.store.DocumentPath(payload.DocumentHash)
if err != nil {
return err
}
workDir, err := os.MkdirTemp(s.previewRoot, ".job-*")
if err != nil {
return err
}
defer os.RemoveAll(workDir)
var generated string
switch payload.Kind {
case "image":
generated = filepath.Join(workDir, "preview.jpg")
err = generateImageThumbnail(source, generated)
case "pdf":
generated = filepath.Join(workDir, "preview.jpg")
err = runPreviewCommand(
"pdftoppm",
"-f", "1", "-singlefile", "-jpeg",
"-scale-to", "480",
source,
strings.TrimSuffix(generated, ".jpg"),
)
case "video":
generated = filepath.Join(workDir, "preview.jpg")
err = runPreviewCommand(
"ffmpeg", "-hide_banner", "-loglevel", "error",
"-ss", "1", "-i", source,
"-frames:v", "1", "-vf", "scale=480:-2",
"-y", generated,
)
case "excalidraw":
generated = filepath.Join(workDir, "preview.svg")
err = generateExcalidrawSVG(source, generated)
case "archive":
generated = filepath.Join(workDir, "preview.jpg")
err = generateArchiveThumbnail(source, generated, payload.Filename)
default:
return fmt.Errorf("unsupported preview kind %q", payload.Kind)
}
if err != nil {
return err
}
if err := os.Chmod(generated, 0644); err != nil {
return err
}
return os.Rename(generated, target)
}
func runPreviewCommand(name string, args ...string) error {
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
command := exec.CommandContext(ctx, "nice", append([]string{"-n", "15", name}, args...)...)
output, err := command.CombinedOutput()
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
return fmt.Errorf("%s failed: %w: %s", name, err, strings.TrimSpace(string(output)))
}
return nil
}
func generateImageThumbnail(source, target string) error {
file, err := os.Open(source)
if err != nil {
return err
}
defer file.Close()
return generateImageThumbnailFromReader(file, target)
}
func generateArchiveThumbnail(source, target, filename string) error {
if strings.EqualFold(filepath.Ext(filename), ".zip") {
return generateZIPThumbnail(source, target)
}
return store.ErrEventSkipped
}
func generateZIPThumbnail(source, target string) error {
reader, err := zip.OpenReader(source)
if err != nil {
return err
}
defer reader.Close()
imageCount, fileCount := 0, 0
for _, file := range reader.File {
if file.FileInfo().IsDir() {
continue
}
fileCount++
if archiveImageName(file.Name) {
imageCount++
}
}
if imageCount < 3 || imageCount*2 < fileCount {
return fmt.Errorf("%w: archive is not image-dominant", store.ErrEventSkipped)
}
for _, file := range reader.File {
if file.FileInfo().IsDir() || !archiveImageName(file.Name) {
continue
}
input, err := file.Open()
if err != nil {
return err
}
err = generateImageThumbnailFromReader(input, target)
input.Close()
if err != nil {
return err
}
return nil
}
return fmt.Errorf("%w: archive has no supported comic images", store.ErrEventSkipped)
}
func archiveImageName(name string) bool {
switch strings.ToLower(filepath.Ext(name)) {
case ".jpg", ".jpeg", ".png", ".gif":
return true
default:
return false
}
}
func generateImageThumbnailFromReader(input io.Reader, target string) error {
decoded, _, err := image.Decode(input)
if err != nil {
return err
}
bounds := decoded.Bounds()
width, height := bounds.Dx(), bounds.Dy()
if width <= 0 || height <= 0 {
return errors.New("invalid image dimensions")
}
scale := min(480.0/float64(width), 300.0/float64(height), 1)
outWidth := max(1, int(float64(width)*scale))
outHeight := max(1, int(float64(height)*scale))
thumbnail := image.NewRGBA(image.Rect(0, 0, outWidth, outHeight))
for y := 0; y < outHeight; y++ {
for x := 0; x < outWidth; x++ {
sourceX := bounds.Min.X + x*width/outWidth
sourceY := bounds.Min.Y + y*height/outHeight
thumbnail.Set(x, y, decoded.At(sourceX, sourceY))
}
}
output, err := os.Create(target)
if err != nil {
return err
}
defer output.Close()
return jpeg.Encode(output, thumbnail, &jpeg.Options{Quality: 78})
}
func generateExcalidrawSVG(source, target string) error {
data, err := os.ReadFile(source)
if err != nil {
return err
}
var scene struct {
Elements []struct {
Type string `json:"type"`
X float64 `json:"x"`
Y float64 `json:"y"`
Width float64 `json:"width"`
Height float64 `json:"height"`
Text string `json:"text"`
StrokeColor string `json:"strokeColor"`
BackgroundColor string `json:"backgroundColor"`
IsDeleted bool `json:"isDeleted"`
} `json:"elements"`
}
if err := json.Unmarshal(data, &scene); err != nil {
return err
}
var body strings.Builder
body.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 500">`)
body.WriteString(`<rect width="100%" height="100%" fill="#fff"/>`)
for _, element := range scene.Elements {
if element.IsDeleted {
continue
}
stroke := element.StrokeColor
if stroke == "" {
stroke = "#1b1b1f"
}
fill := element.BackgroundColor
if fill == "" || fill == "transparent" {
fill = "none"
}
switch element.Type {
case "ellipse":
fmt.Fprintf(&body,
`<ellipse cx="%g" cy="%g" rx="%g" ry="%g" fill="%s" stroke="%s"/>`,
element.X+element.Width/2, element.Y+element.Height/2,
element.Width/2, element.Height/2, html.EscapeString(fill), html.EscapeString(stroke))
case "text":
fmt.Fprintf(&body,
`<text x="%g" y="%g" fill="%s" font-family="sans-serif" font-size="20">%s</text>`,
element.X, element.Y+20, html.EscapeString(stroke), html.EscapeString(element.Text))
default:
fmt.Fprintf(&body,
`<rect x="%g" y="%g" width="%g" height="%g" fill="%s" stroke="%s"/>`,
element.X, element.Y, element.Width, element.Height,
html.EscapeString(fill), html.EscapeString(stroke))
}
}
body.WriteString("</svg>")
return os.WriteFile(target, []byte(body.String()), 0644)
}
Loaded 100 of 699 files, more files were not shown because too many files have changed in this diff. Show more