Refactor architecture into eight components and consolidate reliability work
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s

Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
This commit is contained in:
Anya Tokugawa committed 2026-11-01 15:23:31 +09:00
1 parent 9dd425f765
commit d5d21b7841
302 files changed
+20037 -3492

No files matched your search

+3
View File
@@ -12,6 +12,8 @@ import (
"regexp"
"sort"
"strings"
"pageswiki/internal/kunstzeal"
)
const adapterPluginsConfigKey = "projection_adapter_plugins"
@@ -50,6 +52,7 @@ type builtinAdapterInfo struct {
var adapterIDPattern = regexp.MustCompile(`^[a-z][a-z0-9._-]{1,63}$`)
func builtinAdapterManifest() []builtinAdapterInfo {
projectionAdapters := kunstzeal.Catalog()
out := make([]builtinAdapterInfo, 0, len(projectionAdapters))
for _, adapter := range projectionAdapters {
out = append(out, builtinAdapterInfo{
+2 -2
View File
@@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"
"pageswiki/internal/compiler"
"pageswiki/internal/grammaratur"
"pageswiki/internal/render"
"pageswiki/internal/store"
)
@@ -63,7 +63,7 @@ func (s *Server) workspaceFile(session *store.AdapterSession, semanticPath strin
body := revision.Body
markdown := plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(revision.ContentType), "text/html")
if markdown {
body, err = compiler.HTMLToMarkdown(body)
body, err = grammaratur.HTMLToMarkdown(body)
if err != nil {
return adapterWorkspaceFile{}, err
}
+2 -1
View File
@@ -91,7 +91,7 @@ func (s *Server) tryAnonymousProjection(w http.ResponseWriter, r *http.Request)
}
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)
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
if err != nil {
http.Error(w, "published document unavailable", 503)
return true
@@ -156,6 +156,7 @@ func (s *Server) serveAnonymousRevision(w http.ResponseWriter, r *http.Request,
return true
}
data["Text"] = string(body)
data["CodeLanguage"] = codeLanguage(contentType, filename, string(body))
}
data["SocialMetadata"] = s.socialMetadataForRequest(r, "anonymous_view.gohtml", data)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
+40 -18
View File
@@ -59,17 +59,26 @@ func (s *Server) handleSaveRevisionAPI(
w http.ResponseWriter,
r *http.Request,
) {
var req struct {
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"`
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
api.WriteError(w, 405, "method_not_allowed", "POST required")
return
}
err := json.NewDecoder(r.Body).Decode(&req)
var req struct {
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"`
BaseRevision *string `json:"base_revision_uuid"`
EditorPrincipal *string `json:"editor_principal_uuid"`
}
err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<20)).Decode(&req)
if err != nil {
api.WriteError(
w,
@@ -80,6 +89,17 @@ func (s *Server) handleSaveRevisionAPI(
return
}
domainUUID := s.domainUUIDForRequest(r)
if req.EditorPrincipal != nil {
principal, e := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
id := ""
if e == nil && principal != nil {
id = principal.UUID
}
if id != *req.EditorPrincipal {
api.WriteError(w, 401, "editor_identity_changed", "editor identity changed; reopen with the original account")
return
}
}
if mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode"); mode == "enforcing" {
principal, authErr := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
if authErr != nil {
@@ -109,15 +129,16 @@ func (s *Server) handleSaveRevisionAPI(
}
}
revision, err :=
s.store.SaveRevisionContent(
req.ObjectUUID,
[]byte(req.Body),
req.Message,
req.Language,
req.ContentType,
req.Filename,
)
var revision *store.Revision
if req.BaseRevision != nil {
revision, err = s.store.SaveRevisionContentIfHead(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, *req.BaseRevision)
} else {
revision, err = s.store.SaveRevisionContent(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename)
}
if errors.Is(err, store.ErrRevisionConflict) {
api.WriteError(w, http.StatusConflict, "revision_conflict", "document changed; reload the language head before saving")
return
}
if err != nil {
status := http.StatusInternalServerError
@@ -143,5 +164,6 @@ func (s *Server) handleSaveRevisionAPI(
"lang": revision.Language,
"content_type": revision.ContentType,
"filename": revision.Filename,
"body": revision.Body,
})
}
+27 -19
View File
@@ -2,7 +2,6 @@ package server
import (
"archive/zip"
"bytes"
"context"
"io"
"mime"
@@ -47,23 +46,20 @@ func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) {
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)
s.serveZIPDocumentEntry(w, r, revision.DocumentHash, entry)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
lease, err := s.store.MaterializeDocument(ctx, revision.DocumentHash, 1<<30)
if err != nil {
http.Error(w, "archive source unavailable or exceeds materialization limit", http.StatusServiceUnavailable)
return
}
defer lease.Close()
command := exec.CommandContext(ctx, "nice", "-n", "10",
"7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", source, entry)
"7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", lease.Path, entry)
command.WaitDelay = time.Second
stdout, err := command.StdoutPipe()
if err != nil || command.Start() != nil {
http.Error(w, "cannot extract archive entry", http.StatusUnprocessableEntity)
@@ -85,16 +81,24 @@ func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) serveZIPEntry(
func (s *Server) serveZIPDocumentEntry(
w http.ResponseWriter,
r *http.Request,
body string,
hash string,
entry string,
) {
reader, err := zip.NewReader(
bytes.NewReader([]byte(body)),
int64(len(body)),
)
input, err := s.store.OpenDocumentContext(r.Context(), hash)
if err != nil {
http.NotFound(w, r)
return
}
defer input.Close()
size, err := input.Seek(0, io.SeekEnd)
if err != nil {
http.Error(w, "cannot determine archive size", http.StatusUnprocessableEntity)
return
}
reader, err := zip.NewReader(input, size)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
@@ -103,6 +107,10 @@ func (s *Server) serveZIPEntry(
if file.Name != entry || file.FileInfo().IsDir() {
continue
}
if file.UncompressedSize64 > 128<<20 {
http.Error(w, "archive entry exceeds 128 MiB limit", http.StatusRequestEntityTooLarge)
return
}
source, err := file.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
+228
View File
@@ -0,0 +1,228 @@
package server
import (
"archive/zip"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"image"
"image/color"
"image/png"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"pageswiki/internal/db"
"pageswiki/internal/documentstore"
"pageswiki/internal/store"
)
func TestArchiveEntryReadsCanonicalDocumentWithoutRevisionHydration(t *testing.T) {
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.NewWithDocumentRoot(database, t.TempDir())
domain, _ := st.GetDefaultDomain()
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/archive.zip")
if err != nil {
t.Fatal(err)
}
var payload bytes.Buffer
w := zip.NewWriter(&payload)
file, err := w.Create("hello.txt")
if err != nil {
t.Fatal(err)
}
file.Write([]byte("hello 日本語"))
if err := w.Close(); err != nil {
t.Fatal(err)
}
revision, err := st.SaveRevisionContent(object, payload.Bytes(), "test", "und", "application/zip", "archive.zip")
if err != nil {
t.Fatal(err)
}
// The canonical Document must work even after compatibility DB bodies
// have been removed. This never touches production records.
if _, err := database.Exec(`UPDATE revisions SET body='' WHERE uuid=?`, revision.UUID); err != nil {
t.Fatal(err)
}
s := &Server{store: st}
r := httptest.NewRequest(http.MethodGet, "/_/archive?path=/archive.zip&entry=hello.txt", nil)
response := httptest.NewRecorder()
s.serveZIPDocumentEntry(response, r, revision.DocumentHash, "hello.txt")
if response.Code != http.StatusOK || response.Body.String() != "hello 日本語" {
t.Fatalf("status=%d body=%q", response.Code, response.Body.String())
}
if response.Header().Get("Vary") != "Authorization, Cookie" {
t.Fatal("lost authorization cache partition")
}
missing := httptest.NewRecorder()
s.serveZIPDocumentEntry(missing, r, revision.DocumentHash, "missing")
if missing.Code != http.StatusNotFound {
t.Fatalf("missing status=%d", missing.Code)
}
ctx, cancel := context.WithCancel(r.Context())
cancel()
cancelled := httptest.NewRecorder()
s.serveZIPDocumentEntry(cancelled, r.WithContext(ctx), revision.DocumentHash, "hello.txt")
if cancelled.Code == http.StatusOK {
t.Fatal("cancelled request read Document")
}
}
// Measures the per-request cost of fetching a tiny entry from a large stored
// ZIP. Setup is outside the timer; no DB body hydration or scratch copy occurs.
func BenchmarkZIPDocumentEntry(b *testing.B) {
var payload bytes.Buffer
w := zip.NewWriter(&payload)
file, err := w.Create("hello.txt")
if err != nil {
b.Fatal(err)
}
file.Write([]byte("hello"))
padding, err := w.CreateHeader(&zip.FileHeader{Name: "padding.bin", Method: zip.Store})
if err != nil {
b.Fatal(err)
}
padding.Write(bytes.Repeat([]byte{0xaa}, 16<<20))
if err := w.Close(); err != nil {
b.Fatal(err)
}
root := b.TempDir()
backend, err := documentstore.NewLocal(root)
if err != nil {
b.Fatal(err)
}
sum := sha256.Sum256(payload.Bytes())
hash := hex.EncodeToString(sum[:])
if err := backend.PutIfAbsent(context.Background(), hash, int64(payload.Len()), bytes.NewReader(payload.Bytes())); err != nil {
b.Fatal(err)
}
s := &Server{store: &store.Store{DocumentRoot: root}}
r := httptest.NewRequest(http.MethodGet, "/archive", nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
response := httptest.NewRecorder()
s.serveZIPDocumentEntry(response, r, hash, "hello.txt")
if response.Code != http.StatusOK || response.Body.String() != "hello" {
b.Fatal("entry read failed")
}
}
}
func TestPreviewImageAndZIPWorkWithReaderOnlyLegacyStore(t *testing.T) {
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.New(database)
domain, _ := st.GetDefaultDomain()
s := &Server{store: st, previewRoot: t.TempDir()}
var picture bytes.Buffer
im := image.NewRGBA(image.Rect(0, 0, 10, 10))
im.Set(0, 0, color.White)
if err := png.Encode(&picture, im); err != nil {
t.Fatal(err)
}
var archive bytes.Buffer
w := zip.NewWriter(&archive)
for _, name := range []string{"1.png", "2.png", "3.png"} {
f, err := w.Create(name)
if err != nil {
t.Fatal(err)
}
f.Write(picture.Bytes())
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
kind, filename string
body []byte
}{
{"image", "picture.png", picture.Bytes()}, {"archive", "comic.zip", archive.Bytes()},
} {
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/"+tc.filename)
if err != nil {
t.Fatal(err)
}
revision, err := st.SaveRevisionContent(object, tc.body, "test", "und", "application/octet-stream", tc.filename)
if err != nil {
t.Fatal(err)
}
if err := s.generatePreview(previewEventPayload{DocumentHash: revision.DocumentHash, Kind: tc.kind, Filename: tc.filename}); err != nil {
t.Fatal(err)
}
if info, err := os.Stat(s.previewCachePath(revision.DocumentHash, tc.kind)); err != nil || info.Size() == 0 {
t.Fatalf("preview: %v", err)
}
}
}
func TestProjectionMetadataMaterializesAndCleansSource(t *testing.T) {
database, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if err := db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.NewWithDocumentRoot(database, t.TempDir())
domain, _ := st.GetDefaultDomain()
object, err := st.CreatePageAtPathInDomain(domain.UUID, "/test.pdf")
if err != nil {
t.Fatal(err)
}
revision, err := st.SaveRevisionContent(object, []byte("synthetic PDF fixture"), "test", "und", "application/pdf", "test.pdf")
if err != nil {
t.Fatal(err)
}
// Synthetic tools validate only the storage handoff, not PDF rendering.
bin := t.TempDir()
for name, script := range map[string]string{
"nice": "#!/bin/sh\nshift\nshift\nexec \"$@\"\n",
"pdfinfo": "#!/bin/sh\ntest -r \"$3\" || exit 1\nprintf '%s' \"$3\" > \"$PAGESWIKI_TEST_SOURCE\"\nprintf 'Pages: 3\\n'\n",
} {
if err := os.WriteFile(filepath.Join(bin, name), []byte(script), 0700); err != nil {
t.Fatal(err)
}
}
logFile := filepath.Join(t.TempDir(), "source")
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("PAGESWIKI_TEST_SOURCE", logFile)
s := &Server{store: st}
result, err := s.inspectProjectionMetadata(metadataProjectionPayload{DocumentHash: revision.DocumentHash, Kind: "pdf", Version: metadataProjectionVersion})
if err != nil || result.PDFPages != 3 {
t.Fatalf("result=%+v err=%v", result, err)
}
source, err := os.ReadFile(logFile)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(source), "pageswiki-document-") || strings.HasPrefix(string(source), st.DocumentRoot+string(os.PathSeparator)) {
t.Fatalf("tool received canonical path: %s", source)
}
if _, err := os.Stat(string(source)); !os.IsNotExist(err) {
t.Fatalf("scratch leaked: %v", err)
}
reader, err := st.OpenDocument(revision.DocumentHash)
if err != nil {
t.Fatal("canonical document removed", err)
}
reader.Close()
}
+12
View File
@@ -5,6 +5,7 @@ import (
"log"
"net/http"
"pageswiki/internal/store"
"strconv"
"time"
)
@@ -34,6 +35,17 @@ func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) {
}
panel := "auth"
switch r.FormValue("action") {
case "save_session_policy":
panel = "auth-sessions"
minutes, err := strconv.Atoi(r.FormValue("session_minutes"))
if err != nil || minutes < 5 || minutes > store.MaxAuthSessionMinutes {
http.Error(w, "session lifetime must be between 5 and 43200 minutes", http.StatusBadRequest)
return
}
if err := s.store.SetAuthSessionLifetimeMinutes(domainUUID, minutes); err != nil {
http.Error(w, "could not save session policy", http.StatusInternalServerError)
return
}
case "revoke_session":
panel = "auth-sessions"
if err := s.store.RevokeAuthSessionByID(domainUUID, r.FormValue("session_uuid")); err != nil {
+2 -2
View File
@@ -62,7 +62,7 @@ func requestIsHTTPS(r *http.Request) bool {
}
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})
http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)) * 60})
}
func (s *Server) authTokenFromRequest(r *http.Request) string {
@@ -205,7 +205,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
}
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})
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60})
return
}
s.setAuthCookie(w, r, token)
+1 -1
View File
@@ -245,7 +245,7 @@ func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) {
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})
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60})
return
}
s.setAuthCookie(w, r, token)
@@ -0,0 +1,50 @@
package server
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestSessionPolicyAdminUIAndCookie(t *testing.T) {
s, token, domain := configFixture(t)
form := url.Values{"action": {"save_session_policy"}, "session_minutes": {"60"}}
w := configRequest(s, token, "POST", "/-/sys/config/auth", form)
if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/-/sys/config?panel=auth-sessions&saved=1" {
t.Fatalf("policy save %d %s", w.Code, w.Body.String())
}
if s.store.AuthSessionLifetimeMinutes(domain) != 60 {
t.Fatal("policy not saved")
}
w = configRequest(s, token, "GET", "/-/sys/config?panel=auth-sessions", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), `name="session_minutes" value="60"`) {
t.Fatal("policy control missing")
}
for _, bad := range []string{"", "abc", "0", "99999999999999999999999"} {
form.Set("session_minutes", bad)
if w = configRequest(s, token, "POST", "/-/sys/config/auth", form); w.Code != 400 {
t.Fatalf("bad lifetime %q accepted", bad)
}
}
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
if err != nil {
t.Fatal(err)
}
reader, err := s.store.CreateAuthSession(domain, user.UUID)
if err != nil {
t.Fatal(err)
}
form.Set("session_minutes", "120")
if w = configRequest(s, reader, "POST", "/-/sys/config/auth", form); w.Code != 403 {
t.Fatalf("non-admin policy change: %d", w.Code)
}
w = httptest.NewRecorder()
r := httptest.NewRequest("GET", "https://localhost/", nil)
s.setAuthCookie(w, r, "fixture-token")
cookies := w.Result().Cookies()
if len(cookies) != 1 || cookies[0].MaxAge != 3600 || !cookies[0].HttpOnly || !cookies[0].Secure {
t.Fatalf("cookie policy %#v", cookies)
}
}
+8
View File
@@ -58,6 +58,14 @@ func (s *Server) handleBrowser(
if !strings.HasPrefix(currentPath, "/") {
currentPath = "/" + currentPath
}
// Browser navigation is scoped to this semantic Path, not a Domain-wide
// read grant. Filter each child separately below as well.
if !s.requestCanReadPath(r, currentPath) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Add("Vary", "Cookie, Authorization")
entries, err :=
s.store.ListBrowserEntriesInDomain(
+36
View File
@@ -0,0 +1,36 @@
package server
func init() {
for lang, entries := range map[string]map[string]string{
"en": {
"command.label": "Command · Tab: complete Path · Esc: close", "command.close": "Close command mode", "command.unavailable": "Path completion unavailable. Sign in or retry.", "command.empty": "No matching readable Paths.", "command.more": "More matches exist. Type a longer Path prefix.",
"auth.session_minutes": "Session lifetime (minutes)", "auth.session_policy_help": "5–43200 minutes (30 days); default 1440 (24 hours). Applies to new browser/API logins, including OIDC/LDAP. Existing sessions and CalDAV app passwords are unchanged. This is an absolute expiry, not an inactivity timeout.",
"browser.format": "Document format", "browser.wiki": "Wiki (default editor format)", "browser.plain": "PlainText (text/plain)",
"theme.quick": "Theme", "theme.default": "Domain default", "theme.local": "This browser only", "theme.failed": "Could not load the theme.",
"browser.address": "Path", "browser.go": "Go", "browser.up": "Parent folder",
"browser.command_help": "Enter /path or a relative path · cd .. · vi filename · view filename",
"browser.new": "New document", "browser.folder": "Open virtual folder", "browser.name": "Name or Path",
"browser.create_help": "Open the editor to create a document. If the Path already exists, edit that document.",
"browser.folder_help": "You can enter an empty Path without creating a folder, then create or upload documents there.",
"browser.invalid": "Enter a Wiki Path, not an external URL or a path containing control characters or backslashes.",
"browser.empty": "No visible items. Create a document, upload a file, or enter a different Path.",
},
"ja": {
"command.label": "コマンド · Tab: Path補完 · Esc: 閉じる", "command.close": "コマンドモードを閉じる", "command.unavailable": "Path補完を取得できません。ログイン状態を確認して再試行してください。", "command.empty": "閲覧可能な一致するPathがありません。", "command.more": "候補がほかにもあります。Pathをさらに入力してください。",
"auth.session_minutes": "セッションの有効期間(分)", "auth.session_policy_help": "5〜43200分(30日)。既定は1440分(24時間)。OIDC・LDAPを含む新規ブラウザ/APIログインに適用します。既存セッションとCalDAVのアプリパスワードは変更しません。無操作時間ではなくログインからの絶対期限です。",
"browser.format": "文書形式", "browser.wiki": "Wiki(既定の編集形式)", "browser.plain": "PlainText(text/plain)",
"theme.quick": "テーマ", "theme.default": "ドメインの既定", "theme.local": "このブラウザだけに適用", "theme.failed": "テーマを読み込めませんでした。",
"browser.address": "Path", "browser.go": "移動", "browser.up": "親フォルダ",
"browser.command_help": "/path または相対パス · cd .. · vi ファイル名 · view ファイル名",
"browser.new": "新規文書", "browser.folder": "仮想フォルダを開く", "browser.name": "名前またはPath",
"browser.create_help": "エディタを開いて文書を作成します。既存のPathでは、その文書を編集します。",
"browser.folder_help": "空のPathにもフォルダを作らず移動できます。移動先で文書の作成やアップロードができます。",
"browser.invalid": "WikiのPathを入力してください。外部URL・制御文字・バックスラッシュは使用できません。",
"browser.empty": "表示できる項目はありません。文書の作成、ファイルのアップロード、別のPathへの移動ができます。",
},
} {
for key, value := range entries {
translations[lang][key] = value
}
}
}
@@ -0,0 +1,82 @@
package server
import (
"encoding/json"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestCitySearchContentDestinations(t *testing.T) {
s, token, domain := searchServerFixture(t)
street := seedSearchCity(t, s, token, domain)
createUserSpacePage(t, s, domain, "/city?#%", "<h1>Portal</h1>")
if _, err := s.store.CreateSpotAtPath(domain, "test", street, "nebula-way-undefined", "Undefined", "nebula", "/city?#%/street/_undefined"); err != nil {
t.Fatal(err)
}
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms)
SELECT 'entrance-grant',b.role_uuid,'read','path','/city?#%/street/_undefined',1 FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? LIMIT 1`, domain, principal.UUID); err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/_/api/v1/search/suggest", s.handleSearchSuggestAPI)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/", s.handleSemanticRoute)
w := configRequest(s, token, "GET", "/_/api/v1/search/suggest?q=nebula", nil)
var payload struct {
Items []searchSuggestionItem `json:"items"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &payload) != nil {
t.Fatalf("suggest: %d %s", w.Code, w.Body.String())
}
foundPortal, foundEntrance := false, false
for _, item := range payload.Items {
if strings.HasPrefix(item.Href, "/v/city/") || strings.Contains(item.Href, "_undefined") {
t.Fatalf("management/virtual link: %+v", item)
}
if item.Kind == "quartier" {
foundPortal = item.Href == "/city%3F%23%25"
}
if item.Kind == "spot" && item.Label == "Nebula Street" {
foundEntrance = item.Href == "/v/browser/city%3F%23%25/street"
}
}
if !foundPortal || !foundEntrance {
t.Fatalf("destinations missing: %+v", payload.Items)
}
for _, target := range []struct{ url, marker string }{
{"/city%3F%23%25", "<h1>Portal</h1>"},
{"/v/browser/city%3F%23%25/street", "browser-layout"},
} {
opened := configRequest(s, token, "GET", target.url, nil)
if opened.Code != 200 || !strings.Contains(opened.Body.String(), target.marker) || strings.Contains(opened.Body.String(), `class="city-node-detail"`) {
t.Fatalf("destination %s: %d", target.url, opened.Code)
}
}
if denied := configRequest(s, token, "GET", "/v/browser/private-city", nil); denied.Code != 403 {
t.Fatalf("unreadable folder: %d", denied.Code)
}
w = configRequest(s, token, "GET", "/v/search?q=nebula", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/city%3F%23%25"`) || strings.Contains(w.Body.String(), `href="/v/city/`) {
t.Fatalf("search: %d", w.Code)
}
}
func TestCitySearchViewAndSuggestionsAgree(t *testing.T) {
for _, hasPage := range []bool{false, true} {
result := store.CitySearchResult{Kind: "spot", Name: "notes-undefined", Title: "Undefined", PathPrefix: "/日本語 #?%/_undefined", NavigationPath: "/日本語 #?%", StreetTitle: "Notes", HasPage: hasPage}
view := citySearchResultView(result, "notes", time.UTC)
suggestion := citySuggestion(result, "notes")
if view.Href != suggestion.Href || view.Href != view.PathHref || suggestion.Label != "Notes" {
t.Fatalf("inconsistent: %+v %+v", view, suggestion)
}
if strings.Contains(view.Href, "#") || strings.Contains(view.Href, "?") || strings.Contains(view.Href, "_undefined") {
t.Fatal(view.Href)
}
}
}
+1 -1
View File
@@ -80,7 +80,7 @@ func (s *Server) handleCityMap(w http.ResponseWriter, r *http.Request) {
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)})
s.renderWithContext(w, r, "city_node.gohtml", map[string]any{"Title": detail.DisplayTitle(), "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)})
+13
View File
@@ -34,6 +34,19 @@ func cityPageHref(path string) string {
return (&url.URL{Path: path}).EscapedPath()
}
// Search is a reading entry point, not a locality management shortcut. Both
// title and Path links use this projection; no additional per-hit DB reads.
func cityContentHref(result store.CitySearchResult) string {
path := result.ContentPath()
if !strings.HasPrefix(path, "/") || strings.HasPrefix(path, "//") {
return "/v/browser"
}
if result.HasPage {
return cityPageHref(path)
}
return cityPageHref("/v/browser" + path)
}
type cityPageView struct {
store.CityPagePlacement
Href string
+6 -1
View File
@@ -29,6 +29,11 @@ func filterCityProjection(city store.CityMap, allowed func(string) bool) store.C
continue
}
spot.DetailHref = cityDetailHref("spot", spot.Name, quartier.Name, spot.UUID)
parentTitle := street.Title
if parentTitle == "" {
parentTitle = street.Name
}
spot.DisplayTitle = store.CitySpotDisplayTitle(spot.Name, spot.Title, spot.PathPrefix, parentTitle)
pages := []store.CityPagePlacement{}
unique := map[string]bool{}
spot.PortalPath, spot.PortalPageUUID = "", ""
@@ -113,7 +118,7 @@ func (s *Server) pageSpotAppearancesForRequest(r *http.Request, objectUUID strin
out := []store.CitySpotAppearance{}
allowed := s.pathReadFilter(r)
for _, spot := range spots {
if allowed(spot.PathPrefix) {
if allowed(spot.PathPrefix) && allowed(spot.StreetPath) && allowed(spot.QuartierPath) {
out = append(out, spot)
}
}
+13
View File
@@ -49,6 +49,19 @@ func TestCityReadPortalPriorityAfterFiltering(t *testing.T) {
}
}
func TestCityProjectionDisplayTitleDoesNotMutateCachedIdentity(t *testing.T) {
spot := store.CitySpot{UUID: "spot", Name: "home-undefined", Title: "Undefined", PathPrefix: "/life/home/_undefined"}
city := store.CityMap{Quartiers: []store.CityQuartier{{PathPrefix: "/life", Streets: []store.CityStreet{{Title: "Home", PathPrefix: "/life/home", Spots: []store.CitySpot{spot}}}}}}
projected := filterCityProjection(city, func(string) bool { return true })
got := projected.Quartiers[0].Streets[0].Spots[0]
if got.DisplayTitle != "Home" || got.Title != "Undefined" || got.Name != spot.Name || got.PathPrefix != spot.PathPrefix {
t.Fatalf("projection: %+v", got)
}
if city.Quartiers[0].Streets[0].Spots[0].DisplayTitle != "" {
t.Fatal("shared navigation cache mutated")
}
}
func TestCityReadProjectionHidesPagesLocalitiesPortalsAndRelations(t *testing.T) {
s, _, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
+91
View File
@@ -0,0 +1,91 @@
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestCommandCompletionReadACL(t *testing.T) {
s, token, _ := searchServerFixture(t)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
w := configRequest(s, token, "GET", "/_/api/v1/path-picker?path=/docs&mode=children", nil)
if w.Code != 200 || strings.Contains(w.Body.String(), "hidden") || !strings.Contains(w.Body.String(), "nebula guide?#%") {
t.Fatalf("ACL completion: %d %s", w.Code, w.Body.String())
}
}
func TestCommandPathCompletion(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
createUserSpacePage(t, s, domain, "/manual", "<h1>Parent document and folder</h1>")
for i := 0; i < 260; i++ {
if _, err := s.store.CreatePageAtPathInDomain(domain, fmt.Sprintf("/manual/a%03d", i)); err != nil {
t.Fatal(err)
}
}
createUserSpacePage(t, s, domain, "/manual/日本語 #?", "plain")
w := configRequest(s, token, "GET", "/_/api/v1/path-picker?path=/manual&mode=children&prefix=日本語", nil)
var result struct {
Path string `json:"path"`
Entries []pathPickerEntry `json:"entries"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result.Path != "/manual" || len(result.Entries) != 1 || result.Entries[0].Name != "日本語 #?" {
t.Fatalf("completion: %d %s", w.Code, w.Body.String())
}
w = configRequest(s, "", "GET", "/_/api/v1/path-picker?path=/manual&mode=children", nil)
if w.Code == 200 {
t.Fatal("anonymous API listed private paths")
}
}
func TestCommandAndHighlightChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, _ := editorFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
s.mux.HandleFunc("/notes", s.handleSemanticRoute)
s.mux.HandleFunc("/", s.handleHome)
createUserSpacePage(t, s, domain, "/notes", `<h1>Code sample</h1><pre><code class="language-python"># comment
def hello():
return "&lt;script&gt;literal&lt;/script&gt;"</code></pre>`)
createUserSpacePage(t, s, domain, "/notes/alpha", "<h1>Alpha</h1>")
createUserSpacePage(t, s, domain, "/notes/alpine", "<h1>Alpine</h1>")
createUserSpacePage(t, s, domain, "/日本語 #?/child", "<h1>Unicode</h1>")
script, err := os.ReadFile("internal/server/testdata/command-highlight.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 == "/fixture" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "375"} {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
shot := ""
if base := os.Getenv("PAGESWIKI_COMMAND_SCREENSHOT"); base != "" {
shot = base + "-" + width + ".png"
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/fixture", t.TempDir(), shot, width, "950").CombinedOutput()
cancel()
if err != nil {
t.Fatalf("%s: %v\n%s", width, err, out)
}
}
}
+4 -2
View File
@@ -4,7 +4,7 @@ import (
"net/http"
"strings"
"pageswiki/internal/compiler"
"pageswiki/internal/grammaratur"
)
func (s *Server) compileMarkdownFromPath(
@@ -46,9 +46,11 @@ func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode ma
html := latest.Body
if mode == markdownProjection {
html = s.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r))
} else {
return grammaratur.HTMLToMarkdownAuthoring(html)
}
return compiler.HTMLToMarkdown(
return grammaratur.HTMLToMarkdown(
html,
)
}
+1
View File
@@ -1088,6 +1088,7 @@ func (s *Server) handleConfig(
"CampusTokens": campusTokens,
"AuthPrincipals": authPrincipals,
"AuthSessions": authSessions,
"AuthSessionMinutes": s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)),
"OIDCConfig": oidcConfig,
"SMTPConfig": smtpConfig,
"LDAPConfig": ldapConfig,
+24
View File
@@ -0,0 +1,24 @@
package server
func init() {
for lang, entries := range map[string]map[string]string{
"en": {
"contact.title": "Contact details", "contact.phone": "Phone number",
"contact.help": "Private by default, independently of your profile. A tap opens the phone app. Leave blank to remove. Use 3–15 digits with an optional leading +, spaces, hyphens or parentheses; extensions and service codes are not supported.",
"contact.visibility_help": "The profile audience remains the upper limit. Anonymous access also requires the domain to permit public profiles. Administrators do not automatically see private numbers.",
"contact.confirm": "I explicitly agree to make this phone number public.",
"contact.invalid_phone": "Enter a phone number with 3–15 digits and optional leading +; only spaces, hyphens and parentheses may separate digits.",
},
"ja": {
"contact.title": "連絡先", "contact.phone": "電話番号",
"contact.help": "プロフィールとは別に、既定で非公開です。タップすると電話アプリを開きます。空欄で削除。3〜15桁の数字と先頭の+、空白・ハイフン・括弧を使用できます。内線やサービスコードは未対応です。",
"contact.visibility_help": "プロフィールの公開範囲を上限とします。匿名公開にはドメイン側の許可も必要です。管理者でも非公開の番号は表示されません。",
"contact.confirm": "この電話番号をインターネットに公開することに同意します。",
"contact.invalid_phone": "電話番号は3〜15桁の数字と先頭の+で指定してください。区切りには空白・ハイフン・括弧のみ使用できます。",
},
} {
for key, value := range entries {
translations[lang][key] = value
}
}
}
+11 -216
View File
@@ -1,225 +1,20 @@
package server
import (
"path/filepath"
"strings"
)
import "pageswiki/internal/kunstzeal"
type projectionDelivery string
type projectionDelivery = kunstzeal.Delivery
type contentDescriptor = kunstzeal.Descriptor
type projectionAdapter = kunstzeal.Adapter
const (
deliveryHTML projectionDelivery = "html"
deliveryText projectionDelivery = "text"
deliveryRawMedia projectionDelivery = "raw-media"
deliveryRawFrame projectionDelivery = "raw-frame"
deliveryDownload projectionDelivery = "download"
deliveryHTML = kunstzeal.DeliveryHTML
deliveryText = kunstzeal.DeliveryText
deliveryRawMedia = kunstzeal.DeliveryRawMedia
deliveryRawFrame = kunstzeal.DeliveryRawFrame
deliveryDownload = kunstzeal.DeliveryDownload
inlineTextWindowThreshold = kunstzeal.InlineTextWindowThreshold
)
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/")
}
return kunstzeal.SelectAdapter(descriptor)
}
+2 -2
View File
@@ -14,7 +14,7 @@ func (s *Server) windowedContentViewForRequest(r *http.Request, objectUUID, sema
if err != nil || metadata.UUID == "" {
return contentView{}, false, err
}
size, err := s.store.DocumentSize(metadata.DocumentHash)
size, err := s.store.DocumentSizeContext(r.Context(), metadata.DocumentHash)
if err != nil {
return contentView{}, false, nil
}
@@ -30,7 +30,7 @@ func (s *Server) windowedContentViewForRequest(r *http.Request, objectUUID, sema
if size <= inlineTextWindowThreshold || !adapter.WindowableText {
return contentView{}, false, nil
}
window, err := s.store.ReadDocumentWindow(metadata.DocumentHash, 0, 128<<10)
window, err := s.store.ReadDocumentWindowContext(r.Context(), metadata.DocumentHash, 0, 128<<10)
if err != nil {
return contentView{}, false, err
}
+29 -140
View File
@@ -1,9 +1,6 @@
package server
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net/http"
@@ -13,6 +10,8 @@ import (
"strings"
"sync"
"time"
"pageswiki/internal/kunstzeal"
)
var diagramSourceCache = map[string]diagramCacheEntry{}
@@ -34,60 +33,15 @@ func (s *Server) diagramCachePath(kind, hash string) string {
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 readDiagramCache(path string) ([]byte, error) { return kunstzeal.ReadDiagramCache(path) }
func diagramHash(kind, source, format string) string {
return kunstzeal.DiagramHash(kind, source, format)
}
func diagramHash(
diagramType string,
source string,
format string,
) string {
func allowedDiagramType(kind string) bool { return kunstzeal.AllowedDiagramType(kind) }
sum :=
sha256.Sum256(
[]byte(
diagramType + "\x00" + format + "\x00" + source,
),
)
return hex.EncodeToString(
sum[:],
)
}
func allowedDiagramType(
t string,
) bool {
switch t {
case "mermaid", "plantuml", "graphviz", "dot", "d2":
return true
default:
return false
}
}
func normalizeDiagramType(
t string,
) string {
switch t {
case "dot":
return "graphviz"
default:
return t
}
}
func normalizeDiagramType(kind string) string { return kunstzeal.NormalizeDiagramType(kind) }
func krokiURL() string {
@@ -137,7 +91,7 @@ func (s *Server) renderDiagramsInHTML(
htmlUnescapeString(
m[2],
)
if len(source) > 32<<10 || path == "" {
if len(source) > kunstzeal.DiagramInlineLimit || path == "" {
return block
}
@@ -311,11 +265,11 @@ func (s *Server) handleCompileDiagramPost(
io.ReadAll(
io.LimitReader(
r.Body,
(1<<20)+1,
kunstzeal.DiagramSourceLimit+1,
),
)
if err != nil || len(sourceBytes) > 1<<20 {
if err != nil || len(sourceBytes) > kunstzeal.DiagramSourceLimit {
http.Error(w, "diagram source exceeds limit", http.StatusRequestEntityTooLarge)
return
}
@@ -352,91 +306,26 @@ func (s *Server) handleCompileDiagramPost(
)
}
func (s *Server) renderDiagramSVG(
w http.ResponseWriter,
r *http.Request,
diagramType string,
source string,
cachePath string,
) {
req, err :=
http.NewRequestWithContext(
r.Context(),
http.MethodPost,
krokiURL()+"/"+diagramType+"/svg",
bytes.NewBufferString(source),
)
// The configured service may be private (Compose), but it may not redirect
// source content to another destination. Authorization is checked before this.
func diagramHTTPClient() *http.Client {
return &http.Client{Timeout: kunstzeal.DiagramTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
}
func (s *Server) renderDiagramSVG(w http.ResponseWriter, r *http.Request, diagramType, source, cachePath string) {
provider := kunstzeal.Kroki{Endpoint: krokiURL(), Client: diagramHTTPClient()}
b, err := provider.Render(r.Context(), diagramType, source)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
req.Header.Set(
"Content-Type",
"text/plain; charset=utf-8",
)
client :=
http.Client{
Timeout: 15 * time.Second,
status := http.StatusBadGateway
if errors.Is(err, kunstzeal.ErrProviderConfig) {
status = http.StatusInternalServerError
}
resp, err :=
client.Do(
req,
)
if err != nil {
http.Error(w, err.Error(), 502)
http.Error(w, err.Error(), status)
return
}
defer resp.Body.Close()
if resp.StatusCode < 200 ||
resp.StatusCode >= 300 {
http.Error(w, "diagram renderer failed", 502)
return
}
b, err :=
io.ReadAll(
io.LimitReader(resp.Body, (4<<20)+1),
)
if err != nil || len(b) > 4<<20 {
http.Error(w, "diagram output unavailable or exceeds limit", http.StatusBadGateway)
return
}
_ =
os.MkdirAll(
filepath.Dir(cachePath),
0755,
)
_ =
os.WriteFile(
cachePath,
b,
0644,
)
w.Header().Set(
"Content-Type",
"image/svg+xml; charset=utf-8",
)
w.Header().Set(
"Content-Security-Policy",
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
)
_, _ =
w.Write(
b,
)
// Cache failure must not discard a successfully generated projection.
_ = kunstzeal.WriteDiagramCache(cachePath, b)
w.Header().Set("Content-Type", "image/svg+xml; charset=utf-8")
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:")
_, _ = w.Write(b)
}
+98
View File
@@ -0,0 +1,98 @@
package server
import (
"net/http"
"strings"
"time"
)
// This gate is outside authorization; it never grants access. DB mode=ro and
// query_only enforce a second boundary even if a GET handler attempts a write.
// The data directory should additionally be mounted read-only on standby.
func (s *Server) withStandbyReadOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health/read" || r.URL.Path == "/health/write" {
w.Header().Set("Cache-Control", "no-store")
if r.Method != "GET" && r.Method != "HEAD" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
ready := !s.store.ReadOnly || (r.URL.Path == "/health/read" && time.Now().Before(s.store.StandbyReadUntil))
if ready && s.store.DB != nil {
ready = s.store.DB.PingContext(r.Context()) == nil
}
if !ready {
http.Error(w, "not ready", http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
return
}
if !s.store.ReadOnly {
next.ServeHTTP(w, r)
return
}
w = &standbyResponseWriter{ResponseWriter: w}
w.Header().Set("X-PagesWiki-DR-Role", "standby")
w.Header().Set("Cache-Control", "private, no-store")
fail := func(message string) {
w.Header().Set("Retry-After", "60")
http.Error(w, message, http.StatusServiceUnavailable)
}
// Health remains a liveness endpoint, NOT proof that this node can write.
if r.URL.Path == "/health" && (r.Method == "GET" || r.Method == "HEAD") {
next.ServeHTTP(w, r)
return
}
if s.store.StandbyReadUntil.IsZero() || !time.Now().Before(s.store.StandbyReadUntil) {
fail("DR snapshot expired; routing or offline refresh is required")
return
}
if r.Method != "GET" && r.Method != "HEAD" && r.Method != "OPTIONS" && r.Method != "PROPFIND" && r.Method != "REPORT" {
fail("DR standby is read-only; write request was not executed")
return
}
// Authentication issuance, edit launch, federation and external renderers
// can have side effects even on GET. Do not invoke them on a standby.
for _, prefix := range []string{"/_/auth/", "/-/sys/", "/e/", "/o/e/", "/n/", "/:/", "/new", "/v/upload", "/_/adapter-gateway", "/_/adaptor/", "/_/api/v1/federation", "/_/api/v1/adapter", "/v/open", "/v/import", "/v/compile", "/_/api/v1/import"} {
if strings.HasPrefix(r.URL.Path, prefix) {
fail("operation unavailable on DR standby")
return
}
}
// V1 standby does not serve organizer or DAV endpoints: their current
// read paths repair file-backed indexes and update credential metadata.
// They become available after promotion; no false successful sync responses.
for _, prefix := range []string{"/v/plan", "/v/calendar", "/v/event", "/v/tasks", "/_/api/v1/calendar", "/_/api/v1/caldav", "/_/api/v1/task", "/_/api/v1/extension/", "/.well-known/caldav"} {
if strings.HasPrefix(r.URL.Path, prefix) {
fail("organizer/DAV read service requires DR promotion in this version")
return
}
}
next.ServeHTTP(w, r)
})
}
// Immutable/raw handlers normally set long cache lifetimes. A standby must not
// let those headers outlive the snapshot's authorization freshness deadline.
type standbyResponseWriter struct {
http.ResponseWriter
wrote bool
}
func (w *standbyResponseWriter) WriteHeader(status int) {
if w.wrote {
return
}
w.wrote = true
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("X-PagesWiki-DR-Role", "standby")
w.ResponseWriter.WriteHeader(status)
}
func (w *standbyResponseWriter) Write(b []byte) (int, error) {
if !w.wrote {
w.WriteHeader(http.StatusOK)
}
return w.ResponseWriter.Write(b)
}
func (w *standbyResponseWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
+52
View File
@@ -0,0 +1,52 @@
package server
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"pageswiki/internal/store"
)
func TestDRStandbyGate(t *testing.T) {
s := &Server{store: &store.Store{ReadOnly: true, StandbyReadUntil: time.Now().Add(time.Hour)}}
h := s.withStandbyReadOnly(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) }))
for _, tc := range []struct {
method, path string
status int
}{
{"GET", "/readable-page", 200}, {"HEAD", "/readable-page", 200}, {"GET", "/health", 200}, {"GET", "/health/read", 200}, {"GET", "/health/write", 503},
{"POST", "/_/api/v1/objects", 503}, {"PUT", "/anything", 503}, {"DELETE", "/anything", 503}, {"MKCALENDAR", "/_/api/v1/caldav/new", 503}, {"PATCH", "/anything", 503},
{"GET", "/_/auth/login", 503}, {"GET", "/e/test", 503}, {"GET", "/:/compile/diagram/mermaid/x", 503}, {"GET", "/v/open-with", 503}, {"GET", "/_/api/v1/federation/manifest", 503}, {"REPORT", "/_/api/v1/caldav/", 503},
} {
t.Run(tc.method+tc.path, func(t *testing.T) {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, nil))
if w.Code != tc.status {
t.Fatalf("%d: %s", w.Code, w.Body.String())
}
})
}
s.store.StandbyReadUntil = time.Now().Add(-time.Second)
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", "/readable-page", nil))
if w.Code != 503 {
t.Fatal("expired snapshot served")
}
w = httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", "/health/read", nil))
if w.Code != 503 {
t.Fatal("expired snapshot ready")
}
}
func TestDRPrimaryGatePassThrough(t *testing.T) {
s := &Server{store: &store.Store{}}
h := s.withStandbyReadOnly(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(202) }))
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("POST", "/anything", nil))
if w.Code != 202 {
t.Fatal("primary write blocked")
}
}
+138
View File
@@ -0,0 +1,138 @@
package server
import (
"context"
"crypto/sha256"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"pageswiki/internal/db"
"pageswiki/internal/dr"
"pageswiki/internal/store"
)
// Full startup against a real schema and synthetic data, not a mock connection.
// No production data, imported calendars or outbound requests are involved.
func TestDRRecoveryReadOnlyServer(t *testing.T) {
t.Chdir("../..")
root := t.TempDir()
database, err := db.Open(filepath.Join(root, "pages.db"))
if err != nil {
t.Fatal(err)
}
if err = db.Migrate(database); err != nil {
t.Fatal(err)
}
st := store.NewWithDocumentRoot(database, filepath.Join(root, "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, "dr-fixture", "synthetic long test password", "DR Fixture", "")
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)
}
object, err := st.CreatePageAtPath("/dr-fixture")
if err != nil {
t.Fatal(err)
}
rev, err := st.SaveRevisionForLanguage(object, "<h1>DR fixture</h1><p>Canonical body.</p>", "test", "und")
if err != nil {
t.Fatal(err)
}
if err = database.Close(); err != nil {
t.Fatal(err)
}
if _, err = dr.Init(root); err != nil {
t.Fatal(err)
}
snap := filepath.Join(t.TempDir(), "snapshot")
_, digest, err := dr.Snapshot(context.Background(), root, snap)
if err != nil {
t.Fatal(err)
}
standby := filepath.Join(t.TempDir(), "standby")
node, err := dr.Restore(context.Background(), snap, digest, standby)
if err != nil {
t.Fatal(err)
}
path := filepath.Join(standby, "pages.db")
before, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
ro, err := db.OpenReadOnly(path)
if err != nil {
t.Fatal(err)
}
defer ro.Close()
readonly, err := store.NewReadOnlyWithDocumentRoot(ro, filepath.Join(standby, "documents"))
if err != nil {
t.Fatal(err)
}
readonly.StandbyReadUntil = node.ReadUntil()
s := New(readonly)
if s.previewRoot != "" || s.importRoot != "" || s.schedulerRunning.Load() != 0 {
t.Fatal("started standby worker/cache writers")
}
request := func(method, path, auth string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost"+path, nil)
if auth != "" {
r.Header.Set("Authorization", "Bearer "+auth)
}
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
return w
}
for _, path := range []string{"/v/raw/rev:" + rev.UUID, "/dr-fixture"} {
w := request("GET", path, token)
if w.Code != 200 || !strings.Contains(w.Body.String(), "Canonical body.") {
t.Fatalf("read %s: %d %s", path, w.Code, w.Body.String())
}
if w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatalf("stale authorization cache permitted: %s", w.Header().Get("Cache-Control"))
}
}
if w := request("GET", "/v/raw/rev:"+rev.UUID, ""); w.Code == 200 {
t.Fatal("read-only bypassed authorization")
}
if w := request("PUT", "/_/api/v1/objects/"+object, token); w.Code != 503 {
t.Fatal("standby write not rejected")
}
if _, err = readonly.SaveRevisionForLanguage(object, "must never persist", "test", "und"); err == nil {
t.Fatal("store allowed write")
}
if _, err = store.NewReadOnlyWithDocumentRoot(ro, filepath.Join(t.TempDir(), "documents")); err == nil {
t.Fatal("regenerated missing auth key")
}
readonly.StandbyReadUntil = time.Now().Add(-time.Second)
if w := request("GET", "/dr-fixture", token); w.Code != 503 {
t.Fatal("stale snapshot served")
}
after, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if sha256.Sum256(before) != sha256.Sum256(after) {
t.Fatal("standby changed database bytes")
}
for _, name := range []string{"previews", "imports", "pages.db-wal", "pages.db-shm"} {
if _, err = os.Stat(filepath.Join(standby, name)); !os.IsNotExist(err) {
t.Fatalf("created standby artifact %s", name)
}
}
}
+99
View File
@@ -0,0 +1,99 @@
package server
import (
"context"
"database/sql"
"errors"
"log"
"time"
"pageswiki/internal/dregarnuhr"
"pageswiki/internal/store"
)
// This adapter preserves SQL/storage ownership and all existing queue IDs.
type schedulerQueue struct{ store *store.Store }
func (q schedulerQueue) Settings(ctx context.Context) (dregarnuhr.Settings, error) {
if err := ctx.Err(); err != nil {
return dregarnuhr.Settings{}, err
}
if q.store.ReadOnly || q.store.BackgroundJobsDisabled {
return dregarnuhr.Settings{Paused: true}, nil
}
return q.store.GetQueueSettings()
}
func (q schedulerQueue) Claim(ctx context.Context, now time.Time) (*dregarnuhr.Job, error) {
job, err := q.store.ClaimDueEventAt(ctx, now)
if errors.Is(err, sql.ErrNoRows) {
return nil, dregarnuhr.ErrNoJob
}
return job, err
}
func (q schedulerQueue) Cancellation(ctx context.Context, id string) (bool, error) {
return q.store.EventCancellationState(ctx, id)
}
func (q schedulerQueue) Started(ctx context.Context, job *dregarnuhr.Job) error {
return q.store.AddEventLog(job.UUID, job.EventUUID, "info", "execution started")
}
func (q schedulerQueue) Finish(ctx context.Context, job *dregarnuhr.Job, err error, now time.Time) error {
return q.store.FinishEventAt(ctx, job.UUID, err, now)
}
func (s *Server) eventRunner() *dregarnuhr.Runner {
s.schedulerOnce.Do(func() {
s.schedulerRunner = dregarnuhr.NewRunner(schedulerQueue{s.store}, s.registeredEventHandlers(), dregarnuhr.RunnerOptions{
Active: &s.schedulerRunning,
Foreground: func() bool { return s.foregroundRequests.Load() > 0 },
Wake: s.wakeEventScheduler,
Report: func(stage string, job *dregarnuhr.Job, err error) {
if job != nil {
log.Printf("component=%s event scheduler %s failed job=%s type=%s: %v", dregarnuhr.Component, stage, job.UUID, job.Type, err)
} else {
log.Printf("component=%s event scheduler %s failed: %v", dregarnuhr.Component, stage, err)
}
},
})
})
return s.schedulerRunner
}
func (s *Server) runDueEvents() {
if s.store == nil {
return
}
s.eventRunner().Dispatch(context.Background())
}
func (s *Server) schedulerActivity(ctx context.Context, now time.Time) error {
_, err := s.store.ExpireApprovalRequests(ctx, now.UnixMilli(), 100)
// Preserve independent City processing even if approval expiry fails.
s.enqueueCityActivityEvents()
return err
}
func (s *Server) schedulerLoop() dregarnuhr.Loop {
return dregarnuhr.Loop{
Every: time.Second, Wake: s.schedulerWake, OnWake: s.schedulerActivity,
Triggers: []dregarnuhr.Trigger{
{Name: "initial-preview-sweep", After: 5 * time.Second, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
{Name: "preview-sweep", Every: 15 * time.Minute, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
{Name: "history-cleanup", Every: time.Hour, Run: func(context.Context, time.Time) error {
settings, err := s.store.GetQueueSettings()
if err != nil {
return err
}
return s.store.PurgeEventHistory(settings.RetentionHours)
}},
{Name: "activity-sweep", Every: 10 * time.Second, Run: s.schedulerActivity},
},
Step: func(ctx context.Context, now time.Time) error {
_, err := s.store.EnqueueDueDefinitions(now)
s.eventRunner().Dispatch(ctx)
return err
},
Report: func(stage string, err error) {
log.Printf("component=%s event scheduler %s failed: %v", dregarnuhr.Component, stage, err)
},
}
}
+135
View File
@@ -0,0 +1,135 @@
package server
import (
"context"
"fmt"
"sync"
"testing"
"time"
"pageswiki/internal/db"
"pageswiki/internal/dregarnuhr"
"pageswiki/internal/store"
)
func TestDregarnuhrBuiltinsRemainRegistered(t *testing.T) {
s := &Server{}
registry := s.registeredEventHandlers()
// Duplicate registration proves presence without executing a business handler
// or reading any external calendar/source content.
for _, kind := range []string{
store.NotificationPushEventType, store.TaskFocusEventType, store.TaskReminderEventType,
store.TaskActivityBridgeEventType, calendarSweepType, calendarSyncType,
metadataProjectionEventType, store.ResourceEventType, previewSweepEventType,
previewEventType, cityReconcileEventType, "pdf.page.generate", importAnalyzeEventType, importCommitEventType,
} {
if err := registry.Register(kind, func(context.Context, *dregarnuhr.Job) error { return nil }); err == nil {
t.Errorf("missing builtin: %s", kind)
}
}
if s.registeredEventHandlers() != registry {
t.Fatal("registry recreated")
}
if err := s.runEvent(&store.EventJob{Type: "test.unknown"}); err == nil {
t.Fatal("unknown event accepted")
}
loop := s.schedulerLoop()
if loop.Every != time.Second || loop.OnWake == nil {
t.Fatal("dispatch cadence or activity hook changed")
}
want := map[string][2]time.Duration{
"initial-preview-sweep": {5 * time.Second, 0}, "preview-sweep": {0, 15 * time.Minute},
"history-cleanup": {0, time.Hour}, "activity-sweep": {0, 10 * time.Second},
}
if len(loop.Triggers) != len(want) {
t.Fatal("maintenance hooks changed")
}
for _, trigger := range loop.Triggers {
v, ok := want[trigger.Name]
if !ok || v != [2]time.Duration{trigger.After, trigger.Every} || trigger.Run == nil {
t.Fatalf("unexpected trigger: %+v", trigger)
}
}
}
func TestDregarnuhrServerQueueIntegration(t *testing.T) {
database, err := db.Open(t.TempDir() + "/queue.db")
if err != nil {
t.Fatal(err)
}
defer database.Close()
st := &store.Store{DB: database}
if err := st.EnsureEventJobsSchema(); err != nil {
t.Fatal(err)
}
settings, err := st.GetQueueSettings()
if err != nil {
t.Fatal(err)
}
settings.Concurrency = 2
if err := st.SetQueueSettings(settings); err != nil {
t.Fatal(err)
}
s := &Server{store: st, schedulerWake: make(chan struct{}, 8)}
started := make(chan struct{}, 8)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := s.registeredEventHandlers().Register("test.synthetic", func(ctx context.Context, job *dregarnuhr.Job) error {
started <- struct{}{}
<-ctx.Done()
return nil
}); err != nil {
t.Fatal(err)
}
for n := 0; n < 4; n++ {
if _, err := st.EnqueueEvent("test.synthetic", fmt.Sprint(n), "{}", time.Now().Add(-time.Minute)); err != nil {
t.Fatal(err)
}
}
var wg sync.WaitGroup
for range 16 {
wg.Add(1)
go func() { defer wg.Done(); s.eventRunner().Dispatch(ctx) }()
}
wg.Wait()
// Ensure workers have reached the synthetic handler before cancellation.
for range 2 {
select {
case <-started:
case <-time.After(5 * time.Second):
t.Fatal("handler not started")
}
}
if got := s.schedulerRunning.Load(); got != 2 {
t.Errorf("monitor running=%d, want=2", got)
}
cancel()
for range 2 {
select {
case <-s.schedulerWake:
case <-time.After(5 * time.Second):
t.Fatal("completion not persisted")
}
}
if got := s.schedulerRunning.Load(); got != 0 {
t.Fatalf("monitor leaked running=%d", got)
}
var running, attempted int
if err := database.QueryRow(`SELECT count(*) FROM event_jobs WHERE status='running'`).Scan(&running); err != nil {
t.Fatal(err)
}
if err := database.QueryRow(`SELECT count(*) FROM event_jobs WHERE attempts=1`).Scan(&attempted); err != nil {
t.Fatal(err)
}
if running != 0 || attempted != 2 {
t.Fatalf("persisted running=%d attempts=%d", running, attempted)
}
// Both deployment guards must stop claim before any registered handler runs.
for _, mode := range []string{"readonly", "disabled"} {
st.ReadOnly, st.BackgroundJobsDisabled = mode == "readonly", mode == "disabled"
s.runDueEvents()
if got := s.schedulerRunning.Load(); got != 0 {
t.Fatalf("%s started worker", mode)
}
}
}
+12
View File
@@ -0,0 +1,12 @@
package server
func init() {
for lang, entries := range map[string]map[string]string{
"en": {"editor.tools": "Insert component / drafts", "editor.language": "Edit / fork language", "editor.restore": "Restore a revision into the editor"},
"ja": {"editor.tools": "コンポーネント挿入・下書き", "editor.language": "言語の編集・分岐", "editor.restore": "過去のリビジョンをエディタに復元"},
} {
for key, value := range entries {
translations[lang][key] = value
}
}
}
+86
View File
@@ -0,0 +1,86 @@
package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPlainTextEditorCanonicalAndExistingHTML(t *testing.T) {
s, token, domain, htmlObject := editorFixture(t)
page := configRequest(s, token, "GET", "/e/-/notes.txt?edittype=plain", nil)
if page.Code != 200 || !strings.Contains(page.Body.String(), `value="plain"`) || !strings.Contains(page.Body.String(), `value="text/plain; charset=utf-8"`) {
t.Fatalf("plain editor: %d %s", page.Code, page.Body.String())
}
object, err := s.store.ResolvePathInDomain(domain, "/notes.txt")
if err != nil {
t.Fatal(err)
}
body := "<script>alert('literal')</script>\n# Not Markdown\n{{children}}\n日本語 & <>\n"
payload, _ := json.Marshal(map[string]any{"object_uuid": object, "body": body, "lang": "und", "content_type": "text/plain; charset=utf-8", "filename": "notes.txt", "base_revision_uuid": ""})
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(payload))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("plain save: %d %s", w.Code, w.Body.String())
}
rev, err := s.store.GetRevisionForExactLanguage(object, "und")
if err != nil {
t.Fatal(err)
}
if rev.Body != body || rev.ContentType != "text/plain; charset=utf-8" {
t.Fatalf("plain content mutated: %#v", rev)
}
if view := revisionContentView(rev, "/notes.txt"); view.Kind != "code" {
t.Fatalf("plain rendered as %s", view.Kind)
}
page = configRequest(s, token, "GET", "/e/-/notes.txt?edittype=markdown", nil)
if page.Code != 200 || !strings.Contains(page.Body.String(), `value="plain"`) || strings.Contains(page.Body.String(), `data-editor-action="markdown"`) {
t.Fatal("reopen switched plain file to HTML")
}
before, _ := s.store.GetRevisionForExactLanguage(htmlObject, "und")
page = configRequest(s, token, "GET", "/e/-/editor-fixture?edittype=plain", nil)
if page.Code != http.StatusConflict {
t.Fatalf("existing HTML converted: %d", page.Code)
}
after, _ := s.store.GetRevisionForExactLanguage(htmlObject, "und")
if before.UUID != after.UUID {
t.Fatal("existing HTML changed")
}
page = configRequest(s, token, "GET", "/e/-/invalid-format?edittype=not-a-format", nil)
if page.Code != http.StatusBadRequest {
t.Fatal("invalid editor format accepted")
}
if _, err = s.store.ResolvePathInDomain(domain, "/invalid-format"); err == nil {
t.Fatal("invalid format created a Page")
}
}
func TestBrowserEditorCreationRequiresPermission(t *testing.T) {
s, _, domain, _ := editorFixture(t)
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
if err != nil {
t.Fatal(err)
}
if err := s.store.SetPrincipalState(domain, user.UUID, "readonly"); err != nil {
t.Fatal(err)
}
token, err := s.store.CreateAuthSession(domain, user.UUID)
if err != nil {
t.Fatal(err)
}
for _, format := range []string{"html", "plain"} {
response := configRequest(s, token, "GET", "/e/-/denied?edittype="+format, nil)
if response.Code != http.StatusForbidden {
t.Fatalf("read-only create: %d", response.Code)
}
if _, err := s.store.ResolvePathInDomain(domain, "/denied"); err == nil {
t.Fatal("denied editor created a Page")
}
}
}
+147
View File
@@ -0,0 +1,147 @@
package server
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func editorFixture(t *testing.T) (*Server, string, string, string) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/e/", s.handleEditorRoute)
s.mux.HandleFunc("/_/api/v1/editor/state", s.handleEditorState)
s.mux.HandleFunc("/_/api/v1/editor/convert", s.handleEditorConvert)
s.mux.HandleFunc("/_/api/v1/revisions/save", s.handleSaveRevisionAPI)
s.mux.HandleFunc("/_/api/v1/revisions/list", s.handleRevisionListAPI)
s.mux.HandleFunc("/_/api/v1/render/markdown", s.handleMarkdownRenderAPI)
object, err := s.store.CreatePageAtPathInDomain(domain, "/editor-fixture")
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(object, []byte("<h1>Base</h1><p>one two three</p>"), "base", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
return s, token, domain, object
}
func TestEditorResilienceAPI(t *testing.T) {
s, token, domain, object := editorFixture(t)
principal, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
base, err := s.store.GetRevisionForExactLanguage(object, "und")
if err != nil {
t.Fatal(err)
}
payload := map[string]any{"object_uuid": object, "body": "<h1>Saved</h1>", "lang": "und", "content_type": "text/html", "base_revision_uuid": base.UUID, "editor_principal_uuid": principal.UUID}
save := func(token string) *httptest.ResponseRecorder {
b, _ := json.Marshal(payload)
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(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
}
if w := save(token); w.Code != 200 {
t.Fatalf("save %d %s", w.Code, w.Body.String())
}
if w := save(token); w.Code != 409 {
t.Fatalf("stale %d %s", w.Code, w.Body.String())
}
payload["editor_principal_uuid"] = "different-user"
if w := save(token); w.Code != 401 {
t.Fatalf("identity %d", w.Code)
}
if w := save("expired"); w.Code != 401 {
t.Fatalf("expired %d", w.Code)
}
for _, query := range []string{"&metadata=1", "&revision=" + base.UUID, "&lang=en"} {
w := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+query, nil)
if w.Code != 200 {
t.Fatalf("state %s %d %s", query, w.Code, w.Body.String())
}
var out map[string]any
if err = json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
if out["principal_uuid"] != principal.UUID {
t.Fatal("identity omitted")
}
if query == "&metadata=1" {
if _, ok := out["body"]; ok {
t.Fatal("poll hydrated body")
}
}
if query == "&revision="+base.UUID && out["body"] != base.Body {
t.Fatal("old revision missing")
}
if query == "&lang=en" && out["revision_uuid"] != "" {
t.Fatal("language fallback")
}
}
other, err := s.store.CreatePageAtPathInDomain(domain, "/other")
if err != nil {
t.Fatal(err)
}
old, err := s.store.SaveRevisionForLanguage(other, "private unrelated body", "", "und")
if err != nil {
t.Fatal(err)
}
if w := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&revision="+old.UUID, nil); w.Code != 404 {
t.Fatalf("other object revision %d", w.Code)
}
w := configRequest(s, token, "GET", "/e/-/editor-fixture?lang=en&edittype=markdown", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), `data-base-revision=""`) || !strings.Contains(w.Body.String(), `data-owner="`+principal.UUID+`"`) {
t.Fatalf("editor language/owner %d", w.Code)
}
// Legacy API callers can still save without CAS during the migration.
delete(payload, "base_revision_uuid")
payload["editor_principal_uuid"] = principal.UUID
if w := save(token); w.Code != 200 {
t.Fatalf("legacy API %d", w.Code)
}
}
func TestEditorResilienceChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, _ := editorFixture(t)
if err := s.store.SetConfigInDomain(domain, "editor_keymap", "vim"); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/editor-resilience.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 == "/fixture" {
w.Header().Set("Content-Type", "text/html")
_, _ = io.WriteString(w, `<!doctype html><body><script>`+string(script)+`</script></body>`)
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir()}
if shot := os.Getenv("EDITOR_TEST_SCREENSHOT"); shot != "" {
args = append(args, shot)
}
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
t.Fatalf("browser %v %s", err, out)
}
}
+96
View File
@@ -0,0 +1,96 @@
package server
import (
"encoding/json"
"net/http"
"strings"
"pageswiki/internal/grammaratur"
)
// Converts authoring input only. It never expands macros or writes a Revision.
func (s *Server) handleEditorConvert(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "method not allowed", 405)
return
}
var input struct {
HTML string `json:"html"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&input); err != nil {
http.Error(w, "invalid input", 400)
return
}
markdown, err := grammaratur.HTMLToMarkdownAuthoring(input.HTML)
if err != nil {
http.Error(w, "conversion failed", 422)
return
}
_ = json.NewEncoder(w).Encode(map[string]string{"markdown": markdown})
}
// Restore loads a working buffer only; it never mutates an old Revision.
func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Vary", "Authorization, Cookie")
if r.Method != http.MethodGet {
w.Header().Set("Allow", "GET")
http.Error(w, "method not allowed", 405)
return
}
domain := s.domainUUIDForRequest(r)
principal, authErr := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
if mode, _ := s.store.GetConfigInDomain(domain, "authorization_mode"); mode == "enforcing" && authErr != nil {
http.Error(w, "authentication required", 401)
return
}
object := r.URL.Query().Get("uuid")
if object == "" || !s.store.ObjectBoundInDomain(domain, object) || s.authorizeObjectForRequest(r, "read", object) != nil {
http.Error(w, "forbidden", 403)
return
}
rev, err := s.store.GetRevisionMetadataForExactLanguage(object, r.URL.Query().Get("lang"))
if err != nil {
http.Error(w, "invalid language or unavailable revision", 400)
return
}
head := rev.UUID
if id := r.URL.Query().Get("revision"); id != "" {
metadata, e := s.store.GetRevisionMetadata(id)
if e != nil || metadata.ObjectUUID != object {
http.Error(w, "revision not found", 404)
return
}
rev = metadata
}
owner := ""
if principal != nil {
owner = principal.UUID
}
out := map[string]any{"revision_uuid": rev.UUID, "head_revision_uuid": head, "lang": rev.Language, "content_type": rev.ContentType, "filename": rev.Filename, "principal_uuid": owner}
if r.URL.Query().Get("metadata") != "1" {
if rev.UUID != "" {
rev, err = s.store.GetRevision(rev.UUID)
}
if err != nil {
http.Error(w, "revision unavailable", 503)
return
}
markdown := rev.Body
if strings.HasPrefix(rev.ContentType, "text/html") {
markdown, err = grammaratur.HTMLToMarkdownAuthoring(rev.Body)
}
if err != nil {
http.Error(w, "conversion unavailable", 503)
return
}
out["body"], out["markdown"] = rev.Body, markdown
kind := revisionContentView(rev, "").Kind
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code"
}
_ = json.NewEncoder(w).Encode(out)
}
+4
View File
@@ -37,6 +37,8 @@ const (
)
var endpointPolicies = map[string]endpointPolicy{
"GET /_/api/v1/editor/state": {Name: "editor.state", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"POST /_/api/v1/editor/convert": {Name: "editor.convert", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
"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},
@@ -160,6 +162,8 @@ func policyForRequest(r *http.Request) endpointPolicy {
func readEndpointResolver(path string) (endpointTargetResolver, string) {
switch {
case path == "/v/browser", strings.HasPrefix(path, "/v/browser/"):
return resolveHandler, "read"
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/"):
+7 -38
View File
@@ -4,14 +4,11 @@ import (
"encoding/json"
"errors"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"pageswiki/internal/seheweit"
"pageswiki/internal/store"
)
@@ -250,7 +247,7 @@ func (s *Server) handleQueueAPI(w http.ResponseWriter, r *http.Request) {
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})
writeEventJSON(w, 200, map[string]any{"samples": samples, "hours": hours, "retention_days": seheweit.RetentionDays})
return
}
}
@@ -258,39 +255,11 @@ func (s *Server) handleQueueAPI(w http.ResponseWriter, r *http.Request) {
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], " ")
}
}
func (s *Server) resourceStatus() seheweit.Snapshot {
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},
}
return seheweit.Collect(s.store.DocumentRoot, seheweit.Queue{
Stats: seheweit.QueueStats{Queued: stats.Queued, Running: stats.Running},
ActiveWorkers: s.schedulerRunning.Load(), Paused: settings.Paused,
})
}
+120
View File
@@ -0,0 +1,120 @@
package server
import (
"context"
"encoding/json"
"fmt"
"time"
"pageswiki/internal/dregarnuhr"
"pageswiki/internal/store"
)
// Business handlers stay in the application. Dregarnuhr knows no event types,
// payload schemas, notification recipients or calendar/source configuration.
func (s *Server) registeredEventHandlers() *dregarnuhr.Registry {
s.eventHandlersOnce.Do(func() {
registry := &dregarnuhr.Registry{}
handlers := map[string]dregarnuhr.Handler{
store.NotificationPushEventType: func(ctx context.Context, job *store.EventJob) error {
return s.deliverNotificationPushContext(ctx, job)
},
store.TaskFocusEventType: func(ctx context.Context, job *store.EventJob) error {
processed, err := s.store.CompleteTaskFocus(ctx, 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
},
store.TaskReminderEventType: func(ctx context.Context, job *store.EventJob) error {
checked, sent, err := s.store.DeliverTaskReminders(ctx, 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
},
store.TaskActivityBridgeEventType: func(ctx context.Context, job *store.EventJob) error {
processed, published, err := s.store.PublishTaskActivity(ctx, 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
},
calendarSweepType: func(ctx context.Context, job *store.EventJob) error {
return s.sweepCalendarSources()
},
calendarSyncType: func(ctx context.Context, job *store.EventJob) error {
return s.runCalendarSource(job)
},
metadataProjectionEventType: func(ctx context.Context, job *store.EventJob) error {
var payload metadataProjectionPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generateMetadataProjection(payload)
},
store.ResourceEventType: func(ctx context.Context, job *store.EventJob) error {
if err := ctx.Err(); err != nil {
return err
}
snapshot := s.resourceStatus()
payload, err := json.Marshal(snapshot)
if err != nil {
return err
}
return s.store.SaveResourceSampleContext(ctx, job.UUID, snapshot.TimestampMS, payload)
},
previewSweepEventType: func(ctx context.Context, job *store.EventJob) error {
s.enqueueAllPreviewEvents()
return nil
},
previewEventType: func(ctx context.Context, job *store.EventJob) error {
var payload previewEventPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generatePreview(payload)
},
cityReconcileEventType: func(ctx context.Context, job *store.EventJob) error {
var payload cityReconcilePayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.reconcileCityActivity(payload.DomainUUID)
},
"pdf.page.generate": func(ctx context.Context, job *store.EventJob) error {
var payload previewEventPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.generatePDFPage(payload)
},
importAnalyzeEventType: func(ctx context.Context, job *store.EventJob) error {
var payload importAnalyzePayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.analyzeImport(payload)
},
importCommitEventType: func(ctx context.Context, job *store.EventJob) error {
var payload importCommitPayload
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
return err
}
return s.commitImport(payload)
},
}
for kind, handler := range handlers {
// A duplicate/invalid built-in is a programmer error, never silently replace.
if err := registry.Register(kind, handler); err != nil {
panic(err)
}
}
s.eventHandlers = registry
})
return s.eventHandlers
}
func (s *Server) runEvent(job *store.EventJob) error {
return s.registeredEventHandlers().Run(context.Background(), job)
}
+66 -176
View File
@@ -3,7 +3,6 @@ package server
import (
"archive/zip"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
@@ -78,44 +77,8 @@ func (s *Server) startEventScheduler() {
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()
if err := s.schedulerLoop().Run(context.Background()); err != nil {
log.Printf("event scheduler loop stopped: %v", err)
}
}()
}
@@ -130,132 +93,6 @@ func (s *Server) wakeEventScheduler() {
}
}
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()
@@ -300,17 +137,20 @@ func (s *Server) generatePDFPage(payload previewEventPayload) error {
if info, err := os.Stat(target); err == nil && info.Size() > 0 {
return nil
}
source, err := s.store.DocumentPath(payload.DocumentHash)
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
lease, err := s.store.MaterializeDocument(ctx, payload.DocumentHash, maxMetadataInputBytes)
if err != nil {
return err
}
defer lease.Close()
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 {
if err := runPreviewCommandContext(ctx, "pdftoppm", "-f", strconv.Itoa(payload.Page), "-l", strconv.Itoa(payload.Page), "-singlefile", "-jpeg", "-scale-to", "1600", lease.Path, base); err != nil {
return err
}
return os.Rename(base+".jpg", target)
@@ -428,9 +268,25 @@ func (s *Server) generatePreview(payload previewEventPayload) error {
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
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
// Image and ZIP decoders consume readers directly. Only subprocesses and
// the legacy Excalidraw renderer need a verified local scratch pathname.
var source string
if payload.Kind == "pdf" || payload.Kind == "video" || payload.Kind == "excalidraw" {
limit := int64(maxMetadataInputBytes)
if payload.Kind == "video" {
limit = 1 << 30
}
if payload.Kind == "excalidraw" {
limit = 16 << 20
}
lease, err := s.store.MaterializeDocument(ctx, payload.DocumentHash, limit)
if err != nil {
return err
}
defer lease.Close()
source = lease.Path
}
workDir, err := os.MkdirTemp(s.previewRoot, ".job-*")
if err != nil {
@@ -442,10 +298,15 @@ func (s *Server) generatePreview(payload previewEventPayload) error {
switch payload.Kind {
case "image":
generated = filepath.Join(workDir, "preview.jpg")
err = generateImageThumbnail(source, generated)
input, openErr := s.store.OpenDocumentContext(ctx, payload.DocumentHash)
if openErr != nil {
return openErr
}
defer input.Close()
err = generateImageThumbnailFromReader(input, generated)
case "pdf":
generated = filepath.Join(workDir, "preview.jpg")
err = runPreviewCommand(
err = runPreviewCommandContext(ctx,
"pdftoppm",
"-f", "1", "-singlefile", "-jpeg",
"-scale-to", "480",
@@ -454,7 +315,7 @@ func (s *Server) generatePreview(payload previewEventPayload) error {
)
case "video":
generated = filepath.Join(workDir, "preview.jpg")
err = runPreviewCommand(
err = runPreviewCommandContext(ctx,
"ffmpeg", "-hide_banner", "-loglevel", "error",
"-ss", "1", "-i", source,
"-frames:v", "1", "-vf", "scale=480:-2",
@@ -465,7 +326,23 @@ func (s *Server) generatePreview(payload previewEventPayload) error {
err = generateExcalidrawSVG(source, generated)
case "archive":
generated = filepath.Join(workDir, "preview.jpg")
err = generateArchiveThumbnail(source, generated, payload.Filename)
if !strings.EqualFold(filepath.Ext(payload.Filename), ".zip") {
return store.ErrEventSkipped
}
input, openErr := s.store.OpenDocumentContext(ctx, payload.DocumentHash)
if openErr != nil {
return openErr
}
defer input.Close()
size, seekErr := input.Seek(0, io.SeekEnd)
if seekErr != nil {
return seekErr
}
reader, zipErr := zip.NewReader(input, size)
if zipErr != nil {
return zipErr
}
err = generateZIPThumbnailFromReader(reader, generated)
default:
return fmt.Errorf("unsupported preview kind %q", payload.Kind)
}
@@ -481,13 +358,22 @@ func (s *Server) generatePreview(payload previewEventPayload) error {
func runPreviewCommand(name string, args ...string) error {
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
return runPreviewCommandContext(ctx, name, args...)
}
func runPreviewCommandContext(ctx context.Context, name string, args ...string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
command := exec.CommandContext(ctx, "nice", append([]string{"-n", "15", name}, args...)...)
output, err := command.CombinedOutput()
output := &boundedMetadataOutput{limit: 2 << 20, cancel: cancel}
command.Stdout, command.Stderr = output, output
command.WaitDelay = time.Second
err := command.Run()
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
return fmt.Errorf("%s failed: %w: %s", name, err, strings.TrimSpace(string(output)))
return fmt.Errorf("%s failed: %w: %s", name, err, strings.TrimSpace(output.buffer.String()))
}
return nil
}
@@ -514,6 +400,10 @@ func generateZIPThumbnail(source, target string) error {
return err
}
defer reader.Close()
return generateZIPThumbnailFromReader(&reader.Reader, target)
}
func generateZIPThumbnailFromReader(reader *zip.Reader, target string) error {
imageCount, fileCount := 0, 0
for _, file := range reader.File {
if file.FileInfo().IsDir() {
+1 -1
View File
@@ -52,7 +52,7 @@ func (s *Server) handleImmutableObject(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
reader, err := s.store.OpenDocument(hash)
reader, err := s.store.OpenDocumentContext(r.Context(), hash)
if err != nil {
http.NotFound(w, r)
return
+1 -1
View File
@@ -56,7 +56,7 @@ func (s *Server) handleJSONStructure(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
reader, err := s.store.OpenDocument(revision.DocumentHash)
reader, err := s.store.OpenDocumentContext(r.Context(), revision.DocumentHash)
if err != nil {
http.NotFound(w, r)
return
+144
View File
@@ -0,0 +1,144 @@
package server
import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"pageswiki/internal/compiler"
"pageswiki/internal/kunstzeal"
)
func TestKunstzealCompatibility(t *testing.T) {
var d contentDescriptor = kunstzeal.Descriptor{MediaType: "application/json", Filename: "scene.excalidraw"}
var adapter projectionAdapter = kunstzeal.SelectAdapter(d)
if adapter.ID != selectProjectionAdapter(d).ID || adapter.Delivery != deliveryText || inlineTextWindowThreshold != kunstzeal.InlineTextWindowThreshold {
t.Fatal("adapter compatibility changed")
}
if len(builtinAdapterManifest()) != len(kunstzeal.Catalog()) {
t.Fatal("discovery catalog diverged")
}
for _, kind := range []string{"mermaid", "dot", "graphviz", "d2", "unknown"} {
if diagramHash(kind, "source", "svg") != compiler.DiagramHash(kind, "source", "svg") || diagramHash(kind, "source", "svg") != kunstzeal.DiagramHash(kind, "source", "svg") {
t.Fatal("hash diverged")
}
if allowedDiagramType(kind) != kunstzeal.AllowedDiagramType(kind) || normalizeDiagramType(kind) != kunstzeal.NormalizeDiagramType(kind) {
t.Fatal("kind facade diverged")
}
}
client := diagramHTTPClient()
if client.Timeout != kunstzeal.DiagramTimeout || client.CheckRedirect(nil, nil) != http.ErrUseLastResponse {
t.Fatal("unsafe provider defaults")
}
}
func TestKunstzealRedirectAndErrorIsolation(t *testing.T) {
s, token, _ := configFixture(t)
s.diagramRoot = t.TempDir()
s.mux.HandleFunc("/:/compile/diagram/", s.handleCompileDiagram)
var leaked atomic.Int32
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { leaked.Add(1); io.WriteString(w, "<svg/>") }))
defer other.Close()
var status atomic.Int32
status.Store(http.StatusTemporaryRedirect)
renderer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", other.URL+"/collect-source")
w.WriteHeader(int(status.Load()))
io.WriteString(w, "PRIVATE RENDERER DIAGNOSTICS")
}))
defer renderer.Close()
t.Setenv("KROKI_URL", renderer.URL)
for _, code := range []int{301, 302, 307, 308, 400, 500} {
status.Store(int32(code))
r := httptest.NewRequest("POST", "http://localhost/:/compile/diagram/mermaid", strings.NewReader("private diagram source"))
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 502 || leaked.Load() != 0 || strings.Contains(w.Body.String(), "PRIVATE") || strings.Contains(w.Body.String(), renderer.URL) || w.Header().Get("Cache-Control") != "private, no-store" || !strings.Contains(w.Header().Get("Content-Security-Policy"), "sandbox") {
t.Fatalf("status=%d response=%d leaked=%d body=%s", code, w.Code, leaked.Load(), w.Body.String())
}
}
files, err := filepath.Glob(filepath.Join(s.diagramRoot, "mermaid", "*.svg"))
if err != nil || len(files) != 0 {
t.Fatal("failure cached", files, err)
}
}
func TestKunstzealPostCacheBoundsAndFailureFallback(t *testing.T) {
s, token, _ := configFixture(t)
s.diagramRoot = t.TempDir()
s.mux.HandleFunc("/:/compile/diagram/", s.handleCompileDiagram)
var calls atomic.Int32
renderer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
if r.URL.Path != "/graphviz/svg" {
t.Error("dot not normalized", r.URL.Path)
}
io.WriteString(w, `<svg xmlns="http://www.w3.org/2000/svg"><text>fixture</text></svg>`)
}))
defer renderer.Close()
t.Setenv("KROKI_URL", renderer.URL)
post := func(source string) *httptest.ResponseRecorder {
r := httptest.NewRequest("POST", "http://localhost/:/compile/diagram/dot", strings.NewReader(source))
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
if w := post(strings.Repeat("x", kunstzeal.DiagramSourceLimit+1)); w.Code != 413 || calls.Load() != 0 {
t.Fatal("oversized input sent", w.Code, calls.Load())
}
for i := 0; i < 2; i++ {
w := post("digraph { a -> b }")
if w.Code != 200 || !strings.HasPrefix(w.Body.String(), "<svg") || w.Header().Get("X-Content-Type-Options") != "nosniff" {
t.Fatal(w.Code, w.Body.String())
}
}
if calls.Load() != 1 {
t.Fatal("POST cache key changed", calls.Load())
}
// Cache unavailable: still serve the successful projection, without storing it.
block := filepath.Join(t.TempDir(), "not-directory")
if err := os.WriteFile(block, []byte("blocked"), 0600); err != nil {
t.Fatal(err)
}
s.diagramRoot = block
if w := post("digraph { c -> d }"); w.Code != 200 || calls.Load() != 2 {
t.Fatal("cache failure broke rendering", w.Code, calls.Load())
}
}
func TestKunstzealDiskCacheNeedsLiveProvenance(t *testing.T) {
s, token, domain := configFixture(t)
s.diagramRoot = t.TempDir()
s.mux.HandleFunc("/:/compile/diagram/", s.handleCompileDiagram)
hash := diagramHash("mermaid", t.Name(), "svg")
if err := kunstzeal.WriteDiagramCache(s.diagramCachePath("mermaid", hash), []byte("<svg/>")); err != nil {
t.Fatal(err)
}
url := "/:/compile/diagram/mermaid/" + hash + ".svg"
if w := configRequest(s, token, "GET", url, nil); w.Code != 404 {
t.Fatal("orphan cache was served", w.Code)
}
diagramSourceMu.Lock()
diagramSourceCache[hash] = diagramCacheEntry{Type: "mermaid", Domain: domain, Path: "/guide", Source: "source", At: time.Now().Add(-31 * time.Minute)}
diagramSourceMu.Unlock()
t.Cleanup(func() { diagramSourceMu.Lock(); delete(diagramSourceCache, hash); diagramSourceMu.Unlock() })
if w := configRequest(s, token, "GET", url, nil); w.Code != 404 {
t.Fatal("expired provenance was served", w.Code)
}
diagramSourceMu.Lock()
entry := diagramSourceCache[hash]
entry.At = time.Now()
diagramSourceCache[hash] = entry
diagramSourceMu.Unlock()
if w := configRequest(s, token, "GET", url, nil); w.Code != 200 || w.Body.String() != "<svg/>" {
t.Fatal("authorized cache not served", w.Code, w.Body.String())
}
}
+1 -1
View File
@@ -162,7 +162,7 @@ func (s *Server) expandCityMacrosContext(ctx context.Context, body, currentPath,
if context.Quartier != nil {
quartierName = context.Quartier.Name
}
return `<a href="` + html.EscapeString(cityDetailHref(kind, node.Name, quartierName, node.UUID)) + `">` + html.EscapeString(node.Title) + `</a>`
return `<a href="` + html.EscapeString(cityDetailHref(kind, node.Name, quartierName, node.UUID)) + `">` + html.EscapeString(node.DisplayTitle()) + `</a>`
}
return cityMacroRe.ReplaceAllStringFunc(body, func(macro string) string {
if ctx.Err() != nil {
+9
View File
@@ -0,0 +1,9 @@
package server
import "pageswiki/internal/mestionora"
// Construction is read-only: schema/index maintenance remains an explicit
// application startup operation, never a search request side effect.
func (s *Server) searchEngine() *mestionora.SQLite {
return mestionora.NewSQLite(s.store.DB)
}
+12 -101
View File
@@ -2,18 +2,14 @@ package server
import (
"context"
"crypto/ecdh"
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
webpush "github.com/SherClockHolmes/webpush-go"
"pageswiki/internal/ordoschnellic"
"pageswiki/internal/store"
)
@@ -22,32 +18,10 @@ type browserPushInput struct {
Focused bool `json:"focused"`
Seen []string `json:"seen"`
}
type storedBrowserPush struct {
webpush.Subscription
Origin string `json:"origin"`
}
type storedBrowserPush = ordoschnellic.StoredBrowserPush
// Only browser push vendors are destinations, never arbitrary user-provided URLs.
// DNS pinning/public-address checks at dial time provide a second SSRF boundary.
func validPushSubscription(sub webpush.Subscription) bool {
u, e := url.Parse(sub.Endpoint)
if e != nil || u.Scheme != "https" || u.User != nil || u.Fragment != "" || u.Port() != "" || len(sub.Endpoint) > 4096 {
return false
}
host := u.Hostname()
if host != "fcm.googleapis.com" && host != "updates.push.services.mozilla.com" && host != "web.push.apple.com" && !strings.HasSuffix(host, ".push.services.mozilla.com") {
return false
}
auth, e := base64.RawURLEncoding.DecodeString(sub.Keys.Auth)
if e != nil || len(auth) != 16 {
return false
}
key, e := base64.RawURLEncoding.DecodeString(sub.Keys.P256dh)
if e != nil {
return false
}
_, e = ecdh.P256().NewPublicKey(key)
return e == nil
return ordoschnellic.ValidPushSubscription(sub)
}
func (s *Server) notificationPushKeys(ctx context.Context) (private, public string, err error) {
@@ -93,7 +67,7 @@ func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request,
if r.Method == "DELETE" {
err = s.store.DeleteNotificationPush(ctx, domain, owner, input.Subscription.Endpoint)
} else {
raw, _ := json.Marshal(storedBrowserPush{input.Subscription, s.socialOrigin(r)})
raw, _ := json.Marshal(storedBrowserPush{Subscription: input.Subscription, Origin: s.socialOrigin(r)})
err = s.store.SaveNotificationPush(ctx, domain, owner, s.authTokenFromRequest(r), input.Subscription.Endpoint, string(raw), input.Focused)
if err == nil && input.Focused {
err = s.store.AcknowledgeNotificationPush(ctx, domain, owner, input.Subscription.Endpoint, input.Seen)
@@ -110,79 +84,16 @@ func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request,
http.Error(w, "method not allowed", 405)
}
// Compatibility entry points for existing callers; event registration passes
// its worker context through deliverNotificationPushContext.
func (s *Server) deliverNotificationPush(job *store.EventJob) error {
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
// Bounded retention even for abandoned devices. Inbox is not affected.
_, err := s.store.DB.ExecContext(ctx, `DELETE FROM notification_push_subscriptions WHERE id IN (SELECT id FROM notification_push_subscriptions WHERE expires_at_ms<? LIMIT 100)`, time.Now().UnixMilli())
if err != nil {
return err
}
_, err = s.store.DB.ExecContext(ctx, `DELETE FROM notification_push_queue WHERE rowid IN (SELECT rowid FROM notification_push_queue WHERE created_at_ms<? LIMIT 500)`, time.Now().Add(-24*time.Hour).UnixMilli())
if err != nil {
return err
}
client := calendarSourceClient()
client.Timeout = 5 * time.Second
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
sent, failed := 0, 0
for i := 0; i < 20 && ctx.Err() == nil; i++ {
d, err := s.store.ClaimNotificationPush(ctx)
if err != nil {
return err
}
if d == nil {
break
}
if d.Focused {
if err = s.store.DeferFocusedNotificationPush(ctx, d); err != nil {
return err
}
continue
}
retry, expired := false, false
if d.Subscription != "" && !d.Focused {
var sub storedBrowserPush
if json.Unmarshal([]byte(d.Subscription), &sub) != nil || !validPushSubscription(sub.Subscription) {
expired = true
} else {
var success bool
success, retry, expired = s.sendNotificationPush(ctx, d, sub, client)
if success {
sent++
} else {
failed++
}
}
}
// Finish using an independent short context if outbound delivery exhausted its budget.
finish, stop := context.WithTimeout(context.Background(), 3*time.Second)
err = s.store.FinishNotificationPush(finish, d, retry, expired)
stop()
if err != nil {
return err
}
}
if sent+failed > 0 {
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("push sent=%d failed=%d (no payload or endpoint logged)", sent, failed))
}
return nil
return s.deliverNotificationPushContext(context.Background(), job)
}
func (s *Server) sendNotificationPush(ctx context.Context, d *store.NotificationPushDelivery, sub storedBrowserPush, client webpush.HTTPClient) (success, retry, expired bool) {
if !validPushSubscription(sub.Subscription) {
return false, false, true
}
private, public, e := s.notificationPushKeys(ctx)
if e != nil {
return false, true, false
}
// No task titles, body text or credentials leave the server in a push payload.
payload, _ := json.Marshal(map[string]string{"uuid": d.Notification, "owner": d.Owner, "domain": d.Domain})
resp, e := webpush.SendNotificationWithContext(ctx, payload, &sub.Subscription, &webpush.Options{HTTPClient: client, Subscriber: sub.Origin, VAPIDPrivateKey: private, VAPIDPublicKey: public, TTL: 300, Urgency: webpush.UrgencyNormal, Topic: "pageswiki-inbox"})
if e != nil {
return false, true, false
}
defer resp.Body.Close()
return resp.StatusCode >= 200 && resp.StatusCode < 300, resp.StatusCode == 429 || resp.StatusCode >= 500, resp.StatusCode == 404 || resp.StatusCode == 410
copy := *d
raw, _ := json.Marshal(sub)
copy.Subscription = string(raw)
outcome := (ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: client}).Send(ctx, &copy)
return outcome.Success, outcome.Retry, outcome.Expired
}
+2 -2
View File
@@ -22,7 +22,7 @@ func (f pushHTTPFixture) Do(r *http.Request) (*http.Response, error) { return f(
func TestNotificationPushEncryptedDeliveryResponses(t *testing.T) {
s, _, domain, owner := taskFixture(t)
sub := storedBrowserPush{testPushSubscription(t), "https://wiki.example"}
sub := storedBrowserPush{Subscription: testPushSubscription(t), Origin: "https://wiki.example"}
d := &store.NotificationPushDelivery{Notification: uuid.NewString(), Domain: domain, Owner: owner}
for _, code := range []int{201, 400, 404, 410, 429, 500} {
client := pushHTTPFixture(func(r *http.Request) (*http.Response, error) {
@@ -99,7 +99,7 @@ func TestNotificationPushQueueAtomicityRevocationAndFocus(t *testing.T) {
s, token, domain, owner := taskFixture(t)
ctx := context.Background()
sub := testPushSubscription(t)
raw, _ := json.Marshal(storedBrowserPush{sub, "https://wiki.example"})
raw, _ := json.Marshal(storedBrowserPush{Subscription: sub, Origin: "https://wiki.example"})
if err := s.store.SaveNotificationPush(ctx, domain, owner, token, sub.Endpoint, string(raw), true); err != nil {
t.Fatal(err)
}
+49
View File
@@ -0,0 +1,49 @@
package server
import (
"context"
"fmt"
"net/http"
"time"
"pageswiki/internal/ordoschnellic"
"pageswiki/internal/store"
)
type notificationDeliveryQueue struct{ store *store.Store }
func (q notificationDeliveryQueue) Cleanup(ctx context.Context, at time.Time) error {
return q.store.CleanupNotificationPush(ctx, at)
}
func (q notificationDeliveryQueue) Claim(ctx context.Context, at time.Time) (*ordoschnellic.PushDelivery, error) {
return q.store.ClaimNotificationPushAt(ctx, at)
}
func (q notificationDeliveryQueue) DeferFocused(ctx context.Context, d *ordoschnellic.PushDelivery, at time.Time) error {
return q.store.DeferFocusedNotificationPushAt(ctx, d, at)
}
func (q notificationDeliveryQueue) Finish(ctx context.Context, d *ordoschnellic.PushDelivery, outcome ordoschnellic.Outcome, at time.Time) error {
return q.store.FinishNotificationPushAt(ctx, d, outcome.Retry, outcome.Expired, at)
}
func notificationDeliveryClient() *http.Client {
// Preserve pinned public-address dialing and never follow push redirects.
client := calendarSourceClient()
client.Timeout = 5 * time.Second
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
return client
}
func (s *Server) deliverNotificationPushContext(ctx context.Context, job *store.EventJob) error {
dispatcher := ordoschnellic.Dispatcher{
Queue: notificationDeliveryQueue{s.store},
Sender: ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: notificationDeliveryClient()},
}
stats, err := dispatcher.Run(ctx)
if err != nil {
return err
}
if stats.Sent+stats.Failed > 0 {
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("component=%s push sent=%d failed=%d (no payload or endpoint logged)", ordoschnellic.Component, stats.Sent, stats.Failed))
}
return nil
}
+235
View File
@@ -0,0 +1,235 @@
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/ordoschnellic"
"pageswiki/internal/store"
)
func queuedPushFixture(t *testing.T, focused bool) (*Server, store.Notification, time.Time) {
t.Helper()
s, token, domain, owner := taskFixture(t)
sub := testPushSubscription(t)
raw, err := json.Marshal(storedBrowserPush{Subscription: sub, Origin: "https://wiki.example"})
if err != nil {
t.Fatal(err)
}
if err = s.store.SaveNotificationPush(context.Background(), domain, owner, token, sub.Endpoint, string(raw), focused); err != nil {
t.Fatal(err)
}
tx, err := s.store.DB.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
n, _, err := s.store.CreateNotificationTx(tx, store.Notification{DomainUUID: domain, RecipientUUID: owner, EventUUID: uuid.NewString(), Kind: "task.due", Message: "synthetic private fixture"})
if err != nil {
t.Fatal(err)
}
if err = tx.Commit(); err != nil {
t.Fatal(err)
}
return s, n, time.Now()
}
func TestOrdoschnellicQueueLeaseAndStaleCompletion(t *testing.T) {
s, _, at := queuedPushFixture(t, false)
ctx := context.Background()
var wg sync.WaitGroup
claims := make(chan *store.NotificationPushDelivery, 8)
errs := make(chan error, 8)
for i := 0; i < 8; i++ {
wg.Add(1)
go func() { defer wg.Done(); d, err := s.store.ClaimNotificationPushAt(ctx, at); claims <- d; errs <- err }()
}
wg.Wait()
close(claims)
close(errs)
for err := range errs {
if err != nil {
t.Fatal(err)
}
}
var first *store.NotificationPushDelivery
count := 0
for d := range claims {
if d != nil {
first = d
count++
}
}
if count != 1 || first.Attempt != 1 {
t.Fatalf("claims=%d first=%+v", count, first)
}
if d, err := s.store.ClaimNotificationPushAt(ctx, at.Add(ordoschnellic.PushLeaseDuration-time.Millisecond)); err != nil || d != nil {
t.Fatal("lease not held", d, err)
}
second, err := s.store.ClaimNotificationPushAt(ctx, at.Add(ordoschnellic.PushLeaseDuration))
if err != nil || second == nil || second.Attempt != 2 {
t.Fatal("lease recovery", second, err)
}
if err = s.store.FinishNotificationPushAt(ctx, first, false, false, at); err != nil {
t.Fatal(err)
}
var attempt int
if err = s.store.DB.QueryRow(`SELECT attempts FROM notification_push_queue`).Scan(&attempt); err != nil || attempt != 2 {
t.Fatal("stale completion removed current claim", attempt, err)
}
if err = s.store.FinishNotificationPushAt(ctx, second, true, false, at); err != nil {
t.Fatal(err)
}
var next, lease int64
if err = s.store.DB.QueryRow(`SELECT next_at_ms,lease_until_ms FROM notification_push_queue`).Scan(&next, &lease); err != nil || next != at.Add(4*time.Minute).UnixMilli() || lease != 0 {
t.Fatal("retry state", next, lease, err)
}
}
func TestOrdoschnellicDispatcherDatabaseOutcomes(t *testing.T) {
for _, test := range []struct {
name string
code int
focused bool
queue, subscription, attempts int
}{{"sent", 201, false, 0, 1, 0}, {"retry", 429, false, 1, 1, 1}, {"expired", 410, false, 0, 0, 0}, {"focused", 201, true, 1, 1, 0}} {
t.Run(test.name, func(t *testing.T) {
s, n, at := queuedPushFixture(t, test.focused)
sends := 0
client := pushHTTPFixture(func(*http.Request) (*http.Response, error) {
sends++
return &http.Response{StatusCode: test.code, Body: io.NopCloser(strings.NewReader(""))}, nil
})
stats, err := (ordoschnellic.Dispatcher{Queue: notificationDeliveryQueue{s.store}, Sender: ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: client}, Now: func() time.Time { return at }}).Run(context.Background())
if err != nil {
t.Fatal(err)
}
var queue, subscription, inbox int
for _, query := range []struct {
sql string
out *int
}{{`SELECT COUNT(*) FROM notification_push_queue`, &queue}, {`SELECT COUNT(*) FROM notification_push_subscriptions`, &subscription}, {`SELECT COUNT(*) FROM notification_inbox WHERE uuid=?`, &inbox}} {
args := []any{}
if query.out == &inbox {
args = append(args, n.UUID)
}
if err = s.store.DB.QueryRow(query.sql, args...).Scan(query.out); err != nil {
t.Fatal(err)
}
}
if queue != test.queue || subscription != test.subscription || inbox != 1 {
t.Fatalf("queue=%d subscription=%d inbox=%d", queue, subscription, inbox)
}
if test.focused {
if sends != 0 || stats != (ordoschnellic.Stats{}) {
t.Fatal("foreground sent", sends, stats)
}
} else if sends != 1 || stats.Sent+stats.Failed != 1 {
t.Fatal(sends, stats)
}
if queue > 0 {
var attempts int
var next int64
if err = s.store.DB.QueryRow(`SELECT attempts,next_at_ms FROM notification_push_queue`).Scan(&attempts, &next); err != nil {
t.Fatal(err)
}
delay := 2 * time.Minute
if test.focused {
delay = time.Minute
}
if attempts != test.attempts || next != at.Add(delay).UnixMilli() {
t.Fatal("reschedule", attempts, next)
}
}
})
}
}
func TestOrdoschnellicHostCancellationAndTransport(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
// No store/job is required: cancellation must prevent all persistence and I/O.
if err := (&Server{}).deliverNotificationPushContext(ctx, nil); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
client := notificationDeliveryClient()
if client.Timeout != 5*time.Second || client.Transport == nil || client.CheckRedirect(nil, nil) != http.ErrUseLastResponse {
t.Fatal("unsafe push HTTP defaults")
}
}
func TestOrdoschnellicEligibility(t *testing.T) {
for _, test := range []struct{ name, sql string }{
{"session", `UPDATE auth_sessions SET revoked_at_ms=1`},
{"principal", `UPDATE principals SET state='suspended'`},
{"membership", `UPDATE domain_memberships SET state='suspended'`},
{"domain", `UPDATE domains SET enabled=0`},
{"read", `UPDATE notification_inbox SET status='read'`},
{"recipient", `UPDATE notification_inbox SET recipient_uuid='other-owner'`},
{"scope", `UPDATE notification_inbox SET domain_uuid='other-domain'`},
} {
t.Run(test.name, func(t *testing.T) {
s, _, at := queuedPushFixture(t, false)
if _, err := s.store.DB.Exec(test.sql); err != nil {
t.Fatal(err)
}
d, err := s.store.ClaimNotificationPushAt(context.Background(), at)
if err != nil || d == nil || d.Subscription != "" {
t.Fatalf("ineligible claim exposed delivery %+v %v", d, err)
}
})
}
}
func TestOrdoschnellicCleanupBoundedAndInboxPreserved(t *testing.T) {
s, n, at := queuedPushFixture(t, false)
tx, err := s.store.DB.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
for i := 0; i < 105; i++ {
_, err = tx.Exec(`INSERT INTO notification_push_subscriptions(id,domain_uuid,owner_uuid,session_hash,subscription,expires_at_ms) VALUES(?,?,?,?,?,?)`, fmt.Sprintf("expired-%d", i), n.DomainUUID, n.RecipientUUID, "synthetic-session", "{}", at.Add(-time.Hour).UnixMilli())
if err != nil {
t.Fatal(err)
}
}
for i := 0; i < 505; i++ {
_, err = tx.Exec(`INSERT INTO notification_push_queue(subscription_id,notification_uuid,created_at_ms,next_at_ms) VALUES(?,?,?,0)`, "expired-0", fmt.Sprintf("old-%d", i), at.Add(-25*time.Hour).UnixMilli())
if err != nil {
t.Fatal(err)
}
}
if err = tx.Commit(); err != nil {
t.Fatal(err)
}
if err = s.store.CleanupNotificationPush(context.Background(), at); err != nil {
t.Fatal(err)
}
for _, query := range []struct {
sql string
want int
}{
{`SELECT COUNT(*) FROM notification_push_subscriptions`, 6},
{`SELECT COUNT(*) FROM notification_push_queue`, 6},
{`SELECT COUNT(*) FROM notification_inbox`, 1},
} {
var count int
if err = s.store.DB.QueryRow(query.sql).Scan(&count); err != nil || count != query.want {
t.Fatalf("%s: count=%d want=%d err=%v", query.sql, count, query.want, err)
}
}
s.store.ReadOnly = true
if err = s.store.CleanupNotificationPush(context.Background(), at); !errors.Is(err, store.ErrReadOnly) {
t.Fatal("readonly cleanup", err)
}
}
+24 -4
View File
@@ -1,9 +1,13 @@
package server
import (
"context"
"encoding/json"
"net/http"
"strings"
"time"
"pageswiki/internal/store"
)
type pathPickerEntry struct {
@@ -31,13 +35,13 @@ func (s *Server) handlePathPickerAPI(w http.ResponseWriter, r *http.Request) {
}
mode := strings.TrimSpace(r.URL.Query().Get("mode"))
switch mode {
case "", "any", "file", "folder", "image", "image-or-folder":
case "", "any", "file", "folder", "image", "image-or-folder", "children":
default:
http.Error(w, "invalid picker mode", http.StatusBadRequest)
return
}
selected := ""
if mode != "folder" && current != "/" {
if mode != "folder" && mode != "children" && current != "/" {
if objectUUID, resolveErr := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), current); resolveErr == nil {
if revision, revisionErr := s.latestRevisionMetadataForRequest(r, objectUUID); revisionErr == nil && (mode == "" || mode == "any" || mode == "file" || imageContentType(revision.ContentType)) {
selected = current
@@ -45,14 +49,30 @@ func (s *Server) handlePathPickerAPI(w http.ResponseWriter, r *http.Request) {
}
}
}
entries, err := s.store.ListBrowserEntriesInDomain(s.domainUUIDForRequest(r), current)
prefix := r.URL.Query().Get("prefix")
if len(prefix) > 1024 {
http.Error(w, "prefix too long", http.StatusBadRequest)
return
}
var entries []store.BrowserEntry
var err error
truncated := false
if mode == "children" {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
entries, truncated, err = s.store.ListPathCompletionNames(ctx, s.domainUUIDForRequest(r), current, prefix)
} else {
entries, err = s.store.ListBrowserEntriesInDomain(s.domainUUIDForRequest(r), current)
}
if err != nil {
http.Error(w, "path listing unavailable", http.StatusInternalServerError)
return
}
result := make([]pathPickerEntry, 0, min(len(entries), 250))
truncated := false
for _, entry := range entries {
if prefix != "" && !strings.HasPrefix(strings.ToLower(entry.Name), strings.ToLower(prefix)) {
continue
}
if !s.requestCanReadPath(r, entry.Path) {
continue
}
+8 -3
View File
@@ -1,6 +1,7 @@
package server
import (
"context"
"database/sql"
"errors"
"html/template"
@@ -28,7 +29,7 @@ func (s *Server) handlePrint(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
view, err := s.printContentView(revision, semanticPath)
view, err := s.printContentViewContext(r.Context(), revision, semanticPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -84,7 +85,11 @@ func (s *Server) resolvePrintRevision(r *http.Request, address string) (*store.R
}
func (s *Server) printContentView(metadata *store.Revision, semanticPath string) (contentView, error) {
size, err := s.store.DocumentSize(metadata.DocumentHash)
return s.printContentViewContext(context.Background(), metadata, semanticPath)
}
func (s *Server) printContentViewContext(ctx context.Context, metadata *store.Revision, semanticPath string) (contentView, error) {
size, err := s.store.DocumentSizeContext(ctx, metadata.DocumentHash)
if err != nil {
return contentView{}, err
}
@@ -108,7 +113,7 @@ func (s *Server) printContentView(metadata *store.Revision, semanticPath string)
return view, nil
}
if adapter.WindowableText && size > maxPrintTextBytes {
window, err := s.store.ReadDocumentWindow(metadata.DocumentHash, 0, maxPrintTextBytes)
window, err := s.store.ReadDocumentWindowContext(ctx, metadata.DocumentHash, 0, maxPrintTextBytes)
if err != nil {
return contentView{}, err
}
@@ -0,0 +1,63 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestBrowserAddressCommandsJS(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("node required")
}
t.Chdir("../..")
if out, err := exec.Command(node, "--test", "internal/server/testdata/browser-address.test.cjs").CombinedOutput(); err != nil {
t.Fatalf("%v\n%s", err, out)
}
}
func TestPriorityNavigationChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
for _, layout := range []string{"classic", "search", "directory"} {
t.Run(layout, func(t *testing.T) { priorityNavigationChromium(t, layout) })
}
}
func priorityNavigationChromium(t *testing.T, layout string) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/e/", s.handleEditorRoute)
s.mux.HandleFunc("/", s.handleHome)
if err := s.store.SetConfigInDomain(domain, "home_layout", layout); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/priority-navigation.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 == "/fixture" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body><script>" + string(script) + "</script></body>"))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/fixture"
if out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir()).CombinedOutput(); err != nil {
t.Fatalf("navigation: %v\n%s", err, out)
}
}
+230
View File
@@ -0,0 +1,230 @@
package server
import (
"context"
"errors"
"html"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestProfileContactPrivacyAndRevocation(t *testing.T) {
s, token, domain := configFixture(t)
ctx := context.Background()
admin, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
owner, err := s.store.CreatePasswordAccount(domain, "phone-owner", "strong-password-for-test", "Phone Owner", "")
if err != nil {
t.Fatal(err)
}
contact := store.ProfileContact{PhoneDisplay: "+81 (90) 1234-5678", Visibility: "private"}
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil {
t.Fatal(err)
}
get := func(viewer string, want bool) store.ProfileContact {
t.Helper()
got, err := s.store.ProfileContactForViewer(ctx, domain, owner.UUID, viewer)
if err != nil || (got.PhoneNumber != "") != want {
t.Fatalf("viewer=%q visible=%v want=%v err=%v", viewer, got.PhoneNumber != "", want, err)
}
if !want && got != (store.ProfileContact{}) {
t.Fatal("hidden contact metadata leaked")
}
return got
}
contact = get(owner.UUID, true)
get(admin.UUID, false) // domain administrator is not consent
get("", false)
get("foreign-user", false)
stale := contact
stale.Version = 0
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, stale, false); !errors.Is(err, store.ErrGovernanceConflict) {
t.Fatalf("stale write: %v", err)
}
contact.Visibility = "domain"
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil {
t.Fatal(err)
}
get(admin.UUID, true)
get("", false)
contact = get(owner.UUID, true)
contact.Visibility = "public"
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, true); !errors.Is(err, store.ErrGovernanceCeiling) {
t.Fatalf("ceiling bypass: %v", err)
}
enableDomainPublicCeiling(t, s, domain, admin.UUID)
policy, err := s.store.UserSpacePolicy(ctx, domain, owner.UUID)
if err != nil {
t.Fatal(err)
}
policy.ProfileVisibility = "public"
policy, err = s.store.SaveUserSpacePolicy(ctx, domain, owner.UUID, policy)
if err != nil {
t.Fatal(err)
}
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err == nil {
t.Fatal("public without confirmation")
}
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, true); err != nil {
t.Fatal(err)
}
get("", true)
changed := get(owner.UUID, true)
changed.PhoneDisplay = "03-1234-5678"
if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, changed, false); err == nil {
t.Fatal("new public phone did not require renewed confirmation")
}
governance, err := s.store.CityGovernance(ctx, domain, admin.UUID, "domain", domain)
if err != nil {
t.Fatal(err)
}
closed := "authenticated_acl"
if _, err = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, "domain", domain, store.CityGovernanceChange{Epoch: governance.Epoch, TopologyVersion: governance.TopologyVersion, Policy: &closed}, false); err != nil {
t.Fatal(err)
}
get("", false)
enableDomainPublicCeiling(t, s, domain, admin.UUID)
get("", true)
policy.ProfileVisibility = "private"
if _, err = s.store.SaveUserSpacePolicy(ctx, domain, owner.UUID, policy); err != nil {
t.Fatal(err)
}
get("", false)
get(admin.UUID, false)
contact = get(owner.UUID, true)
if err = s.store.SetPrincipalState(domain, owner.UUID, "readonly"); err != nil {
t.Fatal(err)
}
if err = s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err == nil {
t.Fatal("readonly writer accepted")
}
if err = s.store.SetPrincipalState(domain, owner.UUID, "active"); err != nil {
t.Fatal(err)
}
contact.PhoneDisplay = ""
if err = s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil {
t.Fatal(err)
}
cleared, err := s.store.ProfileContactForViewer(ctx, domain, owner.UUID, owner.UUID)
if err != nil || cleared.PhoneNumber != "" || cleared.PhoneDisplay != "" || cleared.Visibility != "private" {
t.Fatalf("not cleared: %+v %v", cleared, err)
}
if err = s.store.SaveProfileContact(ctx, "unrelated-domain", owner.UUID, store.ProfileContact{Visibility: "private", PhoneDisplay: "12345"}, false); err == nil {
t.Fatal("cross-domain write accepted")
}
}
func TestProfileContactChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, _ := configFixture(t)
s.mux.HandleFunc("/u/", s.handleUserProfile)
script, err := os.ReadFile("internal/server/testdata/profile-contact.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 == "/fixture" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "375"} {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
screenshot := ""
if base := os.Getenv("PAGESWIKI_CONTACT_SCREENSHOT"); base != "" {
screenshot = base + "-" + width + ".png"
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/fixture", t.TempDir(), screenshot, width, "900").CombinedOutput()
cancel()
if err != nil {
t.Fatalf("contact width=%s: %v\n%s", width, err, out)
}
}
}
func TestProfileContactHTTP(t *testing.T) {
s, token, domain := configFixture(t)
owner, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/u/", s.handleUserProfile)
call := func(method, target string, form url.Values, authenticated bool) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "http://localhost"+target, strings.NewReader(form.Encode()))
if method == "POST" {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
if authenticated {
r.Header.Set("Authorization", "Bearer "+token)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
form := url.Values{"action": {"contact"}, "contact_version": {"0"}, "phone": {"+81 (90) 1234-5678"}, "contact_visibility": {"private"}}
w := call("POST", "/u/admin/_/edit", form, true)
if w.Code != 303 || w.Header().Get("Location") != "/u/admin/_/edit?contact=saved#contact" {
t.Fatalf("save: %d %s", w.Code, w.Body.String())
}
if _, err := s.store.CreatePasswordAccount(domain, "other-phone", "strong-password-test", "Other", ""); err != nil {
t.Fatal(err)
}
if other := call("POST", "/u/other-phone/_/edit", form, true); other.Code != 403 {
t.Fatalf("editing another account: %d", other.Code)
}
w = call("GET", "/u/admin", nil, true)
if w.Code != 200 || !strings.Contains(html.UnescapeString(w.Body.String()), `href="tel:+819012345678"`) || w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatalf("owner view: %d %s", w.Code, w.Body.String())
}
if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 409 {
t.Fatalf("conflict: %d", w.Code)
}
form.Set("contact_version", "1")
form.Set("phone", "123;ext=456")
if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 400 {
t.Fatalf("invalid: %d", w.Code)
}
enableDomainPublicCeiling(t, s, domain, owner.UUID)
policy, err := s.store.UserSpacePolicy(context.Background(), domain, owner.UUID)
if err != nil {
t.Fatal(err)
}
policy.ProfileVisibility = "public"
if _, err = s.store.SaveUserSpacePolicy(context.Background(), domain, owner.UUID, policy); err != nil {
t.Fatal(err)
}
w = call("GET", "/u/admin", nil, false)
if w.Code != 200 || strings.Contains(w.Body.String(), "1234-5678") || strings.Contains(w.Body.String(), "tel:") {
t.Fatal("private phone leaked in public profile", w.Code)
}
form.Set("phone", "+81 (90) 1234-5678")
form.Set("contact_visibility", "public")
form.Set("contact_confirm_public", "on")
if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 303 {
t.Fatalf("public save: %d %s", w.Code, w.Body.String())
}
w = call("GET", "/u/admin", nil, false)
if w.Code != 200 || !strings.Contains(html.UnescapeString(w.Body.String()), `href="tel:+819012345678"`) {
t.Fatal("public phone missing", w.Code)
}
if got := profileContactHref(store.ProfileContact{PhoneNumber: "javascript:alert(1)"}); got != "" {
t.Fatal("unsafe phone href")
}
}
+34 -1
View File
@@ -108,6 +108,24 @@ func (s *Server) handleUserProfile(w http.ResponseWriter, r *http.Request) {
return
}
action := r.FormValue("action")
if action == "contact" {
version, err := strconv.ParseInt(r.FormValue("contact_version"), 10, 64)
if err != nil {
http.Error(w, "invalid contact version", http.StatusBadRequest)
return
}
err = s.store.SaveProfileContact(r.Context(), domainUUID, current.UUID, store.ProfileContact{PhoneDisplay: r.FormValue("phone"), Visibility: r.FormValue("contact_visibility"), Version: version}, r.FormValue("contact_confirm_public") == "on")
if err != nil {
status := http.StatusBadRequest
if errors.Is(err, store.ErrGovernanceConflict) {
status = http.StatusConflict
}
http.Error(w, translate(s.getLocale(domainUUID), err.Error()), status)
return
}
http.Redirect(w, r, "/u/"+url.PathEscape(profile.Username)+"/_/edit?contact=saved#contact", http.StatusSeeOther)
return
}
if action == "visibility" {
version, parseErr := strconv.ParseInt(r.FormValue("visibility_version"), 10, 64)
if parseErr != nil {
@@ -235,7 +253,22 @@ func (s *Server) handleUserProfile(w http.ResponseWriter, r *http.Request) {
if editing {
userPages, _ = s.store.ListUserSpacePages(r.Context(), domainUUID, profile.UUID, 512)
}
s.renderWithContext(w, r, "profile.gohtml", map[string]any{"Principal": profile, "Editing": editing, "Saved": r.URL.Query().Get("saved") == "1", "VisibilitySaved": r.URL.Query().Get("visibility") == "saved", "Handle": profile.Username + "@" + profileHost(r), "RenderedHTML": template.HTML(rendered), "TOTPEnabled": totpEnabled, "TOTPSecret": pendingSecret, "TOTPUri": store.TOTPURI(pendingSecret, profile.Username, s.getWikiName(domainUUID)), "TOTPStatus": r.URL.Query().Get("totp"), "UserSpacePolicy": visibility, "UserSpacePages": userPages, "Locale": s.getLocale(domainUUID)})
contact, contactErr := s.store.ProfileContactForViewer(r.Context(), domainUUID, profile.UUID, viewer.UUID)
if contactErr != nil {
http.Error(w, "contact unavailable", http.StatusServiceUnavailable)
return
}
w.Header().Set("Cache-Control", "private, no-store")
s.renderWithContext(w, r, "profile.gohtml", map[string]any{"Principal": profile, "Editing": editing, "Saved": r.URL.Query().Get("saved") == "1", "VisibilitySaved": r.URL.Query().Get("visibility") == "saved", "Handle": profile.Username + "@" + profileHost(r), "RenderedHTML": template.HTML(rendered), "TOTPEnabled": totpEnabled, "TOTPSecret": pendingSecret, "TOTPUri": store.TOTPURI(pendingSecret, profile.Username, s.getWikiName(domainUUID)), "TOTPStatus": r.URL.Query().Get("totp"), "UserSpacePolicy": visibility, "UserSpacePages": userPages, "Contact": contact, "ContactHref": profileContactHref(contact), "ContactSaved": r.URL.Query().Get("contact") == "saved", "Locale": s.getLocale(domainUUID)})
}
func profileContactHref(contact store.ProfileContact) template.URL {
_, number, err := store.NormalizeContactPhone(contact.PhoneNumber)
if err != nil || number == "" {
return ""
}
// Only digits and an optional leading '+' survive validation above.
return template.URL("tel:" + number)
}
func (s *Server) serveProfileAvatar(w http.ResponseWriter, r *http.Request, principalUUID string) {
+5 -11
View File
@@ -10,7 +10,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
@@ -157,19 +156,14 @@ func (s *Server) generateMetadataProjection(payload metadataProjectionPayload) e
func (s *Server) inspectProjectionMetadata(payload metadataProjectionPayload) (metadataProjectionResult, error) {
result := metadataProjectionResult{Status: "ready"}
source, err := s.store.DocumentPath(payload.DocumentHash)
if err != nil || source == "" {
return result, errors.New("metadata projection requires immutable Document storage")
}
info, err := os.Stat(source)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
lease, err := s.store.MaterializeDocument(ctx, payload.DocumentHash, maxMetadataInputBytes)
if err != nil {
return result, err
}
if !info.Mode().IsRegular() || info.Size() > maxMetadataInputBytes {
return result, errors.New("metadata source exceeds the 512 MiB limit")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
defer lease.Close()
source := lease.Path
if payload.Kind == "pdf" {
output, truncated, err := runMetadataCommand(ctx, 64<<10, "pdfinfo", "-enc", "UTF-8", source)
if err != nil || truncated {
+4 -1
View File
@@ -395,7 +395,7 @@ func (s *Server) expandPublicMacro(r *http.Request, macro, currentPath string) s
continue
}
if match[1] == "spot" {
return link(sp.DetailHref, sp.Title)
return link(sp.DetailHref, sp.DisplayTitle)
}
var out strings.Builder
out.WriteString(`<ul>`)
@@ -458,6 +458,9 @@ func (s *Server) publicCityProjection(r *http.Request) (store.CityMap, error) {
sp := &st.Spots[pi]
sp.Name = path.Base(sp.PathPrefix)
sp.Title = sp.Name
// Recompute from public-safe fields; the authenticated display
// projection can contain a private custom title.
sp.DisplayTitle = store.CitySpotDisplayTitle(sp.Name, sp.Name, sp.PathPrefix, st.Name)
sp.Summary = ""
sp.State = "active"
sp.SortKey = 0
+1 -1
View File
@@ -135,7 +135,7 @@ func (s *Server) servePublicPeriodical(w http.ResponseWriter, r *http.Request) b
if i >= 20 || titles[e.Path] != "" || !strings.HasPrefix(v.ContentType, "text/html") {
continue
}
reader, err := s.store.OpenDocument(v.DocumentHash)
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
if err != nil {
http.Error(w, "published document unavailable", 503)
return true
+2 -2
View File
@@ -71,7 +71,7 @@ func (s *Server) renderPublicPeriodical(w http.ResponseWriter, r *http.Request,
http.NotFound(w, r)
return
}
reader, err := s.store.OpenDocument(v.DocumentHash)
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
if err != nil {
http.Error(w, "published document unavailable", 503)
return
@@ -291,7 +291,7 @@ func (s *Server) renderPeriodicalRSS(w http.ResponseWriter, r *http.Request, sit
http.Error(w, "publication changed; retry", 503)
return
}
reader, err := s.store.OpenDocument(v.DocumentHash)
reader, err := s.store.OpenDocumentContext(r.Context(), v.DocumentHash)
if err != nil {
http.Error(w, "published document unavailable", 503)
return
+1 -1
View File
@@ -144,7 +144,7 @@ func (s *Server) serveAssetHead(w http.ResponseWriter, r *http.Request, asset *s
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
reader, err := s.store.OpenDocument(asset.HeadDocumentHash)
reader, err := s.store.OpenDocumentContext(r.Context(), asset.HeadDocumentHash)
if err != nil {
http.NotFound(w, r)
return
+35
View File
@@ -0,0 +1,35 @@
package server
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os/exec"
"testing"
"time"
)
func TestRemoteCLIHTTPIntegration(t *testing.T) {
if _, err := exec.LookPath("python3"); err != nil {
t.Skip("python3 not installed")
}
s, _, _, _ := editorFixture(t)
s.mux.HandleFunc("/_/auth/", s.handleAuth)
s.mux.HandleFunc("/v/raw/", s.handleRawView)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
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, "python3", "pages-cli/test_pages.py", "--integration")
input, _ := json.Marshal(map[string]string{"origin": server.URL})
cmd.Stdin = bytes.NewReader(input)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("remote CLI: %v\n%s", err, out)
}
}
+7 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"pageswiki/internal/grammaratur"
"pageswiki/internal/render"
)
@@ -17,6 +18,7 @@ func (s *Server) handleMarkdownRenderAPI(
Path string `json:"path"`
Projection bool `json:"projection"`
HTML *string `json:"html"`
Authoring bool `json:"authoring"`
}
err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&req)
@@ -33,7 +35,11 @@ func (s *Server) handleMarkdownRenderAPI(
}
html = *req.HTML
} else {
html = render.Markdown(req.Markdown)
if req.Authoring {
html = grammaratur.MarkdownAuthoringHTML(req.Markdown)
} else {
html = render.Markdown(req.Markdown)
}
}
if req.Projection {
path := req.Path
+19
View File
@@ -2,6 +2,7 @@ package server
import (
"net/http"
"net/url"
"pageswiki/internal/store"
)
@@ -46,6 +47,24 @@ func (s *Server) renderWithContext(
data["AuthUser"] = nil
data["AuthIsAdmin"] = false
domainUUID := s.domainUUIDForRequest(r)
data["CommandEnabled"] = name != "home.gohtml" && name != "search_home.gohtml" && r.URL.Path != "/"
data["CommandBrowser"] = name == "browser.gohtml"
data["CommandPath"] = "/"
if semantic, ok := data["Path"].(string); ok && len(semantic) > 0 && semantic[0] == '/' {
data["CommandPath"] = semantic
}
data["QuickThemes"] = themeOptions()
data["DefaultThemeCSS"] = s.getThemeCSS(domainUUID)
data["QuickThemeID"] = ""
if cookie, err := r.Cookie("pageswiki_theme"); err == nil {
id, decodeErr := url.PathUnescape(cookie.Value)
if theme, ok := findTheme(id); ok && decodeErr == nil {
// Presentation only: never change the Domain's configured theme.
data["ThemeCSS"] = theme.CSS
data["QuickThemeID"] = theme.ID
}
}
w.Header().Add("Vary", "Cookie")
data["CalendarEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.calendar")
data["TasksEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.tasks")
if r.URL.Path == "/" {
+4 -46
View File
@@ -1,52 +1,10 @@
package server
import (
"os"
"strconv"
"strings"
import "pageswiki/internal/seheweit"
"github.com/google/uuid"
)
// A new process gets a new identity, even when a container reuses PID 1.
var resourceProcessID = uuid.NewString()
type processIOCounters struct {
Scope string `json:"scope"`
Available bool `json:"available"`
ProcessID string `json:"process_id"`
ReadBytes uint64 `json:"read_bytes"`
WriteBytes uint64 `json:"write_bytes"`
}
func readProcessIO() processIOCounters {
data, _ := os.ReadFile("/proc/self/io")
return parseProcessIO(string(data))
}
// Compatibility facade; the process identity and parser belong to Seheweit.
type processIOCounters = seheweit.ProcessIO
func parseProcessIO(data string) processIOCounters {
result := processIOCounters{Scope: "process", ProcessID: resourceProcessID}
read, write := false, false
for _, line := range strings.Split(data, "\n") {
fields := strings.Fields(line)
if len(fields) != 2 {
continue
}
if fields[0] != "read_bytes:" && fields[0] != "write_bytes:" {
continue
}
value, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return result
}
if fields[0] == "read_bytes:" {
result.ReadBytes = value
read = true
} else {
result.WriteBytes = value
write = true
}
}
result.Available = read && write
return result
return seheweit.ParseProcessIO(data)
}
+2 -2
View File
@@ -169,7 +169,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
// not build the full City graph or traverse its Page placements here.
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
city, err := s.store.SearchCityInDomain(ctx, domain, scope, query, 30, allowed, options)
city, err := s.searchEngine().SearchCityInDomain(ctx, domain, scope, query, 30, allowed, options)
if err != nil {
s.renderSearchFailure(w, r, query, scope, localityKind, localityID, err)
return
@@ -177,7 +177,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
for _, result := range city {
cityResults = append(cityResults, citySearchResultView(result, query, location))
}
found, err := s.store.SearchPathsInDomainWithOptions(
found, err := s.searchEngine().SearchPathsInDomainWithOptions(
ctx, domain, scope, query, 100, allowed, options,
)
if err != nil {
+4 -7
View File
@@ -98,10 +98,7 @@ type searchCityViewResult struct {
}
func citySearchResultView(result store.CitySearchResult, query string, location *time.Location) searchCityViewResult {
title := result.Title
if title == "" {
title = result.Name
}
title := result.DisplayTitle()
context := []string{}
if result.QuartierTitle != "" {
context = append(context, result.QuartierTitle)
@@ -112,12 +109,12 @@ func citySearchResultView(result store.CitySearchResult, query string, location
return searchCityViewResult{
CitySearchResult: result,
KindKey: "city." + result.Kind,
Href: cityDetailHref(result.Kind, result.Name, result.QuartierName, result.UUID),
PathHref: (&url.URL{Path: result.PathPrefix}).EscapedPath(),
Href: cityContentHref(result),
PathHref: cityContentHref(result),
ContextLabel: strings.Join(context, " / "),
Updated: time.UnixMilli(result.UpdatedAtMS).In(location).Format("2006-01-02 15:04"),
NameParts: highlightSearchText(title, query),
PathParts: highlightSearchText(result.PathPrefix, query),
PathParts: highlightSearchText(result.ContentPath(), query),
ExcerptParts: highlightSearchText(searchExcerpt(result.Description, "text/plain", query), query),
}
}
+3 -3
View File
@@ -102,13 +102,13 @@ func TestSearchCityResultsPrecedePagesAndHaveSafeLinks(t *testing.T) {
}
city := body[cityStart:pagesStart]
for _, value := range []string{`data-city-search-kind="quartier"`, `data-city-search-kind="street"`, `data-city-search-kind="spot"`,
`href="/v/city/q/nebula-campus"`, `href="/v/city/s/nebula-campus/nebula-way"`, `href="/v/city/p/nebula-spot"`,
`href="/city%3F%23%25"`, `<mark>Nebula</mark>`, `&lt;img src=x onerror=alert(1)&gt;`} {
`href="/v/browser/city%3F%23%25"`, `href="/v/browser/city%3F%23%25/street"`, `href="/v/browser/city%3F%23%25/street/spot"`,
`<mark>Nebula</mark>`, `&lt;img src=x onerror=alert(1)&gt;`} {
if !strings.Contains(city, value) {
t.Errorf("missing City result element %q", value)
}
}
for _, value := range []string{`data-child-path`, `children-entry`, `CITY_HIDDEN_MARKER`, `<img src=x`} {
for _, value := range []string{`data-child-path`, `children-entry`, `CITY_HIDDEN_MARKER`, `<img src=x`, `href="/v/city/`} {
if strings.Contains(city, value) {
t.Errorf("City result has unsafe/unreadable/file-operation data %q", value)
}
+6 -9
View File
@@ -63,10 +63,7 @@ func pageSuggestion(result store.PathSuggestion, query string) searchSuggestionI
}
func citySuggestion(result store.CitySearchResult, query string) searchSuggestionItem {
label := result.Title
if label == "" {
label = result.Name
}
label := result.DisplayTitle()
parents := make([]string, 0, 2)
if result.QuartierTitle != "" {
parents = append(parents, result.QuartierTitle)
@@ -81,10 +78,10 @@ func citySuggestion(result store.CitySearchResult, query string) searchSuggestio
match = "path"
}
return searchSuggestionItem{
Kind: result.Kind, Label: label, Path: result.PathPrefix,
Href: cityDetailHref(result.Kind, result.Name, result.QuartierName, result.UUID),
Kind: result.Kind, Label: label, Path: result.ContentPath(),
Href: cityContentHref(result),
Context: strings.Join(parents, " / "), Match: match,
Exact: suggestionExact(query, result.Name, result.Title, result.PathPrefix),
Exact: suggestionExact(query, result.Name, label, result.ContentPath()),
}
}
@@ -110,7 +107,7 @@ func (s *Server) handleSearchSuggestAPI(w http.ResponseWriter, r *http.Request)
defer cancel()
domain := s.domainUUIDForRequest(r)
allowed := s.pathReadFilter(r)
pages, err := s.store.SearchPathSuggestionsInDomain(ctx, domain, "/", query, 8, allowed)
pages, err := s.searchEngine().SearchPathSuggestionsInDomain(ctx, domain, "/", query, 8, allowed)
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
@@ -123,7 +120,7 @@ func (s *Server) handleSearchSuggestAPI(w http.ResponseWriter, r *http.Request)
items := make([]searchSuggestionItem, 0, 12)
// City metadata is useful but optional enrichment. A slow City query must
// not discard already-authorized Page suggestions.
if city, cityErr := s.store.SearchCityInDomain(ctx, domain, "/", query, 4, allowed, store.SearchOptions{}); cityErr == nil {
if city, cityErr := s.searchEngine().SearchCityInDomain(ctx, domain, "/", query, 4, allowed, store.SearchOptions{}); cityErr == nil {
for _, result := range city {
items = append(items, citySuggestion(result, query))
}
+56
View File
@@ -0,0 +1,56 @@
package server
import (
"context"
"encoding/json"
"errors"
"testing"
"pageswiki/internal/seheweit"
"pageswiki/internal/store"
)
func TestSeheweitAPIContractAndReadOnlyCollection(t *testing.T) {
s, token, _ := configFixture(t)
seedMonitorJobs(t, s)
s.schedulerRunning.Store(2)
w := configRequest(s, token, "GET", "/_/api/v1/queue/resources", nil)
var snapshot seheweit.Snapshot
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &snapshot) != nil {
t.Fatalf("snapshot %d %s", w.Code, w.Body.String())
}
if snapshot.Queue.Stats.Queued != 1 || snapshot.Queue.Stats.Running != 1 || snapshot.Queue.ActiveWorkers != 2 || snapshot.IO.ProcessID == "" {
t.Fatalf("application connection %+v", snapshot)
}
if w.Header().Get("Cache-Control") != "no-store" {
t.Fatal("live response cached")
}
for _, tc := range []struct {
query string
hours int
}{{"", 24}, {"?hours=invalid", 24}, {"?hours=1", 1}, {"?hours=168", 168}} {
w = configRequest(s, token, "GET", "/_/api/v1/queue/resources/history"+tc.query, nil)
var result struct {
Samples []json.RawMessage
Hours int
RetentionDays int `json:"retention_days"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result.Hours != tc.hours || result.RetentionDays != 7 || result.Samples == nil || len(result.Samples) != 0 {
t.Fatalf("history default/empty shape %s", w.Body.String())
}
}
// Neither current-value nor history GET creates persisted samples.
var count int
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 0 {
t.Fatalf("request persisted sample: %d %v", count, err)
}
ctx, cancel := context.WithCancel(t.Context())
cancel()
err := s.registeredEventHandlers().Run(ctx, &store.EventJob{UUID: "cancelled-monitor", Type: store.ResourceEventType})
if !errors.Is(err, context.Canceled) {
t.Fatalf("cancelled job %v", err)
}
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 0 {
t.Fatal("cancelled job persisted sample")
}
}
+30 -8
View File
@@ -10,6 +10,7 @@ import (
"strings"
"time"
"pageswiki/internal/grammaratur"
"pageswiki/internal/store"
)
@@ -179,6 +180,11 @@ func (s *Server) handleSemanticEditor(
w http.ResponseWriter,
r *http.Request,
) {
requestedEditType := r.URL.Query().Get("edittype")
if requestedEditType != "" && requestedEditType != "html" && requestedEditType != "markdown" && requestedEditType != "plain" {
http.Error(w, "unsupported editor format", http.StatusBadRequest)
return
}
path := semanticEditorPath(r.URL.Path)
@@ -212,6 +218,14 @@ func (s *Server) handleSemanticEditor(
)
body := ""
if lang := r.URL.Query().Get("lang"); lang != "" {
latest, err = s.store.GetRevisionForExactLanguage(objectUUID, lang)
}
if err != nil {
http.Error(w, "revision unavailable or invalid language", 400)
return
}
baseBody := latest.Body
editType := "html"
contentType := "text/html; charset=utf-8"
filename := ""
@@ -221,6 +235,9 @@ func (s *Server) handleSemanticEditor(
view := revisionContentView(latest, path)
if view.Kind == "code" {
editType = "code"
if strings.EqualFold(strings.TrimSpace(strings.Split(latest.ContentType, ";")[0]), "text/plain") {
editType = "plain"
}
} else if view.Kind != "html" {
http.Error(
w,
@@ -235,20 +252,23 @@ func (s *Server) handleSemanticEditor(
body = latest.Body
}
requestedEditType := r.URL.Query().Get("edittype")
if editType != "code" && requestedEditType != "" {
if requestedEditType == "plain" && latest.UUID != "" && editType != "plain" {
http.Error(w, "PlainText is available for new documents and text/plain files; existing content is not converted", http.StatusConflict)
return
}
if latest.UUID == "" && requestedEditType == "plain" {
editType = "plain"
contentType = "text/plain; charset=utf-8"
filename = path[strings.LastIndex(path, "/")+1:]
} else if editType != "code" && editType != "plain" && requestedEditType != "" {
editType = requestedEditType
} else if editType != "code" && requestedEditType == "" {
} else if editType != "code" && editType != "plain" && requestedEditType == "" {
editType = s.getEditorFormat(s.domainUUIDForRequest(r))
}
if editType == "markdown" {
compiled, err :=
s.compileMarkdownFromPath(
r,
path,
)
compiled, err := grammaratur.HTMLToMarkdownAuthoring(body)
if err != nil {
http.Error(
@@ -271,6 +291,8 @@ func (s *Server) handleSemanticEditor(
"Path": path,
"ObjectUUID": objectUUID,
"Body": body,
"BaseBody": baseBody,
"BaseRevision": latest.UUID,
"EditType": editType,
"PageLanguage": latest.Language,
"PageContentType": contentType,
+16 -4
View File
@@ -16,6 +16,7 @@ import (
routespkg "pageswiki/internal/server/routes"
"pageswiki/internal/calendar"
"pageswiki/internal/dregarnuhr"
"pageswiki/internal/store"
)
@@ -37,6 +38,10 @@ type Server struct {
schedulerWake chan struct{}
foregroundRequests atomic.Int64
schedulerRunning atomic.Int64
schedulerOnce sync.Once
schedulerRunner *dregarnuhr.Runner
eventHandlersOnce sync.Once
eventHandlers *dregarnuhr.Registry
}
func New(
@@ -52,12 +57,16 @@ func New(
ldapDial: dialLDAPDirectory,
schedulerWake: make(chan struct{}, 1),
}
s.initializePreviewScheduler()
s.initializeImportRoot()
if !st.ReadOnly {
s.initializePreviewScheduler()
s.initializeImportRoot()
}
s.mustLoadTemplates()
s.registerStaticRoutes()
s.mux.HandleFunc("/_/api/v1/editor/state", s.handleEditorState)
s.mux.HandleFunc("/_/api/v1/editor/convert", s.handleEditorConvert)
s.mux.HandleFunc(
"/o/e/",
@@ -99,8 +108,10 @@ func New(
s.registerRoutes()
s.startGCWorker()
s.startEventScheduler()
if !st.ReadOnly && !st.BackgroundJobsDisabled {
s.startGCWorker()
s.startEventScheduler()
}
return s
}
@@ -348,6 +359,7 @@ func (s *Server) handler() http.Handler {
domainHandler.ServeHTTP(w, r)
})
h = s.withStandbyReadOnly(h)
h = mw.NormalizePath(h)
h = mw.RequestID(h)
h = mw.Logging(h)
+26
View File
@@ -0,0 +1,26 @@
const {test} = require('node:test');
const assert = require('node:assert/strict');
const {destination, resolvePath} = require('../../../static/js/browser-address.js');
test('absolute, relative and virtual paths use the browser route', () => {
assert.equal(destination('/docs/new').href, '/v/browser/docs/new');
assert.equal(destination('child', '/docs').href, '/v/browser/docs/child');
assert.equal(destination('cd ../new', '/docs/old').path, '/docs/new');
assert.equal(destination('cd', '/docs').path, '/');
assert.equal(destination('cd ../../../../', '/docs').path, '/');
assert.equal(destination('./a//b/./c/..', '/docs').path, '/docs/a/b');
});
test('editor and viewer commands preserve names and avoid adapter-route ambiguity', () => {
assert.equal(destination('vi plain/example', '/').href, '/e/-/plain/example');
assert.equal(destination('view', '/docs/page').href, '/docs/page');
assert.equal(destination('vi file name', '/docs').href, '/e/-/docs/file%20name');
assert.equal(destination('vi 日記 #1?%25', '/docs').href, '/e/-/docs/%E6%97%A5%E8%A8%98%20%231%3F%2525');
assert.equal(destination('/cd', '/docs').path, '/cd');
assert.equal(destination('view a;rm -rf', '/').href, '/a%3Brm%20-rf');
});
test('schemes, network paths and controls are not accepted', () => {
for (const value of ['https://outside.test', '//outside.test', 'javascript:alert(1)', 'file:///etc/passwd', 'a\\b', '/a\n/b', '/a\u0000b']) {
assert.throws(() => resolvePath(value), /invalid_path/);
}
assert.equal(destination('view /%2f%2fevil.test').href, '/%252f%252fevil.test');
});
+7
View File
@@ -44,6 +44,13 @@ window.addEventListener('load', async () => {
checkLabels('.city-generated-street-label');
activate(document.querySelector('.city-street-tile'));
await wait(() => document.querySelector('.city-undefined-page-label'));
const entrance = document.querySelector('.city-spot-point-label');
if (!entrance || /undefined|未定義|未分類/i.test(entrance.textContent)) throw new Error('Internal placeholder label is visible');
activate(document.querySelector('.city-spot-entry'));
await wait(() => document.querySelector('.city-spot-monument'));
if (document.querySelector('[data-city-trail] [aria-current="location"]')?.textContent !== entrance.querySelector('title')?.textContent) throw new Error('Spot breadcrumb differs from display title');
activate(document.querySelector('[data-city-level="street"]'));
await wait(() => document.querySelector('.city-undefined-page-label'));
checkLabels('.city-generated-street-label,.city-undefined-page-label');
toggleLabels(false);
if (getComputedStyle(document.querySelector('.city-undefined-page-label')).display !== 'none')
+63
View File
@@ -0,0 +1,63 @@
(async () => {
const assert=(ok,message)=>{if(!ok)throw Error(message)};
const wait=async test=>{for(let n=0;n<160;n++){if(test())return;await new Promise(r=>setTimeout(r,25))}throw Error('Timed out: '+test)};
const frame=document.createElement('iframe');frame.style='width:100%;height:930px;border:0';document.body.append(frame);
const load=path=>new Promise(resolve=>{frame.onload=resolve;frame.src=path});
const key=(d,target,key,extras={})=>target.dispatchEvent(new frame.contentWindow.KeyboardEvent('keydown',{key,bubbles:true,cancelable:true,...extras}));
try {
await load('/');let d=frame.contentDocument;
assert(!d.querySelector('[data-command-bar]'),'Home has command interception');
await load('/v/browser/notes');d=frame.contentDocument;
let bar=d.querySelector('[data-command-bar]'),input=bar.querySelector('input');
assert(!bar.hidden && bar.parentElement===d.body,'Command bar not at body level');
assert(d.querySelector('.topbar [data-browser-address]'),'Address not integrated with topbar');
assert(!!d.querySelector('[data-browser-address]').getClientRects().length === (innerWidth>1100),'Responsive address visibility');
key(d,d.body,'c');assert(input===d.activeElement&&input.value==='c','Type-to-command lost first character');
input.value='cd al';input.dispatchEvent(new Event('input',{bubbles:true}));key(d,input,'Tab');
await wait(()=>input.value==='cd alpha');key(d,input,'Tab');assert(input.value==='cd alpine','Tab did not cycle');
key(d,input,'Tab',{shiftKey:true});assert(input.value==='cd alpha','Shift Tab did not cycle back');
key(d,input,'Escape');assert(!bar.hidden&&input.value==='','Browser Escape must clear, not remove bar');
input.focus();input.value='cd /not-committed';input.dispatchEvent(new frame.contentWindow.CompositionEvent('compositionstart',{bubbles:true}));bar.requestSubmit();assert(frame.contentWindow.location.pathname==='/v/browser/notes','IME confirmation navigated');input.dispatchEvent(new frame.contentWindow.CompositionEvent('compositionend',{bubbles:true}));input.value='';
const other=d.createElement('input');d.body.append(other);other.focus();key(d,other,'x');assert(input.value===''&&d.activeElement===other,'Other input intercepted');other.remove();
const modal=d.createElement('dialog');d.body.append(modal);modal.showModal();key(d,d.body,'x');assert(input.value==='','Modal intercepted');modal.close();modal.remove();
const fetchBefore=frame.contentWindow.fetch;
let release;
frame.contentWindow.fetch=()=>new Promise(resolve=>{release=resolve});
input.focus();input.value='vi stale';input.dispatchEvent(new Event('input',{bubbles:true}));key(d,input,'Tab');
key(d,input,'Escape');release(new Response(JSON.stringify({entries:[{name:'stale',kind:'file'}]}),{headers:{'Content-Type':'application/json'}}));
await new Promise(r=>setTimeout(r,30));assert(input.value===''&&bar.querySelector('[role=listbox]').hidden,'Stale completion reopened list');frame.contentWindow.fetch=fetchBefore;
input.focus();input.value='cd /日本';input.dispatchEvent(new Event('input',{bubbles:true}));key(d,input,'Tab');await wait(()=>input.value==='cd /日本語 #?/');
key(d,input,'Tab');await wait(()=>input.value==='cd /日本語 #?/child');
input.value='cd /notes';input.dispatchEvent(new Event('input',{bubbles:true}));
await new Promise(resolve=>{frame.onload=resolve;bar.requestSubmit()});assert(frame.contentWindow.location.pathname==='/v/browser/notes','Command navigation failed');
await load('/notes');d=frame.contentDocument;bar=d.querySelector('[data-command-bar]');input=bar.querySelector('input');
assert(bar.hidden,'Viewer command bar initially open');key(d,d.body,'g');assert(!bar.hidden&&d.activeElement===input&&input.value==='','g activation failed');
key(d,input,'Escape');assert(bar.hidden,'Viewer Escape failed');
const code=d.querySelector('.page-html pre code');
await wait(()=>code?.querySelector('.pw-syntax-keyword'));
assert(!code.querySelector('script')&&code.textContent.includes('<script>literal<'+'/script>'),'Highlight interpreted HTML');
const original=code.textContent,keyword=code.querySelector('.pw-syntax-keyword');
const theme=d.querySelector('[data-theme-select]');
const change=async value=>{theme.value=value;theme.dispatchEvent(new frame.contentWindow.Event('change',{bubbles:true}));await wait(()=>!theme.disabled);await new Promise(r=>setTimeout(r,60))};
await change('index-white');const light=frame.contentWindow.getComputedStyle(keyword).color;
await change('nanoha-dark');assert(light!==frame.contentWindow.getComputedStyle(keyword).color,'Viewer syntax did not follow theme');
assert(code.textContent===original,'Theme changed code text');
const viewerTarget=d.createElement('div');d.body.append(viewerTarget);
const viewer=frame.contentWindow.PagesWikiEditor.createViewer(viewerTarget,'def sample():\n return "value"','python');
await wait(()=>viewerTarget.querySelector('.pw-syntax-keyword'));
assert(frame.contentWindow.getComputedStyle(viewerTarget.querySelector('.pw-syntax-keyword')).color===frame.contentWindow.getComputedStyle(keyword).color,'File viewer and code block palettes differ');viewer.destroy();viewerTarget.remove();
for(const option of theme.options){if(!option.value)continue;await change(option.value);assert(frame.contentWindow.getComputedStyle(keyword).color!=='rgba(0, 0, 0, 0)','Invisible theme token');assert(code.textContent===original,'Theme altered code');}
const late=d.createElement('pre'), lateCode=d.createElement('code');lateCode.className='language-js';lateCode.textContent='const number = 42;';late.append(lateCode);code.closest('.page-html').append(late);late.scrollIntoView();await wait(()=>lateCode.querySelector('.pw-syntax-keyword'));
const large=d.createElement('code');large.className='language-python';large.textContent='return 1;'.repeat(5000);const largePre=d.createElement('pre');largePre.append(large);code.closest('.page-html').append(largePre);largePre.scrollIntoView();await new Promise(r=>setTimeout(r,80));assert(!large.querySelector('span'),'Unbounded large block parsed');largePre.remove();late.remove();
await load('/e/-/editor-fixture?edittype=markdown');d=frame.contentDocument;
await wait(()=>frame.contentWindow.PagesWikiCodeMirror);
const editor=frame.contentWindow.PagesWikiCodeMirror;editor.focus();key(d,editor.contentDOM,'g');assert(d.querySelector('[data-command-bar]').hidden,'Vim g intercepted');
editor.dispatch({changes:{from:0,to:editor.state.doc.length,insert:'```python\ndef sample():\n return "value"\n```'}});
await wait(()=>d.querySelector('.cm-content .pw-syntax-keyword'));
// End on the browser UI for visual review.
await load('/v/browser/notes');d=frame.contentDocument;bar=d.querySelector('[data-command-bar]');input=bar.querySelector('input');
if(innerWidth<800){assert(d.documentElement.scrollWidth<=frame.clientWidth+1,'Browser overflows mobile');frame.contentWindow.toggleSidebar();assert(d.querySelector('.browser-sidebar').getClientRects().length,'Mobile sidebar cannot open');frame.contentWindow.toggleSidebar();}
input.focus();input.value='view al';input.dispatchEvent(new Event('input',{bubbles:true}));key(d,input,'Tab');await wait(()=>input.value==='view alpha');
document.body.dataset.vimReady='1';
} catch(error){const out=document.createElement('pre');out.id='error';out.textContent=error.stack;document.body.append(out)}
})();
+114
View File
@@ -0,0 +1,114 @@
(async () => {
const assert=(ok,message)=>{if(!ok)throw Error(message)};
const delay=ms=>new Promise(r=>setTimeout(r,ms));
const wait=async fn=>{for(let i=0;i<150;i++){if(fn())return;await delay(25)}throw Error('Editor condition timed out')};
try {
const frame=document.createElement('iframe');frame.style='width:100%;height:930px;border:0';document.body.append(frame);
await new Promise(resolve=>{frame.onload=resolve;frame.src='/e/-/editor-fixture?edittype=html'});
const w=frame.contentWindow,d=frame.contentDocument;
await wait(()=>w.PagesWikiCodeMirror&&d.querySelector('#preview h1'));
const e=w.PagesEditor, cm=w.PagesWikiCodeMirror, realFetch=w.fetch;
e.paused=true;
assert(e.owner,'Owner namespace missing');
assert(e.baseRevision,'Base revision missing');
// Real CodeMirror updates must mark dirty and store an authoring draft.
cm.dispatch({changes:{from:0,to:cm.state.doc.length,insert:'<h1>Local</h1><p>one two three</p>'}});
assert(e.dirty,'CodeMirror did not mark dirty');e.persist();
const key=e.keyPrefix()+e.tab;
assert(JSON.parse(w.localStorage.getItem(key)).text.includes('Local'),'Missing local draft');
w.fetch=async()=>{throw Error('offline fixture')};
assert(await e.saveNow()===false&&e.paused,'Offline save was not paused');
assert(e.text().includes('Local')&&w.localStorage.getItem(key),'Offline save lost text');
w.fetch=async()=>new w.Response('expired',{status:401});
assert(await e.saveNow()===false&&d.querySelector('#editor-save-status').dataset.state==='auth','Expiry undetected');
w.fetch=realFetch;
assert(await e.saveNow(),'Manual retry failed');assert(!w.localStorage.getItem(key),'Saved draft not cleared');
// Another transaction changes the same title: manual review, never overwrite.
const base=e.baseRevision;
const remote=await e.post('/_/api/v1/revisions/save',{object_uuid:e.value('object-uuid'),body:'<h1>Remote</h1><p>one two three</p>',lang:'und',content_type:'text/html',base_revision_uuid:base,editor_principal_uuid:e.owner});
e.paused=true;e.setText('<h1>Mine</h1><p>one two three</p>');
assert(await e.saveNow()===false,'Stale edit silently saved');
assert(d.querySelector('#editor-dialog textarea[readonly]'),'Conflict comparison missing');
assert((await e.state()).revision_uuid===remote.revision_uuid,'Remote overwritten');
const areas=d.querySelectorAll('#editor-dialog textarea');areas[areas.length-1].value='<h1>Resolved</h1><p>one two three</p>';
d.querySelector('#editor-dialog footer button:last-child').click();await wait(()=>!d.querySelector('#editor-dialog'));
assert(await e.saveNow(),'Conflict resolution not saved');
// A save response can be lost; retry acknowledges the already saved head.
e.paused=true;e.setText('<h1>Acknowledged</h1>');
let lost=false;
w.fetch=async(url,opts)=>{const response=await realFetch(url,opts);if(String(url).includes('revisions/save')&&!lost){lost=true;throw Error('lost response')}return response};
assert(!await e.saveNow(),'Lost response incorrectly successful');
w.fetch=realFetch;await e.saveNow();assert(!e.dirty,'Retry did not acknowledge saved head');
// Preserve newly typed text while converting / saving an older snapshot.
e.paused=true;e.setText('# Markdown','markdown');
let release,entered=false;
w.fetch=async(url,opts)=>{if(String(url).includes('render/markdown')&&!JSON.parse(opts.body).projection){entered=true;await new Promise(r=>release=r)}return realFetch(url,opts)};
const pending=e.saveNow();await wait(()=>entered);
e.setText('# Newer');release();await pending;w.fetch=realFetch;
assert(e.dirty&&e.text()==='# Newer','Typing during save was lost');e.paused=true;clearTimeout(e.autosaveTimer);
assert(await e.saveNow(),'Newer text save failed');
// Format changes preserve preview visibility and complete a safe conversion.
w.setEditorPreviewVisible(false);e.paused=true;
await e.switchFormat('html');await e.switchFormat('markdown');
assert(d.querySelector('#preview').hidden,'Format switch changed preview state');
assert(e.text().includes('Newer'),'Format switch lost source');
// Recover a draft into a new tab key and remove only that recovered snapshot.
e.persist();const oldKey=e.keyPrefix()+e.tab;e.tab=w.crypto.randomUUID();
e.offerRecovery();d.querySelector('#editor-dialog footer button:nth-child(2)').click();
await wait(()=>!d.querySelector('#editor-dialog'));
assert(w.localStorage.getItem(e.keyPrefix()+e.tab),'Recovery not stored');
assert(!w.localStorage.getItem(oldKey),'Recovered draft would reappear after save');
// Restore is staged locally, then creates a new revision on confirmation.
const beforeRestore=(await e.state()).revision_uuid;
await e.historyDialog();
const revisions=d.querySelector('#editor-dialog select');revisions.selectedIndex=revisions.options.length-1;
d.querySelector('#editor-dialog footer button:last-child').click();await wait(()=>!d.querySelector('#editor-dialog'));
assert((await e.state()).revision_uuid===beforeRestore,'Restore wrote before confirmation');
assert(e.text().includes('Base'),'Old revision not loaded');assert(await e.saveNow(),'Restore save failed');
assert(e.baseRevision!==beforeRestore,'Restore did not append revision');
// Separate ranges automatically merge, then repeat CAS against the new head.
e.paused=true;e.setText('<p>left middle right</p>','html');assert(await e.saveNow(),'Merge base save failed');
await e.post('/_/api/v1/revisions/save',{object_uuid:e.value('object-uuid'),body:'<p>left middle RIGHT</p>',lang:'und',content_type:'text/html',base_revision_uuid:e.baseRevision,editor_principal_uuid:e.owner});
e.paused=true;e.setText('<p>LEFT middle right</p>');await e.saveNow();
await wait(()=>!e.dirty);
assert((await e.state()).body==='<p>LEFT middle RIGHT</p>','Automatic merge lost a side');
// Protected HTML survives an actual Markdown edit and canonical save.
const html='<figure data-custom="retained"><img src="/photo" alt="Photo"><figcaption>Caption</figcaption></figure>';
e.paused=true;e.setText(html,'html');await e.switchFormat('markdown');
assert(e.text().includes('{=html}'),'Lossy HTML was not protected');
e.setText(e.text().replace('Caption','Edited caption'));
assert(await e.saveNow(),'Protected HTML save failed');
assert((await e.state()).body===html.replace('Caption','Edited caption'),'Protected HTML attributes or structure changed');
// Conversion failure must never save raw Markdown as HTML.
e.paused=true;e.setText('# Conversion failure');
const head=e.baseRevision;
w.fetch=async(url,opts)=>String(url).includes('render/markdown')?new w.Response('failed',{status:503}):realFetch(url,opts);
assert(!await e.saveNow(),'Conversion failure saved anyway');w.fetch=realFetch;
assert((await e.state()).revision_uuid===head,'Conversion failure created revision');
// Fork uses a new empty head for the target language; original stays intact.
e.languageDialog();d.querySelector('#editor-dialog input').value='en';
d.querySelector('#editor-dialog footer button:last-child').click();await wait(()=>!d.querySelector('#editor-dialog'));
assert(e.value('page-language')==='en'&&e.baseRevision==='','Language fork did not reset head');
assert(await e.saveNow(),'Language fork save failed');
assert((await e.state({lang:'und'})).revision_uuid===head,'Fork changed source language');
// Mobile always starts in insert mode, regardless of desktop preference.
const originalMedia=w.matchMedia;w.matchMedia=q=>q==='(pointer: coarse) and (hover: none)'?{matches:true}:originalMedia(q);
w.PagesWikiCodeMirror.destroy();w.PagesWikiCodeMirror=w.PagesWikiEditor.create(e.mount,'{{child}}','markdown',{keymap:'vim',autocomplete:true});
assert(d.querySelector('#editor-vim-mode').textContent==='INSERT','Mobile Vim not disabled');
const mobile=w.PagesWikiCodeMirror;mobile.dispatch({selection:{anchor:7}});mobile.focus();
mobile.contentDOM.dispatchEvent(new w.KeyboardEvent('keydown',{key:' ',code:'Space',ctrlKey:true,bubbles:true}));
await wait(()=>d.querySelector('.cm-tooltip-autocomplete li'));
await delay(100);
mobile.contentDOM.dispatchEvent(new w.KeyboardEvent('keydown',{key:'Enter',code:'Enter',bubbles:true}));
assert(mobile.state.doc.toString()==='{{children}}','Macro completion duplicated brackets: '+mobile.state.doc);
// Escape must not enter Vim normal mode on mobile.
mobile.contentDOM.dispatchEvent(new w.KeyboardEvent('keydown',{key:'Escape',bubbles:true}));
assert(d.querySelector('#editor-vim-mode').textContent==='INSERT','Mobile Vim mode active');
w.matchMedia=originalMedia;
e.paused=true;clearTimeout(e.autosaveTimer);clearInterval(e.stateTimer);
e.tools();
const style=w.getComputedStyle(d.querySelector('#editor-dialog'));
assert(style.color!==style.backgroundColor&&style.backgroundColor!=='rgba(0, 0, 0, 0)','Dialog not themed');
document.body.dataset.vimReady='true';
} catch(error) {const box=document.createElement('div');box.id='error';box.textContent=error.stack||error.message;document.body.prepend(box)}
})();
+61
View File
@@ -0,0 +1,61 @@
(async () => {
const assert = (ok, message) => { if (!ok) throw Error(message); };
const wait = async check => { for (let n=0;n<150;n++) { if(check()) return; await new Promise(r=>setTimeout(r,25)); } throw Error('Timed out: '+check); };
const frame = document.createElement('iframe'); frame.style='width:100%;height:950px;border:0'; document.body.append(frame);
try {
await new Promise(resolve => { frame.onload=resolve; frame.src='/e/-/editor-fixture?edittype=html'; });
const w=frame.contentWindow, d=frame.contentDocument;
await wait(() => w.PagesWikiCodeMirror);
const editor=w.PagesWikiCodeMirror, e=w.PagesEditor;
e.paused=true;
const before=editor.state.doc.toString();
const select=d.querySelector('[data-theme-select]');
const change=async id=>{
select.value=id; select.dispatchEvent(new w.Event('change',{bubbles:true}));
await wait(()=>!select.disabled);
await new Promise(r=>setTimeout(r,80));
assert(!d.querySelector('[data-theme-status]').textContent,'Theme loading error');
};
await change('light');
assert(w.getComputedStyle(editor.dom).backgroundColor==='rgb(247, 247, 247)','Light theme editor remained dark');
assert(w.getComputedStyle(editor.contentDOM).color==='rgb(32, 32, 32)','Light editor text is not dark');
const token=[...d.querySelectorAll('.cm-line span')].find(el=>el.textContent==='h1');
assert(token && w.getComputedStyle(token).color!==w.getComputedStyle(editor.contentDOM).color,'Semantic syntax colors missing');
editor.focus(); await new Promise(r=>setTimeout(r,100));
const cursor=d.querySelector('.cm-fat-cursor,.cm-cursor');
assert(cursor && parseFloat(w.getComputedStyle(cursor).borderBottomWidth)>=2,'Underline cursor missing');
const vimKey=(key,code,keyCode,shiftKey=false)=>editor.contentDOM.dispatchEvent(new w.KeyboardEvent('keydown',{key,code,keyCode,shiftKey,bubbles:true,cancelable:true}));
vimKey('v','KeyV',86);assert(d.body.dataset.editorMode==='visual','Vim visual state not tracked');
vimKey('Escape','Escape',27);assert(d.body.dataset.editorMode==='normal','Vim normal state not tracked');
vimKey('A','KeyA',65,true);assert(d.body.dataset.editorMode==='insert','Vim A insertion not tracked');
vimKey('Escape','Escape',27);
vimKey('R','KeyR',82,true);assert(d.body.dataset.editorMode==='replace','Vim replace state not tracked');
vimKey('Escape','Escape',27);
assert(d.cookie.includes('pageswiki_theme=light'),'Theme choice not retained');
assert(w.PagesWikiCodeMirror===editor && editor.state.doc.toString()===before,'Theme switch replaced editor or lost content');
await change('nanoha-dark');
assert(w.getComputedStyle(editor.dom).backgroundColor!=='rgb(247, 247, 247)','Dark theme did not apply');
await change('index-white');
w.setEditorPreviewVisible(true);
await e.renderPreview();
const article=d.createElement('div'); article.style='width:300px';
const pre=d.createElement('pre'), code=d.createElement('code'); code.textContent='x'.repeat(500); pre.append(code); article.append(pre); d.querySelector('#preview').append(article);
const ancestry=[]; for(let el=pre;el;el=el.parentElement) ancestry.push(el.tagName+'#'+el.id+':'+w.getComputedStyle(el).display+':'+el.clientWidth);
assert(pre.clientWidth>0 && pre.clientWidth<=300 && pre.scrollWidth>pre.clientWidth,'Code block dimensions: '+pre.clientWidth+'/'+pre.scrollWidth+' '+ancestry.join(' > '));
assert(w.getComputedStyle(pre).borderInlineStartWidth==='3px','Code block styling missing');
// PlainText follows the same save/draft machinery but never renders markup.
const plainFrame=d.createElement('iframe'); d.body.append(plainFrame);
await new Promise(resolve=>{plainFrame.onload=resolve;plainFrame.src='/e/-/plain-test.txt?edittype=plain';});
const pw=plainFrame.contentWindow,pd=plainFrame.contentDocument;
await wait(()=>pw.PagesWikiCodeMirror);
const pe=pw.PagesEditor; pe.paused=true;
const literal='<img src=x onerror="alert(1)">\n# Literal\n{{children}}';
pe.setText(literal); await pe.renderPreview();
assert(pd.querySelector('#preview').textContent===literal && !pd.querySelector('#preview img'),'PlainText preview interpreted markup');
assert(await pe.saveNow(),'PlainText save failed');
const state=await pe.state();assert(state.body===literal && state.content_type==='text/plain; charset=utf-8','PlainText canonical data changed');
await pe.switchFormat('html');assert(pe.format()==='plain','PlainText silently switched to HTML');
plainFrame.remove();
document.body.dataset.vimReady='1';
} catch(error) { const out=document.createElement('pre'); out.id='error';out.textContent=error.stack;document.body.append(out); }
})();
+57
View File
@@ -0,0 +1,57 @@
(async () => {
const assert = (ok, message) => { if (!ok) throw Error(message); };
const frame = document.createElement('iframe');
frame.style = 'width:100%;height:900px'; document.body.append(frame);
const load = path => new Promise(resolve => { frame.onload = resolve; frame.src = path; });
const navigate = action => new Promise(resolve => { frame.onload = resolve; action(); });
const key = (target, key, options = {}) => target.dispatchEvent(new frame.contentWindow.KeyboardEvent('keydown', {key, bubbles:true, cancelable:true, ...options}));
try {
await load('/');
let d = frame.contentDocument;
const input = d.querySelector('[data-home-type-search]');
assert(input, 'Home search input missing');
key(d.body, 'x');
assert(d.activeElement === input && input.value === 'x', 'First key lost');
input.blur(); key(d.body, 'l', {ctrlKey:true});
assert(input.value === 'x', 'Browser shortcut captured');
const other = d.createElement('input'); d.body.append(other); other.focus(); key(other, 'z');
assert(input.value === 'x' && d.activeElement === other, 'Other input captured'); other.remove();
const dialog = d.createElement('dialog'); d.body.append(dialog); dialog.showModal(); key(d.body, 'z');
assert(input.value === 'x', 'Modal typing captured'); dialog.close(); dialog.remove();
input.blur(); key(d.body, 'Process', {keyCode:229});
assert(d.activeElement === input && input.value === 'x', 'IME focus failed');
if (d.querySelector('[data-home-pager]')) {
input.blur(); d.querySelector('[data-home-page-target="bookmarks"]').click();
d.activeElement.blur(); key(d.body, 'y');
assert(d.querySelector('[data-home-pager]').dataset.homePage === 'search' && input.value === 'xy', 'Bookmark page did not return to search');
}
await load('/v/browser/'); d = frame.contentDocument;
assert(d.querySelector('[data-browser-address] input').value === '/', 'Root address missing');
d.querySelector('[data-browser-create="cd"]').click();
let create = d.querySelector('[data-browser-create-dialog]');
assert(create.open, 'Folder dialog did not open');
create.querySelector('[name=path]').value = '新規 #?';
await navigate(() => create.querySelector('form').requestSubmit()); d = frame.contentDocument;
assert(d.querySelector('[data-browser-address]').dataset.path === '/新規 #?', 'Virtual folder path corrupted');
assert(d.querySelector('.browser-empty'), 'Virtual folder empty state missing');
let address = d.querySelector('[data-browser-address]');
address.querySelector('input').value = '//evil.example'; address.requestSubmit();
assert(d.querySelector('#browser-address-error').textContent, 'Unsafe URL accepted');
await navigate(() => d.querySelector('[data-browser-up]').click()); d = frame.contentDocument;
assert(d.querySelector('[data-browser-address]').dataset.path === '/', 'Parent navigation failed');
d.querySelector('[data-browser-create="vi"]').click();
create = d.querySelector('[data-browser-create-dialog]'); create.querySelector('[name=path]').value = 'new-document';
await navigate(() => create.querySelector('form').requestSubmit()); d = frame.contentDocument;
assert(d.querySelector('#editor') && frame.contentWindow.location.pathname === '/e/-/new-document', 'New document editor did not open');
await load('/v/browser/'); d = frame.contentDocument;
d.querySelector('[data-browser-create="vi"]').click();
create = d.querySelector('[data-browser-create-dialog]'); create.querySelector('[name=path]').value = 'notes.txt';
create.querySelector('[name=format]').value = 'plain';
await navigate(() => create.querySelector('form').requestSubmit()); d = frame.contentDocument;
assert(d.querySelector('#edit-type').value==='plain' && d.querySelector('#page-content-type').value==='text/plain; charset=utf-8', 'PlainText creation lost format');
document.body.dataset.vimReady = '1';
} catch (error) {
const output = document.createElement('pre'); output.id = 'error'; output.textContent = error.stack; document.body.append(output);
}
})();
+32
View File
@@ -0,0 +1,32 @@
(async () => {
const assert = (ok, message) => { if (!ok) throw Error(message); };
const frame = document.createElement('iframe');
frame.style = 'display:block;width:100%;height:880px;border:0'; document.body.append(frame);
const load = path => new Promise(resolve => { frame.onload = resolve; frame.src = path; });
try {
await load('/u/admin/_/edit');
let d = frame.contentDocument;
let form = d.querySelector('[name=contact_version]').form;
assert(form.querySelector('[name=contact_visibility]').value === 'private', 'Default is not private');
assert(!form.querySelector('[name=contact_confirm_public]').checked, 'Consent was preselected');
const field = form.querySelector('[name=phone]');
assert(field.type === 'tel', 'Phone keyboard hint missing');
field.value = '+81 (90) 1234-5678';
await new Promise(resolve => { frame.onload = resolve; form.requestSubmit(); });
d = frame.contentDocument;
assert(frame.contentWindow.location.search === '?contact=saved', 'Save did not redirect back');
assert(d.querySelector('[name=phone]').value === '+81 (90) 1234-5678', 'Phone display was not retained');
await load('/u/admin'); d = frame.contentDocument;
assert(d.querySelector('.profile-phone a').getAttribute('href') === 'tel:+819012345678', 'tel link malformed');
await load('/u/admin/_/edit#contact'); d = frame.contentDocument;
form = d.querySelector('[name=contact_version]').form;
for (const el of form.querySelectorAll('input:not([type=hidden]),select,button')) {
const r = el.getBoundingClientRect();
assert(r.left >= 0 && r.right <= frame.clientWidth + 1, 'Contact control overflows viewport');
}
d.querySelector('#contact').scrollIntoView();
document.body.dataset.vimReady = '1';
} catch (error) {
const out = document.createElement('pre'); out.id = 'error'; out.textContent = error.stack; document.body.append(out);
}
})();
+7 -3
View File
@@ -30,7 +30,11 @@ window.addEventListener('load', async () => {
assert(!city.querySelector('.children-entry,[data-child-path]'), 'City result is exposed to file operations');
assert(city.querySelector('[data-city-search-kind] .search-result-name mark'), 'City name highlight missing');
assert(city.querySelector('.search-result-path')?.getAttribute('href')?.startsWith('/'), 'City boundary Path link missing');
assert(city.querySelector('.search-result-name')?.getAttribute('href')?.startsWith('/v/city/'), 'City details link missing');
for (const hit of city.querySelectorAll('[data-city-search-kind]')) {
const href = hit.querySelector('.search-result-name')?.getAttribute('href');
assert(href?.startsWith('/') && !href.startsWith('/v/city/'), 'Search links to City settings');
assert(href === hit.querySelector('.search-result-path')?.getAttribute('href'), 'City title and Path destinations differ');
}
assert(!city.querySelector('script,[onload],[onerror]'), 'City source HTML became executable markup');
}
for (const mode of ['list', 'detail', 'icon']) {
@@ -41,7 +45,7 @@ window.addEventListener('load', async () => {
assert(getComputedStyle(panel).display !== 'none', 'Selected mode is hidden: ' + mode);
assert(panel.querySelector('.search-result-name')?.textContent.includes('nebula'), 'Page name missing');
assert(panel.querySelector('.search-result-path')?.textContent.includes('/docs/nebula guide?#%'), 'Full Path missing');
assert(panel.querySelector('.search-result-excerpt mark')?.textContent === 'NEBULA', 'Context highlight missing');
assert([...panel.querySelectorAll('.search-result-excerpt mark')].some(mark => mark.textContent.toLowerCase() === 'nebula'), 'Context highlight missing');
assert(panel.querySelector('[data-match-source="body"]'), 'Match source missing');
assert(panel.querySelector('.search-result-name').closest('a').getAttribute('href').includes('%3F%23%25'), 'Path URL was not encoded');
if (city) assert(getComputedStyle(city).display !== 'none', 'View switching hid City results: ' + mode);
@@ -58,7 +62,7 @@ window.addEventListener('load', async () => {
assert(!cityRow.classList.contains('children-selected'), 'City result became a selectable file');
}
options.open = true;
assert(document.documentElement.scrollWidth <= innerWidth, 'Search introduces horizontal page scrolling');
assert(document.documentElement.scrollWidth <= innerWidth, 'Search introduces horizontal page scrolling: ' + [...document.querySelectorAll('body *')].filter(el => el.getBoundingClientRect().right > innerWidth + 1).slice(0,8).map(el => el.tagName + '.' + el.className + ':' + Math.round(el.getBoundingClientRect().right)).join(', '));
document.body.dataset.vimReady = '1';
console.log('Vim initialized');
} catch (error) {
+1 -1
View File
@@ -25,7 +25,7 @@ func (s *Server) handleTextWindow(w http.ResponseWriter, r *http.Request) {
}
offset, _ := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64)
limit, _ := strconv.ParseInt(r.URL.Query().Get("limit"), 10, 64)
window, err := s.store.ReadDocumentWindow(revision.DocumentHash, offset, limit)
window, err := s.store.ReadDocumentWindowContext(r.Context(), revision.DocumentHash, offset, limit)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
+71
View File
@@ -0,0 +1,71 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestThemePreferenceDoesNotChangeDomain(t *testing.T) {
s, token, domain, _ := editorFixture(t)
for _, tc := range []struct{ value, css string }{
{"light", "/static/css/theme/light.css"},
{"index-white", "/static/css/theme/index-white.css"},
{"https://evil.test/theme.css", "/static/css/theme/dark.css"},
{"unknown", "/static/css/theme/dark.css"},
} {
r := httptest.NewRequest("GET", "http://localhost/e/-/editor-fixture", nil)
r.Header.Set("Authorization", "Bearer "+token)
r.AddCookie(&http.Cookie{Name: "pageswiki_theme", Value: tc.value})
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="`+tc.css+`"`) {
t.Fatalf("%s: %d %s", tc.value, w.Code, w.Body.String())
}
if s.getTheme(domain) != "dark" {
t.Fatal("browser preference overwrote Domain theme")
}
if !strings.Contains(strings.Join(w.Header().Values("Vary"), ","), "Cookie") {
t.Fatal("missing cache variation")
}
}
}
func TestEditorThemeChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, _ := editorFixture(t)
if err := s.store.SetConfigInDomain(domain, "editor_keymap", "vim"); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/editor-theme.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 == "/fixture" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir()}
if shot := os.Getenv("EDITOR_THEME_SCREENSHOT"); shot != "" {
args = append(args, shot)
}
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
t.Fatalf("theme: %v\n%s", err, out)
}
}
+8 -1
View File
@@ -166,7 +166,7 @@ func (s *Server) servePublicUserProfile(w http.ResponseWriter, r *http.Request,
if policy.RootVisibility == "public" {
if revision, revisionErr := s.store.PublicUserSpaceRevision(publicRequest.Context(), s.domainUUIDForRequest(publicRequest), owner.Username, root, s.revisionLanguageCandidates(publicRequest)); revisionErr == nil {
if view := revisionContentView(revision, root); view.Kind == "html" {
reader, openErr := s.store.OpenDocument(revision.DocumentHash)
reader, openErr := s.store.OpenDocumentContext(publicRequest.Context(), revision.DocumentHash)
if openErr == nil {
body, readErr := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
reader.Close()
@@ -178,9 +178,16 @@ func (s *Server) servePublicUserProfile(w http.ResponseWriter, r *http.Request,
}
}
snapshot := publicNavigationFor(publicRequest)
contact, contactErr := s.store.ProfileContactForViewer(publicRequest.Context(), s.domainUUIDForRequest(publicRequest), owner.UUID, "")
if contactErr != nil {
http.Error(w, "contact unavailable", http.StatusServiceUnavailable)
return true
}
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
s.renderWithContext(w, publicRequest, "profile.gohtml", map[string]any{
"Principal": display, "Editing": false, "PublicProfile": true,
"Contact": contact, "ContactHref": profileContactHref(contact),
"Handle": owner.Username + "@" + profileHost(publicRequest), "RenderedHTML": template.HTML(rendered),
"PublicHasChildren": snapshot != nil && snapshot.hasChildren(root),
"PublicChildrenHref": cityPageHref("/v/browser" + root), "Locale": s.getLocale(s.domainUUIDForRequest(publicRequest)),