Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const adapterGatewayPrefix = "/_/adaptor"
|
||||
|
||||
func newAdapterGatewayProxy() http.Handler {
|
||||
origin := strings.TrimSpace(os.Getenv("PAGESWIKI_ADAPTER_GATEWAY_ORIGIN"))
|
||||
if origin == "" {
|
||||
origin = "http://adaptor-gateway:3000"
|
||||
}
|
||||
target, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, "invalid adapter gateway origin", http.StatusBadGateway)
|
||||
})
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
director := proxy.Director
|
||||
proxy.Director = func(request *http.Request) {
|
||||
request.URL.Path = strings.TrimPrefix(request.URL.Path, adapterGatewayPrefix)
|
||||
if request.URL.Path == "" {
|
||||
request.URL.Path = "/"
|
||||
}
|
||||
request.Header.Set("X-Forwarded-Prefix", adapterGatewayPrefix)
|
||||
if request.Header.Get("X-Forwarded-Proto") == "" {
|
||||
if request.TLS != nil {
|
||||
request.Header.Set("X-Forwarded-Proto", "https")
|
||||
} else {
|
||||
request.Header.Set("X-Forwarded-Proto", "http")
|
||||
}
|
||||
}
|
||||
director(request)
|
||||
}
|
||||
return proxy
|
||||
}
|
||||
@@ -0,0 +1,477 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const adapterPluginsConfigKey = "projection_adapter_plugins"
|
||||
|
||||
type adapterPlugin struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Mode string `json:"mode"` // hosted viewer or hosted editor
|
||||
URLTemplate string `json:"url_template"`
|
||||
MediaTypes []string `json:"media_types,omitempty"`
|
||||
Extensions []string `json:"extensions,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
NewTab bool `json:"new_tab,omitempty"`
|
||||
HTMLMarkdown bool `json:"html_markdown,omitempty"`
|
||||
DefaultEditor bool `json:"default_editor,omitempty"`
|
||||
Unsupported bool `json:"unsupported,omitempty"`
|
||||
}
|
||||
|
||||
type adapterAction struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Mode string `json:"mode"`
|
||||
Builtin bool `json:"builtin"`
|
||||
OpenURL string `json:"open_url"`
|
||||
NewTab bool `json:"new_tab,omitempty"`
|
||||
Default bool `json:"default,omitempty"`
|
||||
}
|
||||
|
||||
type builtinAdapterInfo struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Delivery string `json:"delivery"`
|
||||
Selectable bool `json:"selectable"`
|
||||
}
|
||||
|
||||
var adapterIDPattern = regexp.MustCompile(`^[a-z][a-z0-9._-]{1,63}$`)
|
||||
|
||||
func builtinAdapterManifest() []builtinAdapterInfo {
|
||||
out := make([]builtinAdapterInfo, 0, len(projectionAdapters))
|
||||
for _, adapter := range projectionAdapters {
|
||||
out = append(out, builtinAdapterInfo{
|
||||
ID: adapter.ID, Kind: adapter.Kind,
|
||||
Delivery: string(adapter.Delivery), Selectable: true,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) adapterPlugins(domainUUID string) ([]adapterPlugin, error) {
|
||||
raw, err := s.store.GetConfigInDomain(domainUUID, adapterPluginsConfigKey)
|
||||
if err != nil || strings.TrimSpace(raw) == "" {
|
||||
return []adapterPlugin{}, err
|
||||
}
|
||||
var plugins []adapterPlugin
|
||||
if err := json.Unmarshal([]byte(raw), &plugins); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Native execution is reserved for a future local-helper protocol. Keep
|
||||
// legacy records visible, but never advertise or launch them.
|
||||
for index := range plugins {
|
||||
validated := plugins[index]
|
||||
if err := validateAdapterPlugin(&validated); err != nil {
|
||||
plugins[index].Enabled = false
|
||||
plugins[index].Unsupported = true
|
||||
}
|
||||
}
|
||||
return plugins, nil
|
||||
}
|
||||
|
||||
func (s *Server) saveAdapterPlugins(domainUUID string, plugins []adapterPlugin) error {
|
||||
sort.Slice(plugins, func(i, j int) bool { return plugins[i].ID < plugins[j].ID })
|
||||
data, err := json.Marshal(plugins)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.SetConfigInDomain(domainUUID, adapterPluginsConfigKey, string(data))
|
||||
}
|
||||
|
||||
func validateAdapterPlugin(plugin *adapterPlugin) error {
|
||||
plugin.ID = strings.ToLower(strings.TrimSpace(plugin.ID))
|
||||
plugin.Name = strings.TrimSpace(plugin.Name)
|
||||
plugin.Mode = strings.ToLower(strings.TrimSpace(plugin.Mode))
|
||||
plugin.URLTemplate = strings.TrimSpace(plugin.URLTemplate)
|
||||
if !adapterIDPattern.MatchString(plugin.ID) || plugin.Name == "" {
|
||||
return errors.New("invalid adapter id or name")
|
||||
}
|
||||
switch plugin.Mode {
|
||||
case "viewer", "editor":
|
||||
default:
|
||||
return errors.New("mode must be viewer or editor; native integration is not implemented")
|
||||
}
|
||||
if !strings.Contains(plugin.URLTemplate, "{url}") &&
|
||||
!strings.Contains(plugin.URLTemplate, "{path}") &&
|
||||
!strings.Contains(plugin.URLTemplate, "{ticket}") {
|
||||
return errors.New("URL template must contain {url}, {path}, or {ticket}")
|
||||
}
|
||||
if plugin.Mode == "editor" && !strings.Contains(plugin.URLTemplate, "{ticket}") {
|
||||
return errors.New("hosted editor URL template must contain {ticket}")
|
||||
}
|
||||
sample := strings.NewReplacer(
|
||||
"{url}", "https://example.invalid/v/raw/path:/sample.txt",
|
||||
"{path}", "/sample.txt",
|
||||
"{filename}", "sample.txt",
|
||||
"{ticket}", "pwa_t_sample",
|
||||
"{origin}", "https://pages.example.invalid",
|
||||
).Replace(plugin.URLTemplate)
|
||||
parsed, err := url.Parse(sample)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") {
|
||||
return errors.New("hosted adapter URL must use http or https")
|
||||
}
|
||||
for index, extension := range plugin.Extensions {
|
||||
extension = strings.ToLower(strings.TrimSpace(extension))
|
||||
if extension != "" && !strings.HasPrefix(extension, ".") {
|
||||
extension = "." + extension
|
||||
}
|
||||
plugin.Extensions[index] = extension
|
||||
}
|
||||
for index, mediaType := range plugin.MediaTypes {
|
||||
plugin.MediaTypes[index] = strings.ToLower(strings.TrimSpace(mediaType))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pluginMatches(plugin adapterPlugin, mediaType, filename string) bool {
|
||||
if !plugin.Enabled || plugin.Unsupported || plugin.Mode == "native" {
|
||||
return false
|
||||
}
|
||||
if len(plugin.MediaTypes) == 0 && len(plugin.Extensions) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, candidate := range plugin.MediaTypes {
|
||||
if candidate == mediaType ||
|
||||
(strings.HasSuffix(candidate, "/*") &&
|
||||
strings.HasPrefix(mediaType, strings.TrimSuffix(candidate, "*"))) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
extension := strings.ToLower(filepath.Ext(filename))
|
||||
for _, candidate := range plugin.Extensions {
|
||||
if extension == candidate {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) adapterActionsForPath(r *http.Request, semanticPath string) ([]adapterAction, error) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
objectUUID, err := s.store.ResolvePathInDomain(domainUUID, semanticPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
revision, err := s.latestRevisionForRequest(r, objectUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mediaType, _, _ := mime.ParseMediaType(revision.ContentType)
|
||||
filename := revision.Filename
|
||||
if filename == "" {
|
||||
filename = filepath.Base(semanticPath)
|
||||
}
|
||||
selected := selectProjectionAdapter(contentDescriptor{
|
||||
MediaType: strings.ToLower(mediaType), Filename: filename,
|
||||
SizeBytes: int64(len(revision.Body)),
|
||||
})
|
||||
actions := []adapterAction{
|
||||
{ID: selected.ID, Name: selected.ID, Mode: "viewer", Builtin: true, OpenURL: semanticPath},
|
||||
{ID: "download", Name: "download", Mode: "viewer", Builtin: true,
|
||||
OpenURL: rawProjectionURL("rev:" + revision.UUID)},
|
||||
}
|
||||
if selected.NeedsText || selected.ID == "html" || selected.ID == "excalidraw" {
|
||||
actions = append(actions, adapterAction{
|
||||
ID: "editor", Name: "Pages Wiki Editor", Mode: "editor",
|
||||
Builtin: true, OpenURL: "/e" + semanticPath,
|
||||
})
|
||||
}
|
||||
plugins, err := s.adapterPlugins(domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.SliceStable(plugins, func(i, j int) bool {
|
||||
return plugins[i].DefaultEditor && !plugins[j].DefaultEditor
|
||||
})
|
||||
for _, plugin := range plugins {
|
||||
if pluginMatches(plugin, strings.ToLower(mediaType), filename) {
|
||||
actions = append(actions, adapterAction{
|
||||
ID: plugin.ID, Name: plugin.Name, Mode: plugin.Mode,
|
||||
NewTab: plugin.NewTab,
|
||||
Default: plugin.DefaultEditor,
|
||||
OpenURL: "/v/open-with?adapter=" + url.QueryEscape(plugin.ID) +
|
||||
"&path=" + url.QueryEscape(semanticPath),
|
||||
})
|
||||
}
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapterAPI(w http.ResponseWriter, r *http.Request) {
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter"), "/")
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
switch action {
|
||||
case "":
|
||||
if r.Method == http.MethodGet {
|
||||
plugins, err := s.adapterPlugins(domainUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
response := map[string]any{
|
||||
"builtin": builtinAdapterManifest(), "plugins": plugins,
|
||||
}
|
||||
if path := strings.TrimSpace(r.URL.Query().Get("path")); path != "" {
|
||||
actions, err := s.adapterActionsForPath(r, path)
|
||||
if err != nil {
|
||||
status := 500
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
status = 404
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
response["actions"] = actions
|
||||
}
|
||||
writeEventJSON(w, 200, response)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodPut {
|
||||
var plugin adapterPlugin
|
||||
if !decodeEventJSON(w, r, &plugin) {
|
||||
return
|
||||
}
|
||||
if err := validateAdapterPlugin(&plugin); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
plugins, err := s.adapterPlugins(domainUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if plugin.DefaultEditor {
|
||||
if plugin.Mode != "editor" {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "only an editor can be the default editor"})
|
||||
return
|
||||
}
|
||||
for index := range plugins {
|
||||
plugins[index].DefaultEditor = false
|
||||
}
|
||||
}
|
||||
replaced := false
|
||||
for index := range plugins {
|
||||
if plugins[index].ID == plugin.ID {
|
||||
plugins[index] = plugin
|
||||
replaced = true
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
plugins = append(plugins, plugin)
|
||||
}
|
||||
if err := s.saveAdapterPlugins(domainUUID, plugins); err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, plugin)
|
||||
return
|
||||
}
|
||||
case "delete":
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodDelete {
|
||||
var input struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
plugins, err := s.adapterPlugins(domainUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
filtered := plugins[:0]
|
||||
for _, plugin := range plugins {
|
||||
if plugin.ID != input.ID {
|
||||
filtered = append(filtered, plugin)
|
||||
}
|
||||
}
|
||||
if err := s.saveAdapterPlugins(domainUUID, filtered); err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]bool{"deleted": true})
|
||||
return
|
||||
}
|
||||
}
|
||||
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported adapter operation"})
|
||||
}
|
||||
|
||||
func (s *Server) handleOpenWith(w http.ResponseWriter, r *http.Request) {
|
||||
semanticPath := strings.TrimSpace(r.URL.Query().Get("path"))
|
||||
adapterID := strings.TrimSpace(r.URL.Query().Get("adapter"))
|
||||
if semanticPath == "" || adapterID == "" {
|
||||
http.Error(w, "path and adapter are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if s.authorizePathRequest(r, "read", semanticPath) != nil {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
plugins, err := s.adapterPlugins(s.domainUUIDForRequest(r))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
var selected *adapterPlugin
|
||||
for index := range plugins {
|
||||
if plugins[index].ID == adapterID && plugins[index].Enabled {
|
||||
selected = &plugins[index]
|
||||
break
|
||||
}
|
||||
}
|
||||
if selected == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
actions, err := s.adapterActionsForPath(r, semanticPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
return
|
||||
}
|
||||
allowed := false
|
||||
for _, action := range actions {
|
||||
if action.ID == selected.ID {
|
||||
allowed = true
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
http.Error(w, "adapter does not support this document", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), semanticPath)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
revision, err := s.latestRevisionForRequest(r, objectUUID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
scheme := "https"
|
||||
if r.TLS == nil {
|
||||
scheme = "http"
|
||||
}
|
||||
if forwarded := strings.ToLower(strings.TrimSpace(
|
||||
strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0],
|
||||
)); forwarded == "http" || forwarded == "https" {
|
||||
scheme = forwarded
|
||||
}
|
||||
rawURL := fmt.Sprintf("%s://%s%s", scheme, r.Host, rawProjectionURL("rev:"+revision.UUID))
|
||||
filename := revision.Filename
|
||||
if filename == "" {
|
||||
filename = filepath.Base(semanticPath)
|
||||
}
|
||||
ticket := ""
|
||||
if selected.Mode == "editor" {
|
||||
if s.authorizePathRequest(r, "write", semanticPath) != nil {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
principalUUID := ""
|
||||
if principal, principalErr := s.store.PrincipalForSession(s.domainUUIDForRequest(r), s.authTokenFromRequest(r)); principalErr == nil {
|
||||
principalUUID = principal.UUID
|
||||
}
|
||||
ticket, err = s.store.IssueAdapterTicketForPrincipal(
|
||||
principalUUID, s.domainUUIDForRequest(r), selected.ID, objectUUID, revision.UUID, semanticPath,
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
origin := fmt.Sprintf("%s://%s", scheme, r.Host)
|
||||
target := strings.NewReplacer(
|
||||
"{url}", url.QueryEscape(rawURL),
|
||||
"{path}", url.QueryEscape(semanticPath),
|
||||
"{filename}", url.QueryEscape(filename),
|
||||
"{ticket}", url.QueryEscape(ticket),
|
||||
"{origin}", url.QueryEscape(origin),
|
||||
).Replace(selected.URLTemplate)
|
||||
http.Redirect(w, r, target, http.StatusFound)
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapterPluginConfig(w http.ResponseWriter, r *http.Request) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
id := strings.TrimSpace(r.URL.Query().Get("id"))
|
||||
if r.Method == http.MethodPost {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id = strings.TrimSpace(r.FormValue("id"))
|
||||
}
|
||||
plugins, err := s.adapterPlugins(domainUUID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
index := -1
|
||||
for candidate := range plugins {
|
||||
if plugins[candidate].ID == id {
|
||||
index = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
if index < 0 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
if r.FormValue("connection_fields") == "1" {
|
||||
plugins[index].Name = strings.TrimSpace(r.FormValue("name"))
|
||||
plugins[index].Mode = strings.TrimSpace(r.FormValue("mode"))
|
||||
plugins[index].URLTemplate = strings.TrimSpace(r.FormValue("url_template"))
|
||||
plugins[index].Enabled = r.FormValue("enabled") != ""
|
||||
plugins[index].MediaTypes = splitAdapterList(r.FormValue("media_types"))
|
||||
plugins[index].Extensions = splitAdapterList(r.FormValue("extensions"))
|
||||
if err := validateAdapterPlugin(&plugins[index]); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
}
|
||||
plugins[index].NewTab = boolFormValue(r.FormValue("new_tab")) == "true"
|
||||
plugins[index].HTMLMarkdown = boolFormValue(r.FormValue("html_markdown")) == "true"
|
||||
makeDefault := boolFormValue(r.FormValue("default_editor")) == "true"
|
||||
if makeDefault && plugins[index].Mode != "editor" {
|
||||
http.Error(w, "only an editor can be the default editor", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if makeDefault {
|
||||
for candidate := range plugins {
|
||||
plugins[candidate].DefaultEditor = candidate == index
|
||||
}
|
||||
} else {
|
||||
plugins[index].DefaultEditor = false
|
||||
}
|
||||
if err := s.saveAdapterPlugins(domainUUID, plugins); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/-/sys/config?panel="+url.QueryEscape("adapter-"+id)+"&saved=1", http.StatusFound)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/-/sys/config?panel="+url.QueryEscape("adapter-"+id), http.StatusFound)
|
||||
}
|
||||
|
||||
func splitAdapterList(value string) []string {
|
||||
var values []string
|
||||
for _, item := range strings.Split(value, ",") {
|
||||
if item = strings.TrimSpace(item); item != "" {
|
||||
values = append(values, item)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package server
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateAdapterPlugin(t *testing.T) {
|
||||
plugin := adapterPlugin{
|
||||
ID: "Code.Server", Name: "code-server", Mode: "editor",
|
||||
URLTemplate: "https://code.example.test/open?ticket={ticket}&origin={origin}",
|
||||
Extensions: []string{"md", ".txt"},
|
||||
Enabled: true,
|
||||
}
|
||||
if err := validateAdapterPlugin(&plugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if plugin.ID != "code.server" || plugin.Extensions[0] != ".md" {
|
||||
t.Fatalf("normalized plugin = %#v", plugin)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkspaceFilenamePreservesHierarchyAndMarkdownExtension(t *testing.T) {
|
||||
if got := workspaceFilename("/guide/install", "/guide", "install.html", true); got != "install.md" {
|
||||
t.Fatalf("child workspace filename = %q", got)
|
||||
}
|
||||
if got := workspaceFilename("/guide/linux/setup", "/guide", "setup.py", false); got != "linux/setup.py" {
|
||||
t.Fatalf("nested workspace filename = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAdapterPluginRejectsUnsafeOrUnboundTemplate(t *testing.T) {
|
||||
tests := []adapterPlugin{
|
||||
{ID: "bad.scheme", Name: "Bad", Mode: "viewer", URLTemplate: "javascript:{path}"},
|
||||
{ID: "native.todo", Name: "Vim", Mode: "native", URLTemplate: "vim:{path}"},
|
||||
{ID: "no.binding", Name: "Bad", Mode: "viewer", URLTemplate: "https://example.invalid/open"},
|
||||
{ID: "bad.mode", Name: "Bad", Mode: "execute", URLTemplate: "https://example.invalid/{url}"},
|
||||
{ID: "editor.raw", Name: "Bad", Mode: "editor", URLTemplate: "https://example.invalid/{url}"},
|
||||
}
|
||||
for _, plugin := range tests {
|
||||
if err := validateAdapterPlugin(&plugin); err == nil {
|
||||
t.Fatalf("plugin unexpectedly accepted: %#v", plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginMatchesMimeWildcardAndExtension(t *testing.T) {
|
||||
plugin := adapterPlugin{
|
||||
Enabled: true, MediaTypes: []string{"image/*"}, Extensions: []string{".drawio"},
|
||||
}
|
||||
if !pluginMatches(plugin, "image/png", "image.bin") {
|
||||
t.Fatal("MIME wildcard did not match")
|
||||
}
|
||||
if !pluginMatches(plugin, "application/xml", "diagram.drawio") {
|
||||
t.Fatal("extension did not match")
|
||||
}
|
||||
if pluginMatches(plugin, "text/plain", "notes.txt") {
|
||||
t.Fatal("unrelated document matched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginMatchesNeverOffersLegacyNativeAdapter(t *testing.T) {
|
||||
plugin := adapterPlugin{Enabled: true, Mode: "native"}
|
||||
if pluginMatches(plugin, "text/plain", "notes.txt") {
|
||||
t.Fatal("legacy native adapter was offered as a runtime action")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAdapterPluginRejectsLegacyEditorWithoutTicket(t *testing.T) {
|
||||
plugin := adapterPlugin{ID: "vscode", Name: "VS Code", Mode: "editor",
|
||||
URLTemplate: "vscode://file/{url}", Enabled: true}
|
||||
if err := validateAdapterPlugin(&plugin); err == nil {
|
||||
t.Fatal("legacy executable URL was accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/compiler"
|
||||
"pageswiki/internal/render"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type adapterWorkspaceFile struct {
|
||||
Path string `json:"path"`
|
||||
SemanticPath string `json:"semantic_path"`
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
ContentType string `json:"content_type"`
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
}
|
||||
|
||||
func (s *Server) adapterPluginForSession(session *store.AdapterSession) adapterPlugin {
|
||||
plugins, _ := s.adapterPlugins(session.DomainUUID)
|
||||
for _, plugin := range plugins {
|
||||
if plugin.ID == session.PluginID {
|
||||
return plugin
|
||||
}
|
||||
}
|
||||
return adapterPlugin{}
|
||||
}
|
||||
|
||||
func workspaceFilename(semanticPath, rootPath, filename string, markdown bool) string {
|
||||
if semanticPath == rootPath {
|
||||
filename = filepath.Base(filename)
|
||||
if filename == "." || filename == "" || filename == "/" {
|
||||
filename = "index.html"
|
||||
}
|
||||
if markdown {
|
||||
filename = strings.TrimSuffix(filename, filepath.Ext(filename)) + ".md"
|
||||
}
|
||||
return filename
|
||||
}
|
||||
relative := strings.TrimPrefix(semanticPath, strings.TrimRight(rootPath, "/")+"/")
|
||||
if filepath.Ext(relative) == "" && filepath.Ext(filename) != "" {
|
||||
relative += filepath.Ext(filename)
|
||||
}
|
||||
if markdown {
|
||||
relative = strings.TrimSuffix(relative, filepath.Ext(relative)) + ".md"
|
||||
}
|
||||
return relative
|
||||
}
|
||||
|
||||
func (s *Server) workspaceFile(session *store.AdapterSession, semanticPath string, plugin adapterPlugin) (adapterWorkspaceFile, error) {
|
||||
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
|
||||
if err != nil {
|
||||
return adapterWorkspaceFile{}, err
|
||||
}
|
||||
revision, err := s.store.GetLatestRevisionForLanguages(objectUUID, []string{s.getLocale(session.DomainUUID), "und"})
|
||||
if err != nil {
|
||||
return adapterWorkspaceFile{}, err
|
||||
}
|
||||
body := revision.Body
|
||||
markdown := plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(revision.ContentType), "text/html")
|
||||
if markdown {
|
||||
body, err = compiler.HTMLToMarkdown(body)
|
||||
if err != nil {
|
||||
return adapterWorkspaceFile{}, err
|
||||
}
|
||||
}
|
||||
return adapterWorkspaceFile{
|
||||
Path: workspaceFilename(semanticPath, session.RootPath, revision.Filename, markdown),
|
||||
SemanticPath: semanticPath,
|
||||
RevisionUUID: revision.UUID, ContentType: revision.ContentType,
|
||||
ContentBase64: base64.StdEncoding.EncodeToString([]byte(body)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func adapterBearer(r *http.Request) string {
|
||||
value := strings.TrimSpace(r.Header.Get("Authorization"))
|
||||
if !strings.HasPrefix(value, "Bearer ") {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(strings.TrimPrefix(value, "Bearer "))
|
||||
}
|
||||
|
||||
// authorizeAdapterSession re-checks the current principal on every operation;
|
||||
// exchanging a ticket must not pin permissions for the lifetime of a session.
|
||||
func (s *Server) authorizeAdapterSession(r *http.Request, session *store.AdapterSession, capability string, path string) error {
|
||||
if session.PrincipalUUID == "" {
|
||||
if mode, _ := s.store.GetConfigInDomain(session.DomainUUID, "authorization_mode"); mode == "enforcing" {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// The session token is intentionally separate from the browser auth token;
|
||||
// use the principal captured at ticket issuance and re-evaluate its current
|
||||
// grants on every request.
|
||||
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: session.DomainUUID, PrincipalUUID: session.PrincipalUUID, Capability: capability, TargetType: "path", TargetID: path})
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapterSessionAPI(w http.ResponseWriter, r *http.Request) {
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter/session"), "/")
|
||||
if action == "exchange" && r.Method == http.MethodPost {
|
||||
var input struct {
|
||||
Ticket string `json:"ticket"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
token, session, err := s.store.ExchangeAdapterTicket(input.Ticket)
|
||||
if err != nil {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
revision, err := s.store.GetRevision(session.BaseRevisionUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{
|
||||
"session_token": token, "expires_at_ms": session.ExpiresAtMS,
|
||||
"plugin_id": session.PluginID,
|
||||
"revision_uuid": revision.UUID, "filename": revision.Filename,
|
||||
"content_type": revision.ContentType, "lang": revision.Language,
|
||||
})
|
||||
return
|
||||
}
|
||||
if action == "content" {
|
||||
session, err := s.store.GetAdapterSession(adapterBearer(r))
|
||||
if err != nil {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodGet {
|
||||
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
||||
return
|
||||
}
|
||||
file, err := s.workspaceFile(session, session.RootPath, s.adapterPluginForSession(session))
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, file)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPut {
|
||||
if err := s.authorizeAdapterSession(r, session, "write", session.RootPath); err != nil {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
Message string `json:"message"`
|
||||
Path string `json:"path"`
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
payload, err := base64.StdEncoding.DecodeString(input.ContentBase64)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid content_base64"})
|
||||
return
|
||||
}
|
||||
if len(payload) > 64<<20 {
|
||||
writeEventJSON(w, 413, map[string]string{"error": "content exceeds 64 MiB"})
|
||||
return
|
||||
}
|
||||
semanticPath := session.RootPath
|
||||
if input.Path != "" {
|
||||
semanticPath = strings.TrimSpace(input.Path)
|
||||
}
|
||||
rootPrefix := strings.TrimRight(session.RootPath, "/") + "/"
|
||||
if semanticPath != session.RootPath && !strings.HasPrefix(semanticPath, rootPrefix) {
|
||||
writeEventJSON(w, 403, map[string]string{"error": "path is outside the adapter workspace"})
|
||||
return
|
||||
}
|
||||
if err := s.authorizeAdapterSession(r, session, "write", semanticPath); err != nil {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter path authorization revoked"})
|
||||
return
|
||||
}
|
||||
objectUUID, err := s.store.ResolvePathInDomain(session.DomainUUID, semanticPath)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 404, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
baseRevisionUUID := input.RevisionUUID
|
||||
if baseRevisionUUID == "" && semanticPath == session.RootPath {
|
||||
baseRevisionUUID = session.BaseRevisionUUID
|
||||
}
|
||||
base, err := s.store.GetRevision(baseRevisionUUID)
|
||||
if err != nil || base.ObjectUUID != objectUUID {
|
||||
writeEventJSON(w, 409, map[string]string{"error": "invalid base revision"})
|
||||
return
|
||||
}
|
||||
plugin := s.adapterPluginForSession(session)
|
||||
if plugin.HTMLMarkdown && strings.HasPrefix(strings.ToLower(base.ContentType), "text/html") {
|
||||
payload = []byte(render.Markdown(string(payload)))
|
||||
}
|
||||
revision, err := s.store.SaveHostedRevisionAt(session, objectUUID, baseRevisionUUID, payload, input.Message)
|
||||
if err != nil {
|
||||
status := 500
|
||||
if errors.Is(err, store.ErrRevisionConflict) {
|
||||
status = http.StatusConflict
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"revision_uuid": revision.UUID, "document_hash": revision.DocumentHash})
|
||||
return
|
||||
}
|
||||
}
|
||||
if action == "workspace" && r.Method == http.MethodGet {
|
||||
session, err := s.store.GetAdapterSession(adapterBearer(r))
|
||||
if err != nil {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := s.authorizeAdapterSession(r, session, "read", session.RootPath); err != nil {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "adapter session authorization revoked"})
|
||||
return
|
||||
}
|
||||
paths := []string{session.RootPath}
|
||||
children, err := s.store.ListChildrenInDomain(session.DomainUUID, session.RootPath)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
paths = append(paths, children...)
|
||||
plugin := s.adapterPluginForSession(session)
|
||||
files := make([]adapterWorkspaceFile, 0, len(paths))
|
||||
for _, path := range paths {
|
||||
file, fileErr := s.workspaceFile(session, path, plugin)
|
||||
if fileErr == nil {
|
||||
files = append(files, file)
|
||||
}
|
||||
}
|
||||
payload := map[string]any{"root_path": session.RootPath, "files": files}
|
||||
if session.PluginID == "vim.wasm" {
|
||||
payload["vim_settings"] = s.domainVimSettings(session.DomainUUID)
|
||||
}
|
||||
writeEventJSON(w, 200, payload)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported adapter session operation"})
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestAnonymousPublicationChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
admin, e := s.store.PrincipalForSession(domain, token)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
q, e := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
o, e := s.store.CreatePageAtPathInDomain(domain, "/public/page")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.SaveRevisionContent(o, []byte(`<h1>Public browser sentinel</h1><script>window.authoredRan=true</script><p>{{children}}</p>`), "", "und", "text/html", ""); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
child, e := s.store.CreatePageAtPathInDomain(domain, "/public/page/child")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.SaveRevisionContent(child, []byte("<p>Child</p>"), "", "und", "text/html", ""); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for _, path := range []string{"/public/page/child/grandchild", "/public/page-other"} {
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte("<p>Navigation fixture</p>"), "", "und", "text/html", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
policy := "public"
|
||||
for _, node := range []struct{ kind, id string }{{"domain", domain}, {"quartier", q}} {
|
||||
g, e := s.store.CityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.ChangeCityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy, ConfirmRelaxation: true}, false); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
script, e := os.ReadFile("internal/server/testdata/anonymous-publication.js")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
if r.Header.Get("Origin") != "" {
|
||||
r.Header.Set("Origin", "http://localhost")
|
||||
}
|
||||
// Fixture authentication only on settings, never on public content.
|
||||
if strings.HasPrefix(r.URL.Path, "/_/api/v1/city/") && r.URL.Path != "/_/api/v1/city/map" || strings.HasPrefix(r.URL.Path, "/v/city/") {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
if r.Method == "GET" && r.URL.Path == "/v/city/q/public" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for k, v := range rec.Header() {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, width := range []string{"1440", "390"} {
|
||||
t.Run(width, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
||||
defer cancel()
|
||||
screenshot := ""
|
||||
if dir := os.Getenv("PAGESWIKI_PUBLIC_NAV_SCREENSHOT_DIR"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
screenshot = filepath.Join(dir, "public-navigation-"+width+".png")
|
||||
}
|
||||
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/city/q/public", t.TempDir(), screenshot, width).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("browser %v %s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"html/template"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type anonymousProjectionKey struct{}
|
||||
|
||||
func isAnonymousProjection(r *http.Request) bool {
|
||||
value, _ := r.Context().Value(anonymousProjectionKey{}).(bool)
|
||||
return value
|
||||
}
|
||||
|
||||
func (s *Server) tryAnonymousProjection(w http.ResponseWriter, r *http.Request) bool {
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
return false
|
||||
}
|
||||
// A bad or expired credential must not fall back to a weaker identity.
|
||||
if len(r.Header.Values("Authorization")) > 0 {
|
||||
return false
|
||||
}
|
||||
for _, c := range r.Cookies() {
|
||||
if c.Name == authCookieName {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if s.servePublicPeriodical(w, r) {
|
||||
return true
|
||||
}
|
||||
if s.servePublicUserSpace(w, r) {
|
||||
return true
|
||||
}
|
||||
if s.servePublicUserNavigation(w, r) {
|
||||
return true
|
||||
}
|
||||
if s.servePublicNavigation(w, r) {
|
||||
return true
|
||||
}
|
||||
semantic := r.URL.Path
|
||||
raw := strings.HasPrefix(semantic, "/v/raw/path:")
|
||||
if raw {
|
||||
semantic = strings.TrimPrefix(semantic, "/v/raw/path:")
|
||||
}
|
||||
if !store.PublicSemanticPath(semantic) {
|
||||
return false
|
||||
}
|
||||
v, err := s.store.AnonymousRevision(r.Context(), s.domainUUIDForRequest(r), semantic, s.revisionLanguageCandidates(r))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// A structural folder discloses only the already-public descendants,
|
||||
// never an unapproved Page at the same Path. Raw reads stay exact-only.
|
||||
if !raw {
|
||||
publicRequest, navigationErr := s.withPublicNavigation(r)
|
||||
if navigationErr != nil {
|
||||
http.Error(w, "navigation unavailable", 503)
|
||||
return true
|
||||
}
|
||||
if publicNavigationFor(publicRequest).hasChildren(semantic) {
|
||||
s.renderPublicDirectory(w, publicRequest, semantic, true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "publication unavailable", 503)
|
||||
return true
|
||||
}
|
||||
r = r.WithContext(context.WithValue(r.Context(), anonymousProjectionKey{}, true))
|
||||
r, err = s.withPublicNavigation(r)
|
||||
if err != nil {
|
||||
http.Error(w, "navigation unavailable", 503)
|
||||
return true
|
||||
}
|
||||
if current, ok := publicNavigationFor(r).pages[semantic]; !ok || current.UUID != v.UUID {
|
||||
http.Error(w, "publication changed; retry", 503)
|
||||
return true
|
||||
}
|
||||
return s.serveAnonymousRevision(w, r, semantic, raw, v)
|
||||
}
|
||||
|
||||
func (s *Server) serveAnonymousRevision(w http.ResponseWriter, r *http.Request, semantic string, raw bool, v *store.Revision) bool {
|
||||
reader, err := s.store.OpenDocument(v.DocumentHash)
|
||||
if err != nil {
|
||||
http.Error(w, "published document unavailable", 503)
|
||||
return true
|
||||
}
|
||||
defer reader.Close()
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Add("Vary", "Accept-Language")
|
||||
contentType, _, _ := mime.ParseMediaType(v.ContentType)
|
||||
filename := path.Base(v.Filename)
|
||||
if filename == "." || filename == "/" || filename == "" {
|
||||
filename = path.Base(semantic)
|
||||
}
|
||||
if raw {
|
||||
// Raw authored HTML, SVG and unknown formats are attachments, never
|
||||
// executable same-origin content. Native passive formats can stream.
|
||||
inline := contentType == "application/pdf" || contentType == "image/png" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/webp" || strings.HasPrefix(contentType, "audio/") || strings.HasPrefix(contentType, "video/")
|
||||
disposition := "attachment"
|
||||
if inline {
|
||||
disposition = "inline"
|
||||
}
|
||||
csp := "default-src 'none'; frame-ancestors 'self'"
|
||||
if !inline {
|
||||
csp = "sandbox; " + csp
|
||||
}
|
||||
w.Header().Set("Content-Security-Policy", csp)
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": filename}))
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("ETag", `"`+v.DocumentHash+`"`)
|
||||
setDocumentIdentityHeaders(w.Header(), v.ObjectUUID, v.UUID, v.DocumentHash)
|
||||
http.ServeContent(w, r, filename, time.UnixMilli(v.CreatedAtMS), reader)
|
||||
return true
|
||||
}
|
||||
data := s.publicNavigationData(r, semantic)
|
||||
data["Title"] = filename
|
||||
data["RawURL"] = cityPageHref("/v/raw/path:"+semantic) + "?lang=" + url.QueryEscape(v.Language)
|
||||
switch {
|
||||
case contentType == "text/html":
|
||||
body, e := io.ReadAll(io.LimitReader(reader, maxProjectionInput+1))
|
||||
if e != nil {
|
||||
http.Error(w, "published document unavailable", 503)
|
||||
return true
|
||||
}
|
||||
data["HTML"] = template.HTML(s.safeHTMLProjection(r, string(body), semantic, true))
|
||||
data["SocialSource"] = string(body)
|
||||
case contentType == "image/png" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/webp":
|
||||
data["Media"] = "image"
|
||||
case contentType == "application/pdf":
|
||||
data["Media"] = "pdf"
|
||||
case strings.HasPrefix(contentType, "audio/"):
|
||||
data["Media"] = "audio"
|
||||
case strings.HasPrefix(contentType, "video/"):
|
||||
data["Media"] = "video"
|
||||
case strings.HasPrefix(contentType, "text/") || contentType == "application/json":
|
||||
body, e := io.ReadAll(io.LimitReader(reader, 64<<10))
|
||||
if e != nil {
|
||||
http.Error(w, "published document unavailable", 503)
|
||||
return true
|
||||
}
|
||||
data["Text"] = string(body)
|
||||
}
|
||||
data["SocialMetadata"] = s.socialMetadataForRequest(r, "anonymous_view.gohtml", data)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
s.render(w, "anonymous_view.gohtml", data)
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestAnonymousApprovedPublication(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
ctx := context.Background()
|
||||
admin, err := s.store.PrincipalForSession(domain, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st, err := s.store.CreateZoneAtPath(domain, admin.UUID, "street", q, "private", "Private", "", "/public/private")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
create := func(path, body, ct string) (string, *store.Revision) {
|
||||
t.Helper()
|
||||
o, e := s.store.CreatePageAtPathInDomain(domain, path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
v, e := s.store.SaveRevisionContent(o, []byte(body), "", "und", ct, "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return o, v
|
||||
}
|
||||
object, rev := create("/public/page", "<h1>Approved page</h1><script>window.stolen=true</script><p>{{children:/private}}</p>", "text/html")
|
||||
create("/public/movie", "FAKE-MEDIA-CONTENT", "video/mp4")
|
||||
create("/public/private/secret", "<h1>PRIVATE-SENTINEL</h1>", "text/html")
|
||||
create("/outside", "<h1>OUTSIDE-SENTINEL</h1>", "text/html")
|
||||
setPolicy := func(kind, id, value string) {
|
||||
t.Helper()
|
||||
g, e := s.store.CityGovernance(ctx, domain, admin.UUID, kind, id)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
_, e = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
setPolicy("domain", domain, "public")
|
||||
setPolicy("quartier", q, "public")
|
||||
setPolicy("street", st, "authenticated_acl")
|
||||
url := governanceEndpoint("quartier", q) + "/publication"
|
||||
call := func(method, path, bearer string, body any, headers map[string]string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
b, _ := json.Marshal(body)
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(string(b)))
|
||||
if bearer != "" {
|
||||
r.Header.Set("Authorization", bearer)
|
||||
}
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
for k, v := range headers {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
preview := func() store.LocalityPublication {
|
||||
t.Helper()
|
||||
w := call("GET", url, "Bearer "+token, nil, nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("preview %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var p store.LocalityPublication
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
approve := func(p store.LocalityPublication) {
|
||||
t.Helper()
|
||||
w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("approve %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
denied := func(path string) {
|
||||
t.Helper()
|
||||
w := call("GET", path, "", nil, nil)
|
||||
if w.Code == 200 || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") {
|
||||
t.Fatalf("leaked %s: %d", path, w.Code)
|
||||
}
|
||||
}
|
||||
denied("/public/page")
|
||||
p := preview()
|
||||
if !p.Eligible || len(p.Entries) != 2 || len(p.Excluded) != 1 {
|
||||
t.Fatalf("impact=%+v", p)
|
||||
}
|
||||
if w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: true, Confirm: true}, nil); w.Code != 400 {
|
||||
t.Fatalf("unapproved=%d", w.Code)
|
||||
}
|
||||
approve(p)
|
||||
w := call("GET", "/public/page", "", nil, nil)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "Approved page") || !strings.Contains(w.Body.String(), `property="og:title"`) || strings.Contains(w.Body.String(), "window.stolen") || !strings.Contains(w.Body.String(), "data-children-src") || strings.Contains(w.Body.String(), "auth-menu") || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") || strings.Contains(w.Body.String(), " download") {
|
||||
t.Fatalf("public viewer %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w.Header().Get("Content-Security-Policy") == "" || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
||||
t.Fatal("public CSP/cache missing")
|
||||
}
|
||||
for _, path := range []string{"/public/private/secret", "/outside", "/o/uuid:" + object, "/v/raw/uuid:" + object, "/v/raw/rev:" + rev.UUID, "/_/obj/sha256:" + rev.DocumentHash, "/e/public/page", "/v/search?q=Approved"} {
|
||||
denied(path)
|
||||
}
|
||||
for _, bearer := range []string{"Bearer invalid", "Basic invalid"} {
|
||||
if w = call("GET", "/public/page", bearer, nil, nil); w.Code == 200 {
|
||||
t.Fatal("invalid credentials downgraded")
|
||||
}
|
||||
}
|
||||
if w = call("GET", "/public/page", "", nil, map[string]string{"Cookie": authCookieName + "=expired"}); w.Code == 200 {
|
||||
t.Fatal("expired cookie downgraded")
|
||||
}
|
||||
if w = call("GET", "/v/raw/path:/public/movie", "", nil, map[string]string{"Range": "bytes=0-3"}); w.Code != 206 || w.Body.String() != "FAKE" {
|
||||
t.Fatalf("range=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w = call("GET", "/v/raw/path:/public/page", "", nil, nil); w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Disposition"), "attachment") {
|
||||
t.Fatalf("raw HTML unsafe %d", w.Code)
|
||||
}
|
||||
if w = call("HEAD", "/v/raw/path:/public/movie", "", nil, nil); w.Code != 200 || w.Body.Len() != 0 {
|
||||
t.Fatal("raw HEAD")
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
|
||||
t.Fatal("publication downgraded to legacy")
|
||||
}
|
||||
// A new revision invalidates publication before any conditional 304 check.
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte("<h1>NOT-APPROVED</h1>"), "", "und", "text/html", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denied("/public/page")
|
||||
if w = call("GET", "/v/raw/path:/public/page", "", nil, map[string]string{"If-None-Match": `"` + rev.DocumentHash + `"`}); w.Code == 304 || w.Code == 200 {
|
||||
t.Fatal("revoked conditional access")
|
||||
}
|
||||
approve(preview())
|
||||
create("/public/new", "UNAPPROVED-NEW", "text/plain")
|
||||
denied("/public/new")
|
||||
// Stale impact token cannot approve intervening edits.
|
||||
p = preview()
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte("changed again"), "", "und", "text/html", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 400 {
|
||||
t.Fatalf("stale impact %d", w.Code)
|
||||
}
|
||||
approve(preview())
|
||||
if _, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,?,?,?,0)`, domain, "/alias", object, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denied("/public/page")
|
||||
denied("/alias")
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM paths WHERE domain_uuid=? AND path='/alias'`, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
denied("/public/page")
|
||||
approve(preview())
|
||||
setPolicy("domain", domain, "authenticated_acl")
|
||||
denied("/public/page")
|
||||
setPolicy("domain", domain, "public")
|
||||
denied("/public/page")
|
||||
approve(preview())
|
||||
// Revocation is explicit and does not change the ceiling/draft.
|
||||
p = preview()
|
||||
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: false}, nil); w.Code != 200 {
|
||||
t.Fatalf("revoke=%d", w.Code)
|
||||
}
|
||||
denied("/public/page")
|
||||
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 409 {
|
||||
t.Fatal("pre-revocation consent replayed")
|
||||
}
|
||||
approve(preview())
|
||||
a, e := s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, nil)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
a.Mode = "restricted"
|
||||
a.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: "admin", Permission: "read_write"}}
|
||||
if _, e = s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, &a); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
denied("/public/page")
|
||||
if p = preview(); p.Eligible || len(p.Entries) != 0 {
|
||||
t.Fatalf("ACL publicly approved %+v", p)
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@ package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/api"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleCreateObjectAPI(
|
||||
@@ -29,7 +31,7 @@ func (s *Server) handleCreateObjectAPI(
|
||||
return
|
||||
}
|
||||
|
||||
id, err := s.store.CreateObject(req.Kind)
|
||||
id, err := s.store.CreateObjectInDomain(s.domainUUIDForRequest(r), req.Kind)
|
||||
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
@@ -50,19 +52,7 @@ func (s *Server) handleListObjectsAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
objs, err := s.store.ListObjects()
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
500,
|
||||
"list_failed",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(objs)
|
||||
s.serveObjectMetadataList(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) handleSaveRevisionAPI(
|
||||
@@ -71,9 +61,12 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Body string `json:"body"`
|
||||
Message string `json:"message"`
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Body string `json:"body"`
|
||||
Message string `json:"message"`
|
||||
Language string `json:"lang"`
|
||||
ContentType string `json:"content_type"`
|
||||
Filename string `json:"filename"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
@@ -86,17 +79,57 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
)
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
if mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode"); mode == "enforcing" {
|
||||
principal, authErr := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
if authErr != nil {
|
||||
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
|
||||
return
|
||||
}
|
||||
var revisionCount int
|
||||
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
|
||||
capability := "write"
|
||||
if revisionCount == 0 {
|
||||
capability = "create"
|
||||
}
|
||||
allowed := false
|
||||
links, _ := s.store.ListPathLinksInDomain(domainUUID, req.ObjectUUID)
|
||||
for _, link := range links {
|
||||
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: link.Path}) == nil {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed && s.authorizePrincipalObject(domainUUID, principal.UUID, capability, req.ObjectUUID) == nil {
|
||||
allowed = true
|
||||
}
|
||||
if !allowed {
|
||||
api.WriteError(w, http.StatusForbidden, "forbidden", "forbidden")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err = s.store.SaveRevision(
|
||||
req.ObjectUUID,
|
||||
req.Body,
|
||||
req.Message,
|
||||
)
|
||||
revision, err :=
|
||||
s.store.SaveRevisionContent(
|
||||
req.ObjectUUID,
|
||||
[]byte(req.Body),
|
||||
req.Message,
|
||||
req.Language,
|
||||
req.ContentType,
|
||||
req.Filename,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
status := http.StatusInternalServerError
|
||||
if errors.Is(err, store.ErrInvalidRevisionLanguage) ||
|
||||
errors.Is(err, store.ErrPageNotFound) ||
|
||||
errors.Is(err, store.ErrEmbeddedAssetUnavailable) {
|
||||
status = http.StatusBadRequest
|
||||
}
|
||||
|
||||
api.WriteError(
|
||||
w,
|
||||
500,
|
||||
status,
|
||||
"save_failed",
|
||||
err.Error(),
|
||||
)
|
||||
@@ -104,6 +137,11 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "ok",
|
||||
"status": "ok",
|
||||
"revision_uuid": revision.UUID,
|
||||
"document_hash": revision.DocumentHash,
|
||||
"lang": revision.Language,
|
||||
"content_type": revision.ContentType,
|
||||
"filename": revision.Filename,
|
||||
})
|
||||
}
|
||||
@@ -9,68 +9,17 @@ type renamePreviewResponse struct {
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
||||
func (s *Server) handleRenamePreview(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
oldPath :=
|
||||
r.URL.Query().Get(
|
||||
"old_path",
|
||||
)
|
||||
|
||||
rows, err :=
|
||||
s.store.DB.Query(`
|
||||
SELECT path
|
||||
FROM paths
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
ORDER BY LENGTH(path) ASC
|
||||
`,
|
||||
oldPath,
|
||||
oldPath+"/%",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
func (s *Server) handleRenamePreview(w http.ResponseWriter, r *http.Request) {
|
||||
if !requirePathMutationPost(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var targets []string
|
||||
|
||||
for rows.Next() {
|
||||
|
||||
var p string
|
||||
|
||||
err := rows.Scan(&p)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
targets =
|
||||
append(
|
||||
targets,
|
||||
p,
|
||||
)
|
||||
snapshot, err := s.authorizedMutationSnapshot(r, "rename", "", r.URL.Query().Get("old_path"), "", true)
|
||||
if err != nil {
|
||||
writePathMutationError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
renamePreviewResponse{
|
||||
Targets: targets,
|
||||
},
|
||||
)
|
||||
// Return the checked set, not a fresh query that could reveal a newly added
|
||||
// descendant for which the requester has never been authorized.
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(renamePreviewResponse{Targets: snapshot.Paths()})
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
@@ -16,175 +15,37 @@ type renamePreview struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func (s *Server) handleRenamePath(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
func (s *Server) handleRenamePath(w http.ResponseWriter, r *http.Request) {
|
||||
if !requirePathMutationPost(w, r) {
|
||||
return
|
||||
}
|
||||
var req renamePathRequest
|
||||
|
||||
err := json.NewDecoder(
|
||||
r.Body,
|
||||
).Decode(&req)
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
req.OldPath = normalizeAuthorizationPath(req.OldPath)
|
||||
req.NewPath = normalizeAuthorizationPath(req.NewPath)
|
||||
if req.OldPath == "" || req.NewPath == "" || req.OldPath == "/" || req.NewPath == "/" {
|
||||
http.Error(w, "invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
snapshot, err := s.authorizedMutationSnapshot(r, "rename", "create", req.OldPath, req.NewPath, req.Recursive)
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
400,
|
||||
)
|
||||
|
||||
writePathMutationError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if req.OldPath == "" ||
|
||||
req.NewPath == "" {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
"invalid path",
|
||||
400,
|
||||
)
|
||||
|
||||
if r.URL.Query().Get("preview") == "1" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(renamePreview{Count: len(snapshot.Paths())})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Recursive {
|
||||
|
||||
var count int
|
||||
|
||||
err :=
|
||||
s.store.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM paths
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("preview") == "1" {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
renamePreview{
|
||||
Count: count,
|
||||
},
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
tx, err :=
|
||||
s.store.DB.BeginTx(
|
||||
r.Context(),
|
||||
&sql.TxOptions{},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
UPDATE paths
|
||||
SET path =
|
||||
CASE
|
||||
WHEN path = ?
|
||||
THEN ?
|
||||
ELSE REPLACE(
|
||||
path,
|
||||
? || '/',
|
||||
? || '/'
|
||||
)
|
||||
END
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err =
|
||||
s.store.DB.Exec(`
|
||||
UPDATE paths
|
||||
SET path = ?
|
||||
WHERE path = ?
|
||||
`,
|
||||
req.NewPath,
|
||||
req.OldPath,
|
||||
)
|
||||
|
||||
count, err := s.store.RenamePathsInDomainChecked(s.domainUUIDForRequest(r), req.OldPath, req.NewPath, req.Recursive, snapshot)
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
writePathMutationError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(renamePreview{Count: count})
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Query().Get("path")
|
||||
entry := r.URL.Query().Get("entry")
|
||||
metadataOnly := r.URL.Query().Get("metadata") == "1"
|
||||
if path == "" || (!metadataOnly && entry == "") || len(entry) > 4096 || strings.ContainsAny(entry, "\x00\r\n") {
|
||||
http.Error(w, "path and entry are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !s.requestCanReadPath(r, path) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
var revision *store.Revision
|
||||
if revisionUUID := r.URL.Query().Get("rev"); revisionUUID != "" {
|
||||
revision, err = s.store.GetRevisionMetadata(revisionUUID)
|
||||
} else {
|
||||
revision, err = s.latestRevisionMetadataForRequest(r, objectUUID)
|
||||
}
|
||||
if err != nil || revision.ObjectUUID != objectUUID {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if metadataOnly {
|
||||
s.serveMetadataProjection(w, r, revision.DocumentHash, "archive", path, revision.UUID)
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(filepath.Ext(revision.Filename), ".zip") {
|
||||
hydrated, err := s.store.GetRevision(revision.UUID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
s.serveZIPEntry(w, r, hydrated.Body, entry)
|
||||
return
|
||||
}
|
||||
source, err := s.store.DocumentPath(revision.DocumentHash)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
command := exec.CommandContext(ctx, "nice", "-n", "10",
|
||||
"7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", source, entry)
|
||||
stdout, err := command.StdoutPipe()
|
||||
if err != nil || command.Start() != nil {
|
||||
http.Error(w, "cannot extract archive entry", http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", archiveEntryContentType(entry, nil))
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(entry)}))
|
||||
w.Header().Set("Cache-Control", "private, max-age=60")
|
||||
w.Header().Set("Vary", "Authorization, Cookie")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
n, copyErr := io.Copy(w, io.LimitReader(stdout, 128<<20))
|
||||
if copyErr != nil || n == 128<<20 {
|
||||
// Never wait for a decoder whose output is no longer being drained.
|
||||
cancel()
|
||||
}
|
||||
waitErr := command.Wait()
|
||||
if copyErr != nil || waitErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) serveZIPEntry(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
body string,
|
||||
entry string,
|
||||
) {
|
||||
reader, err := zip.NewReader(
|
||||
bytes.NewReader([]byte(body)),
|
||||
int64(len(body)),
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
for _, file := range reader.File {
|
||||
if file.Name != entry || file.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
source, err := file.Open()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer source.Close()
|
||||
w.Header().Set("Content-Type", archiveEntryContentType(file.Name, nil))
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(file.Name)}))
|
||||
w.Header().Set("Cache-Control", "private, max-age=60")
|
||||
w.Header().Set("Vary", "Authorization, Cookie")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
_, _ = io.Copy(w, io.LimitReader(source, 128<<20))
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleAssetsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil {
|
||||
s.authRequired(w, r)
|
||||
return
|
||||
}
|
||||
if !s.canManageAssets(r) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
address := strings.TrimSpace(r.URL.Query().Get("address"))
|
||||
if address == "" {
|
||||
assets, err := s.store.ListAssets(domainUUID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"assets": assets})
|
||||
return
|
||||
}
|
||||
asset, err := s.store.GetAsset(domainUUID, address)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
history, err := s.store.ListAssetHistory(domainUUID, address)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
dependencies, err := s.store.ListAssetDependencies(domainUUID, address)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
setAssetETag(w, asset.HeadDocumentHash)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "history": history, "dependencies": dependencies})
|
||||
case http.MethodPost:
|
||||
if strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "application/json") {
|
||||
var input struct {
|
||||
Action string `json:"action"`
|
||||
Address string `json:"address"`
|
||||
HistoryID int64 `json:"history_id"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
if input.Action != "restore" {
|
||||
http.Error(w, "unsupported action", 400)
|
||||
return
|
||||
}
|
||||
expected := parseAssetETag(r.Header.Get("If-Match"))
|
||||
if expected == "" {
|
||||
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
|
||||
return
|
||||
}
|
||||
asset, err := s.store.RestoreAssetHead(domainUUID, input.Address, input.HistoryID, expected, principal.UUID)
|
||||
if errors.Is(err, store.ErrAssetConflict) {
|
||||
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
|
||||
return
|
||||
} else if errors.Is(err, store.ErrAssetNotFound) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
setAssetETag(w, asset.HeadDocumentHash)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "restored": true})
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 256<<20)
|
||||
if err := r.ParseMultipartForm(8 << 20); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
defer r.MultipartForm.RemoveAll()
|
||||
address := r.FormValue("address")
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, "file is required", 400)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
payload, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
current, currentErr := s.store.GetAsset(domainUUID, address)
|
||||
expected := parseAssetETag(r.Header.Get("If-Match"))
|
||||
if currentErr == nil && expected == "" {
|
||||
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
|
||||
return
|
||||
}
|
||||
if errors.Is(currentErr, store.ErrAssetNotFound) && r.Header.Get("If-None-Match") != "*" {
|
||||
http.Error(w, "If-None-Match: * is required", http.StatusPreconditionRequired)
|
||||
return
|
||||
}
|
||||
contentType := strings.TrimSpace(r.FormValue("content_type"))
|
||||
if contentType == "" {
|
||||
contentType = header.Header.Get("Content-Type")
|
||||
}
|
||||
filename := strings.TrimSpace(r.FormValue("filename"))
|
||||
if filename == "" {
|
||||
filename = header.Filename
|
||||
}
|
||||
asset, changed, err := s.store.PutAsset(domainUUID, address, payload, contentType, filename, expected, principal.UUID)
|
||||
if errors.Is(err, store.ErrAssetConflict) {
|
||||
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
setAssetETag(w, asset.HeadDocumentHash)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "changed": changed, "previous": current})
|
||||
case http.MethodDelete:
|
||||
address := strings.TrimSpace(r.URL.Query().Get("address"))
|
||||
expected := parseAssetETag(r.Header.Get("If-Match"))
|
||||
if expected == "" {
|
||||
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
|
||||
return
|
||||
}
|
||||
if err := s.store.DeleteAsset(domainUUID, address, expected, principal.UUID); errors.Is(err, store.ErrAssetConflict) {
|
||||
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
|
||||
return
|
||||
} else if errors.Is(err, store.ErrAssetNotFound) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST, DELETE")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) canManageAssets(r *http.Request) bool {
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil {
|
||||
return false
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
domain := s.domainForRequest(r)
|
||||
return s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") ||
|
||||
(domain != nil && s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID))
|
||||
}
|
||||
|
||||
func parseAssetETag(value string) string {
|
||||
return strings.TrimPrefix(strings.Trim(strings.TrimSpace(value), `"`), "sha256:")
|
||||
}
|
||||
func setAssetETag(w http.ResponseWriter, hash string) {
|
||||
if hash != "" {
|
||||
w.Header().Set("ETag", `"sha256:`+hash+`"`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"pageswiki/internal/store"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ldapConfigFromForm(r *http.Request) store.AuthProviderConfig {
|
||||
return store.AuthProviderConfig{
|
||||
Kind: "ldap", Enabled: r.FormValue("enabled") == "on",
|
||||
LDAPURL: r.FormValue("ldap_url"), LDAPBindDN: r.FormValue("ldap_bind_dn"), LDAPBindPassword: r.FormValue("ldap_bind_password"),
|
||||
LDAPSearchBase: r.FormValue("ldap_search_base"), LDAPSearchFilter: r.FormValue("ldap_search_filter"),
|
||||
LDAPStartTLS: r.FormValue("ldap_start_tls") == "on", LDAPSkipTLSVerify: r.FormValue("ldap_verify_tls") != "on", LDAPCAPEM: r.FormValue("ldap_ca_pem"),
|
||||
LDAPUIDAttribute: r.FormValue("ldap_uid_attribute"), LDAPUsernameAttribute: r.FormValue("ldap_username_attribute"),
|
||||
LDAPEmailAttribute: r.FormValue("ldap_email_attribute"), LDAPDisplayAttribute: r.FormValue("ldap_display_attribute"),
|
||||
LDAPMatchBy: r.FormValue("ldap_match_by"), LDAPAutoProvision: r.FormValue("ldap_auto_provision") == "on",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil || !s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
||||
http.Error(w, "domain admin required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
panel := "auth"
|
||||
switch r.FormValue("action") {
|
||||
case "revoke_session":
|
||||
panel = "auth-sessions"
|
||||
if err := s.store.RevokeAuthSessionByID(domainUUID, r.FormValue("session_uuid")); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
case "principal_state":
|
||||
if err := s.store.SetPrincipalState(domainUUID, r.FormValue("principal_uuid"), r.FormValue("state")); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
case "create_account":
|
||||
if _, err := s.store.CreatePasswordAccount(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email")); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
case "save_oidc":
|
||||
panel = "auth-providers"
|
||||
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "oidc", Enabled: r.FormValue("enabled") == "on", Issuer: r.FormValue("issuer"), ClientID: r.FormValue("client_id"), ClientSecret: r.FormValue("client_secret"), OIDCMatchBy: r.FormValue("oidc_match_by")})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
case "save_smtp":
|
||||
panel = "auth-providers"
|
||||
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "smtp", Enabled: r.FormValue("enabled") == "on", SMTPAddress: r.FormValue("smtp_address"), SMTPUsername: r.FormValue("smtp_username"), SMTPPassword: r.FormValue("smtp_password"), SMTPFrom: r.FormValue("smtp_from")})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
case "save_ldap":
|
||||
panel = "auth-providers"
|
||||
if err := s.store.SetAuthProviderConfig(domainUUID, ldapConfigFromForm(r)); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
case "test_ldap":
|
||||
panel = "auth-providers"
|
||||
config, err := s.store.AuthProviderConfig(domainUUID, "ldap")
|
||||
if err == nil && config.Enabled {
|
||||
select {
|
||||
case s.ldapSlots <- struct{}{}:
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
|
||||
_, err = s.authenticateLDAPDirectory(ctx, config, r.FormValue("test_username"), r.FormValue("test_password"))
|
||||
cancel()
|
||||
<-s.ldapSlots
|
||||
default:
|
||||
err = context.DeadlineExceeded
|
||||
}
|
||||
}
|
||||
if err != nil || !config.Enabled {
|
||||
log.Printf("LDAP connection test failed domain=%s error=%v", domainUUID, err)
|
||||
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=failed", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=ok", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/-/sys/config?panel="+panel+"&saved=1", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/-/sys/config?panel=auth", http.StatusFound)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBrowserCookieLoginLogoutChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, apiToken, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
||||
script, err := os.ReadFile("internal/server/testdata/auth-browser.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Real browser cookies and Fetch Metadata: do not inject credentials or
|
||||
// rewrite Origin/Host. localhost maps to this fixture's default Domain.
|
||||
if r.Method == "GET" && (strings.HasPrefix(r.URL.Path, "/_/auth/") || r.URL.Path == "/-/sys/config") {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
||||
defer cancel()
|
||||
target := strings.Replace(server.URL, "127.0.0.1", "localhost", 1) + "/_/auth/login?return=%2F-%2Fsys%2Fconfig%3Fpanel%3Dauth"
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", target, t.TempDir())
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("cookie browser flow: %v %s", err, out)
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, apiToken); err != nil {
|
||||
t.Fatalf("browser logout revoked another API session: %v", err)
|
||||
}
|
||||
var revoked int
|
||||
if err := s.store.DB.QueryRow(`SELECT count(*) FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&revoked); err != nil || revoked != 1 {
|
||||
t.Fatalf("browser revocations=%d %v", revoked, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type oidcChallenge struct{ Verifier, Nonce, Return, Issuer, ClientID, BrowserHash string }
|
||||
|
||||
func logOIDCFailure(stage, domainUUID, issuer string, err error) {
|
||||
host := "invalid"
|
||||
if parsed, parseErr := url.Parse(issuer); parseErr == nil && parsed.Hostname() != "" {
|
||||
host = parsed.Hostname()
|
||||
}
|
||||
cause := err
|
||||
for errors.Unwrap(cause) != nil {
|
||||
cause = errors.Unwrap(cause)
|
||||
}
|
||||
log.Printf("OIDC %s failed domain=%s issuer_host=%s cause=%T: %v", stage, domainUUID, host, cause, cause)
|
||||
}
|
||||
|
||||
func randomURLToken(size int) string {
|
||||
raw := make([]byte, size)
|
||||
_, _ = rand.Read(raw)
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
func externalURL(r *http.Request, path string) string {
|
||||
scheme := "http"
|
||||
if requestIsHTTPS(r) {
|
||||
scheme = "https"
|
||||
}
|
||||
return scheme + "://" + r.Host + path
|
||||
}
|
||||
|
||||
func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
config, err := s.store.AuthProviderConfig(domainUUID, "oidc")
|
||||
if err != nil || !config.Enabled {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second)
|
||||
defer cancel()
|
||||
metadata, err := fetchOIDCMetadata(ctx, config.Issuer)
|
||||
if err != nil {
|
||||
logOIDCFailure("discovery", domainUUID, config.Issuer, err)
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
verifier, nonce, browser := randomURLToken(32), randomURLToken(24), randomURLToken(32)
|
||||
browserDigest := sha256.Sum256([]byte(browser))
|
||||
state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: safeReturnPath(r.URL.Query().Get("return")), Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:])}, 5*time.Minute)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
digest := sha256.Sum256([]byte(verifier))
|
||||
query := url.Values{"response_type": {"code"}, "client_id": {config.ClientID}, "redirect_uri": {externalURL(r, "/_/auth/callback/oidc")}, "scope": {"openid email profile"}, "state": {state}, "nonce": {nonce}, "code_challenge_method": {"S256"}, "code_challenge": {base64.RawURLEncoding.EncodeToString(digest[:])}}
|
||||
authorizationURL, _ := url.Parse(metadata.AuthorizationEndpoint)
|
||||
parameters := authorizationURL.Query()
|
||||
for key, value := range query {
|
||||
parameters[key] = value
|
||||
}
|
||||
authorizationURL.RawQuery = parameters.Encode()
|
||||
setOIDCBrowserCookie(w, r, browser, 300)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
http.Redirect(w, r, authorizationURL.String(), http.StatusFound)
|
||||
}
|
||||
|
||||
func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
var challenge oidcChallenge
|
||||
if _, err := s.store.ConsumeAuthChallenge(domainUUID, "oidc", r.URL.Query().Get("state"), &challenge); err != nil {
|
||||
http.Error(w, "invalid OIDC state", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !validOIDCBrowserChallenge(oidcBrowserCookie(r), challenge.BrowserHash) {
|
||||
http.Error(w, "OIDC state belongs to a different browser", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
setOIDCBrowserCookie(w, r, "", -1)
|
||||
config, err := s.store.AuthProviderConfig(domainUUID, "oidc")
|
||||
if err != nil || !config.Enabled {
|
||||
http.Error(w, "OIDC is disabled", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if config.Issuer != challenge.Issuer || config.ClientID != challenge.ClientID || r.URL.Query().Get("code") == "" || r.URL.Query().Get("error") != "" {
|
||||
http.Error(w, "OIDC login must be restarted", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
metadata, err := fetchOIDCMetadata(ctx, config.Issuer)
|
||||
if err != nil {
|
||||
logOIDCFailure("callback discovery", domainUUID, config.Issuer, err)
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
form := url.Values{"grant_type": {"authorization_code"}, "code": {r.URL.Query().Get("code")}, "redirect_uri": {externalURL(r, "/_/auth/callback/oidc")}, "client_id": {config.ClientID}, "client_secret": {config.ClientSecret}, "code_verifier": {challenge.Verifier}}
|
||||
var tokens struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
IDToken string `json:"id_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
}
|
||||
if tokenErr := oidcVerifier.requestJSON(ctx, http.MethodPost, metadata.TokenEndpoint, form, "", &tokens); tokenErr != nil {
|
||||
logOIDCFailure("token exchange", domainUUID, config.Issuer, tokenErr)
|
||||
http.Error(w, "OIDC token exchange failed", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
if tokens.AccessToken == "" || len(tokens.AccessToken) > 16<<10 || !strings.EqualFold(tokens.TokenType, "Bearer") {
|
||||
http.Error(w, "OIDC token exchange failed", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
idClaims, err := oidcVerifier.verify(ctx, metadata, tokens.IDToken, config.ClientID, challenge.Nonce, tokens.AccessToken)
|
||||
if err != nil {
|
||||
logOIDCFailure("ID-token verification", domainUUID, config.Issuer, err)
|
||||
http.Error(w, "OIDC ID-token verification failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var claims struct {
|
||||
Sub, Email string
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
PreferredName string `json:"preferred_username"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
}
|
||||
if userinfoErr := oidcVerifier.requestJSON(ctx, http.MethodGet, metadata.UserinfoEndpoint, nil, tokens.AccessToken, &claims); userinfoErr != nil {
|
||||
logOIDCFailure("UserInfo", domainUUID, config.Issuer, userinfoErr)
|
||||
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if claims.Sub == "" {
|
||||
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if claims.Sub != idClaims.Sub {
|
||||
http.Error(w, "OIDC subject mismatch", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
principal, err := s.store.PrincipalByExternalIdentity(domainUUID, "oidc", "oidc", config.Issuer, claims.Sub)
|
||||
if err != nil {
|
||||
switch config.OIDCMatchBy {
|
||||
case "preferred_username":
|
||||
principal, err = s.store.PrincipalByExternalUsername(domainUUID, claims.PreferredName)
|
||||
if err != nil {
|
||||
log.Printf("OIDC local account match failed domain=%s mode=preferred_username present=%t", domainUUID, strings.TrimSpace(claims.PreferredName) != "")
|
||||
}
|
||||
case "display_name":
|
||||
displayName := strings.TrimSpace(claims.Name)
|
||||
claimSource := "name"
|
||||
if displayName == "" {
|
||||
displayName = strings.TrimSpace(claims.DisplayName)
|
||||
claimSource = "display_name"
|
||||
}
|
||||
principal, err = s.store.PrincipalByDisplayName(domainUUID, displayName)
|
||||
if err != nil {
|
||||
log.Printf("OIDC local account match failed domain=%s mode=display_name claim=%s present=%t", domainUUID, claimSource, displayName != "")
|
||||
}
|
||||
default:
|
||||
if claims.Email == "" || !claims.EmailVerified {
|
||||
http.Error(w, "OIDC identity is not a verified account", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
principal, err = s.store.PrincipalByVerifiedEmail(domainUUID, claims.Email)
|
||||
}
|
||||
if err == nil {
|
||||
err = s.store.BindExternalIdentity(principal.UUID, "oidc", "oidc", config.Issuer, claims.Sub, claims)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "No local account matches the configured OIDC identity field", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
token, err := s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
http.Redirect(w, r, challenge.Return, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *Server) requestMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", "POST")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "invalid sign-in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
|
||||
requested, mailboxErr := smtpMailbox(r.FormValue("email"))
|
||||
if configErr == nil && config.Enabled && mailboxErr == nil {
|
||||
principal, principalErr := s.store.PrincipalByVerifiedEmail(domainUUID, requested.Address)
|
||||
if principalErr == nil {
|
||||
_, _, _, _, principalErr = smtpMessage(config, principal.Email, externalURL(r, "/_/auth/magic-link/verify"))
|
||||
}
|
||||
if principalErr == nil {
|
||||
select {
|
||||
case s.smtpSlots <- struct{}{}:
|
||||
token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email}, 15*time.Minute)
|
||||
if err != nil {
|
||||
<-s.smtpSlots
|
||||
break
|
||||
}
|
||||
link := externalURL(r, "/_/auth/magic-link/verify?token="+url.QueryEscape(token))
|
||||
go func() {
|
||||
defer func() { <-s.smtpSlots }()
|
||||
_ = sendMagicLinkSMTP(context.Background(), config, principal.Email, link)
|
||||
}()
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
s.renderAuthPage(w, r, map[string]any{"MagicSent": true, "Return": safeReturnPath(r.FormValue("return"))})
|
||||
}
|
||||
|
||||
func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", "GET")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
var payload magicLinkChallenge
|
||||
principalUUID, err := s.store.ConsumeAuthChallenge(domainUUID, "magic-link", r.URL.Query().Get("token"), &payload)
|
||||
if err != nil {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
|
||||
var valid bool
|
||||
stateErr := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.state='active' AND p.email=?)`, domainUUID, principalUUID, payload.Email).Scan(&valid)
|
||||
if configErr != nil || !config.Enabled || stateErr != nil || !valid || payload.Email == "" {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
token, err := s.store.CreateBrowserAuthSession(domainUUID, principalUUID, 0)
|
||||
if err != nil {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
http.Redirect(w, r, safeReturnPath(payload.Return), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *Server) renderAuthPage(w http.ResponseWriter, r *http.Request, data map[string]any) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
if data == nil {
|
||||
data = map[string]any{}
|
||||
}
|
||||
if config, err := s.store.AuthProviderConfig(domainUUID, "oidc"); err == nil {
|
||||
data["OIDCEnabled"] = config.Enabled
|
||||
}
|
||||
if config, err := s.store.AuthProviderConfig(domainUUID, "smtp"); err == nil {
|
||||
data["MagicEnabled"] = config.Enabled
|
||||
}
|
||||
if config, err := s.store.AuthProviderConfig(domainUUID, "ldap"); err == nil {
|
||||
data["LDAPEnabled"] = config.Enabled
|
||||
}
|
||||
data["Locale"] = s.getLocale(domainUUID)
|
||||
transparency := s.homeGlassTransparency(domainUUID)
|
||||
data["HomeGlassTransparency"] = transparency
|
||||
data["HomeGlassOpacity"] = 100 - transparency
|
||||
configured, opacity := s.homeWallpaperConfig(domainUUID)
|
||||
data["HomeWallpaperOpacity"] = strconv.FormatFloat(float64(opacity)/100, 'f', 2, 64)
|
||||
if configured != "" {
|
||||
// Only the administrator-selected login wallpaper is exposed; the
|
||||
// request cannot choose an arbitrary document address.
|
||||
data["HomeWallpaperURL"] = "/_/auth/wallpaper"
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
}
|
||||
s.renderWithContext(w, r, "auth.gohtml", data)
|
||||
}
|
||||
@@ -0,0 +1,406 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const authCookieName = "pageswiki_session"
|
||||
const mfaChallengeCookieName = "pageswiki_mfa"
|
||||
|
||||
func (s *Server) ensureBootstrapToken(domainUUID string) error {
|
||||
if value, _ := s.store.GetConfigInDomain(domainUUID, "auth_bootstrap_token_hash"); value != "" {
|
||||
return nil
|
||||
}
|
||||
raw := make([]byte, 24)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return err
|
||||
}
|
||||
token := base64.RawURLEncoding.EncodeToString(raw)
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
if err := s.store.SetConfigInDomain(domainUUID, "auth_bootstrap_token_hash", base64.RawURLEncoding.EncodeToString(sum[:])); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("AUTH BOOTSTRAP TOKEN domain=%s token=%s (one-time; create the owner at /_/auth/login)", domainUUID, token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) validBootstrapToken(domainUUID, token string) bool {
|
||||
want, err := s.store.GetConfigInDomain(domainUUID, "auth_bootstrap_token_hash")
|
||||
if err != nil || want == "" {
|
||||
return false
|
||||
}
|
||||
sum := sha256.Sum256([]byte(strings.TrimSpace(token)))
|
||||
got := base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
return subtle.ConstantTimeCompare([]byte(want), []byte(got)) == 1
|
||||
}
|
||||
|
||||
func safeReturnPath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
parsed, err := url.Parse(value)
|
||||
if err != nil || !strings.HasPrefix(parsed.Path, "/") || strings.HasPrefix(parsed.Path, "//") || strings.ContainsAny(parsed.Path, "\\\r\n\x00") || parsed.IsAbs() || parsed.Host != "" {
|
||||
return "/"
|
||||
}
|
||||
return parsed.RequestURI()
|
||||
}
|
||||
|
||||
func requestIsHTTPS(r *http.Request) bool {
|
||||
if r.TLS != nil {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0]), "https")
|
||||
}
|
||||
|
||||
func (s *Server) setAuthCookie(w http.ResponseWriter, r *http.Request, token string) {
|
||||
http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: 24 * 60 * 60})
|
||||
}
|
||||
|
||||
func (s *Server) authTokenFromRequest(r *http.Request) string {
|
||||
if headers := r.Header.Values("Authorization"); len(headers) != 0 {
|
||||
// An explicitly supplied but invalid credential must never fall back to
|
||||
// an ambient browser session (including unknown credential schemes).
|
||||
if len(headers) != 1 {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Fields(headers[0])
|
||||
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") || len(parts[1]) > 4096 || strings.Contains(parts[1], ",") {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(parts[1], "pw_browser_") {
|
||||
return ""
|
||||
}
|
||||
return parts[1]
|
||||
}
|
||||
var token string
|
||||
count := 0
|
||||
for _, cookie := range r.Cookies() {
|
||||
if cookie.Name == authCookieName {
|
||||
token = cookie.Value
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count == 1 {
|
||||
if strings.HasPrefix(token, "pw_api_") {
|
||||
return ""
|
||||
}
|
||||
return token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/auth"), "/")
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
switch action {
|
||||
case "wallpaper":
|
||||
s.handleAuthWallpaper(w, r)
|
||||
return
|
||||
case "oidc":
|
||||
s.beginOIDC(w, r)
|
||||
return
|
||||
case "ldap":
|
||||
s.handleLDAPLogin(w, r)
|
||||
return
|
||||
case "callback/oidc":
|
||||
s.completeOIDC(w, r)
|
||||
return
|
||||
case "magic-link":
|
||||
if r.Method == http.MethodPost {
|
||||
s.requestMagicLink(w, r)
|
||||
return
|
||||
}
|
||||
case "magic-link/verify":
|
||||
s.verifyMagicLink(w, r)
|
||||
return
|
||||
}
|
||||
if action == "logout" {
|
||||
s.handleLogout(w, r)
|
||||
return
|
||||
}
|
||||
if action != "login" && action != "setup" && action != "totp" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", "GET, HEAD, POST")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
count, err := s.store.PrincipalCount()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
setup := count == 0
|
||||
if setup {
|
||||
if err := s.ensureBootstrapToken(domainUUID); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
var principal *store.Principal
|
||||
mfaLevel := 0
|
||||
if action == "setup" && setup && s.validBootstrapToken(domainUUID, r.FormValue("bootstrap_token")) {
|
||||
principal, err = s.store.BootstrapOwner(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email"))
|
||||
if err == nil {
|
||||
err = s.store.SetConfigInDomain(domainUUID, "authorization_mode", "enforcing")
|
||||
if err == nil {
|
||||
_ = s.store.SetConfigInDomain(domainUUID, "auth_bootstrap_token_hash", "")
|
||||
}
|
||||
}
|
||||
} else if action == "login" && !setup {
|
||||
principal, err = s.store.AuthenticatePassword(r.FormValue("username"), r.FormValue("password"))
|
||||
if err == nil && s.store.HasTOTP(principal.UUID) {
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
|
||||
if challengeErr != nil {
|
||||
err = challengeErr
|
||||
} else if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]any{"mfa_required": true, "challenge_token": challenge, "method": "totp"})
|
||||
return
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Value: challenge, Path: "/_/auth/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode, MaxAge: 300})
|
||||
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": safeReturnPath(r.FormValue("return"))})
|
||||
return
|
||||
}
|
||||
}
|
||||
} else if action == "totp" && !setup {
|
||||
challenge := r.FormValue("challenge_token")
|
||||
if challenge == "" {
|
||||
if cookie, cookieErr := r.Cookie(mfaChallengeCookieName); cookieErr == nil {
|
||||
challenge = cookie.Value
|
||||
}
|
||||
}
|
||||
var principalUUID string
|
||||
principalUUID, err = s.store.CompleteTOTPLoginChallenge(domainUUID, challenge, r.FormValue("code"))
|
||||
if err == nil {
|
||||
principal = &store.Principal{UUID: principalUUID}
|
||||
mfaLevel = 1
|
||||
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Path: "/_/auth/", MaxAge: -1, HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode})
|
||||
}
|
||||
} else {
|
||||
err = store.ErrAccessDenied
|
||||
}
|
||||
if err == nil {
|
||||
var token string
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
token, err = s.store.CreateAuthSessionWithMFA(domainUUID, principal.UUID, mfaLevel)
|
||||
} else {
|
||||
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, mfaLevel)
|
||||
}
|
||||
if err == nil {
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": 86400})
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
http.Redirect(w, r, safeReturnPath(r.FormValue("return")), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
}
|
||||
if action == "totp" {
|
||||
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Error": translate(s.getLocale(domainUUID), "auth.totp_invalid"), "Return": safeReturnPath(r.FormValue("return"))})
|
||||
return
|
||||
}
|
||||
s.renderAuthPage(w, r, map[string]any{"Setup": setup, "Error": translate(s.getLocale(domainUUID), "auth.invalid_credentials"), "Return": safeReturnPath(r.FormValue("return"))})
|
||||
return
|
||||
}
|
||||
s.renderAuthPage(w, r, map[string]any{"Setup": setup, "Return": safeReturnPath(r.URL.Query().Get("return"))})
|
||||
}
|
||||
|
||||
func authorizationTargetForRequest(r *http.Request, domainUUID string) (string, string) {
|
||||
policy := policyForRequest(r)
|
||||
if policy.Target == targetDomain {
|
||||
return string(targetDomain), domainUUID
|
||||
}
|
||||
path := r.URL.Path
|
||||
if strings.HasPrefix(path, "/e/") {
|
||||
return "path", semanticEditorPath(path)
|
||||
}
|
||||
return string(targetPath), path
|
||||
}
|
||||
|
||||
func (s *Server) authorizePathRequest(r *http.Request, capability, path string) error {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mode != "enforcing" {
|
||||
return nil
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: "path", TargetID: path})
|
||||
}
|
||||
|
||||
func ownUserEditorPath(path, username string) bool {
|
||||
for _, prefix := range []string{"/e/-/u/", "/e/u/"} {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
owner := strings.Split(strings.TrimPrefix(path, prefix), "/")[0]
|
||||
return owner == username
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) withAuthorization(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// HTML and dynamic listings contain per-principal data. They must not
|
||||
// enter shared caches even in legacy mode; immutable projections can
|
||||
// choose a more specific private cache policy in their own handlers.
|
||||
if r.URL.Path != "/health" && !strings.HasPrefix(r.URL.Path, "/static/") && !strings.HasPrefix(r.URL.Path, "/o/path:/system/static/") {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Add("Vary", "Cookie, Authorization")
|
||||
}
|
||||
// Runs before handler-owned auth and legacy-mode bypasses, protecting
|
||||
// login, groups, configuration and upload forms as well as JSON APIs.
|
||||
if !validBrowserMutationOrigin(r) {
|
||||
rejectBrowserMutation(w, r)
|
||||
return
|
||||
}
|
||||
// No process-wide principal/policy cache: revocation is observed by the
|
||||
// next request. Read snapshots never authorize mutating store operations.
|
||||
r = cacheRequestDomain(withReadPolicyContext(r))
|
||||
if r.URL.Path == "/g" || strings.HasPrefix(r.URL.Path, "/g/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/_/auth/") || r.URL.Path == "/health" || strings.HasPrefix(r.URL.Path, "/static/") || strings.HasPrefix(r.URL.Path, adapterGatewayPrefix+"/") || strings.HasPrefix(r.URL.Path, "/_/api/v1/adapter/session/") || r.URL.Path == "/_/api/v1/federation/enroll" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
policy := policyForRequest(r)
|
||||
// Calendar handlers enforce per-calendar ACLs, including public reads,
|
||||
// regardless of the domain's legacy/enforcing switch.
|
||||
if taskRoute(r.URL.Path) || calendarRoute(r.URL.Path) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
// Compatibility URL for public application assets, not document objects.
|
||||
// Serve from a confined static FileServer, never from object resolution.
|
||||
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && strings.HasPrefix(r.URL.Path, "/o/path:/system/static/") {
|
||||
http.StripPrefix("/o/path:/system/static/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if policy.Capability == "deny" {
|
||||
http.Error(w, "forbidden: endpoint authorization policy is not defined", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
mode, configErr := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
||||
if configErr != nil {
|
||||
http.Error(w, "authorization policy unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
userSpacePath := ""
|
||||
if store.IsUserSpacePath(r.URL.Path) {
|
||||
userSpacePath = r.URL.Path
|
||||
} else if strings.HasPrefix(r.URL.Path, "/v/raw/path:/u/") {
|
||||
userSpacePath = strings.TrimPrefix(r.URL.Path, "/v/raw/path:")
|
||||
} else if username := publicUserNavigationUsername(r); username != "" {
|
||||
if r.URL.Path == "/v/children" {
|
||||
userSpacePath = r.URL.Query().Get("path")
|
||||
} else {
|
||||
userSpacePath = "/u/" + username
|
||||
}
|
||||
}
|
||||
if mode != "enforcing" && userSpacePath == "" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
token := s.authTokenFromRequest(r)
|
||||
if token == "" {
|
||||
if s.tryAnonymousProjection(w, r) {
|
||||
return
|
||||
}
|
||||
s.authRequired(w, r)
|
||||
return
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domainUUID, token)
|
||||
if err != nil {
|
||||
s.authRequired(w, r)
|
||||
return
|
||||
}
|
||||
if userSpacePath != "" {
|
||||
allowed, audienceErr := s.store.UserSpaceCanReadAuthenticated(r.Context(), domainUUID, principal.UUID, userSpacePath)
|
||||
if audienceErr != nil || !allowed {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
}
|
||||
if mode != "enforcing" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/-/sys/config") {
|
||||
domain := s.domainForRequest(r)
|
||||
if s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") || (domain != nil && s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID)) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/_/api/v1/city/") && r.Method != http.MethodGet && r.Method != http.MethodHead && s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
capability := policy.Capability
|
||||
if capability == "write" && principal.State == "active" && ownUserEditorPath(r.URL.Path, principal.Username) && s.store.CheckLocalityAccessCaps(domainUUID, principal.UUID, "write", semanticEditorPath(r.URL.Path)) == nil {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if handled, resolvedErr := s.authorizeResolvedEndpoint(r, principal, policy); handled {
|
||||
if resolvedErr != nil {
|
||||
if errors.Is(resolvedErr, store.ErrAccessDenied) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
http.Error(w, resolvedErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
targetType, targetID := authorizationTargetForRequest(r, domainUUID)
|
||||
if capability == "write" && targetType == "path" {
|
||||
if _, resolveErr := s.store.ResolvePathInDomain(domainUUID, targetID); errors.Is(resolveErr, sql.ErrNoRows) {
|
||||
capability = "create"
|
||||
}
|
||||
}
|
||||
err = s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: capability, TargetType: targetType, TargetID: targetID})
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrAccessDenied) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) authRequired(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/_/api/") {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]string{"error": "authentication required"})
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/_/auth/login?return="+url.QueryEscape(r.URL.RequestURI()), http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
ldap "github.com/go-ldap/ldap/v3"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type ldapDirectory interface {
|
||||
Bind(string, string) error
|
||||
Search(*ldap.SearchRequest) (*ldap.SearchResult, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type ldapDialFunc func(context.Context, store.AuthProviderConfig) (ldapDirectory, error)
|
||||
|
||||
type ldapIdentity struct {
|
||||
Subject, Username, Email, DisplayName, DN string
|
||||
}
|
||||
|
||||
func ldapTLSConfig(config store.AuthProviderConfig) (*tls.Config, error) {
|
||||
parsed, err := url.Parse(config.LDAPURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig := &tls.Config{ // #nosec G402 -- opt-out is an explicit administrator setting.
|
||||
MinVersion: tls.VersionTLS12,
|
||||
ServerName: parsed.Hostname(),
|
||||
InsecureSkipVerify: config.LDAPSkipTLSVerify,
|
||||
}
|
||||
if config.LDAPCAPEM != "" {
|
||||
pool, poolErr := x509.SystemCertPool()
|
||||
if poolErr != nil || pool == nil {
|
||||
pool = x509.NewCertPool()
|
||||
}
|
||||
if !pool.AppendCertsFromPEM([]byte(config.LDAPCAPEM)) {
|
||||
return nil, errors.New("LDAP CA PEM contains no certificate")
|
||||
}
|
||||
tlsConfig.RootCAs = pool
|
||||
}
|
||||
return tlsConfig, nil
|
||||
}
|
||||
|
||||
func dialLDAPDirectory(_ context.Context, config store.AuthProviderConfig) (ldapDirectory, error) {
|
||||
if err := store.ValidateLDAPProviderConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig, err := ldapTLSConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := ldap.DialURL(config.LDAPURL,
|
||||
ldap.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}),
|
||||
ldap.DialWithTLSConfig(tlsConfig),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn.SetTimeout(8 * time.Second)
|
||||
if config.LDAPStartTLS {
|
||||
if err = conn.StartTLS(tlsConfig); err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func ldapStableSubject(raw []byte) (string, error) {
|
||||
if len(raw) == 0 || len(raw) > 4096 {
|
||||
return "", errors.New("LDAP immutable UID is missing or too large")
|
||||
}
|
||||
return "attr:" + base64.RawURLEncoding.EncodeToString(raw), nil
|
||||
}
|
||||
|
||||
func (s *Server) authenticateLDAPDirectory(ctx context.Context, config store.AuthProviderConfig, login, password string) (ldapIdentity, error) {
|
||||
login = strings.TrimSpace(login)
|
||||
if login == "" || len(login) > 512 || password == "" || len(password) > 4096 || config.UUID == "" {
|
||||
return ldapIdentity{}, store.ErrAccessDenied
|
||||
}
|
||||
if err := store.ValidateLDAPProviderConfig(config); err != nil || !config.Enabled {
|
||||
return ldapIdentity{}, store.ErrAccessDenied
|
||||
}
|
||||
dial := s.ldapDial
|
||||
if dial == nil {
|
||||
dial = dialLDAPDirectory
|
||||
}
|
||||
directory, err := dial(ctx, config)
|
||||
if err != nil {
|
||||
return ldapIdentity{}, err
|
||||
}
|
||||
defer func() { _ = directory.Close() }()
|
||||
if config.LDAPBindDN != "" {
|
||||
if config.LDAPBindPassword == "" {
|
||||
return ldapIdentity{}, errors.New("LDAP service bind password is unavailable")
|
||||
}
|
||||
if err = directory.Bind(config.LDAPBindDN, config.LDAPBindPassword); err != nil {
|
||||
return ldapIdentity{}, fmt.Errorf("LDAP service bind: %w", err)
|
||||
}
|
||||
}
|
||||
attributes := []string{config.LDAPUIDAttribute, config.LDAPUsernameAttribute, config.LDAPEmailAttribute, config.LDAPDisplayAttribute}
|
||||
unique := make([]string, 0, len(attributes))
|
||||
seen := map[string]bool{}
|
||||
for _, attribute := range attributes {
|
||||
key := strings.ToLower(attribute)
|
||||
if !seen[key] {
|
||||
seen[key] = true
|
||||
unique = append(unique, attribute)
|
||||
}
|
||||
}
|
||||
filter := strings.ReplaceAll(config.LDAPSearchFilter, "{{username}}", ldap.EscapeFilter(login))
|
||||
request := ldap.NewSearchRequest(config.LDAPSearchBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 2, 6, false, filter, unique, nil)
|
||||
result, err := directory.Search(request)
|
||||
if err != nil {
|
||||
return ldapIdentity{}, fmt.Errorf("LDAP user search: %w", err)
|
||||
}
|
||||
if len(result.Entries) != 1 || result.Entries[0].DN == "" {
|
||||
return ldapIdentity{}, store.ErrAccessDenied
|
||||
}
|
||||
entry := result.Entries[0]
|
||||
subject, err := ldapStableSubject(entry.GetEqualFoldRawAttributeValue(config.LDAPUIDAttribute))
|
||||
if err != nil {
|
||||
return ldapIdentity{}, err
|
||||
}
|
||||
identity := ldapIdentity{
|
||||
Subject: subject, DN: entry.DN,
|
||||
Username: strings.ToLower(strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPUsernameAttribute))),
|
||||
Email: strings.ToLower(strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPEmailAttribute))),
|
||||
DisplayName: strings.TrimSpace(entry.GetEqualFoldAttributeValue(config.LDAPDisplayAttribute)),
|
||||
}
|
||||
if identity.Username == "" || !utf8.ValidString(identity.Username) || !utf8.ValidString(identity.DisplayName) || !utf8.ValidString(identity.Email) {
|
||||
return ldapIdentity{}, errors.New("LDAP identity attributes are invalid")
|
||||
}
|
||||
// The successful user bind is the password proof. Never retain or log the
|
||||
// supplied password, and do not accept a search result alone as login.
|
||||
if err = directory.Bind(entry.DN, password); err != nil {
|
||||
return ldapIdentity{}, store.ErrAccessDenied
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
|
||||
func (s *Server) authenticateLDAPPrincipal(ctx context.Context, domainUUID, login, password string) (*store.Principal, error) {
|
||||
config, err := s.store.AuthProviderConfig(domainUUID, "ldap")
|
||||
if err != nil || !config.Enabled {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
identity, err := s.authenticateLDAPDirectory(ctx, config, login, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
attributes := map[string]string{"dn": identity.DN, "username": identity.Username, "email": identity.Email, "display_name": identity.DisplayName}
|
||||
principal, err := s.store.PrincipalByExternalIdentity(domainUUID, "ldap", config.UUID, "", identity.Subject)
|
||||
if err == nil {
|
||||
if bindErr := s.store.BindExternalIdentity(principal.UUID, "ldap", config.UUID, "", identity.Subject, attributes); bindErr != nil {
|
||||
return nil, bindErr
|
||||
}
|
||||
return principal, nil
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
switch config.LDAPMatchBy {
|
||||
case "email":
|
||||
principal, err = s.store.PrincipalByVerifiedEmail(domainUUID, identity.Email)
|
||||
default:
|
||||
principal, err = s.store.PrincipalByExternalUsername(domainUUID, identity.Username)
|
||||
}
|
||||
if err == nil {
|
||||
if err = s.store.BindExternalIdentityIfProviderUnbound(domainUUID, principal.UUID, "ldap", config.UUID, "", identity.Subject, attributes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return principal, nil
|
||||
}
|
||||
if !errors.Is(err, store.ErrAccessDenied) || !config.LDAPAutoProvision {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
return s.store.ProvisionExternalAccount(domainUUID, identity.Username, identity.DisplayName, identity.Email, "ldap", config.UUID, "", identity.Subject, attributes)
|
||||
}
|
||||
|
||||
func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "invalid sign-in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
returnPath := safeReturnPath(r.FormValue("return"))
|
||||
select {
|
||||
case s.ldapSlots <- struct{}{}:
|
||||
defer func() { <-s.ldapSlots }()
|
||||
default:
|
||||
http.Error(w, "directory authentication is busy", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
|
||||
defer cancel()
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal, err := s.authenticateLDAPPrincipal(ctx, domainUUID, r.FormValue("username"), r.FormValue("password"))
|
||||
if err != nil {
|
||||
log.Printf("LDAP login failed domain=%s error=%v", domainUUID, err)
|
||||
s.renderAuthPage(w, r, map[string]any{"Error": translate(s.getLocale(domainUUID), "auth.invalid_credentials"), "Return": returnPath})
|
||||
return
|
||||
}
|
||||
if s.store.HasTOTP(principal.UUID) {
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
|
||||
if challengeErr != nil {
|
||||
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusUnauthorized, map[string]any{"mfa_required": true, "challenge_token": challenge, "method": "totp"})
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: mfaChallengeCookieName, Value: challenge, Path: "/_/auth/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteStrictMode, MaxAge: 300})
|
||||
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": returnPath})
|
||||
return
|
||||
}
|
||||
var token string
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
token, err = s.store.CreateAuthSession(domainUUID, principal.UUID)
|
||||
} else {
|
||||
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": 86400})
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
http.Redirect(w, r, returnPath, http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
ldap "github.com/go-ldap/ldap/v3"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type fakeLDAPDirectory struct {
|
||||
result *ldap.SearchResult
|
||||
searchErr error
|
||||
userBindErr error
|
||||
binds [][2]string
|
||||
searchFilter string
|
||||
}
|
||||
|
||||
func (f *fakeLDAPDirectory) Bind(dn, password string) error {
|
||||
f.binds = append(f.binds, [2]string{dn, password})
|
||||
if len(f.binds) > 1 {
|
||||
return f.userBindErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLDAPDirectory) Search(request *ldap.SearchRequest) (*ldap.SearchResult, error) {
|
||||
f.searchFilter = request.Filter
|
||||
return f.result, f.searchErr
|
||||
}
|
||||
|
||||
func (f *fakeLDAPDirectory) Close() error { return nil }
|
||||
|
||||
func ldapEntry(uid, username, email, displayName string) *ldap.Entry {
|
||||
return ldap.NewEntry("uid="+username+",ou=people,dc=example,dc=test", map[string][]string{
|
||||
"entryUUID": {uid},
|
||||
"uid": {username},
|
||||
"mail": {email},
|
||||
"displayName": {displayName},
|
||||
})
|
||||
}
|
||||
|
||||
func configuredLDAPServer(t *testing.T) (*Server, string, store.AuthProviderConfig) {
|
||||
t.Helper()
|
||||
s, _, domain := configFixture(t)
|
||||
config := store.AuthProviderConfig{
|
||||
Kind: "ldap", Enabled: true, LDAPURL: "ldaps://directory.example.test:636",
|
||||
LDAPBindDN: "cn=pageswiki,ou=services,dc=example,dc=test", LDAPBindPassword: "service-secret",
|
||||
LDAPSearchBase: "ou=people,dc=example,dc=test", LDAPSearchFilter: "(&(objectClass=person)(uid={{username}}))",
|
||||
LDAPUIDAttribute: "entryUUID", LDAPUsernameAttribute: "uid", LDAPEmailAttribute: "mail", LDAPDisplayAttribute: "displayName",
|
||||
LDAPMatchBy: "username",
|
||||
}
|
||||
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config, err := s.store.AuthProviderConfig(domain, "ldap")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.ldapSlots = make(chan struct{}, 4)
|
||||
return s, domain, config
|
||||
}
|
||||
|
||||
func TestLDAPAuthenticationSearchesSafelyAndBindsAsUser(t *testing.T) {
|
||||
s, domain, _ := configuredLDAPServer(t)
|
||||
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Directory Admin")}}}
|
||||
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
||||
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "admin", "user-secret")
|
||||
if err != nil || principal.Username != "admin" {
|
||||
t.Fatalf("LDAP principal = %#v, %v", principal, err)
|
||||
}
|
||||
if len(directory.binds) != 2 || directory.binds[0] != [2]string{"cn=pageswiki,ou=services,dc=example,dc=test", "service-secret"} || directory.binds[1][1] != "user-secret" {
|
||||
t.Fatalf("LDAP binds = %#v", directory.binds)
|
||||
}
|
||||
if directory.searchFilter != "(&(objectClass=person)(uid=admin))" {
|
||||
t.Fatalf("search filter = %q", directory.searchFilter)
|
||||
}
|
||||
loaded, err := s.store.AuthProviderConfig(domain, "ldap")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subject, _ := ldapStableSubject([]byte("stable-id"))
|
||||
bound, err := s.store.PrincipalByExternalIdentity(domain, "ldap", loaded.UUID, "", subject)
|
||||
if err != nil || bound.UUID != principal.UUID {
|
||||
t.Fatalf("immutable LDAP binding = %#v, %v", bound, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLDAPAuthenticationEscapesFilterAndRejectsAmbiguousOrBadPassword(t *testing.T) {
|
||||
s, _, config := configuredLDAPServer(t)
|
||||
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}}
|
||||
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
||||
login := `admin*)(uid=*)`
|
||||
if _, err := s.authenticateLDAPDirectory(context.Background(), config, login, "user-secret"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(directory.searchFilter, login) || !strings.Contains(directory.searchFilter, ldap.EscapeFilter(login)) {
|
||||
t.Fatalf("LDAP search input was not escaped: %q", directory.searchFilter)
|
||||
}
|
||||
|
||||
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("one", "admin", "", "Admin"), ldapEntry("two", "admin", "", "Admin")}}
|
||||
directory.binds = nil
|
||||
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "user-secret"); !errors.Is(err, store.ErrAccessDenied) {
|
||||
t.Fatalf("ambiguous directory result accepted: %v", err)
|
||||
}
|
||||
if len(directory.binds) != 1 {
|
||||
t.Fatalf("ambiguous result attempted a user bind: %#v", directory.binds)
|
||||
}
|
||||
|
||||
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}
|
||||
directory.binds = nil
|
||||
directory.userBindErr = errors.New("invalid credentials")
|
||||
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "wrong"); !errors.Is(err, store.ErrAccessDenied) {
|
||||
t.Fatalf("bad LDAP password accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLDAPAutoProvisionUsesImmutableSubjectOnFollowingLogins(t *testing.T) {
|
||||
s, domain, config := configuredLDAPServer(t)
|
||||
config.LDAPAutoProvision = true
|
||||
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "directory-user", "directory@example.test", "Directory User")}}}
|
||||
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
||||
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "directory-user", "user-secret")
|
||||
if err != nil || principal.Username != "directory-user" {
|
||||
t.Fatalf("provisioned LDAP principal = %#v, %v", principal, err)
|
||||
}
|
||||
// Mutable directory attributes may change, but the immutable configured UID
|
||||
// remains the authoritative identity after first login.
|
||||
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "renamed-user", "renamed@example.test", "Renamed User")}}
|
||||
directory.binds = nil
|
||||
again, err := s.authenticateLDAPPrincipal(context.Background(), domain, "renamed-user", "user-secret")
|
||||
if err != nil || again.UUID != principal.UUID {
|
||||
t.Fatalf("immutable LDAP re-login = %#v, %v", again, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLDAPLoginHandlerCreatesTypedBrowserSession(t *testing.T) {
|
||||
s, domain, _ := configuredLDAPServer(t)
|
||||
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Admin")}}}
|
||||
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
||||
form := url.Values{"username": {"admin"}, "password": {"user-secret"}, "return": {"/v/tree/docs"}}
|
||||
request := httptest.NewRequest(http.MethodPost, "https://localhost/_/auth/ldap", strings.NewReader(form.Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
response := httptest.NewRecorder()
|
||||
s.handleLDAPLogin(response, request)
|
||||
if response.Code != http.StatusSeeOther || response.Header().Get("Location") != "/v/tree/docs" {
|
||||
t.Fatalf("LDAP browser login: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
cookies := response.Result().Cookies()
|
||||
if len(cookies) != 1 || cookies[0].Name != authCookieName || !cookies[0].HttpOnly || !cookies[0].Secure || !strings.HasPrefix(cookies[0].Value, "pw_browser_") {
|
||||
t.Fatalf("LDAP browser cookie: %#v", cookies)
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domain, cookies[0].Value)
|
||||
if err != nil || principal.Username != "admin" {
|
||||
t.Fatalf("LDAP browser session: %#v, %v", principal, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestAutomaticPublication(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
ctx := context.Background()
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
actor, err := s.store.PrincipalForSession(domain, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
q, err := s.store.CreateZoneAtPath(domain, actor.UUID, "quartier", "", "auto", "Auto", "", "/auto")
|
||||
check(err)
|
||||
street, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "street", "Street", "", "/auto/street")
|
||||
check(err)
|
||||
spot, err := s.store.CreateSpotAtPath(domain, actor.UUID, street, "spot", "Spot", "", "/auto/street/spot")
|
||||
check(err)
|
||||
setAudience := func(kind, id, value string) {
|
||||
t.Helper()
|
||||
g, e := s.store.CityGovernance(ctx, domain, actor.UUID, kind, id)
|
||||
check(e)
|
||||
_, e = s.store.ChangeCityGovernance(ctx, domain, actor.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false)
|
||||
check(e)
|
||||
}
|
||||
setAudience("domain", domain, "public")
|
||||
setAudience("quartier", q, "public")
|
||||
change := func(kind, id, mode string) {
|
||||
t.Helper()
|
||||
p, e := s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, nil)
|
||||
check(e)
|
||||
_, e = s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, &store.AutoPublicationChange{Mode: mode, Version: p.Version, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Confirm: true, ImpactToken: p.Impact.ImpactToken})
|
||||
check(e)
|
||||
}
|
||||
create := func(path, body string) string {
|
||||
t.Helper()
|
||||
id, e := s.store.CreatePageAtPathInDomain(domain, path)
|
||||
check(e)
|
||||
_, e = s.store.SaveRevisionContent(id, []byte(body), "", "und", "text/html", "")
|
||||
check(e)
|
||||
return id
|
||||
}
|
||||
get := func(path string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest("GET", "http://localhost"+path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
page := create("/auto/street/first", "<p>Original public text</p>")
|
||||
if w := get("/auto/street/first"); w.Code == 200 {
|
||||
t.Fatal("default published")
|
||||
}
|
||||
preview, err := s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, nil)
|
||||
check(err)
|
||||
_, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, ImpactToken: preview.Impact.ImpactToken})
|
||||
if err == nil {
|
||||
t.Fatal("missing confirmation")
|
||||
}
|
||||
create("/auto/street/stale-preview", "<p>Changed impact</p>")
|
||||
_, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, Confirm: true, ImpactToken: preview.Impact.ImpactToken})
|
||||
if err == nil {
|
||||
t.Fatal("stale impact accepted")
|
||||
}
|
||||
change("street", street, "automatic")
|
||||
if err := s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
|
||||
t.Fatal("automatic scope allowed disabling authorization")
|
||||
}
|
||||
if w := get("/auto/street/first"); w.Code != 200 || !strings.Contains(w.Body.String(), "Original public text") {
|
||||
t.Fatal("existing", w.Code, w.Body.String())
|
||||
}
|
||||
create("/auto/street/second", "<p>Future public text</p>")
|
||||
_, err = s.store.SaveRevisionContent(page, []byte("<p>Updated public text</p>"), "", "und", "text/html", "")
|
||||
check(err)
|
||||
for _, path := range []string{"/auto/street/first", "/auto/street/second"} {
|
||||
if w := get(path); w.Code != 200 {
|
||||
t.Fatal("auto head", path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if w := get("/auto/street/first"); !strings.Contains(w.Body.String(), "Updated public text") {
|
||||
t.Fatal("stale head")
|
||||
}
|
||||
create("/auto/streets/not-in-scope", "PRIVATE-BOUNDARY")
|
||||
if w := get("/auto/streets/not-in-scope"); w.Code == 200 {
|
||||
t.Fatal("scope escaped")
|
||||
}
|
||||
create("/auto/street/spot/child", "<p>Spot body</p>")
|
||||
create("/auto/street/spot", "<p>Spot portal</p>")
|
||||
if w := get("/auto/street/spot"); w.Code != 200 {
|
||||
t.Fatal("scope root excluded", w.Code)
|
||||
}
|
||||
if w := get("/auto/street/spot/child"); w.Code != 200 {
|
||||
t.Fatal("inherited auto", w.Code)
|
||||
}
|
||||
change("spot", spot, "required")
|
||||
if w := get("/auto/street/spot"); w.Code == 200 {
|
||||
t.Fatal("scope root escaped manual floor")
|
||||
}
|
||||
if w := get("/auto/street/spot/child"); w.Code == 200 {
|
||||
t.Fatal("manual child did not narrow")
|
||||
}
|
||||
change("spot", spot, "inherit")
|
||||
if w := get("/auto/street/spot/child"); w.Code != 200 {
|
||||
t.Fatal("inherit did not restore")
|
||||
}
|
||||
change("quartier", q, "required")
|
||||
if w := get("/auto/street/first"); w.Code == 200 {
|
||||
t.Fatal("superior floor ignored")
|
||||
}
|
||||
p, err := s.store.AutoPublication(ctx, domain, actor.UUID, "spot", spot, nil)
|
||||
check(err)
|
||||
if p.CanAutomatic {
|
||||
t.Fatal("subordinate bypass offered")
|
||||
}
|
||||
change("quartier", q, "inherit")
|
||||
setAudience("spot", spot, "authenticated_acl")
|
||||
if w := get("/auto/street/spot/child"); w.Code == 200 {
|
||||
t.Fatal("private audience leaked")
|
||||
}
|
||||
setAudience("spot", spot, "inherit")
|
||||
acl, err := s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, nil)
|
||||
check(err)
|
||||
acl.Mode = "restricted"
|
||||
acl.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: actor.Username, Permission: "read_write"}}
|
||||
acl, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl)
|
||||
check(err)
|
||||
if w := get("/auto/street/spot/child"); w.Code == 200 {
|
||||
t.Fatal("restricted ACL leaked")
|
||||
}
|
||||
acl.Mode = "inherit"
|
||||
acl.Entries = nil
|
||||
_, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl)
|
||||
check(err)
|
||||
aliasPage := create("/auto/street/aliased", "PRIVATE-ALIAS")
|
||||
_, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,'/private-alias',?,0,0)`, domain, aliasPage)
|
||||
check(err)
|
||||
if w := get("/auto/street/aliased"); w.Code == 200 {
|
||||
t.Fatal("aliased object published")
|
||||
}
|
||||
// The existing revoke action must stop inherited automatic publication too.
|
||||
publish, err := s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, nil)
|
||||
check(err)
|
||||
_, err = s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, &store.PublicationChange{Epoch: publish.Epoch, TopologyVersion: publish.TopologyVersion, PublicationVersion: publish.PublicationVersion, Enabled: false})
|
||||
check(err)
|
||||
if w := get("/auto/street/spot/child"); w.Code == 200 {
|
||||
t.Fatal("revoke left automatic publication active")
|
||||
}
|
||||
endpoint := "/_/api/v1/city/zones/" + street + "/governance/auto-publication"
|
||||
if w := get(endpoint); w.Code == 200 {
|
||||
t.Fatal("anonymous policy API")
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost"+endpoint, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
var state store.AutoPublication
|
||||
check(json.Unmarshal(w.Body.Bytes(), &state))
|
||||
if w.Code != 200 || state.Mode != "automatic" {
|
||||
t.Fatal("API", w.Code, w.Body.String())
|
||||
}
|
||||
empty, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "empty", "Empty", "", "/auto/empty")
|
||||
check(err)
|
||||
change("street", empty, "automatic")
|
||||
create("/auto/empty/new", "<p>First article in empty scope</p>")
|
||||
if w := get("/auto/empty/new"); w.Code != 200 {
|
||||
t.Fatal("empty scope cannot auto publish", w.Code)
|
||||
}
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") == "1" {
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
script, e := os.ReadFile("internal/server/testdata/automatic-publication.js")
|
||||
check(e)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.Header.Get("Origin") != "" {
|
||||
r.Header.Set("Origin", "http://localhost")
|
||||
}
|
||||
if r.URL.Path == "/v/city/s/auto/empty" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for k, v := range rec.Header() {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, width := range []string{"1440", "390"} {
|
||||
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
|
||||
out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/city/s/auto/empty", t.TempDir(), "", width).CombinedOutput()
|
||||
cancel()
|
||||
if e != nil {
|
||||
t.Fatalf("automatic publication UI %s: %v %s", width, e, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Revoking the enabling principal also invalidates the public-navigation cache.
|
||||
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor.UUID)
|
||||
check(err)
|
||||
if w := get("/auto/street/first"); w.Code == 200 {
|
||||
t.Fatal("revoked enabling authority")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type bookmarkSection struct {
|
||||
Label string
|
||||
Source string
|
||||
Links []NavItem
|
||||
}
|
||||
|
||||
func (s *Server) bookmarkSections(r *http.Request) []bookmarkSection {
|
||||
sections := []bookmarkSection{}
|
||||
for i, item := range s.homeBookmarks(s.domainUUIDForRequest(r)) {
|
||||
if item.Kind == "section" {
|
||||
section := bookmarkSection{Label: item.Label}
|
||||
if item.Children {
|
||||
section.Source = "/v/bookmarks?section=" + strconv.Itoa(i)
|
||||
}
|
||||
sections = append(sections, section)
|
||||
continue
|
||||
}
|
||||
if !validHomeBookmarkURL(item.Href) {
|
||||
continue
|
||||
}
|
||||
u, _ := url.Parse(item.Href)
|
||||
if u.Host == "" && !s.requestCanReadPath(r, u.Path) {
|
||||
continue
|
||||
}
|
||||
if len(sections) == 0 {
|
||||
sections = append(sections, bookmarkSection{Label: translate(s.getLocale(s.domainUUIDForRequest(r)), "home.bookmarks")})
|
||||
}
|
||||
section := §ions[len(sections)-1]
|
||||
if section.Source == "" {
|
||||
section.Links = append(section.Links, item)
|
||||
}
|
||||
}
|
||||
return sections
|
||||
}
|
||||
|
||||
// A small, keyset-paged listing: no revision bodies, stats, or preview work.
|
||||
// The section index is resolved against domain config, not a caller's URL.
|
||||
func (s *Server) handleBookmarkChildren(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", 405)
|
||||
return
|
||||
}
|
||||
items := s.homeBookmarks(s.domainUUIDForRequest(r))
|
||||
i, err := strconv.Atoi(r.URL.Query().Get("section"))
|
||||
if err != nil || i < 0 || i >= len(items) || items[i].Kind != "section" || !items[i].Children {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
u, err := url.Parse(items[i].Href)
|
||||
if err != nil || u.Host != "" || u.Scheme != "" || !strings.HasPrefix(u.Path, "/") || !s.requestCanReadPath(r, u.Path) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
names, more, err := s.store.ListBookmarkChildren(r.Context(), s.domainUUIDForRequest(r), u.Path, r.URL.Query().Get("after"))
|
||||
if err != nil {
|
||||
http.Error(w, "could not load bookmarks", 500)
|
||||
return
|
||||
}
|
||||
links := []NavItem{}
|
||||
prefix := strings.TrimRight(u.Path, "/") + "/"
|
||||
for _, name := range names {
|
||||
p := prefix + name
|
||||
if prefix == "/" && (name == "u" || name == "_" || name == "-") {
|
||||
continue
|
||||
}
|
||||
if s.requestCanReadPath(r, p) {
|
||||
links = append(links, NavItem{Label: name, Href: (&url.URL{Path: p}).String()})
|
||||
}
|
||||
}
|
||||
next := ""
|
||||
if more && len(names) > 0 {
|
||||
next = names[len(names)-1]
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeEventJSON(w, 200, map[string]any{"links": links, "next": next})
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type homeBookmark struct {
|
||||
Label string `json:"label"`
|
||||
Href string `json:"href"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Children bool `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func bookmarksFromForm(r *http.Request) ([]homeBookmark, error) {
|
||||
labels, targets, kinds := r.Form["bookmark_label"], r.Form["bookmark_target"], r.Form["bookmark_kind"]
|
||||
if len(labels) > 64 || len(labels) != len(targets) || len(labels) != len(kinds) {
|
||||
return nil, errors.New("invalid bookmark rows")
|
||||
}
|
||||
if children := r.Form["bookmark_children"]; len(children) != 0 && len(children) != len(labels) {
|
||||
return nil, errors.New("invalid bookmark toggle rows")
|
||||
}
|
||||
bookmarks := []homeBookmark{}
|
||||
sections := 0
|
||||
for i, label := range labels {
|
||||
label, target := strings.TrimSpace(label), strings.TrimSpace(targets[i])
|
||||
if label == "" && target == "" {
|
||||
continue
|
||||
}
|
||||
if label == "" || len(label) > 200 || len(target) > 4096 {
|
||||
return nil, errors.New("invalid bookmark")
|
||||
}
|
||||
switch kinds[i] {
|
||||
case "section":
|
||||
sections++
|
||||
if sections > 16 || strings.ContainsAny(target, "?#\\\r\n") {
|
||||
return nil, errors.New("invalid bookmark section")
|
||||
}
|
||||
children := len(r.Form["bookmark_children"]) > i && r.Form["bookmark_children"][i] == "true"
|
||||
if children && target == "" {
|
||||
return nil, errors.New("missing section parent path")
|
||||
}
|
||||
if target != "" {
|
||||
target = (&url.URL{Path: path.Clean("/" + strings.TrimPrefix(target, "/"))}).String()
|
||||
}
|
||||
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: "section", Children: children})
|
||||
continue
|
||||
case "path":
|
||||
if target == "" || strings.ContainsAny(target, "?#\\\r\n") {
|
||||
return nil, errors.New("invalid bookmark path")
|
||||
}
|
||||
target = path.Clean("/" + strings.TrimPrefix(target, "/"))
|
||||
// Store an escaped local URL so spaces, % and Unicode retain their
|
||||
// literal semantic Path meaning when the link is opened.
|
||||
target = (&url.URL{Path: target}).String()
|
||||
case "url":
|
||||
if !validHomeBookmarkURL(target) {
|
||||
return nil, errors.New("invalid bookmark URL")
|
||||
}
|
||||
default:
|
||||
return nil, errors.New("invalid bookmark kind")
|
||||
}
|
||||
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: kinds[i]})
|
||||
}
|
||||
return bookmarks, nil
|
||||
}
|
||||
|
||||
func (s *Server) managedBookmarks(domainUUID string) []homeBookmark {
|
||||
items := s.homeBookmarks(domainUUID)
|
||||
var out []homeBookmark
|
||||
for _, item := range items {
|
||||
kind := item.Kind
|
||||
if kind == "" {
|
||||
kind = "url"
|
||||
if strings.HasPrefix(item.Href, "/") {
|
||||
kind = "path"
|
||||
}
|
||||
}
|
||||
target := item.Href
|
||||
if kind == "path" || kind == "section" {
|
||||
if u, err := url.Parse(target); err == nil {
|
||||
target = u.Path
|
||||
}
|
||||
}
|
||||
out = append(out, homeBookmark{Label: item.Label, Href: target, Kind: kind, Children: item.Children})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -2,9 +2,44 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type browserEntryView struct {
|
||||
store.BrowserEntry
|
||||
ThumbnailURL string
|
||||
}
|
||||
|
||||
func (s *Server) browserEntryViews(entries []store.BrowserEntry) []browserEntryView {
|
||||
views := make([]browserEntryView, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
view := browserEntryView{BrowserEntry: entry}
|
||||
if !entry.IsDir && entry.RevisionUUID != "" {
|
||||
filename := entry.Filename
|
||||
if filename == "" {
|
||||
filename = filepath.Base(entry.Path)
|
||||
}
|
||||
adapter := selectProjectionAdapter(contentDescriptor{
|
||||
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
|
||||
Filename: filename,
|
||||
SizeBytes: int64(entry.Size),
|
||||
})
|
||||
if adapter.ID == "image" {
|
||||
if cachedURL, cached := s.entryPreviewState(entry, "image", false); cached {
|
||||
view.ThumbnailURL = cachedURL
|
||||
} else {
|
||||
view.ThumbnailURL = rawProjectionURL("rev:"+entry.RevisionUUID) + "?preview=1"
|
||||
}
|
||||
}
|
||||
}
|
||||
views = append(views, view)
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
func (s *Server) handleBrowser(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
@@ -25,7 +60,8 @@ func (s *Server) handleBrowser(
|
||||
}
|
||||
|
||||
entries, err :=
|
||||
s.store.ListBrowserEntries(
|
||||
s.store.ListBrowserEntriesInDomain(
|
||||
s.domainUUIDForRequest(r),
|
||||
currentPath,
|
||||
)
|
||||
|
||||
@@ -33,20 +69,40 @@ func (s *Server) handleBrowser(
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
visibleEntries := entries[:0]
|
||||
for _, entry := range entries {
|
||||
if s.requestCanReadPath(r, entry.Path) {
|
||||
visibleEntries = append(visibleEntries, entry)
|
||||
}
|
||||
}
|
||||
entries = visibleEntries
|
||||
var navigatorLinks []store.BrowserNavigationLink
|
||||
if principal := s.requestPrincipal(r); principal != nil {
|
||||
if links, linkErr := s.store.ListBrowserNavigationLinks(s.domainUUIDForRequest(r), principal.UUID); linkErr == nil {
|
||||
for _, link := range links {
|
||||
if s.requestCanReadPath(r, link.Path) {
|
||||
navigatorLinks = append(navigatorLinks, link)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.render(
|
||||
s.renderWithContext(
|
||||
w,
|
||||
r,
|
||||
"browser.gohtml",
|
||||
map[string]any{
|
||||
"NavItems": resolveNavItems(
|
||||
s.getSystemNavItems(),
|
||||
s.getSystemNavItems(s.domainUUIDForRequest(r)),
|
||||
NavContext{
|
||||
Path: currentPath,
|
||||
},
|
||||
),
|
||||
"Path": currentPath,
|
||||
"Breadcrumbs": buildBreadcrumbs(currentPath),
|
||||
"Entries": entries,
|
||||
"Path": currentPath,
|
||||
"Breadcrumbs": buildBreadcrumbs(currentPath),
|
||||
"Entries": s.browserEntryViews(entries),
|
||||
"NavigatorLinks": navigatorLinks,
|
||||
"CanEditNavigator": s.requestPrincipal(r) != nil,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type browserNavigationInput struct {
|
||||
UUID string `json:"uuid"`
|
||||
Label string `json:"label"`
|
||||
Path string `json:"path"`
|
||||
IsDir bool `json:"is_directory"`
|
||||
}
|
||||
|
||||
func (s *Server) handleBrowserNavigationAPI(w http.ResponseWriter, r *http.Request) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil || principal.State != "active" {
|
||||
http.Error(w, "authentication required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
links, err := s.store.ListBrowserNavigationLinks(domainUUID, principal.UUID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
visible := links[:0]
|
||||
for _, link := range links {
|
||||
if s.requestCanReadPath(r, link.Path) {
|
||||
visible = append(visible, link)
|
||||
}
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"links": visible})
|
||||
case http.MethodPost:
|
||||
var input browserNavigationInput
|
||||
if !decodeBrowserNavigationInput(w, r, &input) {
|
||||
return
|
||||
}
|
||||
input.Path = normalizeAuthorizationPath(input.Path)
|
||||
if input.Path == "" || input.Path == "/" || !s.requestCanReadPath(r, input.Path) {
|
||||
http.Error(w, "invalid or forbidden path", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
link, err := s.store.AddBrowserNavigationLink(domainUUID, principal.UUID, input.Label, input.Path, input.IsDir)
|
||||
if err != nil {
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, store.ErrBrowserNavigationLimit) {
|
||||
status = http.StatusConflict
|
||||
}
|
||||
http.Error(w, err.Error(), status)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(link)
|
||||
case http.MethodPatch:
|
||||
var input browserNavigationInput
|
||||
if !decodeBrowserNavigationInput(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if err := s.store.RenameBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID), input.Label); err != nil {
|
||||
writeBrowserNavigationError(w, err)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
||||
case http.MethodDelete:
|
||||
var input browserNavigationInput
|
||||
if !decodeBrowserNavigationInput(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if err := s.store.DeleteBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID)); err != nil {
|
||||
writeBrowserNavigationError(w, err)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST, PATCH, DELETE")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeBrowserNavigationInput(w http.ResponseWriter, r *http.Request, input *browserNavigationInput) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 16<<10)
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(input); err != nil {
|
||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeBrowserNavigationError(w http.ResponseWriter, err error) {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
http.Error(w, "navigator link not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func browserNavigationRequest(s *Server, token, method string, body any) *httptest.ResponseRecorder {
|
||||
var encoded bytes.Buffer
|
||||
if body != nil {
|
||||
_ = json.NewEncoder(&encoded).Encode(body)
|
||||
}
|
||||
request := httptest.NewRequest(method, "http://localhost/_/api/v1/browser/navigation", &encoded)
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(response, request)
|
||||
return response
|
||||
}
|
||||
|
||||
func TestBrowserMarqueeAndNavigatorDragChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
|
||||
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
|
||||
for _, path := range []string{"/drag/alpha", "/drag/beta"} {
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
testScript := `<script>(async()=>{
|
||||
await new Promise(resolve=>setTimeout(resolve,50));
|
||||
const rows=[...document.querySelectorAll('.browser-row')],grid=document.getElementById('browser-grid');
|
||||
const first=rows[0].getBoundingClientRect(),last=rows.at(-1).getBoundingClientRect(),pointerId=9;
|
||||
grid.dispatchEvent(new PointerEvent('pointerdown',{bubbles:true,button:0,pointerId,clientX:first.left+1,clientY:first.top+1}));
|
||||
grid.dispatchEvent(new PointerEvent('pointermove',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
|
||||
grid.dispatchEvent(new PointerEvent('pointerup',{bubbles:true,button:0,pointerId,clientX:last.right-1,clientY:last.bottom-1}));
|
||||
const transfer=new DataTransfer();
|
||||
transfer.setData('application/x-pageswiki-paths',JSON.stringify(rows.map(row=>({path:row.dataset.path,name:row.dataset.name,is_directory:false}))));
|
||||
await navigatorDrop({preventDefault(){},stopPropagation(){},dataTransfer:transfer});
|
||||
document.body.dataset.browserInteractions=selectedEntries().length===2&&document.querySelectorAll('.navigator-link').length===2?'ready':'failed';
|
||||
})()</script>`
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if strings.HasPrefix(r.URL.Path, "/static/") {
|
||||
s.mux.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/v/browser/drag" {
|
||||
recorded := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(recorded, r)
|
||||
for key, values := range recorded.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(recorded.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(recorded.Body.String(), "</body>", testScript+"</body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
command := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=3000", server.URL+"/v/browser/drag")
|
||||
output, err := command.CombinedOutput()
|
||||
if body := string(output); err != nil || !strings.Contains(body, `data-browser-interactions="ready"`) {
|
||||
t.Fatalf("browser interactions: err=%v body=%s", err, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowserNavigatorAPIAndUI(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
|
||||
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, "/docs/page"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
response := browserNavigationRequest(s, token, http.MethodPost, browserNavigationInput{Label: "Docs", Path: "/docs", IsDir: true})
|
||||
if response.Code != http.StatusCreated {
|
||||
t.Fatalf("add navigator link: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
var link struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &link); err != nil || link.UUID == "" {
|
||||
t.Fatalf("add response: %#v %v", link, err)
|
||||
}
|
||||
response = browserNavigationRequest(s, token, http.MethodPatch, browserNavigationInput{UUID: link.UUID, Label: "Documents"})
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("rename navigator link: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
|
||||
page := configRequest(s, token, http.MethodGet, "/v/browser/", nil)
|
||||
body := page.Body.String()
|
||||
for _, expected := range []string{
|
||||
`data-navigation-id="` + link.UUID + `"`,
|
||||
`href="/v/browser/docs"`,
|
||||
`>Documents</a>`,
|
||||
`/static/js/browser-interactions.js?v=1`,
|
||||
`id="selection-marquee"`,
|
||||
`id="browser-operation-stack"`,
|
||||
} {
|
||||
if page.Code != http.StatusOK || !strings.Contains(body, expected) {
|
||||
t.Fatalf("browser UI missing %q: status=%d body=%s", expected, page.Code, body)
|
||||
}
|
||||
}
|
||||
|
||||
response = browserNavigationRequest(s, token, http.MethodDelete, browserNavigationInput{UUID: link.UUID})
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("delete navigator link: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const browserThumbnailPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
||||
|
||||
func TestBrowserImageThumbnailUsesLazyImmutableRevision(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
|
||||
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/pictures/sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
png, err := base64.StdEncoding.DecodeString(browserThumbnailPNG)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revision, err := s.store.SaveRevisionContent(object, png, "test", "und", "image/png", "sample.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.previewRoot = t.TempDir()
|
||||
|
||||
w := configRequest(s, token, http.MethodGet, "/v/browser/pictures", nil)
|
||||
body := w.Body.String()
|
||||
want := `data-preview-src="/v/raw/rev:` + revision.UUID + `?preview=1"`
|
||||
if w.Code != http.StatusOK || !strings.Contains(body, want) ||
|
||||
!strings.Contains(body, `class="browser-thumbnail children-preview children-preview-pending"`) ||
|
||||
!strings.Contains(body, `/static/js/browser-thumbnails.js?v=1`) {
|
||||
t.Fatalf("browser thumbnail: status=%d want=%q body=%s", w.Code, want, body)
|
||||
}
|
||||
raw := configRequest(s, token, http.MethodGet, "/v/raw/rev:"+revision.UUID+"?preview=1", nil)
|
||||
if raw.Code != http.StatusOK || raw.Header().Get("Content-Type") != "image/png" {
|
||||
t.Fatalf("thumbnail source: status=%d type=%q body=%s", raw.Code, raw.Header().Get("Content-Type"), raw.Body.String())
|
||||
}
|
||||
|
||||
if err = os.WriteFile(filepath.Join(s.previewRoot, revision.DocumentHash+".jpg"), []byte("cached thumbnail"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = configRequest(s, token, http.MethodGet, "/v/browser/pictures", nil)
|
||||
if cached := `/v/preview-cache/` + revision.DocumentHash + `.jpg`; w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `data-preview-src="`+cached+`"`) {
|
||||
t.Fatalf("cached browser thumbnail: status=%d cache=%q body=%s", w.Code, cached, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowserImageThumbnailChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
|
||||
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/pictures/browser.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
png, err := base64.StdEncoding.DecodeString(browserThumbnailPNG)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revision, err := s.store.SaveRevisionContent(object, png, "test", "und", "image/png", "browser.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if strings.HasPrefix(r.URL.Path, "/static/") {
|
||||
s.mux.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=3000", server.URL+"/v/browser/pictures")
|
||||
out, err := cmd.CombinedOutput()
|
||||
body := string(out)
|
||||
scriptReady := strings.Contains(body, `data-browser-thumbnails="ready"`)
|
||||
previewLoaded := strings.Contains(body, `data-preview-loaded="true"`)
|
||||
sourceLoaded := strings.Contains(body, `src="/v/raw/rev:`+revision.UUID+`?preview=1"`)
|
||||
if err != nil || !scriptReady || !previewLoaded || !sourceLoaded {
|
||||
t.Fatalf("chromium thumbnail: err=%v script=%t preview=%t source=%t", err, scriptReady, previewLoaded, sourceLoaded)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"pageswiki/internal/calendar"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type calendarAuthTransport struct {
|
||||
base http.RoundTripper
|
||||
origin, user, password string
|
||||
}
|
||||
|
||||
func (t calendarAuthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
origin, e := url.Parse(t.origin)
|
||||
if e != nil || !strings.EqualFold(origin.Host, r.URL.Host) || origin.Scheme != r.URL.Scheme {
|
||||
return nil, errors.New("calendar credential origin mismatch")
|
||||
}
|
||||
clone := r.Clone(r.Context())
|
||||
clone.SetBasicAuth(t.user, t.password)
|
||||
return t.base.RoundTrip(clone)
|
||||
}
|
||||
|
||||
type remoteCalendarObject struct{ href, etag, data string }
|
||||
|
||||
func remoteCalendarReport(ctx context.Context, client *http.Client, address, body string) ([]remoteCalendarObject, error) {
|
||||
req, e := http.NewRequestWithContext(ctx, "REPORT", address, strings.NewReader(body))
|
||||
if e != nil {
|
||||
return nil, errors.New("invalid CalDAV URL")
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/xml; charset=utf-8")
|
||||
req.Header.Set("Depth", "1")
|
||||
response, e := client.Do(req)
|
||||
if e != nil {
|
||||
return nil, calendarSourceRequestError(e)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != 207 {
|
||||
return nil, errors.New("external CalDAV REPORT returned " + http.StatusText(response.StatusCode))
|
||||
}
|
||||
b, e := io.ReadAll(io.LimitReader(response.Body, calendar.MaxFeed+1))
|
||||
if e != nil || len(b) > calendar.MaxFeed {
|
||||
return nil, calendar.ErrLimit
|
||||
}
|
||||
dec := xml.NewDecoder(bytes.NewReader(b))
|
||||
depth, count := 0, 0
|
||||
for {
|
||||
token, e := dec.Token()
|
||||
if e == io.EOF {
|
||||
break
|
||||
}
|
||||
if e != nil {
|
||||
return nil, errors.New("invalid CalDAV XML")
|
||||
}
|
||||
switch token.(type) {
|
||||
case xml.Directive:
|
||||
return nil, errors.New("CalDAV DTD rejected")
|
||||
case xml.StartElement:
|
||||
depth++
|
||||
count++
|
||||
if depth > 16 || count > 100000 {
|
||||
return nil, calendar.ErrLimit
|
||||
}
|
||||
case xml.EndElement:
|
||||
depth--
|
||||
}
|
||||
}
|
||||
var root davNode
|
||||
if xml.Unmarshal(b, &root) != nil || root.XMLName.Space != davNS || root.XMLName.Local != "multistatus" {
|
||||
return nil, errors.New("invalid CalDAV multistatus")
|
||||
}
|
||||
origin, _ := url.Parse(address)
|
||||
prefix := strings.TrimSuffix(path.Clean(origin.Path), "/") + "/"
|
||||
seen := map[string]bool{}
|
||||
out := []remoteCalendarObject{}
|
||||
for _, row := range root.Children {
|
||||
if row.XMLName.Space != davNS || row.XMLName.Local != "response" {
|
||||
return nil, errors.New("unsupported CalDAV report response")
|
||||
}
|
||||
href := row.child(davNS, "href")
|
||||
if href == nil {
|
||||
return nil, errors.New("CalDAV href missing")
|
||||
}
|
||||
u, e := url.Parse(strings.TrimSpace(href.Text))
|
||||
if e != nil {
|
||||
return nil, errors.New("invalid CalDAV href")
|
||||
}
|
||||
u = origin.ResolveReference(u)
|
||||
if !strings.EqualFold(u.Host, origin.Host) || u.Scheme != origin.Scheme || u.User != nil || u.RawQuery != "" || u.Fragment != "" || !strings.HasPrefix(path.Clean(u.Path), prefix) {
|
||||
return nil, errors.New("CalDAV href outside collection")
|
||||
}
|
||||
key := u.EscapedPath()
|
||||
if seen[key] {
|
||||
return nil, errors.New("duplicate CalDAV href")
|
||||
}
|
||||
seen[key] = true
|
||||
item := remoteCalendarObject{href: key}
|
||||
for _, ps := range row.Children {
|
||||
if ps.XMLName.Space == davNS && ps.XMLName.Local == "status" {
|
||||
return nil, errors.New("incomplete CalDAV report")
|
||||
}
|
||||
if ps.XMLName.Space != davNS || ps.XMLName.Local != "propstat" {
|
||||
continue
|
||||
}
|
||||
status := ps.child(davNS, "status")
|
||||
props := ps.child(davNS, "prop")
|
||||
if status == nil || props == nil || !strings.Contains(status.Text, " 200 ") {
|
||||
return nil, errors.New("CalDAV property unavailable")
|
||||
}
|
||||
if tag := props.child(davNS, "getetag"); tag != nil {
|
||||
item.etag = tag.Text
|
||||
}
|
||||
if data := props.child(calNS, "calendar-data"); data != nil {
|
||||
item.data = data.Text
|
||||
}
|
||||
}
|
||||
if item.etag == "" {
|
||||
return nil, errors.New("CalDAV ETag missing")
|
||||
}
|
||||
out = append(out, item)
|
||||
if len(out) > 5000 {
|
||||
return nil, calendar.ErrLimit
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Materialize a bounded read-only window: 90 days back, 275 days forward.
|
||||
// REPORT fetches validators; calendar-multiget retrieves only changed resources.
|
||||
func fetchCalDAVSource(ctx context.Context, client *http.Client, repo *calendar.Repository, domain string, c calendar.Calendar) ([]calendar.Event, map[string]calendar.RemoteResource, error) {
|
||||
start := time.Now().UTC().Truncate(24*time.Hour).AddDate(0, 0, -90)
|
||||
end := start.AddDate(0, 0, 365)
|
||||
query := `<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:getetag/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT"><c:time-range start="` + start.Format("20060102T150405Z") + `" end="` + end.Format("20060102T150405Z") + `"/></c:comp-filter></c:comp-filter></c:filter></c:calendar-query>`
|
||||
rows, e := remoteCalendarReport(ctx, client, c.Source.URL, query)
|
||||
if e != nil {
|
||||
return nil, nil, e
|
||||
}
|
||||
result := []calendar.Event{}
|
||||
resources := map[string]calendar.RemoteResource{}
|
||||
missing := []remoteCalendarObject{}
|
||||
uids := map[string]bool{}
|
||||
totalBytes := 0
|
||||
add := func(row remoteCalendarObject, event calendar.Event) error {
|
||||
totalBytes += len(event.Data)
|
||||
if totalBytes > calendar.MaxFeed {
|
||||
return calendar.ErrLimit
|
||||
}
|
||||
if uids[event.UID] {
|
||||
return errors.New("CalDAV contains duplicate UID or recurrence exceptions")
|
||||
}
|
||||
uids[event.UID] = true
|
||||
result = append(result, event)
|
||||
resources[row.href] = calendar.RemoteResource{UID: event.UID, ETag: row.etag}
|
||||
return nil
|
||||
}
|
||||
for _, row := range rows {
|
||||
old, ok := c.Source.Resources[row.href]
|
||||
if ok && old.ETag == row.etag {
|
||||
event, e := repo.Event(ctx, domain, c.ID, c.Owner, old.UID)
|
||||
if e == nil {
|
||||
if e = add(row, event); e != nil {
|
||||
return nil, nil, e
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !errors.Is(e, calendar.ErrMissing) {
|
||||
return nil, nil, e
|
||||
}
|
||||
}
|
||||
missing = append(missing, row)
|
||||
}
|
||||
for offset := 0; offset < len(missing); offset += 100 {
|
||||
limit := offset + 100
|
||||
if limit > len(missing) {
|
||||
limit = len(missing)
|
||||
}
|
||||
wanted := map[string]string{}
|
||||
var body strings.Builder
|
||||
body.WriteString(`<c:calendar-multiget xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:getetag/><c:calendar-data/></d:prop>`)
|
||||
for _, row := range missing[offset:limit] {
|
||||
wanted[row.href] = row.etag
|
||||
body.WriteString("<d:href>" + xmlText(row.href) + "</d:href>")
|
||||
}
|
||||
body.WriteString("</c:calendar-multiget>")
|
||||
batch, e := remoteCalendarReport(ctx, client, c.Source.URL, body.String())
|
||||
if e != nil {
|
||||
return nil, nil, e
|
||||
}
|
||||
if len(batch) != len(wanted) {
|
||||
return nil, nil, errors.New("incomplete CalDAV multiget")
|
||||
}
|
||||
for _, row := range batch {
|
||||
tag, ok := wanted[row.href]
|
||||
if !ok || tag != row.etag {
|
||||
return nil, nil, errors.New("CalDAV changed during synchronization; retry required")
|
||||
}
|
||||
event, e := calendar.Parse(row.data)
|
||||
if e != nil {
|
||||
return nil, nil, e
|
||||
}
|
||||
if e = add(row, event); e != nil {
|
||||
return nil, nil, e
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, resources, nil
|
||||
}
|
||||
@@ -0,0 +1,489 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
const davNS = "DAV:"
|
||||
const calNS = "urn:ietf:params:xml:ns:caldav"
|
||||
|
||||
type davNode struct {
|
||||
XMLName xml.Name
|
||||
Attrs []xml.Attr `xml:",any,attr"`
|
||||
Text string `xml:",chardata"`
|
||||
Children []davNode `xml:",any"`
|
||||
}
|
||||
|
||||
func (n davNode) child(ns, name string) *davNode {
|
||||
for i := range n.Children {
|
||||
if n.Children[i].XMLName.Space == ns && n.Children[i].XMLName.Local == name {
|
||||
return &n.Children[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (n davNode) attr(name string) string {
|
||||
for _, a := range n.Attrs {
|
||||
if a.Name.Space == "" && a.Name.Local == name {
|
||||
return a.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func readDAV(w http.ResponseWriter, r *http.Request) (davNode, error) {
|
||||
b, e := io.ReadAll(http.MaxBytesReader(w, r.Body, 64<<10))
|
||||
if e != nil {
|
||||
return davNode{}, e
|
||||
}
|
||||
if len(bytes.TrimSpace(b)) == 0 {
|
||||
return davNode{}, nil
|
||||
}
|
||||
// Reject DTDs and cap nesting before recursive decoding.
|
||||
d := xml.NewDecoder(bytes.NewReader(b))
|
||||
depth, count := 0, 0
|
||||
for {
|
||||
t, e := d.Token()
|
||||
if e == io.EOF {
|
||||
break
|
||||
}
|
||||
if e != nil {
|
||||
return davNode{}, e
|
||||
}
|
||||
switch t.(type) {
|
||||
case xml.Directive:
|
||||
return davNode{}, fmt.Errorf("DTD not allowed")
|
||||
case xml.StartElement:
|
||||
depth++
|
||||
count++
|
||||
if depth > 12 || count > 1000 {
|
||||
return davNode{}, fmt.Errorf("XML complexity limit")
|
||||
}
|
||||
case xml.EndElement:
|
||||
depth--
|
||||
}
|
||||
}
|
||||
var n davNode
|
||||
e = xml.Unmarshal(b, &n)
|
||||
return n, e
|
||||
}
|
||||
func xmlText(s string) string { var b bytes.Buffer; xml.EscapeText(&b, []byte(s)); return b.String() }
|
||||
|
||||
type davResponse struct {
|
||||
href string
|
||||
props map[xml.Name]string
|
||||
status int
|
||||
}
|
||||
|
||||
func davProperty(ns, key, value string) (xml.Name, string) {
|
||||
return xml.Name{Space: ns, Local: key}, value
|
||||
}
|
||||
func writeDAV(w http.ResponseWriter, rows []davResponse, requested []davNode) {
|
||||
var b strings.Builder
|
||||
b.WriteString(`<?xml version="1.0" encoding="UTF-8"?><d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">`)
|
||||
for _, row := range rows {
|
||||
b.WriteString("<d:response><d:href>" + xmlText(row.href) + "</d:href>")
|
||||
if row.status != 0 {
|
||||
fmt.Fprintf(&b, "<d:status>HTTP/1.1 %d %s</d:status>", row.status, http.StatusText(row.status))
|
||||
} else {
|
||||
present, missing := strings.Builder{}, strings.Builder{}
|
||||
props := requested
|
||||
if len(props) == 0 {
|
||||
for n := range row.props {
|
||||
props = append(props, davNode{XMLName: n})
|
||||
}
|
||||
}
|
||||
for _, prop := range props {
|
||||
name := prop.XMLName
|
||||
value, ok := row.props[name]
|
||||
tag := "p:" + name.Local
|
||||
piece := "<" + tag + " xmlns:p=\"" + xmlText(name.Space) + "\">" + value + "</" + tag + ">"
|
||||
if ok {
|
||||
present.WriteString(piece)
|
||||
} else {
|
||||
missing.WriteString(piece)
|
||||
}
|
||||
}
|
||||
if present.Len() > 0 {
|
||||
b.WriteString("<d:propstat><d:prop>" + present.String() + "</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat>")
|
||||
}
|
||||
if missing.Len() > 0 {
|
||||
b.WriteString("<d:propstat><d:prop>" + missing.String() + "</d:prop><d:status>HTTP/1.1 404 Not Found</d:status></d:propstat>")
|
||||
}
|
||||
}
|
||||
b.WriteString("</d:response>")
|
||||
}
|
||||
b.WriteString("</d:multistatus>")
|
||||
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
||||
w.WriteHeader(207)
|
||||
io.WriteString(w, b.String())
|
||||
}
|
||||
func eventDAVRow(c calendar.Calendar, e calendar.Event) davResponse {
|
||||
return davResponse{href: calendarDAV + c.ID + "/" + calendar.DAVFilename(c, e.UID), props: map[xml.Name]string{{Space: davNS, Local: "getetag"}: xmlText(e.ETag), {Space: davNS, Local: "getcontenttype"}: "text/calendar; charset=utf-8", {Space: davNS, Local: "resourcetype"}: "", {Space: calNS, Local: "calendar-data"}: xmlText(e.Data)}}
|
||||
}
|
||||
func calendarDAVRow(c calendar.Calendar) davResponse {
|
||||
return davResponse{href: calendarDAV + c.ID + "/", props: map[xml.Name]string{
|
||||
{Space: davNS, Local: "displayname"}: xmlText(c.PrettyName), {Space: davNS, Local: "resourcetype"}: "<d:collection/><c:calendar/>",
|
||||
{Space: calNS, Local: "supported-calendar-component-set"}: `<c:comp name="VEVENT"/>`,
|
||||
{Space: calNS, Local: "supported-calendar-data"}: `<c:calendar-data content-type="text/calendar" version="2.0"/>`,
|
||||
{Space: davNS, Local: "supported-report-set"}: `<d:supported-report><d:report><c:calendar-query/></d:report></d:supported-report><d:supported-report><d:report><c:calendar-multiget/></d:report></d:supported-report>`,
|
||||
}}
|
||||
}
|
||||
func (s *Server) handleCalendarDAV(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
if strings.TrimSuffix(r.URL.Path, "/") == strings.TrimSuffix(calendarDAV, "/") {
|
||||
s.handleDAVHome(w, r)
|
||||
return
|
||||
}
|
||||
if r.Method == "MKCALENDAR" {
|
||||
s.handleTaskMKCalendar(w, r)
|
||||
return
|
||||
}
|
||||
segment := strings.Split(strings.TrimPrefix(r.URL.Path, calendarDAV), "/")[0]
|
||||
if found, err := s.store.IsTaskDAVCollection(r.Context(), s.domainUUIDForRequest(r), segment); err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
} else if found {
|
||||
s.handleTaskDAVAt(w, r, segment)
|
||||
return
|
||||
}
|
||||
repo, ok := s.calendarReady(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
actor, active, _, credential, authenticated := s.davActor(w, r, "calendar", true)
|
||||
if !authenticated {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.Trim(strings.TrimPrefix(r.URL.Path, calendarDAV), "/"), "/")
|
||||
root := len(parts) == 1 && parts[0] == ""
|
||||
s.setDAVCapabilities(w, r, actor, credential)
|
||||
w.Header().Set("Allow", "OPTIONS, PROPFIND, REPORT, GET, HEAD, PUT, DELETE")
|
||||
if r.Method == "OPTIONS" {
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
var c calendar.Calendar
|
||||
if !root {
|
||||
var e error
|
||||
c, e = repo.Get(domain, parts[0], actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(parts) > 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if r.Method == "PROPFIND" {
|
||||
depth := r.Header.Get("Depth")
|
||||
if depth != "0" && depth != "1" {
|
||||
http.Error(w, "Depth 0 or 1 required", 403)
|
||||
return
|
||||
}
|
||||
n, e := readDAV(w, r)
|
||||
if e != nil {
|
||||
http.Error(w, "invalid XML", 400)
|
||||
return
|
||||
}
|
||||
if n.XMLName.Local != "" && (n.XMLName.Local != "propfind" || n.XMLName.Space != davNS) {
|
||||
http.Error(w, "invalid PROPFIND", 400)
|
||||
return
|
||||
}
|
||||
var req []davNode
|
||||
if p := n.child(davNS, "prop"); p != nil {
|
||||
req = p.Children
|
||||
}
|
||||
rows := []davResponse{}
|
||||
if root {
|
||||
rows = append(rows, davResponse{href: calendarDAV, props: map[xml.Name]string{{Space: davNS, Local: "resourcetype"}: "<d:collection/>", {Space: davNS, Local: "displayname"}: "PagesWiki calendars", {Space: calNS, Local: "calendar-home-set"}: "<d:href>" + calendarDAV + "</d:href>"}})
|
||||
if depth == "1" {
|
||||
cs, e := repo.List(domain, actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for _, v := range cs {
|
||||
rows = append(rows, calendarDAVRow(v))
|
||||
}
|
||||
}
|
||||
} else if len(parts) == 1 {
|
||||
row := calendarDAVRow(c)
|
||||
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(active && c.CanWrite(actor))
|
||||
rows = append(rows, row)
|
||||
if depth == "1" {
|
||||
var items []calendar.Event
|
||||
withData := false
|
||||
for _, p := range req {
|
||||
if !taskDAVFullProperty(p) {
|
||||
http.Error(w, "unsupported property projection", 403)
|
||||
return
|
||||
}
|
||||
if p.XMLName == (xml.Name{Space: calNS, Local: "calendar-data"}) {
|
||||
withData = true
|
||||
}
|
||||
}
|
||||
c, items, e = repo.DAVSnapshot(r.Context(), domain, c.ID, actor, withData)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
rows = append(rows, eventDAVRow(c, item))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uid, e := calendar.DAVUID(c, parts[1])
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
row := eventDAVRow(c, v)
|
||||
row.href = r.URL.Path
|
||||
rows = append(rows, row)
|
||||
}
|
||||
writeDAV(w, rows, req)
|
||||
return
|
||||
}
|
||||
if root {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if r.Method == "REPORT" && len(parts) == 1 {
|
||||
n, e := readDAV(w, r)
|
||||
if e != nil {
|
||||
http.Error(w, "invalid XML", 400)
|
||||
return
|
||||
}
|
||||
if n.XMLName.Space != calNS {
|
||||
http.Error(w, "unsupported report", 405)
|
||||
return
|
||||
}
|
||||
seen := map[string]int{}
|
||||
for _, child := range n.Children {
|
||||
key := child.XMLName.Space + "|" + child.XMLName.Local
|
||||
seen[key]++
|
||||
allowed := child.XMLName.Space == davNS && child.XMLName.Local == "prop" || n.XMLName.Local == "calendar-query" && child.XMLName.Space == calNS && child.XMLName.Local == "filter" || n.XMLName.Local == "calendar-multiget" && child.XMLName.Space == davNS && child.XMLName.Local == "href"
|
||||
if !allowed || child.XMLName.Local != "href" && seen[key] > 1 {
|
||||
http.Error(w, "unsupported report element", 403)
|
||||
return
|
||||
}
|
||||
}
|
||||
p := n.child(davNS, "prop")
|
||||
if p == nil || len(p.Children) == 0 {
|
||||
http.Error(w, "explicit properties required", 400)
|
||||
return
|
||||
}
|
||||
for _, prop := range p.Children {
|
||||
if !taskDAVFullProperty(prop) {
|
||||
http.Error(w, "calendar-data projection/expansion unsupported", 403)
|
||||
return
|
||||
}
|
||||
}
|
||||
rows := []davResponse{}
|
||||
switch n.XMLName.Local {
|
||||
case "calendar-query":
|
||||
filter := n.child(calNS, "filter")
|
||||
if filter == nil || len(filter.Children) != 1 {
|
||||
http.Error(w, "time-range filter required", 403)
|
||||
return
|
||||
}
|
||||
outer := filter.Children[0]
|
||||
if outer.XMLName.Space != calNS || outer.XMLName.Local != "comp-filter" || outer.attr("name") != "VCALENDAR" || len(outer.Children) > 1 || taskDAVAttrCount(outer) != 1 || taskDAVAttrCount(*filter) != 0 {
|
||||
http.Error(w, "unsupported filter", 403)
|
||||
return
|
||||
}
|
||||
inner := davNode{XMLName: xml.Name{Space: calNS, Local: "comp-filter"}}
|
||||
if len(outer.Children) == 1 {
|
||||
inner = outer.Children[0]
|
||||
}
|
||||
if len(outer.Children) == 1 && (inner.XMLName.Space != calNS || inner.XMLName.Local != "comp-filter" || inner.attr("name") != "VEVENT" || len(inner.Children) > 1 || taskDAVAttrCount(inner) != 1) {
|
||||
http.Error(w, "unsupported filter", 403)
|
||||
return
|
||||
}
|
||||
if len(inner.Children) == 0 {
|
||||
withData := false
|
||||
for _, prop := range p.Children {
|
||||
if prop.XMLName == (xml.Name{Space: calNS, Local: "calendar-data"}) {
|
||||
withData = true
|
||||
}
|
||||
}
|
||||
var items []calendar.Event
|
||||
c, items, e = repo.DAVSnapshot(r.Context(), domain, c.ID, actor, withData)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
rows = append(rows, eventDAVRow(c, item))
|
||||
}
|
||||
break
|
||||
}
|
||||
tr := inner.Children[0]
|
||||
if tr.XMLName.Space != calNS || tr.XMLName.Local != "time-range" || len(tr.Children) != 0 || taskDAVAttrCount(tr) != 2 {
|
||||
http.Error(w, "unsupported filter", 403)
|
||||
return
|
||||
}
|
||||
start, e1 := time.Parse("20060102T150405Z", tr.attr("start"))
|
||||
end, e2 := time.Parse("20060102T150405Z", tr.attr("end"))
|
||||
if e1 != nil || e2 != nil {
|
||||
http.Error(w, "UTC start and end required", 400)
|
||||
return
|
||||
}
|
||||
query := repo.QueryETags
|
||||
for _, prop := range p.Children {
|
||||
if prop.XMLName.Space == calNS && prop.XMLName.Local == "calendar-data" {
|
||||
query = repo.Query
|
||||
}
|
||||
}
|
||||
events, e := query(r.Context(), domain, c.ID, actor, start, end)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for _, event := range events {
|
||||
rows = append(rows, eventDAVRow(c, event))
|
||||
}
|
||||
case "calendar-multiget":
|
||||
count := 0
|
||||
for _, node := range n.Children {
|
||||
if node.XMLName.Space != davNS || node.XMLName.Local != "href" {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
if count > 200 {
|
||||
http.Error(w, "too many hrefs", 413)
|
||||
return
|
||||
}
|
||||
u, e := url.Parse(strings.TrimSpace(node.Text))
|
||||
prefix := calendarDAV + c.ID + "/"
|
||||
if e != nil || u.IsAbs() || u.RawQuery != "" || u.Fragment != "" || !strings.HasPrefix(u.Path, prefix) {
|
||||
http.Error(w, "foreign href", 400)
|
||||
return
|
||||
}
|
||||
uid, e := calendar.DAVUID(c, strings.TrimPrefix(u.Path, prefix))
|
||||
if e != nil {
|
||||
http.Error(w, "invalid href", 400)
|
||||
return
|
||||
}
|
||||
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
|
||||
if e == calendar.ErrMissing {
|
||||
rows = append(rows, davResponse{href: u.Path, status: 404})
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
row := eventDAVRow(c, v)
|
||||
row.href = u.Path
|
||||
rows = append(rows, row)
|
||||
}
|
||||
default:
|
||||
http.Error(w, "unsupported report", 403)
|
||||
return
|
||||
}
|
||||
writeDAV(w, rows, p.Children)
|
||||
return
|
||||
}
|
||||
if len(parts) != 2 {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
uid, e := calendar.DAVUID(c, parts[1])
|
||||
if e != nil && r.Method != "PUT" {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case "GET", "HEAD":
|
||||
v, e := repo.Event(r.Context(), domain, c.ID, actor, uid)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
w.Header().Set("ETag", v.ETag)
|
||||
w.Header().Set("Content-Type", "text/calendar; charset=utf-8")
|
||||
if r.Header.Get("If-None-Match") == v.ETag {
|
||||
w.WriteHeader(304)
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
io.WriteString(w, v.Data)
|
||||
}
|
||||
case "PUT", "DELETE":
|
||||
if !active {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
match := r.Header.Get("If-Match")
|
||||
if match == "" && r.Header.Get("If-None-Match") == "*" && r.Method == "PUT" {
|
||||
match = "*"
|
||||
}
|
||||
if match == "" && r.Method != "PUT" {
|
||||
http.Error(w, "If-Match or If-None-Match: * required", 428)
|
||||
return
|
||||
}
|
||||
var event *calendar.Event
|
||||
unchangedRepresentation := false
|
||||
if r.Method == "PUT" {
|
||||
if !strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "text/calendar") {
|
||||
http.Error(w, "text/calendar required", 415)
|
||||
return
|
||||
}
|
||||
data, e := io.ReadAll(http.MaxBytesReader(w, r.Body, calendar.MaxResource))
|
||||
if e != nil {
|
||||
http.Error(w, "resource too large", 413)
|
||||
return
|
||||
}
|
||||
v, e := calendar.Parse(string(data))
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
event = &v
|
||||
unchangedRepresentation = string(data) == v.Data
|
||||
}
|
||||
var authorize func() error
|
||||
if credential != "" {
|
||||
authorize = func() error {
|
||||
return s.store.CheckDAVCredential(r.Context(), domain, actor, credential, true, "calendar")
|
||||
}
|
||||
}
|
||||
v, created, e := repo.ChangeDAV(r.Context(), domain, c.ID, actor, parts[1], match, event, authorize)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
if event != nil && unchangedRepresentation {
|
||||
w.Header().Set("ETag", v.ETag)
|
||||
}
|
||||
if created {
|
||||
w.Header().Set("Location", r.URL.Path)
|
||||
w.WriteHeader(201)
|
||||
} else {
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(405)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
const calendarAPI = "/_/api/v1/extension/organizer.calendar/"
|
||||
const calendarDAV = "/_/api/v1/caldav/"
|
||||
const davCredentialsAPI = "/_/api/v1/caldav-credentials"
|
||||
|
||||
func calendarRoute(path string) bool {
|
||||
return path == "/v/plan" || strings.HasPrefix(path, "/v/plan/") || strings.HasPrefix(path, "/v/event/") || path == "/v/calendar" || strings.HasPrefix(path, calendarAPI) || strings.HasPrefix(path, calendarDAV) || path == strings.TrimSuffix(calendarDAV, "/") || path == davCredentialsAPI || strings.HasPrefix(path, davCredentialsAPI+"/")
|
||||
}
|
||||
func (s *Server) registerCalendarRoutes() {
|
||||
for _, p := range []string{"/v/plan", "/v/plan/", "/v/event/", "/v/calendar"} {
|
||||
s.mux.HandleFunc(p, s.handleCalendarPage)
|
||||
}
|
||||
s.mux.HandleFunc(calendarAPI, s.handleCalendarAPI)
|
||||
s.mux.HandleFunc(calendarDAV, s.handleCalendarDAV)
|
||||
s.mux.HandleFunc(strings.TrimSuffix(calendarDAV, "/"), s.handleCalendarDAV)
|
||||
s.mux.HandleFunc(davCredentialsAPI, s.handleDAVCredentials)
|
||||
s.mux.HandleFunc(davCredentialsAPI+"/", s.handleDAVCredentials)
|
||||
}
|
||||
func (s *Server) calendars() (*calendar.Repository, error) {
|
||||
s.calendarOnce.Do(func() {
|
||||
if s.store.DocumentRoot == "" {
|
||||
s.calendarErr = errors.New("calendar storage unavailable")
|
||||
return
|
||||
}
|
||||
s.calendarRepo, s.calendarErr = calendar.New(filepath.Join(s.store.DocumentRoot, "calendars"), s.store.DB)
|
||||
if s.calendarErr == nil {
|
||||
s.calendarRepo.PublicAllowed = func(domain string) bool {
|
||||
var allowed bool
|
||||
err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM locality_publication_policies p JOIN domains d ON d.uuid=p.domain_uuid AND d.enabled=1 JOIN config c ON c.domain_uuid=p.domain_uuid AND c.key='authorization_mode' AND c.value='enforcing' WHERE p.domain_uuid=? AND p.node_kind='domain' AND p.node_uuid=p.domain_uuid AND p.policy='public')`, domain).Scan(&allowed)
|
||||
return err == nil && allowed
|
||||
}
|
||||
}
|
||||
})
|
||||
return s.calendarRepo, s.calendarErr
|
||||
}
|
||||
func (s *Server) calendarActor(r *http.Request) (string, bool) {
|
||||
p, e := s.store.PrincipalForSession(s.domainUUIDForRequest(r), s.authTokenFromRequest(r))
|
||||
if e != nil {
|
||||
return "", false
|
||||
}
|
||||
return p.UUID, p.State == "active"
|
||||
}
|
||||
func (s *Server) calendarReady(w http.ResponseWriter, r *http.Request) (*calendar.Repository, bool) {
|
||||
if !s.systemExtensionEnabled(s.domainUUIDForRequest(r), "organizer.calendar") {
|
||||
http.NotFound(w, r)
|
||||
return nil, false
|
||||
}
|
||||
repo, e := s.calendars()
|
||||
if e != nil {
|
||||
http.Error(w, "calendar storage unavailable", 503)
|
||||
return nil, false
|
||||
}
|
||||
return repo, true
|
||||
}
|
||||
func calendarError(w http.ResponseWriter, e error) {
|
||||
status := 500
|
||||
message := "calendar operation failed"
|
||||
switch {
|
||||
case errors.Is(e, calendar.ErrMissing):
|
||||
status = 404
|
||||
message = "calendar or event not found"
|
||||
case errors.Is(e, calendar.ErrForbidden):
|
||||
status = 403
|
||||
message = e.Error()
|
||||
case errors.Is(e, calendar.ErrConflict):
|
||||
status = 412
|
||||
message = "The calendar changed. Reload before saving."
|
||||
case errors.Is(e, calendar.ErrPreconditionRequired):
|
||||
status = 428
|
||||
message = "If-Match required"
|
||||
case errors.Is(e, calendar.ErrNameTaken):
|
||||
status = 409
|
||||
message = e.Error()
|
||||
case errors.Is(e, calendar.ErrInvalid):
|
||||
status = 422
|
||||
message = e.Error()
|
||||
case errors.Is(e, calendar.ErrLimit):
|
||||
status = 507
|
||||
message = e.Error()
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
func calendarHref(id, uid string) string {
|
||||
return calendarDAV + id + "/" + base64.RawURLEncoding.EncodeToString([]byte(uid)) + ".ics"
|
||||
}
|
||||
func calendarUID(filename string) (string, error) {
|
||||
if !strings.HasSuffix(filename, ".ics") {
|
||||
return "", calendar.ErrMissing
|
||||
}
|
||||
b, e := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(filename, ".ics"))
|
||||
if e != nil || len(b) == 0 || len(b) > 512 {
|
||||
return "", calendar.ErrMissing
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
func decodeCalendarJSON(w http.ResponseWriter, r *http.Request, dst any) bool {
|
||||
d := json.NewDecoder(http.MaxBytesReader(w, r.Body, calendar.MaxResource))
|
||||
d.DisallowUnknownFields()
|
||||
if e := d.Decode(dst); e != nil {
|
||||
http.Error(w, "invalid JSON", 400)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (s *Server) handleCalendarAPI(w http.ResponseWriter, r *http.Request) {
|
||||
repo, ok := s.calendarReady(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
actor, active := s.calendarActor(r)
|
||||
path := strings.Trim(strings.TrimPrefix(r.URL.Path, calendarAPI), "/")
|
||||
parts := strings.Split(path, "/")
|
||||
if path == "upcoming" {
|
||||
s.handleCalendarUpcoming(w, r, repo, domain, actor)
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "source" {
|
||||
s.handleCalendarSource(w, r, repo, domain, parts[1], actor, active)
|
||||
return
|
||||
}
|
||||
if path == "preferences" {
|
||||
if actor == "" {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
if r.Method != "PUT" {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Calendars []string `json:"calendars"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
if err := repo.SaveDefaults(r.Context(), domain, actor, body.Calendars); err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"calendars": body.Calendars})
|
||||
return
|
||||
}
|
||||
if path == "event" && r.Method == "GET" {
|
||||
cs, e := repo.List(domain, actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
if len(cs) > 256 {
|
||||
calendarError(w, calendar.ErrLimit)
|
||||
return
|
||||
}
|
||||
var found *calendar.Event
|
||||
id := ""
|
||||
for _, c := range cs {
|
||||
v, e := repo.Event(r.Context(), domain, c.ID, actor, r.URL.Query().Get("uid"))
|
||||
if errors.Is(e, calendar.ErrMissing) {
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
if found != nil {
|
||||
writeEventJSON(w, 409, map[string]string{"error": "UID exists in multiple calendars; select a calendar"})
|
||||
return
|
||||
}
|
||||
found = &v
|
||||
id = c.ID
|
||||
}
|
||||
if found == nil {
|
||||
calendarError(w, calendar.ErrMissing)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"calendar": id, "event": found})
|
||||
return
|
||||
}
|
||||
if path == "calendars" {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
cs, e := repo.List(domain, actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for i := range cs {
|
||||
cs[i].Months = nil
|
||||
if cs[i].Source != nil {
|
||||
source := *cs[i].Source
|
||||
source.HasCredential = source.Credential != ""
|
||||
source.Credential = ""
|
||||
source.Resources = nil
|
||||
cs[i].Source = &source
|
||||
}
|
||||
if cs[i].Owner != actor {
|
||||
cs[i].Source = nil // source URLs may contain private subscription tokens
|
||||
role := cs[i].Members[actor]
|
||||
cs[i].Members = map[string]string{}
|
||||
if actor != "" && role != "" {
|
||||
cs[i].Members[actor] = role
|
||||
}
|
||||
}
|
||||
}
|
||||
defaults, e := repo.Defaults(domain, actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"calendars": cs, "actor": actor, "active": active, "public_allowed": repo.PublicAllowed(domain), "defaults": defaults})
|
||||
case "POST":
|
||||
if !active {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
PrettyName *string `json:"pretty_name"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
var pretty []string
|
||||
if body.PrettyName != nil {
|
||||
if !calendar.ValidPrettyName(*body.PrettyName) {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "pretty_name: use letters, numbers, - or _ (1–120 UTF-8 bytes; no commas, spaces or UUIDs)"})
|
||||
return
|
||||
}
|
||||
pretty = []string{*body.PrettyName}
|
||||
}
|
||||
c, e := repo.Create(r.Context(), domain, actor, body.Name, pretty...)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 201, c)
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST")
|
||||
w.WriteHeader(405)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "settings" {
|
||||
if r.Method != "PUT" && r.Method != "DELETE" {
|
||||
w.Header().Set("Allow", "PUT, DELETE")
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if !active {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
c, err := repo.Get(domain, parts[1], actor)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
if c.Owner != actor {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
if r.Method == "DELETE" {
|
||||
var body struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
if body.Version == "" || body.Version != c.Version {
|
||||
calendarError(w, calendar.ErrConflict)
|
||||
return
|
||||
}
|
||||
if err := s.store.CancelEventJobsByTypeKey(calendarSyncType, domain+":"+c.ID); err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
if err := repo.Delete(r.Context(), domain, c.ID, actor, body.Version); err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
Public bool `json:"public"`
|
||||
Members map[string]string `json:"members"`
|
||||
Version string `json:"version"`
|
||||
PrettyName *string `json:"pretty_name"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
// Only existing local-domain users may be collaborators.
|
||||
members := map[string]string{}
|
||||
for id, role := range body.Members {
|
||||
if _, e := uuid.Parse(id); e != nil {
|
||||
p, e := s.store.PrincipalByUsername(domain, id)
|
||||
if e != nil {
|
||||
http.Error(w, "unknown collaborator", 400)
|
||||
return
|
||||
}
|
||||
id = p.UUID
|
||||
}
|
||||
var count int
|
||||
e := s.store.DB.QueryRowContext(r.Context(), `SELECT COUNT(*) FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=? AND state='active'`, domain, id).Scan(&count)
|
||||
if e != nil || count != 1 {
|
||||
http.Error(w, "unknown collaborator", 400)
|
||||
return
|
||||
}
|
||||
members[id] = role
|
||||
}
|
||||
body.Members = members
|
||||
var pretty []string
|
||||
if body.PrettyName != nil {
|
||||
if !calendar.ValidPrettyName(*body.PrettyName) {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "pretty_name: use letters, numbers, - or _ (1–120 UTF-8 bytes; no commas, spaces or UUIDs)"})
|
||||
return
|
||||
}
|
||||
pretty = []string{*body.PrettyName}
|
||||
}
|
||||
c, e := repo.Settings(r.Context(), domain, parts[1], actor, body.Version, body.Name, body.Public, body.Members, pretty...)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
c.Months = nil
|
||||
if c.Source != nil {
|
||||
c.Source.HasCredential = c.Source.Credential != ""
|
||||
c.Source.Credential = ""
|
||||
c.Source.Resources = nil
|
||||
}
|
||||
writeEventJSON(w, 200, c)
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "calendars" && parts[2] == "events" {
|
||||
if r.Method == "GET" {
|
||||
uid := r.URL.Query().Get("uid")
|
||||
if resource := r.URL.Query().Get("resource"); resource != "" {
|
||||
c, err := repo.Get(domain, parts[1], actor)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
uid, err = calendar.DAVUID(c, resource)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
v, e := repo.Event(r.Context(), domain, parts[1], actor, uid)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, v)
|
||||
return
|
||||
}
|
||||
if r.Method != "POST" {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if !active {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
UID string `json:"uid"`
|
||||
Summary string `json:"summary"`
|
||||
Description string `json:"description"`
|
||||
Location string `json:"location"`
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
AllDay bool `json:"all_day"`
|
||||
ETag string `json:"etag"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
old := ""
|
||||
match := "*"
|
||||
status := 201
|
||||
if body.UID != "" {
|
||||
v, e := repo.Event(r.Context(), domain, parts[1], actor, body.UID)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
old = v.Data
|
||||
match = body.ETag
|
||||
status = 200
|
||||
} else {
|
||||
body.UID = uuid.NewString()
|
||||
}
|
||||
v, e := calendar.Build(old, calendar.Event{UID: body.UID, Summary: body.Summary, Description: body.Description, Location: body.Location, Start: body.Start, End: body.End, AllDay: body.AllDay})
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
v, e = repo.Change(r.Context(), domain, parts[1], actor, body.UID, match, &v)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Location", calendarHref(parts[1], v.UID))
|
||||
w.Header().Set("ETag", v.ETag)
|
||||
writeEventJSON(w, status, v)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) handleCalendarPage(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if _, ok := s.calendarReady(w, r); !ok {
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/v/calendar" {
|
||||
http.Redirect(w, r, "/v/plan", 302)
|
||||
return
|
||||
}
|
||||
actor, _ := s.calendarActor(r)
|
||||
edit := strings.HasPrefix(r.URL.Path, "/v/event/") || strings.HasSuffix(r.URL.Path, "/new")
|
||||
if edit && actor == "" {
|
||||
s.authRequired(w, r)
|
||||
return
|
||||
}
|
||||
// The client parses only these URI shapes; malformed dates must be real 404s.
|
||||
if !validCalendarViewPath(r.URL.Path) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
s.renderWithContext(w, r, "calendar.gohtml", map[string]any{"Title": "Calendar", "CalendarActor": actor, "CalendarTimezone": s.getTimezoneName(s.domainUUIDForRequest(r)), "Path": r.URL.Path})
|
||||
}
|
||||
func validCalendarViewPath(path string) bool {
|
||||
if path == "/v/plan" || path == "/v/plan/" || path == "/v/event/new" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(path, "/v/plan/event/") {
|
||||
return len(strings.TrimPrefix(path, "/v/plan/event/")) > 0
|
||||
}
|
||||
if strings.HasPrefix(path, "/v/event/") {
|
||||
return strings.HasSuffix(path, "/edit") && len(strings.TrimSuffix(strings.TrimPrefix(path, "/v/event/"), "/edit")) > 0
|
||||
}
|
||||
if !strings.HasPrefix(path, "/v/plan/cal/") {
|
||||
return false
|
||||
}
|
||||
p := strings.Split(strings.TrimPrefix(path, "/v/plan/cal/"), "/")
|
||||
if len(p) == 5 && p[3] == "week" && p[4] == "list" {
|
||||
p = p[:3]
|
||||
}
|
||||
if len(p) > 4 || len(p) < 1 {
|
||||
return false
|
||||
}
|
||||
if len(p) == 4 {
|
||||
switch p[3] {
|
||||
case "week", "timeline", "events", "new":
|
||||
default:
|
||||
return false
|
||||
}
|
||||
p = p[:3]
|
||||
}
|
||||
layout := []string{"2006", "2006/01", "2006/01/02"}[len(p)-1]
|
||||
v := strings.Join(p, "/")
|
||||
t, e := time.Parse(layout, v)
|
||||
return e == nil && t.Year() > 0 && t.Year() < 9999 && t.Format(layout) == v
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"pageswiki/internal/calendar"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalendarMobileChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium mobile fixture")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Mobile fixture calendar","pretty_name":"mobile"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
var c calendar.Calendar
|
||||
json.Unmarshal(w.Body.Bytes(), &c)
|
||||
for _, title := range []string{"Mobile meeting with a readable title", "Overlapping meeting"} {
|
||||
raw, _ := json.Marshal(map[string]string{"summary": title, "start": "2026-09-22T09:00:00Z", "end": "2026-09-22T10:00:00Z"})
|
||||
if w = calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", string(raw), ""); w.Code != 201 {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer srv.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/calendar-mobile-smoke.mjs", srv.URL, t.TempDir())
|
||||
if out, e := cmd.CombinedOutput(); e != nil {
|
||||
t.Fatalf("mobile calendar: %v %s", e, out)
|
||||
} else {
|
||||
t.Log(string(out))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const calendarSyncType = "calendar.source.sync"
|
||||
const calendarSweepType = "calendar.source.sweep"
|
||||
|
||||
var errCalendarSourceBlocked = errors.New("calendar source address blocked: only public HTTPS endpoints are allowed")
|
||||
var errCalendarSourceDNS = errors.New("calendar source DNS lookup failed")
|
||||
|
||||
// Return only a classified cause, never URL.Error (which can contain tokens).
|
||||
func calendarSourceRequestError(err error) error {
|
||||
if errors.Is(err, errCalendarSourceBlocked) {
|
||||
return errCalendarSourceBlocked
|
||||
}
|
||||
if errors.Is(err, errCalendarSourceDNS) {
|
||||
return errCalendarSourceDNS
|
||||
}
|
||||
var cert *tls.CertificateVerificationError
|
||||
var unknown x509.UnknownAuthorityError
|
||||
if errors.As(err, &cert) || errors.As(err, &unknown) {
|
||||
return errors.New("external calendar TLS certificate verification failed")
|
||||
}
|
||||
var network net.Error
|
||||
if errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &network) && network.Timeout()) {
|
||||
return errors.New("external calendar request timed out")
|
||||
}
|
||||
return errors.New("external calendar connection failed (no reachable address, TLS handshake or redirect failure)")
|
||||
}
|
||||
|
||||
type calendarSyncPayload struct {
|
||||
Domain string `json:"domain"`
|
||||
Calendar string `json:"calendar"`
|
||||
Force bool `json:"force,omitempty"`
|
||||
}
|
||||
|
||||
func validCalendarSourceURL(raw string) bool {
|
||||
u, e := url.Parse(raw)
|
||||
if e != nil || len(raw) > 4096 || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.Fragment != "" || (u.Port() != "" && u.Port() != "443") {
|
||||
return false
|
||||
}
|
||||
host := strings.TrimSuffix(strings.ToLower(u.Hostname()), ".")
|
||||
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
|
||||
return false
|
||||
}
|
||||
if ip, err := netip.ParseAddr(host); err == nil && !publicCalendarIP(ip) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func publicCalendarIP(ip netip.Addr) bool {
|
||||
ip = ip.Unmap()
|
||||
if !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
||||
return false
|
||||
}
|
||||
for _, cidr := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "64:ff9b::/96", "64:ff9b:1::/48", "2002::/16"} {
|
||||
if netip.MustParsePrefix(cidr).Contains(ip) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// DNS is checked at dial time and the approved address is pinned for this
|
||||
// connection. No proxy environment or alternate-port bypass is permitted.
|
||||
func calendarSourceClient() *http.Client {
|
||||
tr := &http.Transport{TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 25 * time.Second, DisableKeepAlives: true}
|
||||
tr.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
host, port, e := net.SplitHostPort(address)
|
||||
if e != nil || port != "443" {
|
||||
return nil, errCalendarSourceBlocked
|
||||
}
|
||||
ips, e := net.DefaultResolver.LookupNetIP(ctx, "ip", host)
|
||||
if e != nil || len(ips) == 0 {
|
||||
return nil, errCalendarSourceDNS
|
||||
}
|
||||
for _, ip := range ips {
|
||||
if !publicCalendarIP(ip) {
|
||||
return nil, errCalendarSourceBlocked
|
||||
}
|
||||
}
|
||||
// Try all validated addresses rather than failing on an unreachable
|
||||
// first IPv6/IPv4 address. Never re-resolve after the safety check.
|
||||
for _, ip := range ips {
|
||||
var conn net.Conn
|
||||
conn, e = (&net.Dialer{Timeout: 2 * time.Second}).DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
|
||||
if e == nil {
|
||||
return conn, nil
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
return nil, e
|
||||
}
|
||||
return &http.Client{Transport: tr, Timeout: 45 * time.Second, CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
if len(via) >= 3 || !validCalendarSourceURL(req.URL.String()) {
|
||||
return errors.New("calendar source redirect blocked")
|
||||
}
|
||||
req.Header.Del("Referer")
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
func fetchCalendarSource(ctx context.Context, client *http.Client, c calendar.Calendar) ([]calendar.Event, string, string, bool, error) {
|
||||
if c.Source == nil || !validCalendarSourceURL(c.Source.URL) {
|
||||
return nil, "", "", false, errors.New("invalid calendar source URL")
|
||||
}
|
||||
req, e := http.NewRequestWithContext(ctx, "GET", c.Source.URL, nil)
|
||||
if e != nil {
|
||||
return nil, "", "", false, errors.New("invalid calendar source URL")
|
||||
}
|
||||
req.Header.Set("Accept", "text/calendar")
|
||||
req.Header.Set("User-Agent", "PagesWiki-Calendar/1")
|
||||
if c.Source.ETag != "" {
|
||||
req.Header.Set("If-None-Match", c.Source.ETag)
|
||||
}
|
||||
if c.Source.Modified != "" {
|
||||
req.Header.Set("If-Modified-Since", c.Source.Modified)
|
||||
}
|
||||
response, e := client.Do(req)
|
||||
if e != nil {
|
||||
return nil, "", "", false, calendarSourceRequestError(e)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode == 304 {
|
||||
if c.Source.Success == 0 {
|
||||
return nil, "", "", false, errors.New("unexpected 304 before initial synchronization")
|
||||
}
|
||||
return nil, "", "", true, nil
|
||||
}
|
||||
if response.StatusCode != 200 {
|
||||
return nil, "", "", false, errors.New("external calendar returned HTTP " + http.StatusText(response.StatusCode))
|
||||
}
|
||||
body, e := io.ReadAll(io.LimitReader(response.Body, calendar.MaxFeed+1))
|
||||
if e != nil {
|
||||
return nil, "", "", false, errors.New("external calendar body could not be read")
|
||||
}
|
||||
now := time.Now().UTC().Truncate(24 * time.Hour)
|
||||
events, e := calendar.ParseExternalFeed(body, now.AddDate(0, 0, -90), now.AddDate(0, 0, 275))
|
||||
if e != nil {
|
||||
e = fmt.Errorf("ICS validation stage failed: %w", e)
|
||||
}
|
||||
return events, response.Header.Get("ETag"), response.Header.Get("Last-Modified"), false, e
|
||||
}
|
||||
func (s *Server) ensureCalendarSweep() error {
|
||||
var exists bool
|
||||
if e := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM event_definitions WHERE uuid=?)`, calendarSweepType).Scan(&exists); e != nil {
|
||||
return e
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
return s.store.SaveEventDefinition(&store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
|
||||
}
|
||||
func (s *Server) enqueueCalendarSource(domain, id string, force bool) (bool, error) {
|
||||
b, _ := json.Marshal(calendarSyncPayload{Domain: domain, Calendar: id, Force: force})
|
||||
return s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+id, string(b), time.Now(), -10, 1)
|
||||
}
|
||||
func (s *Server) sweepCalendarSources() error {
|
||||
repo, e := s.calendars()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
domains, e := s.store.ListDomains()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
for _, d := range domains {
|
||||
if !d.Enabled || !s.systemExtensionEnabled(d.UUID, "organizer.calendar") {
|
||||
continue
|
||||
}
|
||||
cs, e := repo.Sources(d.UUID)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
for _, c := range cs {
|
||||
if c.Source.Enabled && c.Source.Next <= time.Now().UnixMilli() {
|
||||
if _, e = s.enqueueCalendarSource(d.UUID, c.ID, false); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *Server) runCalendarSource(job *store.EventJob) error {
|
||||
var p calendarSyncPayload
|
||||
if e := json.Unmarshal([]byte(job.Payload), &p); e != nil {
|
||||
return e
|
||||
}
|
||||
if !s.systemExtensionEnabled(p.Domain, "organizer.calendar") {
|
||||
return nil
|
||||
}
|
||||
repo, e := s.calendars()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
c, e := repo.SourceCalendar(p.Domain, p.Calendar)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if !c.Source.Enabled || (!p.Force && c.Source.Next > time.Now().UnixMilli()) {
|
||||
return nil
|
||||
}
|
||||
var active bool
|
||||
e = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? AND m.state='active' AND p.state='active' AND d.enabled=1)`, p.Domain, c.Owner).Scan(&active)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if !active {
|
||||
return errors.New("calendar source owner is inactive")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
||||
defer cancel()
|
||||
client := calendarSourceClient()
|
||||
if c.Source.Credential != "" {
|
||||
user, password, err := s.store.OpenCalendarCredential(p.Domain, c.ID, c.Source.Credential)
|
||||
if err != nil {
|
||||
return errors.New("calendar credential unavailable")
|
||||
}
|
||||
client.Transport = calendarAuthTransport{base: client.Transport, origin: c.Source.URL, user: user, password: password}
|
||||
}
|
||||
var events []calendar.Event
|
||||
var etag, modified string
|
||||
var unchanged bool
|
||||
var syncErr error
|
||||
var resources map[string]calendar.RemoteResource
|
||||
if c.Source.Kind == "caldav" {
|
||||
events, resources, syncErr = fetchCalDAVSource(ctx, client, repo, p.Domain, c)
|
||||
} else {
|
||||
events, etag, modified, unchanged, syncErr = fetchCalendarSource(ctx, client, c)
|
||||
}
|
||||
if syncErr != nil {
|
||||
// Deliberately omit source URL, credentials, UID, summary and body.
|
||||
log.Printf("calendar source synchronization failed domain=%s calendar=%s protocol=%s stage=fetch-validate: %v", p.Domain, p.Calendar, c.Source.Kind, syncErr)
|
||||
}
|
||||
if s.store.EventCancellationRequested(job.UUID) {
|
||||
return errors.New("calendar synchronization cancelled")
|
||||
}
|
||||
if e = repo.ApplySource(ctx, p.Domain, c, events, etag, modified, unchanged, syncErr, resources); e != nil {
|
||||
return e
|
||||
}
|
||||
return syncErr
|
||||
}
|
||||
func (s *Server) handleCalendarSource(w http.ResponseWriter, r *http.Request, repo *calendar.Repository, domain, id, actor string, active bool) {
|
||||
if !active {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
c, e := repo.Get(domain, id, actor)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
if c.Owner != actor {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case "PUT":
|
||||
var body struct {
|
||||
Kind string `json:"kind"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
ClearCredential bool `json:"clear_credential"`
|
||||
URL string `json:"url"`
|
||||
IntervalSeconds int64 `json:"interval_seconds"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
if !decodeCalendarJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
body.URL = strings.TrimSpace(body.URL)
|
||||
if !validCalendarSourceURL(body.URL) {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "Use a public HTTPS ICS URL (port 443, no URL username/password)."})
|
||||
return
|
||||
}
|
||||
if body.Kind == "" {
|
||||
body.Kind = "ics"
|
||||
}
|
||||
credential := ""
|
||||
if c.Source != nil && c.Source.URL == body.URL && !body.ClearCredential {
|
||||
credential = c.Source.Credential
|
||||
}
|
||||
if body.Username != "" || body.Password != "" {
|
||||
if body.Username == "" || body.Password == "" || strings.Contains(body.Username, ":") || len(body.Username) > 256 || len(body.Password) > 4096 {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "Supply both username and password (username must not contain a colon)."})
|
||||
return
|
||||
}
|
||||
credential, e = s.store.SealCalendarCredential(domain, id, body.Username, body.Password)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
}
|
||||
c, e = repo.SetSource(r.Context(), domain, id, actor, body.Version, calendar.Source{Kind: body.Kind, Credential: credential, URL: body.URL, IntervalSeconds: body.IntervalSeconds, Enabled: body.Enabled})
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
if body.Enabled {
|
||||
if _, e = s.enqueueCalendarSource(domain, id, true); e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
}
|
||||
c.Months = nil
|
||||
c.Source.HasCredential = c.Source.Credential != ""
|
||||
c.Source.Credential = ""
|
||||
c.Source.Resources = nil
|
||||
writeEventJSON(w, 200, c)
|
||||
case "POST":
|
||||
if c.Source == nil || !c.Source.Enabled {
|
||||
writeEventJSON(w, 409, map[string]string{"error": "Enable a source first"})
|
||||
return
|
||||
}
|
||||
queued, e := s.enqueueCalendarSource(domain, id, true)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
writeEventJSON(w, 202, map[string]bool{"queued": queued})
|
||||
default:
|
||||
w.Header().Set("Allow", "PUT, POST")
|
||||
w.WriteHeader(405)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
func TestCalendarSourceErrorsDoNotExposeSubscriptionURL(t *testing.T) {
|
||||
for _, cause := range []error{errCalendarSourceBlocked, errCalendarSourceDNS, context.DeadlineExceeded} {
|
||||
err := calendarSourceRequestError(fmt.Errorf("https://provider.example/private-token: %w", cause))
|
||||
if strings.Contains(err.Error(), "private-token") || strings.Contains(err.Error(), "https://") {
|
||||
t.Fatal("subscription URL exposed")
|
||||
}
|
||||
if cause == errCalendarSourceBlocked && !strings.Contains(err.Error(), "blocked") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type calendarTestTransport func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f calendarTestTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
func sourceResponse(code int, body string) *http.Response {
|
||||
return &http.Response{StatusCode: code, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}
|
||||
}
|
||||
|
||||
func TestCalendarSourceSSRFAndConditionalGET(t *testing.T) {
|
||||
for _, address := range []string{"127.0.0.1", "::1", "::ffff:127.0.0.1", "10.0.0.1", "192.168.1.1", "169.254.169.254", "100.64.0.1", "2001:db8::1", "198.18.0.1", "64:ff9b::7f00:1"} {
|
||||
if publicCalendarIP(netip.MustParseAddr(address)) {
|
||||
t.Fatal("allowed", address)
|
||||
}
|
||||
}
|
||||
if !publicCalendarIP(netip.MustParseAddr("8.8.8.8")) {
|
||||
t.Fatal("public blocked")
|
||||
}
|
||||
for _, u := range []string{"http://example.org/a", "https://user:pass@example.org/a", "https://example.org:22/a", "file:///etc/passwd", "https://localhost/a", "https://[::1]/a", "https://127.0.0.1/a"} {
|
||||
if validCalendarSourceURL(u) {
|
||||
t.Fatal(u)
|
||||
}
|
||||
}
|
||||
c := calendar.Calendar{Source: &calendar.Source{URL: "https://source.example/feed.ics", ETag: `"etag"`, Success: 1}}
|
||||
client := &http.Client{Transport: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
|
||||
if r.Header.Get("If-None-Match") != `"etag"` {
|
||||
t.Fatal(r.Header)
|
||||
}
|
||||
return sourceResponse(304, ""), nil
|
||||
})}
|
||||
_, _, _, unchanged, e := fetchCalendarSource(context.Background(), client, c)
|
||||
if e != nil || !unchanged {
|
||||
t.Fatal(unchanged, e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchCalendarSourceOutlookProjection(t *testing.T) {
|
||||
feed := "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Microsoft Corporation//Outlook//EN\r\nMETHOD:PUBLISH\r\nX-WR-CALNAME:Synthetic\r\n" +
|
||||
"BEGIN:VTIMEZONE\r\nTZID:Tokyo Standard Time\r\nBEGIN:STANDARD\r\nDTSTART:16010101T000000\r\nTZOFFSETFROM:+0900\r\nTZOFFSETTO:+0900\r\nRRULE:FREQ=YEARLY;BYMONTH=1;BYDAY=1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n" +
|
||||
"BEGIN:VEVENT\r\nUID:synthetic\r\nDTSTAMP:20260901T000000Z\r\nDTSTART;TZID=\"Tokyo Standard Time\":20260922T090000\r\nDTEND;TZID=\"Tokyo Standard Time\":20260922T100000\r\nRRULE:FREQ=DAILY;COUNT=2\r\nSUMMARY:Synthetic\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
|
||||
client := &http.Client{Transport: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
|
||||
response := sourceResponse(200, feed)
|
||||
response.Header.Set("ETag", `"outlook"`)
|
||||
return response, nil
|
||||
})}
|
||||
c := calendar.Calendar{Source: &calendar.Source{URL: "https://outlook.example/calendar.ics"}}
|
||||
events, etag, _, unchanged, err := fetchCalendarSource(context.Background(), client, c)
|
||||
if err != nil || unchanged || etag != `"outlook"` || len(events) != 2 {
|
||||
t.Fatal(len(events), etag, unchanged, err)
|
||||
}
|
||||
if events[0].Start.Format(time.RFC3339) != "2026-09-22T00:00:00Z" {
|
||||
t.Fatal(events[0].Start)
|
||||
}
|
||||
}
|
||||
func TestCalendarCalDAVETagsAndCredentialIsolation(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
repo, e := s.calendars()
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
owner, e := s.store.PrincipalForSession(domain, token)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
c, e := repo.Create(context.Background(), domain, owner.UUID, "Remote", "remote")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
cipher, e := s.store.SealCalendarCredential(domain, c.ID, "remote-user", "secret-password")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if strings.Contains(cipher, "secret-password") {
|
||||
t.Fatal("plaintext secret")
|
||||
}
|
||||
if _, _, e = s.store.OpenCalendarCredential(domain, "wrong-calendar", cipher); e == nil {
|
||||
t.Fatal("credential context bypass")
|
||||
}
|
||||
c, e = repo.SetSource(context.Background(), domain, c.ID, owner.UUID, c.Version, calendar.Source{Kind: "caldav", URL: "https://source.example/remote/", IntervalSeconds: 60, Enabled: true, Credential: cipher})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
event, e := calendar.Build("", calendar.Event{UID: "remote-event", Summary: "Remote", Start: time.Now().UTC().Truncate(time.Hour), End: time.Now().UTC().Truncate(time.Hour).Add(time.Hour)})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
queries, multigets := 0, 0
|
||||
client := &http.Client{Transport: calendarAuthTransport{origin: c.Source.URL, user: "remote-user", password: "secret-password", base: calendarTestTransport(func(r *http.Request) (*http.Response, error) {
|
||||
user, password, ok := r.BasicAuth()
|
||||
if !ok || user != "remote-user" || password != "secret-password" {
|
||||
t.Fatal("Basic auth missing")
|
||||
}
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
withData := strings.Contains(string(b), "calendar-multiget")
|
||||
if withData {
|
||||
multigets++
|
||||
} else {
|
||||
queries++
|
||||
}
|
||||
row := eventDAVRow(c, event)
|
||||
row.href = "/remote/a.ics"
|
||||
wanted := []davNode{{XMLName: xml.Name{Space: davNS, Local: "getetag"}}}
|
||||
if withData {
|
||||
wanted = append(wanted, davNode{XMLName: xml.Name{Space: calNS, Local: "calendar-data"}})
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
writeDAV(w, []davResponse{row}, wanted)
|
||||
return sourceResponse(207, w.Body.String()), nil
|
||||
})}}
|
||||
events, resources, e := fetchCalDAVSource(context.Background(), client, repo, domain, c)
|
||||
if e != nil || len(events) != 1 {
|
||||
t.Fatal(events, e)
|
||||
}
|
||||
if e = repo.ApplySource(context.Background(), domain, c, events, "", "", false, nil, resources); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
c, _ = repo.Get(domain, c.ID, owner.UUID)
|
||||
events, _, e = fetchCalDAVSource(context.Background(), client, repo, domain, c)
|
||||
if e != nil || len(events) != 1 || queries != 2 || multigets != 1 {
|
||||
t.Fatal(queries, multigets, e)
|
||||
}
|
||||
req, _ := http.NewRequest("GET", "https://other.example/remote/", nil)
|
||||
if _, e = client.Transport.RoundTrip(req); e == nil {
|
||||
t.Fatal("credential forwarded to another origin")
|
||||
}
|
||||
w := calendarRequest(s, token, "GET", calendarAPI+"calendars", "", "")
|
||||
if w.Code != 200 || strings.Contains(w.Body.String(), cipher) || strings.Contains(w.Body.String(), "secret-password") || strings.Contains(w.Body.String(), "/remote/a.ics") {
|
||||
t.Fatal("source secret/cache exposed", w.Code)
|
||||
}
|
||||
var payload struct{ Calendars []calendar.Calendar }
|
||||
if e = json.Unmarshal(w.Body.Bytes(), &payload); e != nil || len(payload.Calendars) != 1 || !payload.Calendars[0].Source.HasCredential {
|
||||
t.Fatal(e, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarSourceAPIQueueAndDisable(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
repo, err := s.calendars()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := s.store.PrincipalForSession(domain, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := repo.Create(context.Background(), domain, owner.UUID, "Import", "import")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
endpoint := calendarAPI + "calendars/" + c.ID + "/source"
|
||||
body := func(version string, enabled bool, interval int) string {
|
||||
b, _ := json.Marshal(map[string]any{"version": version, "url": "https://source.example/feed.ics", "kind": "ics", "enabled": enabled, "interval_seconds": interval})
|
||||
return string(b)
|
||||
}
|
||||
if w := calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 59), ""); w.Code != 422 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w := calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 600), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if w = calendarRequest(s, token, "PUT", endpoint, body(c.Version, true, 600), ""); w.Code != 412 {
|
||||
t.Fatal("stale config accepted", w.Code)
|
||||
}
|
||||
if w = calendarRequest(s, token, "POST", endpoint, "{}", ""); w.Code != 202 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
var jobs int
|
||||
if err = s.store.DB.QueryRow(`SELECT COUNT(*) FROM event_jobs WHERE event_type=?`, calendarSyncType).Scan(&jobs); err != nil || jobs != 1 {
|
||||
t.Fatal("queue deduplication", jobs, err)
|
||||
}
|
||||
c, err = repo.Get(domain, c.ID, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = calendarRequest(s, token, "PUT", endpoint, body(c.Version, false, 600), ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if w = calendarRequest(s, token, "POST", endpoint, "{}", ""); w.Code != 409 {
|
||||
t.Fatal("disabled sync accepted", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
func TestCalendarChromiumSmoke(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.registerCalendarRoutes()
|
||||
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar Smoke"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
var c calendar.Calendar
|
||||
json.Unmarshal(w.Body.Bytes(), &c)
|
||||
w = calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", `{"summary":"Browser meeting","start":"2026-09-22T09:00:00Z","end":"2026-09-22T10:00:00Z"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
var event calendar.Event
|
||||
json.Unmarshal(w.Body.Bytes(), &event)
|
||||
// Exercise the Web viewer with a DAV-client filename, not base64url(UID).
|
||||
native, err := s.calendarRepo.Event(context.Background(), domain, c.ID, c.Owner, event.UID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.calendarRepo.Change(context.Background(), domain, c.ID, c.Owner, event.UID, native.ETag, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err = s.calendarRepo.ChangeDAV(context.Background(), domain, c.ID, c.Owner, "browser-client-event.ics", "", &native, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, route := range []string{"/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline", "/v/plan/event/" + event.UID} {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=4000", server.URL+route+"?calendar="+c.ID)
|
||||
out, e := cmd.CombinedOutput()
|
||||
cancel()
|
||||
if e != nil || strings.Contains(string(out), "Uncaught ") || !strings.Contains(string(out), "Browser meeting") {
|
||||
t.Fatalf("%s: %v %s", route, e, out)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/calendar-browser-smoke.mjs", server.URL+"/v/plan/cal/2026/09?calendar="+c.ID, t.TempDir())
|
||||
if out, e := cmd.CombinedOutput(); e != nil {
|
||||
t.Fatalf("browser interactions: %v %s", e, out)
|
||||
} else {
|
||||
t.Log(string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func calendarRequest(s *Server, token, method, path, body, etag string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
|
||||
if token != "" {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
if method == "REPORT" || method == "PROPFIND" {
|
||||
r.Header.Set("Content-Type", "application/xml")
|
||||
r.Header.Set("Depth", "1")
|
||||
}
|
||||
if method == "PUT" && !strings.Contains(path, "settings") {
|
||||
r.Header.Set("Content-Type", "text/calendar")
|
||||
}
|
||||
if etag == "*" {
|
||||
r.Header.Set("If-None-Match", "*")
|
||||
} else if etag != "" {
|
||||
r.Header.Set("If-Match", etag)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
func TestCalendarEndToEndDAVAndPublicRevocation(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO locality_publication_policies(domain_uuid,node_kind,node_uuid,policy,actor_uuid,updated_at_ms) VALUES(?,'domain',?,'public','test',0)`, domain, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
var c calendar.Calendar
|
||||
if e := json.Unmarshal(w.Body.Bytes(), &c); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
path := calendarAPI + "calendars/" + c.ID + "/events"
|
||||
w = calendarRequest(s, token, "POST", path, `{"summary":"Meeting","start":"2026-09-30T23:00:00Z","end":"2026-10-01T02:00:00Z"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
var event calendar.Event
|
||||
json.Unmarshal(w.Body.Bytes(), &event)
|
||||
href := w.Header().Get("Location")
|
||||
report := `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT"><c:time-range start="20261001T000000Z" end="20261002T000000Z"/></c:comp-filter></c:comp-filter></c:filter></c:calendar-query>`
|
||||
w = calendarRequest(s, token, "REPORT", calendarDAV+c.ID+"/", report, "")
|
||||
if w.Code != 207 || !strings.Contains(w.Body.String(), "Meeting") {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w = calendarRequest(s, "", "GET", href, "", "")
|
||||
if w.Code != 404 {
|
||||
t.Fatal("private leaked", w.Code)
|
||||
}
|
||||
w = calendarRequest(s, token, "GET", href, "", "")
|
||||
ics := w.Body.String()
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, ics)
|
||||
}
|
||||
w = calendarRequest(s, token, "PUT", href, ics, "")
|
||||
if w.Code != 428 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w = calendarRequest(s, token, "PUT", href, ics, "*")
|
||||
if w.Code != 412 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
c, _ = s.calendarRepo.Get(s.domainUUIDForRequest(httptest.NewRequest("GET", "http://localhost/", nil)), c.ID, c.Owner)
|
||||
settings := calendarAPI + "calendars/" + c.ID + "/settings"
|
||||
body, _ := json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": c.Version})
|
||||
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
json.Unmarshal(w.Body.Bytes(), &c)
|
||||
w = calendarRequest(s, "", "REPORT", calendarDAV+c.ID+"/", report, "")
|
||||
if w.Code != 207 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w = calendarRequest(s, "", "DELETE", href, "", event.ETag)
|
||||
if w.Code != 403 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": false, "members": map[string]string{}, "version": c.Version})
|
||||
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w = calendarRequest(s, "", "GET", href, "", "")
|
||||
if w.Code != 404 {
|
||||
t.Fatal("revocation failed", w.Code)
|
||||
}
|
||||
// Calendar-level publication cannot override a newly restricted Domain.
|
||||
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": func() string { fresh, _ := s.calendarRepo.Get(domain, c.ID, c.Owner); return fresh.Version }()})
|
||||
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
if _, e := s.store.DB.Exec(`UPDATE locality_publication_policies SET policy='authenticated_acl' WHERE domain_uuid=? AND node_kind='domain'`, domain); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
w = calendarRequest(s, "", "GET", href, "", "")
|
||||
if w.Code != 404 {
|
||||
t.Fatal("Domain ceiling bypass", w.Code)
|
||||
}
|
||||
// A disabled extension closes both protocols, even for its owner.
|
||||
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", false); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
w = calendarRequest(s, token, "GET", href, "", "")
|
||||
if w.Code != 404 {
|
||||
t.Fatal(w.Code)
|
||||
}
|
||||
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", true); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for _, p := range []string{"/v/plan", "/v/plan/cal/2026", "/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline"} {
|
||||
w = calendarRequest(s, token, "GET", p, "", "")
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "calendar-app") {
|
||||
t.Fatal(p, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, p := range []string{"/v/plan/cal/2026/02/30", "/v/plan/cal/2026/13", "/v/plan/bogus"} {
|
||||
w = calendarRequest(s, token, "GET", p, "", "")
|
||||
if w.Code != 404 {
|
||||
t.Fatal(p, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarDeleteAPIUsesVersionAndCancelsQueuedSync(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Disposable","pretty_name":"disposable"}`, "")
|
||||
if w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
var c calendar.Calendar
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+c.ID, `{}`, time.Now().Add(time.Hour), -10, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings := calendarAPI + "calendars/" + c.ID + "/settings"
|
||||
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+uuid.NewString()+`"}`, "")
|
||||
if w.Code != 412 {
|
||||
t.Fatal("stale delete", w.Code, w.Body.String())
|
||||
}
|
||||
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+c.Version+`"}`, "")
|
||||
if w.Code != 204 || w.Body.Len() != 0 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := s.calendarRepo.Get(domain, c.ID, c.Owner); !errors.Is(err, calendar.ErrMissing) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var status string
|
||||
if err := s.store.DB.QueryRow(`SELECT status FROM event_jobs WHERE event_type=? AND event_key=? ORDER BY created_at_ms DESC LIMIT 1`, calendarSyncType, domain+":"+c.ID).Scan(&status); err != nil || status != "cancelled" {
|
||||
t.Fatal(status, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"pageswiki/internal/calendar"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) handleCalendarUpcoming(w http.ResponseWriter, r *http.Request, repo *calendar.Repository, domain, actor string) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", "GET")
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if actor == "" {
|
||||
calendarError(w, calendar.ErrForbidden)
|
||||
return
|
||||
}
|
||||
cs, err := repo.List(domain, actor)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
defaults, err := repo.Defaults(domain, actor)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
if len(defaults) == 0 && len(cs) > 0 {
|
||||
defaults = []string{cs[0].ID}
|
||||
}
|
||||
selected := map[string]bool{}
|
||||
for _, id := range defaults {
|
||||
selected[id] = true
|
||||
}
|
||||
type entry struct {
|
||||
calendar.Event
|
||||
Calendar string `json:"calendar"`
|
||||
Href string `json:"href"`
|
||||
}
|
||||
events := []entry{}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
// Calendar day boundaries use the configured Domain timezone, including
|
||||
// 23/25-hour DST days. The browser sends a civil date only as a hint.
|
||||
location := s.getLocation(domain)
|
||||
now := time.Now().In(location)
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
|
||||
start := today
|
||||
if raw := r.URL.Query().Get("date"); raw != "" {
|
||||
day, e := time.ParseInLocation("2006-01-02", raw, location)
|
||||
if e != nil || day.Before(today.AddDate(0, 0, -2)) || day.After(today.AddDate(0, 0, 1)) {
|
||||
calendarError(w, calendar.ErrInvalid)
|
||||
return
|
||||
}
|
||||
start = day
|
||||
}
|
||||
end := start.AddDate(0, 0, 31)
|
||||
for _, c := range cs {
|
||||
if !selected[c.ID] {
|
||||
continue
|
||||
}
|
||||
items, e := repo.Upcoming(ctx, domain, c.ID, actor, start, end)
|
||||
if e != nil {
|
||||
calendarError(w, e)
|
||||
return
|
||||
}
|
||||
for _, event := range items {
|
||||
events = append(events, entry{Event: event, Calendar: c.Name, Href: "/v/plan/event/" + url.PathEscape(event.UID) + "?calendar=" + url.QueryEscape(c.PrettyName)})
|
||||
}
|
||||
}
|
||||
sort.SliceStable(events, func(i, j int) bool { return events[i].Start.Before(events[j].Start) })
|
||||
if len(events) > 20 {
|
||||
events = events[:20]
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"events": events})
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
func TestCalendarUpcomingDefaultsAndBounds(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.registerCalendarRoutes()
|
||||
repo, e := s.calendars()
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
owner, e := s.store.PrincipalForSession(domain, token)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
c, e := repo.Create(context.Background(), domain, owner.UUID, "Default", "default")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
other, e := repo.Create(context.Background(), domain, owner.UUID, "Other", "other")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = repo.SaveDefaults(context.Background(), domain, owner.UUID, []string{c.ID}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for i := 0; i < 25; i++ {
|
||||
start := time.Now().UTC().Add(time.Duration(i+1) * time.Hour).Truncate(time.Second)
|
||||
b, _ := json.Marshal(map[string]any{"summary": fmt.Sprintf("Event %02d", i), "start": start, "end": start.Add(time.Hour)})
|
||||
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", string(b), ""); w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
start := time.Now().UTC().Add(time.Hour).Truncate(time.Second)
|
||||
b, _ := json.Marshal(map[string]any{"summary": "Not selected", "start": start, "end": start.Add(time.Hour)})
|
||||
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+other.ID+"/events", string(b), ""); w.Code != 201 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
w := calendarRequest(s, token, "GET", calendarAPI+"upcoming", "", "")
|
||||
var result struct {
|
||||
Events []struct {
|
||||
calendar.Event
|
||||
Href string
|
||||
}
|
||||
}
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Events) != 20 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(result.Events[0].Href, "calendar=default") || result.Events[0].Summary != "Event 00" || result.Events[19].Summary != "Event 19" {
|
||||
t.Fatal(result.Events)
|
||||
}
|
||||
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
||||
t.Fatal("private response cached")
|
||||
}
|
||||
if w = calendarRequest(s, "", "GET", calendarAPI+"upcoming", "", ""); w.Code != 403 {
|
||||
t.Fatal("anonymous access", w.Code)
|
||||
}
|
||||
if w = calendarRequest(s, token, "GET", calendarAPI+"upcoming?date=1900-01-01", "", ""); w.Code != 422 {
|
||||
t.Fatal("unbounded dates", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func TestChildrenPathsKeepLiteralURLCharacters(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
path := "/encoding/file ?#%_日本語.txt"
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.SaveRevisionContent(object, []byte("example"), "", "und", "text/plain", "file.txt"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, view := range []string{"list", "detail", "icon"} {
|
||||
rendered, err := s.renderChildrenProjectionFiltered(domain, childrenMacroArgs{TargetPath: "/encoding", View: view, Limit: 100}, func(string) bool { return true })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
z := html.NewTokenizer(strings.NewReader(rendered))
|
||||
found := false
|
||||
for z.Next() != html.ErrorToken {
|
||||
token := z.Token()
|
||||
if token.Data != "a" {
|
||||
continue
|
||||
}
|
||||
for _, attr := range token.Attr {
|
||||
if attr.Key != "href" {
|
||||
continue
|
||||
}
|
||||
parsed, err := url.Parse(attr.Val)
|
||||
if err != nil || parsed.Path != path || parsed.RawQuery != "" || parsed.Fragment != "" {
|
||||
t.Fatalf("%s link=%q (%v)", view, attr.Val, err)
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("%s link missing: %s", view, rendered)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleChildrenProjection(w http.ResponseWriter, r *http.Request) {
|
||||
args := parseChildrenMacroArgs("", r.URL.Query().Get("path"))
|
||||
args.View = r.URL.Query().Get("view")
|
||||
if args.View == "compact" {
|
||||
args.View = "icon"
|
||||
}
|
||||
if args.View != "icon" && args.View != "detail" {
|
||||
args.View = "list"
|
||||
}
|
||||
args.Excludes = splitExcludePatterns(r.URL.Query().Get("exclude"))
|
||||
args.Offset, _ = strconv.Atoi(r.URL.Query().Get("offset"))
|
||||
args.Limit, _ = strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if args.Limit <= 0 || args.Limit > 400 {
|
||||
args.Limit = 120
|
||||
}
|
||||
|
||||
rendered, err := s.renderReadableChildren(r, args)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
_, _ = w.Write([]byte(rendered))
|
||||
}
|
||||
|
||||
func (s *Server) renderChildrenProjection(
|
||||
domainUUID string,
|
||||
args childrenMacroArgs,
|
||||
) (string, error) {
|
||||
return s.renderChildrenProjectionFiltered(domainUUID, args, nil)
|
||||
}
|
||||
|
||||
func (s *Server) renderChildrenProjectionFiltered(
|
||||
domainUUID string,
|
||||
args childrenMacroArgs,
|
||||
allowed func(string) bool,
|
||||
) (string, error) {
|
||||
// /u is a profile/user-Wiki namespace, not a root directory entry. Keep it
|
||||
// reachable by URL while hiding it from every root children projection,
|
||||
// including asynchronous view switches and cached continuation requests.
|
||||
if args.TargetPath == "/" && !isExcludedPath("/u", args.Excludes) {
|
||||
args.Excludes = append(args.Excludes, "/u")
|
||||
}
|
||||
if args.View == "list" {
|
||||
entries, total, err := s.store.ListChildEntriesPageInDomain(domainUUID, args.TargetPath, args.Offset, args.Limit)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var body strings.Builder
|
||||
body.WriteString(`<ul class="children-view-list">`)
|
||||
for _, entry := range entries {
|
||||
child := entry.Path
|
||||
if isExcludedPath(child, args.Excludes) || (allowed != nil && !allowed(child)) {
|
||||
continue
|
||||
}
|
||||
displayName := entry.Title
|
||||
if displayName == "" {
|
||||
displayName = childName(args.TargetPath, child)
|
||||
}
|
||||
body.WriteString(fmt.Sprintf(
|
||||
`<li class="children-entry" data-child-path="%s"><a href="%s">%s</a></li>`,
|
||||
html.EscapeString(child),
|
||||
html.EscapeString((&url.URL{Path: child}).EscapedPath()),
|
||||
html.EscapeString(displayName),
|
||||
))
|
||||
}
|
||||
body.WriteString("</ul>")
|
||||
writeChildrenMore(
|
||||
&body,
|
||||
args,
|
||||
args.Offset+len(entries),
|
||||
total,
|
||||
)
|
||||
return body.String(), nil
|
||||
}
|
||||
|
||||
entries, total, err := s.store.ListChildEntriesPageInDomain(
|
||||
domainUUID,
|
||||
args.TargetPath,
|
||||
args.Offset,
|
||||
args.Limit,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var body strings.Builder
|
||||
if args.View == "icon" {
|
||||
body.WriteString(`<div class="children-icon-grid">`)
|
||||
} else {
|
||||
body.WriteString(`<table class="children-detail"><thead><tr>` +
|
||||
`<th>Name</th><th>Type</th><th>Size</th><th>Updated</th>` +
|
||||
`</tr></thead><tbody>`)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if isExcludedPath(entry.Path, args.Excludes) || (allowed != nil && !allowed(entry.Path)) {
|
||||
continue
|
||||
}
|
||||
if args.View == "icon" {
|
||||
body.WriteString(s.renderChildIcon(args.TargetPath, entry))
|
||||
continue
|
||||
}
|
||||
contentType := entry.ContentType
|
||||
if entry.IsDir {
|
||||
contentType = "folder"
|
||||
}
|
||||
updated := "—"
|
||||
if entry.UpdatedAt > 0 {
|
||||
updated = time.Unix(entry.UpdatedAt, 0).Format("2006-01-02 15:04")
|
||||
}
|
||||
displayName := entry.Title
|
||||
if displayName == "" {
|
||||
displayName = childName(args.TargetPath, entry.Path)
|
||||
}
|
||||
body.WriteString(fmt.Sprintf(
|
||||
`<tr class="children-entry" data-child-path="%s"><td><a href="%s">%s</a></td><td>%s</td><td>%d</td><td>%s</td></tr>`,
|
||||
html.EscapeString(entry.Path),
|
||||
html.EscapeString((&url.URL{Path: entry.Path}).EscapedPath()),
|
||||
html.EscapeString(displayName),
|
||||
html.EscapeString(contentType),
|
||||
entry.Size,
|
||||
updated,
|
||||
))
|
||||
}
|
||||
if args.View == "icon" {
|
||||
body.WriteString("</div>")
|
||||
} else {
|
||||
body.WriteString("</tbody></table>")
|
||||
}
|
||||
writeChildrenMore(&body, args, args.Offset+len(entries), total)
|
||||
return body.String(), nil
|
||||
}
|
||||
|
||||
func writeChildrenMore(
|
||||
body *strings.Builder,
|
||||
args childrenMacroArgs,
|
||||
nextOffset, total int,
|
||||
) {
|
||||
if nextOffset >= total {
|
||||
return
|
||||
}
|
||||
query := url.Values{
|
||||
"path": {args.TargetPath},
|
||||
"view": {args.View},
|
||||
"offset": {strconv.Itoa(nextOffset)},
|
||||
"limit": {strconv.Itoa(args.Limit)},
|
||||
}
|
||||
if len(args.Excludes) > 0 {
|
||||
query.Set("exclude", strings.Join(args.Excludes, ","))
|
||||
}
|
||||
body.WriteString(fmt.Sprintf(
|
||||
`<div class="children-next" data-children-next="%s" aria-label="Load more items"></div>`,
|
||||
html.EscapeString("/v/children?"+query.Encode()),
|
||||
))
|
||||
}
|
||||
|
||||
func childName(parent, child string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(child, parent), "/")
|
||||
}
|
||||
|
||||
func (s *Server) renderChildIcon(parent string, entry store.BrowserEntry) string {
|
||||
path := html.EscapeString(entry.Path)
|
||||
name := entry.Title
|
||||
if name == "" {
|
||||
name = childName(parent, entry.Path)
|
||||
}
|
||||
name = html.EscapeString(name)
|
||||
preview := s.entryPreviewHTML(entry, true)
|
||||
return fmt.Sprintf(
|
||||
`<a class="children-icon-item children-entry" data-child-path="%s" href="%s">%s<span class="children-icon-name">%s</span></a>`,
|
||||
path,
|
||||
html.EscapeString((&url.URL{Path: entry.Path}).EscapedPath()),
|
||||
preview,
|
||||
name,
|
||||
)
|
||||
}
|
||||
|
||||
// entryPreviewHTML is shared by directory listings and search results. Callers
|
||||
// that render broad result sets can disable enqueueing and rely on the periodic
|
||||
// preview scheduler, avoiding a burst of database writes during navigation.
|
||||
func (s *Server) entryPreviewHTML(
|
||||
entry store.BrowserEntry,
|
||||
enqueueMissing bool,
|
||||
) string {
|
||||
if entry.IsDir {
|
||||
return childTypeIcon("folder")
|
||||
}
|
||||
filename := entry.Filename
|
||||
if filename == "" {
|
||||
filename = filepath.Base(entry.Path)
|
||||
}
|
||||
adapter := selectProjectionAdapter(contentDescriptor{
|
||||
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
|
||||
Filename: filename,
|
||||
SizeBytes: int64(entry.Size),
|
||||
})
|
||||
previewURL, cached := s.entryPreviewState(entry, adapter.ID, enqueueMissing)
|
||||
if cached {
|
||||
badge := ""
|
||||
switch adapter.ID {
|
||||
case "pdf":
|
||||
badge = "PDF"
|
||||
case "video":
|
||||
badge = "▶"
|
||||
case "excalidraw":
|
||||
badge = "EX"
|
||||
case "archive":
|
||||
badge = "ZIP"
|
||||
}
|
||||
badgeHTML := ""
|
||||
if badge != "" {
|
||||
badgeHTML = `<span class="children-preview-badge">` + badge + `</span>`
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
`<span class="children-preview children-preview-pending"><img data-preview-kind="image" data-preview-src="%s" alt="">%s</span>`,
|
||||
html.EscapeString(previewURL),
|
||||
badgeHTML,
|
||||
)
|
||||
}
|
||||
switch adapter.ID {
|
||||
case "image":
|
||||
return pendingPreviewIcon("image", previewURL, "")
|
||||
case "video":
|
||||
return pendingPreviewIcon("video", previewURL, "▶")
|
||||
case "pdf":
|
||||
return pendingPreviewIcon("pdf", previewURL, "PDF")
|
||||
case "excalidraw":
|
||||
return pendingPreviewIcon("drawing", previewURL, "EX")
|
||||
case "archive":
|
||||
return childTypeIcon("archive")
|
||||
case "debian-package":
|
||||
return childTypeIcon("package")
|
||||
case "audio":
|
||||
return childTypeIcon("audio")
|
||||
case "text", "csv":
|
||||
if isProgramFilename(filename) {
|
||||
return childTypeIcon("code")
|
||||
}
|
||||
return childTypeIcon("text")
|
||||
default:
|
||||
if isProgramFilename(filename) {
|
||||
return childTypeIcon("code")
|
||||
}
|
||||
switch strings.ToLower(filepath.Ext(filename)) {
|
||||
case ".txt", ".md", ".log", ".rst":
|
||||
return childTypeIcon("text")
|
||||
}
|
||||
return childTypeIcon("file")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) entryPreviewState(
|
||||
entry store.BrowserEntry,
|
||||
kind string,
|
||||
enqueueMissing bool,
|
||||
) (string, bool) {
|
||||
if enqueueMissing {
|
||||
return s.enqueuePreview(entry)
|
||||
}
|
||||
switch kind {
|
||||
case "image", "pdf", "video", "excalidraw", "archive":
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
if s.previewRoot == "" || entry.DocumentHash == "" {
|
||||
return "", false
|
||||
}
|
||||
cacheURL := s.previewCacheURL(entry.DocumentHash, kind)
|
||||
info, err := os.Stat(s.previewCachePath(entry.DocumentHash, kind))
|
||||
return cacheURL, err == nil && info.Size() > 0
|
||||
}
|
||||
|
||||
func pendingPreviewIcon(kind, cacheURL, badge string) string {
|
||||
if cacheURL == "" {
|
||||
return childTypeIcon(kind)
|
||||
}
|
||||
badgeHTML := ""
|
||||
if badge != "" {
|
||||
badgeHTML = `<span class="children-preview-badge">` + badge + `</span>`
|
||||
}
|
||||
return `<span class="children-preview children-preview-placeholder" ` +
|
||||
`data-preview-cache="` + html.EscapeString(cacheURL) + `" ` +
|
||||
`data-preview-badge="` + html.EscapeString(badge) + `">` +
|
||||
childTypeIcon(kind) + badgeHTML + `</span>`
|
||||
}
|
||||
|
||||
func isProgramFilename(filename string) bool {
|
||||
switch strings.ToLower(filepath.Ext(filename)) {
|
||||
case ".py", ".sh", ".bash", ".zsh", ".js", ".jsx", ".ts", ".tsx",
|
||||
".go", ".rs", ".c", ".h", ".cc", ".cpp", ".java", ".rb", ".php",
|
||||
".sql", ".css", ".html", ".yaml", ".yml", ".toml":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func childTypeIcon(kind string) string {
|
||||
paths := map[string]string{
|
||||
"folder": `<path d="M3 6h7l2 2h9v11H3z"/>`,
|
||||
"archive": `<path d="M5 3h14v18H5zM9 3v4h3v4H9v4h3v4H9"/>`,
|
||||
"package": `<path d="m4 8 8-4 8 4v9l-8 4-8-4zM4 8l8 4 8-4M12 12v9"/>`,
|
||||
"audio": `<path d="M9 18V6l10-2v12M9 10l10-2M6 21a3 3 0 1 0 0-6 3 3 0 0 0 0 6M16 19a3 3 0 1 0 0-6 3 3 0 0 0 0 6"/>`,
|
||||
"image": `<path d="M4 5h16v14H4zM7 15l4-4 3 3 2-2 4 4M8 9h.01"/>`,
|
||||
"video": `<path d="M4 6h12v12H4zM16 10l4-2v8l-4-2z"/>`,
|
||||
"pdf": `<path d="M6 3h9l4 4v14H6zM15 3v5h4M8 16h2a2 2 0 0 0 0-4H8v6M13 18v-6h3M13 15h3"/>`,
|
||||
"drawing": `<path d="M4 19l4-1 10-10-3-3L5 15zM13 7l3 3M4 21h16"/>`,
|
||||
"code": `<path d="m9 7-5 5 5 5M15 7l5 5-5 5M14 4l-4 16"/>`,
|
||||
"text": `<path d="M6 3h9l4 4v14H6zM15 3v5h4M9 12h7M9 16h7"/>`,
|
||||
"file": `<path d="M6 3h9l4 4v14H6zM15 3v5h4"/>`,
|
||||
}
|
||||
path := paths[kind]
|
||||
if path == "" {
|
||||
path = paths["file"]
|
||||
}
|
||||
return `<span class="children-type-icon children-type-` + kind +
|
||||
`" aria-hidden="true"><svg viewBox="0 0 24 24">` + path +
|
||||
`</svg></span>`
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type cityAutoPlanItem struct {
|
||||
Path string `json:"path"`
|
||||
PageUUID string `json:"page_uuid"`
|
||||
Quartier, Street, Spot string
|
||||
QuartierPath, StreetPath, SpotPath string `json:",omitempty"`
|
||||
Undefined bool `json:"undefined_spot"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
func cityAutoName(raw, prefix string) string {
|
||||
decoded, err := url.PathUnescape(raw)
|
||||
if err == nil {
|
||||
raw = decoded
|
||||
}
|
||||
name := citySuggestionName(raw)
|
||||
if len(name) >= 2 {
|
||||
return name
|
||||
}
|
||||
sum := sha256.Sum256([]byte(raw))
|
||||
return prefix + "-" + hex.EncodeToString(sum[:4])
|
||||
}
|
||||
|
||||
// Names are identifiers, not ownership. In particular Spot names are unique
|
||||
// across the entire Domain, even when their Streets differ.
|
||||
func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary string) (string, error) {
|
||||
for attempt := 0; attempt < 32; attempt++ {
|
||||
name := base
|
||||
if attempt > 0 {
|
||||
sum := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%d", kind, boundary, attempt)))
|
||||
stem := base
|
||||
if len(stem) > 47 {
|
||||
stem = stem[:47]
|
||||
}
|
||||
name = stem + "-" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
var exists bool
|
||||
var err error
|
||||
if kind == "spot" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM spots WHERE domain_uuid=? AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else if kind == "quartier" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND kind='quartier' AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND parent_uuid=? AND name=? AND state!='archived')`, domain, parent, name).Scan(&exists)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !exists {
|
||||
return name, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("unable to allocate unique generated City name")
|
||||
}
|
||||
|
||||
func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error) {
|
||||
rows, err := s.store.DB.Query(`SELECT p.path,p.object_uuid FROM paths p WHERE p.domain_uuid=? AND p.path LIKE '/%' AND p.path!='/u' AND p.path NOT LIKE '/u/%'
|
||||
AND NOT EXISTS(SELECT 1 FROM city_page_overrides ov JOIN spots dest ON dest.uuid=ov.spot_uuid AND dest.domain_uuid=ov.domain_uuid AND dest.state!='archived' WHERE ov.domain_uuid=p.domain_uuid AND ov.page_uuid=p.object_uuid)
|
||||
ORDER BY p.path`, domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []cityAutoPlanItem
|
||||
for rows.Next() {
|
||||
var semanticPath, pageUUID string
|
||||
if err := rows.Scan(&semanticPath, &pageUUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parts := strings.Split(strings.Trim(semanticPath, "/"), "/")
|
||||
if len(parts) < 3 || parts[0] == "system" {
|
||||
continue
|
||||
}
|
||||
item := cityAutoPlanItem{Path: semanticPath, PageUUID: pageUUID, Quartier: cityAutoName(parts[0], "quartier"), Street: cityAutoName(parts[1], "street"), QuartierPath: "/" + parts[0], StreetPath: "/" + parts[0] + "/" + parts[1]}
|
||||
if len(parts) >= 4 {
|
||||
item.Spot = cityAutoName(parts[2], "spot")
|
||||
item.SpotPath = item.StreetPath + "/" + parts[2]
|
||||
} else {
|
||||
item.Undefined = true
|
||||
item.Spot = item.Street + "-undefined"
|
||||
item.SpotPath = item.StreetPath + "/_undefined"
|
||||
}
|
||||
item.Action = "create"
|
||||
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
|
||||
item.Quartier = q.Name
|
||||
item.Action = "merge"
|
||||
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", q.UUID, item.StreetPath); e == nil {
|
||||
item.Street = st.Name
|
||||
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", st.UUID, item.SpotPath); e == nil {
|
||||
item.Spot = spot.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Server) applyCityAutoPlan(domainUUID, actor string) (int, error) {
|
||||
plan, err := s.buildCityAutoPlan(domainUUID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
quartiers, streets, spots := map[string]string{}, map[string]string{}, map[string]string{}
|
||||
applied := 0
|
||||
for _, item := range plan {
|
||||
qID := quartiers[item.QuartierPath]
|
||||
if qID == "" {
|
||||
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
|
||||
qID = q.UUID
|
||||
} else {
|
||||
if !errors.Is(e, sql.ErrNoRows) {
|
||||
return applied, e
|
||||
}
|
||||
name, err := s.availableCityAutoName(domainUUID, "quartier", "", item.Quartier, item.QuartierPath)
|
||||
if err != nil {
|
||||
return applied, err
|
||||
}
|
||||
qID, e = s.store.CreateZoneAtPath(domainUUID, actor, "quartier", "", name, item.Quartier, "Generated from URL Paths", item.QuartierPath)
|
||||
if e != nil {
|
||||
return applied, e
|
||||
}
|
||||
}
|
||||
quartiers[item.QuartierPath] = qID
|
||||
}
|
||||
streetKey := qID + item.StreetPath
|
||||
streetID := streets[streetKey]
|
||||
if streetID == "" {
|
||||
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", qID, item.StreetPath); e == nil {
|
||||
streetID = st.UUID
|
||||
} else {
|
||||
if !errors.Is(e, sql.ErrNoRows) {
|
||||
return applied, e
|
||||
}
|
||||
name, err := s.availableCityAutoName(domainUUID, "street", qID, item.Street, item.StreetPath)
|
||||
if err != nil {
|
||||
return applied, err
|
||||
}
|
||||
streetID, e = s.store.CreateZoneAtPath(domainUUID, actor, "street", qID, name, item.Street, "", item.StreetPath)
|
||||
if e != nil {
|
||||
return applied, e
|
||||
}
|
||||
}
|
||||
streets[streetKey] = streetID
|
||||
}
|
||||
spotKey := streetID + item.SpotPath
|
||||
spotID := spots[spotKey]
|
||||
if spotID == "" {
|
||||
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", streetID, item.SpotPath); e == nil {
|
||||
spotID = spot.UUID
|
||||
} else {
|
||||
if !errors.Is(e, sql.ErrNoRows) {
|
||||
return applied, e
|
||||
}
|
||||
name, err := s.availableCityAutoName(domainUUID, "spot", streetID, item.Spot, item.SpotPath)
|
||||
if err != nil {
|
||||
return applied, err
|
||||
}
|
||||
spotID, e = s.store.CreateSpotAtPath(domainUUID, actor, streetID, name, item.Spot, "Generated from URL Paths", item.SpotPath)
|
||||
if e != nil {
|
||||
return applied, e
|
||||
}
|
||||
}
|
||||
spots[spotKey] = spotID
|
||||
}
|
||||
if err := s.store.AddSpotPage(domainUUID, actor, spotID, item.PageUUID, item.Path, "primary", path.Base(item.Path)); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return applied, err
|
||||
}
|
||||
applied++
|
||||
}
|
||||
return applied, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCityAutoPlanUsesFixedURLDepthAndMergesDescendants(t *testing.T) {
|
||||
database, err := db.Open(filepath.Join(t.TempDir(), "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.New(database)
|
||||
domain, err := st.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range []string{"/tech/dev/readme", "/tech/dev/lang/python/page", "/tech/dev/lang/py3/page2"} {
|
||||
if _, err = st.CreatePageAtPathInDomain(domain.UUID, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
srv := &Server{store: st}
|
||||
plan, err := srv.buildCityAutoPlan(domain.UUID)
|
||||
if err != nil || len(plan) != 3 {
|
||||
t.Fatalf("plan=%#v, %v", plan, err)
|
||||
}
|
||||
if count, err := srv.applyCityAutoPlan(domain.UUID, "actor"); err != nil || count != 3 {
|
||||
t.Fatalf("apply count=%d err=%v", count, err)
|
||||
}
|
||||
city, err := st.GetCityMap(domain.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(city.Quartiers) != 1 || len(city.Quartiers[0].Streets) != 1 || len(city.Quartiers[0].Streets[0].Spots) != 2 {
|
||||
t.Fatalf("city=%#v", city)
|
||||
}
|
||||
for _, spot := range city.Quartiers[0].Streets[0].Spots {
|
||||
if spot.Name == "lang" && len(spot.Pages) != 2 {
|
||||
t.Fatalf("lang pages=%#v", spot.Pages)
|
||||
}
|
||||
}
|
||||
if err := srv.reconcileCityActivity(domain.UUID); err != nil {
|
||||
t.Fatal("event-driven reconcile:", err)
|
||||
}
|
||||
checkpoint, err := st.ActivityOutboxCheckpoint("city", domain.UUID)
|
||||
if err != nil || checkpoint.LastSequence == 0 {
|
||||
t.Fatalf("city checkpoint=%+v err=%v", checkpoint, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCityAutoPlanSameSpotNameInDifferentStreets(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
for _, p := range []string{"/alpha/dev/lang/one", "/beta/dev/lang/two", "/alpha/dev/readme", "/beta/dev/readme"} {
|
||||
if _, e := s.store.CreatePageAtPathInDomain(domain, p); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, e := s.applyCityAutoPlan(domain, "system:city"); e != nil {
|
||||
t.Fatal("reconcile", i, e)
|
||||
}
|
||||
var spots, names int
|
||||
if e := s.store.DB.QueryRow(`SELECT count(*),count(DISTINCT name) FROM spots WHERE domain_uuid=? AND state!='archived'`, domain).Scan(&spots, &names); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if spots != 4 || names != 4 {
|
||||
t.Fatal("colliding names merged or duplicated", spots, names)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func cityGovernanceRoute(path string) bool {
|
||||
if !strings.HasPrefix(path, "/_/api/v1/city/") {
|
||||
return false
|
||||
}
|
||||
p := strings.Split(strings.TrimPrefix(path, "/_/api/v1/city/"), "/")
|
||||
return (len(p) == 3 || (len(p) == 4 && (p[3] == "preview" || p[3] == "members" || p[3] == "access" || p[3] == "publication" || p[3] == "auto-publication"))) && (p[0] == "zones" || p[0] == "spots" || p[0] == "domains") && p[1] != "" && p[2] == "governance"
|
||||
}
|
||||
|
||||
func (s *Server) handleCityGovernance(w http.ResponseWriter, r *http.Request) {
|
||||
privateCityResponse(w)
|
||||
p := strings.Split(strings.TrimPrefix(r.URL.Path, "/_/api/v1/city/"), "/")
|
||||
preview := len(p) == 4 && p[3] == "preview"
|
||||
members := len(p) == 4 && p[3] == "members"
|
||||
access := len(p) == 4 && p[3] == "access"
|
||||
publication := len(p) == 4 && p[3] == "publication"
|
||||
automatic := len(p) == 4 && p[3] == "auto-publication"
|
||||
if !((len(p) == 3 || access || publication || automatic) && (r.Method == "GET" || r.Method == "PATCH")) && !(preview && r.Method == "POST") && !(members && r.Method == "GET") {
|
||||
writeEventJSON(w, 405, map[string]string{"error": "Method not allowed"})
|
||||
return
|
||||
}
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil {
|
||||
writeEventJSON(w, 403, map[string]string{"error": translate(s.getLocale(s.domainUUIDForRequest(r)), "city.governance.denied")})
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
kind := map[string]string{"spots": "spot", "domains": "domain"}[p[0]]
|
||||
var err error
|
||||
if p[0] == "zones" {
|
||||
err = s.store.DB.QueryRowContext(r.Context(), `SELECT kind FROM zones WHERE uuid=? AND domain_uuid=?`, p[1], domain).Scan(&kind)
|
||||
}
|
||||
var result any
|
||||
if err == nil && automatic {
|
||||
var change *store.AutoPublicationChange
|
||||
if r.Method == "PATCH" {
|
||||
change = &store.AutoPublicationChange{}
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
||||
err = store.ErrGovernanceInvalid
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
result, err = s.store.AutoPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
|
||||
}
|
||||
} else if err == nil && publication {
|
||||
var change *store.PublicationChange
|
||||
if r.Method == "PATCH" {
|
||||
change = &store.PublicationChange{}
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
||||
err = store.ErrGovernanceInvalid
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
result, err = s.store.LocalityPublication(r.Context(), domain, principal.UUID, kind, p[1], change)
|
||||
}
|
||||
} else if err == nil && access {
|
||||
var change *store.LocalityAccess
|
||||
if r.Method == "PATCH" {
|
||||
change = &store.LocalityAccess{}
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 32<<10))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(change) != nil || decoder.Decode(new(any)) != io.EOF {
|
||||
err = store.ErrGovernanceInvalid
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
result, err = s.store.LocalityAccess(r.Context(), domain, principal.UUID, kind, p[1], change)
|
||||
}
|
||||
} else if err == nil && members {
|
||||
result, err = s.store.CityGovernanceMembers(r.Context(), domain, principal.UUID, kind, p[1], r.URL.Query().Get("q"))
|
||||
} else if err == nil && r.Method == "GET" {
|
||||
result, err = s.store.CityGovernance(r.Context(), domain, principal.UUID, kind, p[1])
|
||||
} else if err == nil {
|
||||
var change store.CityGovernanceChange
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&change) != nil || decoder.Decode(new(any)) != io.EOF {
|
||||
err = store.ErrGovernanceInvalid
|
||||
} else {
|
||||
result, err = s.store.ChangeCityGovernance(r.Context(), domain, principal.UUID, kind, p[1], change, preview)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
status, key := 503, "city.governance.unavailable"
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows), errors.Is(err, store.ErrAccessDenied):
|
||||
status, key = 403, "city.governance.denied"
|
||||
case errors.Is(err, store.ErrGovernanceConflict):
|
||||
status, key = 409, err.Error()
|
||||
case errors.Is(err, store.ErrLocalityAccessEnforcing), errors.Is(err, store.ErrLocalityAccessAlias), errors.Is(err, store.ErrPublicationImpact), errors.Is(err, store.ErrGovernanceCeiling), errors.Is(err, store.ErrGovernanceConfirm), errors.Is(err, store.ErrGovernanceInvalid):
|
||||
status, key = 400, err.Error()
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": translate(s.getLocale(domain), key)})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, result)
|
||||
}
|
||||
|
||||
func governanceEndpoint(kind, id string) string {
|
||||
collection := map[string]string{"quartier": "zones", "street": "zones", "spot": "spots", "domain": "domains"}[kind]
|
||||
return "/_/api/v1/city/" + collection + "/" + id + "/governance"
|
||||
}
|
||||
|
||||
func (s *Server) governanceForUI(r *http.Request, kind, id string) string {
|
||||
p := s.requestPrincipal(r)
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
if _, err := s.store.CityGovernance(r.Context(), s.domainUUIDForRequest(r), p.UUID, kind, id); err != nil {
|
||||
return ""
|
||||
}
|
||||
return governanceEndpoint(kind, id)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCityGovernanceChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
s.mux.HandleFunc("/v/city", s.handleCityMap)
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
q, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "life", "Life", "", "/life")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, err := s.store.CreatePasswordAccount(domain, "delegate", "a long test password", "Delegate", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/city-governance.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.Header.Get("Origin") != "" {
|
||||
r.Header.Set("Origin", "http://localhost")
|
||||
}
|
||||
if r.Method == "GET" && (r.URL.Path == "/v/city/q/life" || r.URL.Path == "/v/city") {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for k, v := range rec.Header() {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, path := range []string{"/v/city/q/life", "/v/city"} {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+path, t.TempDir())
|
||||
out, e := cmd.CombinedOutput()
|
||||
cancel()
|
||||
if e != nil {
|
||||
t.Fatalf("%s: %v %s", path, e, out)
|
||||
}
|
||||
}
|
||||
g, err := s.store.CityGovernance(context.Background(), domain, p.UUID, "quartier", q)
|
||||
if err != nil || len(g.Owners) != 1 || g.Policy != "authenticated_acl" || g.PublicationEnabled {
|
||||
t.Fatalf("UI persisted %+v %v", g, err)
|
||||
}
|
||||
a, err := s.store.LocalityAccess(context.Background(), domain, p.UUID, "quartier", q, nil)
|
||||
if err != nil || len(a.Entries) != 1 || a.Entries[0].Permission != "read_write" || a.Mode != "restricted" {
|
||||
t.Fatalf("ACL UI persisted %+v %v", a, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestCityGovernanceDelegationAndCaps(t *testing.T) {
|
||||
s, admin, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
q, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "life", "Life", "", "/life")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
street, err := s.store.CreateZoneAtPath(domain, "test", "street", q, "home", "Home", "", "/life/home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spot, err := s.store.CreateSpotAtPath(domain, "test", street, "desk", "Desk", "", "/life/home/desk")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q2, err := s.store.CreateZoneAtPath(domain, "test", "quartier", "", "work", "Work", "", "/work")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tokens, members, principals := map[string]string{}, map[string]string{}, map[string]string{}
|
||||
for _, name := range []string{"quartierowner", "spotowner", "coowner", "outsider"} {
|
||||
p, e := s.store.CreatePasswordAccount(domain, name, "a long test password", name, "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
principals[name] = p.UUID
|
||||
tokens[name], e = s.store.CreateAuthSession(domain, p.UUID)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
var membership string
|
||||
if e = s.store.DB.QueryRow(`SELECT uuid FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=?`, domain, p.UUID).Scan(&membership); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
members[name] = membership
|
||||
// Do not let default author grants accidentally admit the owner API.
|
||||
if _, e = s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid IN (SELECT role_uuid FROM auth_role_bindings WHERE membership_uuid=?)`, members[name]); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
call := func(token, method, url string, body any) *httptest.ResponseRecorder {
|
||||
b, _ := json.Marshal(body)
|
||||
r := httptest.NewRequest(method, "http://localhost"+url, strings.NewReader(string(b)))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
dURL, qURL, sURL := governanceEndpoint("domain", domain), governanceEndpoint("quartier", q), governanceEndpoint("spot", spot)
|
||||
read := func(token, url string) store.CityGovernance {
|
||||
t.Helper()
|
||||
w := call(token, "GET", url, nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("GET %s=%d %s", url, w.Code, w.Body.String())
|
||||
}
|
||||
var g store.CityGovernance
|
||||
if e := json.Unmarshal(w.Body.Bytes(), &g); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return g
|
||||
}
|
||||
change := func(token, url, policy string, owners *[]string, confirm bool, code int) store.CityGovernance {
|
||||
t.Helper()
|
||||
g := read(token, url)
|
||||
c := store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Owners: owners, ConfirmRelaxation: confirm}
|
||||
if policy != "" {
|
||||
c.Policy = &policy
|
||||
}
|
||||
w := call(token, "PATCH", url, c)
|
||||
if w.Code != code {
|
||||
t.Fatalf("PATCH %s=%d want %d: %s", url, w.Code, code, w.Body.String())
|
||||
}
|
||||
if code == 200 {
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &g)
|
||||
}
|
||||
return g
|
||||
}
|
||||
if g := read(admin, dURL); g.Policy != "authenticated_acl" || g.DraftEffectivePublic || g.PublicationEnabled {
|
||||
t.Fatalf("unsafe default: %+v", g)
|
||||
}
|
||||
change(admin, qURL, "public", nil, true, 400)
|
||||
change(admin, dURL, "public", nil, false, 400)
|
||||
if g := change(admin, dURL, "public", nil, true, 200); g.DraftEffectivePublic {
|
||||
t.Fatal("Domain ceiling published root")
|
||||
}
|
||||
qs := []string{members["quartierowner"]}
|
||||
change(admin, qURL, "", &qs, false, 200)
|
||||
qg := read(tokens["quartierowner"], qURL)
|
||||
if qg.CanAppoint {
|
||||
t.Fatal("Quartier owner can appoint peers")
|
||||
}
|
||||
w := call(tokens["quartierowner"], "PATCH", qURL, store.CityGovernanceChange{Epoch: qg.Epoch, TopologyVersion: qg.TopologyVersion, Owners: &qs})
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("peer appointment=%d", w.Code)
|
||||
}
|
||||
if w = call(tokens["quartierowner"], "GET", governanceEndpoint("quartier", q2), nil); w.Code != 403 {
|
||||
t.Fatal("foreign Quartier accessible")
|
||||
}
|
||||
ss := []string{members["spotowner"], members["coowner"]}
|
||||
change(tokens["quartierowner"], sURL, "", &ss, false, 200)
|
||||
if g := read(tokens["spotowner"], sURL); len(g.Owners) != 2 || g.CanAppoint {
|
||||
t.Fatalf("co-owners: %+v", g)
|
||||
}
|
||||
if w = call(tokens["spotowner"], "GET", sURL+"/members", nil); w.Code != 403 {
|
||||
t.Fatal("Spot owner enumerated appointment candidates")
|
||||
}
|
||||
if w = call(tokens["outsider"], "GET", sURL, nil); w.Code != 403 {
|
||||
t.Fatal("outsider governance")
|
||||
}
|
||||
if w = call(tokens["spotowner"], "POST", "/_/api/v1/city/spots/"+spot+"/title", map[string]any{"title": "changed"}); w.Code != 403 {
|
||||
t.Fatal("ownership granted topology write")
|
||||
}
|
||||
change(tokens["spotowner"], sURL, "public", nil, true, 200)
|
||||
change(tokens["quartierowner"], qURL, "authenticated_acl", nil, false, 200)
|
||||
if g := read(tokens["spotowner"], sURL); g.DraftEffectivePublic || g.SuperiorAllowsPublic || g.Policy != "public" {
|
||||
t.Fatal("ancestor narrowing did not clamp preserved intent")
|
||||
}
|
||||
change(tokens["spotowner"], sURL, "public", nil, true, 400)
|
||||
change(tokens["quartierowner"], qURL, "inherit", nil, false, 400)
|
||||
change(tokens["quartierowner"], qURL, "inherit", nil, true, 200)
|
||||
if !read(tokens["spotowner"], sURL).DraftEffectivePublic {
|
||||
t.Fatal("intent lost after relaxation")
|
||||
}
|
||||
change(tokens["spotowner"], sURL, "authenticated_acl", nil, false, 200)
|
||||
change(tokens["quartierowner"], qURL, "public", nil, true, 200)
|
||||
if read(tokens["spotowner"], sURL).DraftEffectivePublic {
|
||||
t.Fatal("parent bypassed child restriction")
|
||||
}
|
||||
// Policy preview changes neither epoch nor persistence, even on relaxation.
|
||||
g := read(tokens["spotowner"], sURL)
|
||||
policy := "inherit"
|
||||
patch := store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy}
|
||||
if w = call(tokens["spotowner"], "POST", sURL+"/preview", patch); w.Code != 200 || !strings.Contains(w.Body.String(), `"draft_effective_public":true`) {
|
||||
t.Fatalf("preview=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if after := read(tokens["spotowner"], sURL); after.Epoch != g.Epoch || after.Policy != g.Policy {
|
||||
t.Fatal("preview persisted")
|
||||
}
|
||||
change(tokens["spotowner"], sURL, "inherit", nil, true, 200)
|
||||
if w = call(tokens["spotowner"], "PATCH", sURL, patch); w.Code != 409 {
|
||||
t.Fatal("stale epoch accepted")
|
||||
}
|
||||
// Same-version writers cannot both commit, even when both are legitimate co-owners.
|
||||
g = read(tokens["spotowner"], sURL)
|
||||
policy = "authenticated_acl"
|
||||
patch = store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy}
|
||||
var wg sync.WaitGroup
|
||||
codes := make(chan int, 2)
|
||||
for _, name := range []string{"spotowner", "coowner"} {
|
||||
wg.Add(1)
|
||||
go func(token string) { defer wg.Done(); codes <- call(token, "PATCH", sURL, patch).Code }(tokens[name])
|
||||
}
|
||||
wg.Wait()
|
||||
close(codes)
|
||||
success, conflict := 0, 0
|
||||
for c := range codes {
|
||||
if c == 200 {
|
||||
success++
|
||||
}
|
||||
if c == 409 {
|
||||
conflict++
|
||||
}
|
||||
}
|
||||
if success != 1 || conflict != 1 {
|
||||
t.Fatalf("concurrent saves: %d success %d conflicts", success, conflict)
|
||||
}
|
||||
g = read(tokens["spotowner"], sURL)
|
||||
patch.Epoch = g.Epoch
|
||||
patch.TopologyVersion = g.TopologyVersion
|
||||
if _, err = s.store.DB.Exec(`UPDATE domain_topology_versions SET version=version+1 WHERE domain_uuid=?`, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = call(tokens["spotowner"], "PATCH", sURL, patch); w.Code != 409 {
|
||||
t.Fatal("stale topology accepted")
|
||||
}
|
||||
// Browser-origin fencing remains ahead of the handler-owned permission path.
|
||||
r := httptest.NewRequest("PATCH", "http://localhost"+sURL, strings.NewReader(`{}`))
|
||||
r.Header.Set("Authorization", "Bearer "+admin)
|
||||
r.Header.Set("Origin", "https://untrusted.invalid")
|
||||
r.Header.Set("Sec-Fetch-Site", "cross-site")
|
||||
w = httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
if w.Code != 403 {
|
||||
t.Fatal("governance bypassed browser-origin fencing")
|
||||
}
|
||||
// Revocation is live and does not depend on an in-memory principal cache.
|
||||
if _, err = s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, principals["spotowner"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.CityGovernance(context.Background(), domain, principals["spotowner"], "spot", spot); err != store.ErrAccessDenied {
|
||||
t.Fatalf("suspended owner=%v", err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE uuid=?`, members["coowner"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.CityGovernance(context.Background(), domain, principals["coowner"], "spot", spot); err != store.ErrAccessDenied {
|
||||
t.Fatalf("revoked member=%v", err)
|
||||
}
|
||||
bad := []string{members["coowner"]}
|
||||
change(admin, sURL, "", &bad, false, 400)
|
||||
bad = []string{"foreign-membership"}
|
||||
change(admin, sURL, "", &bad, false, 400)
|
||||
if w = call(admin, "DELETE", sURL, nil); w.Code != 405 {
|
||||
t.Fatal("unsupported method")
|
||||
}
|
||||
if w = call(admin, "GET", governanceEndpoint("domain", "foreign"), nil); w.Code != 403 {
|
||||
t.Fatal("foreign Domain")
|
||||
}
|
||||
var events int
|
||||
if err = s.store.DB.QueryRow(`SELECT COUNT(*) FROM city_governance_events WHERE domain_uuid=?`, domain).Scan(&events); err != nil || events < 8 {
|
||||
t.Fatalf("audit events=%d %v", events, err)
|
||||
}
|
||||
none := []string{}
|
||||
change(admin, qURL, "", &none, false, 200)
|
||||
if w = call(tokens["quartierowner"], "GET", qURL, nil); w.Code != 403 {
|
||||
t.Fatal("removed ownership remained effective")
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE groups SET state='suspended' WHERE domain_uuid=? AND builtin_key='domain_admin'`, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = call(admin, "GET", dURL, nil); w.Code != 403 {
|
||||
t.Fatal("revoked admin remained authoritative")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,409 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func cityReadErrorStatus(err error) int {
|
||||
switch {
|
||||
case errors.Is(err, store.ErrAccessDenied), errors.Is(err, sql.ErrNoRows), errors.Is(err, store.ErrCityNameAmbiguous):
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, store.ErrCityMapBudget), errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled):
|
||||
return http.StatusServiceUnavailable
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleCityMap(w http.ResponseWriter, r *http.Request) {
|
||||
privateCityResponse(w)
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
canPlan := principal != nil && (s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") || s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) == nil)
|
||||
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/v/city"), "/")
|
||||
if path != "" {
|
||||
parts := strings.Split(path, "/")
|
||||
kinds := map[string]string{"q": "quartier", "s": "street", "p": "spot"}
|
||||
kind := ""
|
||||
if len(parts) == 2 || (len(parts) == 3 && parts[0] == "s") {
|
||||
kind = kinds[parts[0]]
|
||||
}
|
||||
if kind == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
detail, err := s.store.CityNodeContext(r.Context(), domainUUID, kind, strings.Join(parts[1:], "/"))
|
||||
if err != nil {
|
||||
status := cityReadErrorStatus(err)
|
||||
if status == http.StatusNotFound {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if status == http.StatusServiceUnavailable {
|
||||
w.Header().Set("Retry-After", "5")
|
||||
}
|
||||
http.Error(w, http.StatusText(status), status)
|
||||
return
|
||||
}
|
||||
if !canReadCityNode(detail, s.pathReadFilter(r)) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
pages := []store.CityPagePlacement{}
|
||||
if kind == "spot" {
|
||||
pages, err = s.spotPagesForRequest(r, detail.UUID)
|
||||
if err != nil {
|
||||
status := cityReadErrorStatus(err)
|
||||
if status == http.StatusServiceUnavailable {
|
||||
w.Header().Set("Retry-After", "5")
|
||||
}
|
||||
http.Error(w, http.StatusText(status), status)
|
||||
return
|
||||
}
|
||||
}
|
||||
pageViews := make([]cityPageView, 0, len(pages))
|
||||
for _, page := range pages {
|
||||
pageViews = append(pageViews, cityPageView{page, cityPageHref(page.Path)})
|
||||
}
|
||||
quartierHref, streetHref := "", ""
|
||||
if detail.Quartier != nil {
|
||||
quartierHref = cityDetailHref("quartier", detail.Quartier.Name, "", detail.Quartier.UUID)
|
||||
}
|
||||
if detail.Street != nil {
|
||||
streetHref = cityDetailHref("street", detail.Street.Name, detail.Quartier.Name, detail.Street.UUID)
|
||||
}
|
||||
s.renderWithContext(w, r, "city_node.gohtml", map[string]any{"Title": detail.Title, "Node": detail, "Pages": pageViews, "QuartierHref": quartierHref, "StreetHref": streetHref, "MapHref": "/v/city?" + kind + "=" + detail.UUID, "CanPlan": canPlan, "Locale": s.getLocale(domainUUID), "GovernanceEndpoint": s.governanceForUI(r, kind, detail.UUID)})
|
||||
return
|
||||
}
|
||||
s.renderWithContext(w, r, "city_map.gohtml", map[string]any{"Title": translate(s.getLocale(domainUUID), "city.map"), "CanPlan": canPlan, "Locale": s.getLocale(domainUUID), "GovernanceEndpoint": s.governanceForUI(r, "domain", domainUUID)})
|
||||
}
|
||||
|
||||
func (s *Server) handleCityAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if cityGovernanceRoute(r.URL.Path) {
|
||||
s.handleCityGovernance(w, r)
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/city/"), "/")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
if action == "map" && r.Method == http.MethodGet {
|
||||
privateCityResponse(w)
|
||||
city, err := s.cityProjectionForRequest(r)
|
||||
if err != nil {
|
||||
status := cityReadErrorStatus(err)
|
||||
if status == http.StatusServiceUnavailable {
|
||||
w.Header().Set("Retry-After", "5")
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(city)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(action, "/")
|
||||
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "pages" && r.Method == http.MethodGet {
|
||||
privateCityResponse(w)
|
||||
pages, err := s.spotPagesForRequest(r, parts[1])
|
||||
if err != nil {
|
||||
status := cityReadErrorStatus(err)
|
||||
if status == http.StatusServiceUnavailable {
|
||||
w.Header().Set("Retry-After", "5")
|
||||
}
|
||||
message := http.StatusText(status)
|
||||
if status == http.StatusNotFound {
|
||||
message = "Spot not found"
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": message})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"pages": pages})
|
||||
return
|
||||
}
|
||||
principal := s.requestPrincipal(r)
|
||||
if principal == nil || (!s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") && s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) != nil) {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "domain admin required"})
|
||||
return
|
||||
}
|
||||
if action == "organize" && r.Method == http.MethodPost {
|
||||
var request store.CityOrganizeRequest
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 128<<10)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_selection"})
|
||||
return
|
||||
}
|
||||
result, err := s.store.OrganizeCity(domainUUID, principal.UUID, request)
|
||||
if err != nil {
|
||||
writeCityOrganizeError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, result)
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && (parts[0] == "spots" || parts[0] == "zones") && parts[2] == "title" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
BaseVersion int64 `json:"base_version"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_title"})
|
||||
return
|
||||
}
|
||||
if parts[0] == "spots" {
|
||||
request.Kind = "spot"
|
||||
} else if request.Kind != "quartier" && request.Kind != "street" {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "city_invalid_selection"})
|
||||
return
|
||||
}
|
||||
if err := s.store.RenameCityTitle(domainUUID, principal.UUID, request.Kind, parts[1], request.Title, request.BaseVersion); err != nil {
|
||||
writeCityOrganizeError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if action == "health" && r.Method == http.MethodGet {
|
||||
health, err := s.store.GetCityHealth(domainUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(health)
|
||||
return
|
||||
}
|
||||
if action == "events" && r.Method == http.MethodGet {
|
||||
afterVersion, _ := strconv.ParseInt(r.URL.Query().Get("after_version"), 10, 64)
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
events, err := s.store.ListTopologyEvents(domainUUID, afterVersion, limit)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"events": events, "count": len(events)})
|
||||
return
|
||||
}
|
||||
if (action == "plans/preview" || action == "plans/apply") && r.Method == http.MethodPost {
|
||||
var plan store.CityPlan
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&plan) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid City plan JSON"})
|
||||
return
|
||||
}
|
||||
var preview store.CityPlanPreview
|
||||
var err error
|
||||
if action == "plans/apply" {
|
||||
preview, err = s.store.ApplyCityPlan(domainUUID, principal.UUID, plan)
|
||||
} else {
|
||||
preview, err = s.store.PreviewCityPlan(domainUUID, plan)
|
||||
}
|
||||
if err != nil {
|
||||
writeEventJSON(w, 409, map[string]any{"error": err.Error(), "preview": preview})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(preview)
|
||||
return
|
||||
}
|
||||
if action == "autoplan" && r.Method == http.MethodGet {
|
||||
plan, err := s.buildCityAutoPlan(domainUUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"items": plan, "count": len(plan)})
|
||||
return
|
||||
}
|
||||
if action == "autoplan" && r.Method == http.MethodPost {
|
||||
count, err := s.applyCityAutoPlan(domainUUID, principal.UUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"status": "ok", "applied": count})
|
||||
return
|
||||
}
|
||||
if action == "zones" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
Kind string `json:"kind"`
|
||||
ParentUUID string `json:"parent_uuid"`
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
PathPrefix string `json:"path_prefix"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
id, err := s.store.CreateZoneAtPath(domainUUID, principal.UUID, request.Kind, request.ParentUUID, request.Name, request.Title, request.Description, request.PathPrefix)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusCreated, map[string]string{"uuid": id})
|
||||
return
|
||||
}
|
||||
if action == "spots" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
StreetUUID string `json:"street_uuid"`
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
PathPrefix string `json:"path_prefix"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
id, err := s.store.CreateSpotAtPath(domainUUID, principal.UUID, request.StreetUUID, request.Name, request.Title, request.Summary, request.PathPrefix)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusCreated, map[string]string{"uuid": id})
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && (parts[0] == "zones" || parts[0] == "spots") && r.Method == http.MethodDelete {
|
||||
kind := "spot"
|
||||
if parts[0] == "zones" {
|
||||
var request struct {
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
kind = request.Kind
|
||||
}
|
||||
if err := s.store.DeleteCityNode(domainUUID, principal.UUID, kind, parts[1]); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && (parts[0] == "zones" || parts[0] == "spots") && r.Method == http.MethodPatch {
|
||||
var request struct {
|
||||
Kind string `json:"kind"`
|
||||
PathPrefix string `json:"path_prefix"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil || strings.TrimSpace(request.PathPrefix) == "" {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON or Path"})
|
||||
return
|
||||
}
|
||||
kind := "spot"
|
||||
if parts[0] == "zones" {
|
||||
kind = request.Kind
|
||||
}
|
||||
if err := s.store.UpdateCityNodePath(domainUUID, principal.UUID, kind, parts[1], request.PathPrefix); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "pages" && (r.Method == http.MethodPost || r.Method == http.MethodDelete) {
|
||||
var request struct {
|
||||
PageUUID string `json:"page_uuid"`
|
||||
Path string `json:"path"`
|
||||
EdgeType string `json:"edge_type"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
var err error
|
||||
if r.Method == http.MethodDelete {
|
||||
err = s.store.RemoveSpotPage(domainUUID, principal.UUID, parts[1], request.PageUUID, request.EdgeType)
|
||||
} else {
|
||||
err = s.store.AddSpotPage(domainUUID, principal.UUID, parts[1], request.PageUUID, request.Path, request.EdgeType, request.Label)
|
||||
}
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "relations" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
TargetSpotUUID string `json:"target_spot_uuid"`
|
||||
RelationType string `json:"relation_type"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
if err := s.store.AddSpotRelation(domainUUID, principal.UUID, parts[1], request.TargetSpotUUID, request.RelationType); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[0] == "spots" && parts[2] == "relations" && r.Method == http.MethodDelete {
|
||||
var request struct {
|
||||
TargetSpotUUID string `json:"target_spot_uuid"`
|
||||
RelationType string `json:"relation_type"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
if err := s.store.RemoveSpotRelation(domainUUID, principal.UUID, parts[1], request.TargetSpotUUID, request.RelationType); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
if len(parts) == 3 && parts[2] == "move" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
ParentUUID string `json:"parent_uuid"`
|
||||
StreetUUID string `json:"street_uuid"`
|
||||
SortKey int `json:"sort_key"`
|
||||
}
|
||||
if json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&request) != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid JSON"})
|
||||
return
|
||||
}
|
||||
var err error
|
||||
if parts[0] == "zones" {
|
||||
err = s.store.MoveStreet(domainUUID, principal.UUID, parts[1], request.ParentUUID, request.SortKey)
|
||||
} else if parts[0] == "spots" {
|
||||
err = s.store.MoveSpot(domainUUID, principal.UUID, parts[1], request.StreetUUID, request.SortKey)
|
||||
} else {
|
||||
err = store.ErrAccessDenied
|
||||
}
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Allow", "GET, POST")
|
||||
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
}
|
||||
|
||||
func jsonNumber(value int64) string {
|
||||
return strconv.FormatInt(value, 10)
|
||||
}
|
||||
|
||||
func writeCityOrganizeError(w http.ResponseWriter, err error) {
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, store.ErrCityChanged) {
|
||||
status = http.StatusConflict
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeEventJSON(w, http.StatusNotFound, map[string]string{"error": "city_invalid_selection"})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, status, map[string]string{"error": err.Error()})
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCityLabelsChromium(t *testing.T) {
|
||||
runCityBrowserFixture(t, "city-labels.js", []string{"/life/home/readme", "/life/work/tools/guide"}, []string{"normal", "popup"})
|
||||
}
|
||||
|
||||
func TestCityOrganizerChromium(t *testing.T) {
|
||||
for _, mode := range []string{"normal", "popup"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
runCityBrowserFixture(t, "city-organizer.js", []string{"/life/home/readme", "/life/home/notes", "/life/home/alpha/page", "/life/home/beta/page", "/life/work/tools/guide"}, []string{mode})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCityImmersiveChromium(t *testing.T) {
|
||||
paths := []string{"/life/home/readme", "/life/home/alpha"}
|
||||
for i := 0; i < 27; i++ {
|
||||
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%02d", i))
|
||||
}
|
||||
for i := 0; i < 7; i++ {
|
||||
paths = append(paths, fmt.Sprintf("/life/home/spot-%02d/page", i))
|
||||
}
|
||||
runCityBrowserFixture(t, "city-immersive.js", paths, []string{"normal", "popup", "menu", "mobile-light"})
|
||||
}
|
||||
|
||||
func TestCityKeyboardChromium(t *testing.T) {
|
||||
paths := []string{"/life/home/alpha", "/life/home/beta/page", "/life/work/tools/page", "/work/dev/code/page"}
|
||||
for i := 0; i < 6; i++ {
|
||||
paths = append(paths, fmt.Sprintf("/life/home/alpha/page-%d", i))
|
||||
}
|
||||
runCityBrowserFixture(t, "city-keyboard.js", paths, []string{"normal", "popup"})
|
||||
}
|
||||
|
||||
func TestCityWorkerChromium(t *testing.T) {
|
||||
runCityBrowserFixture(t, "city-worker.js", []string{"/life/home/alpha/page", "/life/work/tools/page"}, []string{"normal", "popup"})
|
||||
}
|
||||
|
||||
func TestCityNamedNavigationChromium(t *testing.T) {
|
||||
runCityBrowserFixture(t, "city-links.js", []string{"/life/home/alpha/page?#%", "/work/home/beta/page"}, []string{"normal", "popup", "quartier", "street", "spot"})
|
||||
}
|
||||
|
||||
func runCityBrowserFixture(t *testing.T, scriptName string, paths, modes []string) {
|
||||
t.Helper()
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
for _, path := range paths {
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.mux.HandleFunc("/v/city", s.handleCityMap)
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
s.mux.HandleFunc("/v/search", s.handleSearch)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
script, err := os.ReadFile("internal/server/testdata/" + scriptName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/static/vendor/city-generator/gen-city.bundle.js" && scriptName != "city-worker.js" {
|
||||
// Deliberately vertical/diagonal roads: labels must remain horizontal.
|
||||
// Real template, renderer, styles, data and authorization are retained.
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
_, _ = io.WriteString(w, `globalThis.PagesWikiGenCity={generateFabric:async(key,width,height)=>({version:'test',width,height,roads:Array.from({length:24},(_,i)=>[20+(i%6)*12,15+Math.floor(i/6)*18,20+(i%6)*12+(i%2?4:0),30+Math.floor(i/6)*18].map((v,j)=>v*(j%2?height:width)/100)),buildings:[[[25,30],[28,30],[28,34],[25,34]],[[32,50],[35,50],[35,54],[32,54]],[[62,32],[65,32],[65,36],[62,36]],[[78,50],[81,50],[81,54],[78,54]]].map(b=>b.map(p=>[p[0]*width/100,p[1]*height/100]))})};`)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/v/city" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = io.WriteString(w, strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, mode := range modes {
|
||||
url := server.URL + "/v/city"
|
||||
if mode == "popup" {
|
||||
url += "?popup=1"
|
||||
} else if mode == "menu" || mode == "mobile-light" {
|
||||
url += "?fixture=" + mode
|
||||
} else if mode == "quartier" || mode == "street" || mode == "spot" {
|
||||
id := map[string]string{"quartier": "life", "street": "life/home", "spot": "alpha"}[mode]
|
||||
node, err := s.store.CityNode(domain, mode, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
url += "?" + mode + "=" + node.UUID
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", url, t.TempDir())
|
||||
screenshot := ""
|
||||
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
cancel()
|
||||
t.Fatal(err)
|
||||
}
|
||||
screenshot = filepath.Join(dir, strings.TrimSuffix(scriptName, ".js")+"-"+mode+".png")
|
||||
}
|
||||
cmd.Args = append(cmd.Args, screenshot)
|
||||
if mode == "mobile-light" {
|
||||
cmd.Args = append(cmd.Args, "390", "844")
|
||||
}
|
||||
out, err := cmd.CombinedOutput()
|
||||
cancel()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v %s", mode, err, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// Names identify Streets only within their parent Quartier. UUID references
|
||||
// remain available for stable identity across renames and moves.
|
||||
func cityDetailHref(kind, name, quartierName, uuid string) string {
|
||||
prefix := map[string]string{"quartier": "q", "street": "s", "spot": "p"}[strings.ToLower(kind)]
|
||||
if prefix == "" {
|
||||
return "/v/city"
|
||||
}
|
||||
ref := "uuid:" + uuid
|
||||
if name != "" && (prefix != "s" || quartierName != "") {
|
||||
ref = url.PathEscape(name)
|
||||
if prefix == "s" {
|
||||
ref = url.PathEscape(quartierName) + "/" + ref
|
||||
}
|
||||
} else {
|
||||
ref = url.PathEscape(ref)
|
||||
}
|
||||
return "/v/city/" + prefix + "/" + ref
|
||||
}
|
||||
|
||||
func canReadCityNode(node store.CityNodeDetail, allowed func(string) bool) bool {
|
||||
return allowed(node.PathPrefix) && (node.Quartier == nil || allowed(node.Quartier.PathPrefix)) && (node.Street == nil || allowed(node.Street.PathPrefix))
|
||||
}
|
||||
|
||||
func cityPageHref(path string) string {
|
||||
return (&url.URL{Path: path}).EscapedPath()
|
||||
}
|
||||
|
||||
type cityPageView struct {
|
||||
store.CityPagePlacement
|
||||
Href string
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCityNamedLinksAndLightweightDetails(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
s.mux.HandleFunc("/v/search", s.handleSearch)
|
||||
for _, path := range []string{"/life/home/alpha/page?#%", "/work/home/beta/page"} {
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call := func(route string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost"+route, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
street, err := s.store.CityNode(domain, "street", "life/home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, route := range []string{"/v/city/s/life/home", "/v/city/s/work/home", "/v/city/s/uuid:" + street.UUID} {
|
||||
if w := call(route); w.Code != 200 {
|
||||
t.Fatalf("%s=%d %s", route, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, route := range []string{"/v/city/s/home", "/v/city/s/missing/home", "/v/city/q/life/extra"} {
|
||||
if w := call(route); w.Code != 404 {
|
||||
t.Fatalf("%s=%d", route, w.Code)
|
||||
}
|
||||
}
|
||||
w := call("/v/city/p/alpha")
|
||||
for _, wanted := range []string{`href="/v/city/q/life"`, `href="/v/city/s/life/home"`, `href="/life/home/alpha/page%3F%23%25"`, `href="/v/city?spot=`} {
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), wanted) {
|
||||
t.Fatalf("detail missing %s (%d): %s", wanted, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, selector := range []string{"life/home", "uuid:" + street.UUID} {
|
||||
if w := call("/v/search?q=page&locality=street&locality_id=" + selector); w.Code != 200 {
|
||||
t.Fatalf("locality search %s=%d: %s", selector, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost/v/city", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
city, err := s.cityProjectionForRequest(cacheRequestDomain(withReadPolicyContext(r)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, q := range city.Quartiers {
|
||||
for _, st := range q.Streets {
|
||||
if st.DetailHref != "/v/city/s/"+q.Name+"/"+st.Name {
|
||||
t.Fatalf("projection link=%s", st.DetailHref)
|
||||
}
|
||||
}
|
||||
}
|
||||
macro := s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(string) bool { return true })
|
||||
if !strings.Contains(macro, `href="/v/city/s/life/home"`) || !strings.Contains(macro, `href="/life/home/alpha/page%3F%23%25"`) {
|
||||
t.Fatalf("macro=%s", macro)
|
||||
}
|
||||
macro = s.expandCityMacros("{{city-breadcrumbs}} {{spot-pages}}", "/life/home/alpha/page?#%", domain, func(p string) bool { return p != "/life" })
|
||||
if strings.Contains(macro, "alpha") || strings.Contains(macro, "home") {
|
||||
t.Fatalf("hidden ancestry leaked=%s", macro)
|
||||
}
|
||||
// Detail and breadcrumbs must not enumerate the entire City/Page graph.
|
||||
if _, err := s.store.DB.Exec(`DROP TABLE spot_pages`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = call("/v/city/s/life/home")
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/v/city?street=`+street.UUID+`"`) {
|
||||
t.Fatalf("lightweight detail=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
macro = s.expandCityMacros("{{street}}", "/life/home/page", domain, func(string) bool { return true })
|
||||
if !strings.Contains(macro, `href="/v/city/s/life/home"`) {
|
||||
t.Fatalf("lightweight macro=%s", macro)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCityOrganizerAPIRequiresAdminAndVersion(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
for _, p := range []string{"/life/home/one/page", "/life/home/two/page"} {
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
city, err := s.store.GetCityMap(domain)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
one, two := city.Quartiers[0].Streets[0].Spots[0], city.Quartiers[0].Streets[0].Spots[1]
|
||||
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readerToken, err := s.store.CreateAuthSession(domain, user.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call := func(token, url string, body any) *httptest.ResponseRecorder {
|
||||
b, _ := json.Marshal(body)
|
||||
r := httptest.NewRequest(http.MethodPost, "http://localhost"+url, strings.NewReader(string(b)))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
rename := map[string]any{"title": "Changed", "base_version": city.Version}
|
||||
merge := map[string]any{"action": "merge", "base_version": city.Version, "spots": []string{one.UUID}, "target_spot_uuid": two.UUID}
|
||||
for url, body := range map[string]any{"/_/api/v1/city/spots/" + one.UUID + "/title": rename, "/_/api/v1/city/organize": merge} {
|
||||
if w := call(readerToken, url, body); w.Code != http.StatusForbidden {
|
||||
t.Fatalf("non-admin: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if w := call(token, "/_/api/v1/city/spots/"+one.UUID+"/title", rename); w.Code != 200 {
|
||||
t.Fatalf("rename: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != http.StatusConflict {
|
||||
t.Fatalf("stale merge: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
merge["base_version"] = city.Version + 1
|
||||
if w := call(token, "/_/api/v1/city/organize", merge); w.Code != 200 {
|
||||
t.Fatalf("merge: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
plan, err := s.buildCityAutoPlan(domain)
|
||||
if err != nil || len(plan) != 1 {
|
||||
t.Fatalf("manual membership re-inferred: %+v %v", plan, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// City locality metadata requires read permission on its boundary; Page edges
|
||||
// have their own checks. Never expose a hidden Page via a portal, count, nearby
|
||||
// relation or editable destination list even when its Spot is visible.
|
||||
func filterCityProjection(city store.CityMap, allowed func(string) bool) store.CityMap {
|
||||
result := store.CityMap{DomainUUID: city.DomainUUID, Version: city.Version, Quartiers: []store.CityQuartier{}, Relations: []store.CitySpotRelation{}}
|
||||
visible := map[string]bool{}
|
||||
for _, quartier := range city.Quartiers {
|
||||
if !allowed(quartier.PathPrefix) {
|
||||
continue
|
||||
}
|
||||
quartier.DetailHref = cityDetailHref("quartier", quartier.Name, "", quartier.UUID)
|
||||
streets := []store.CityStreet{}
|
||||
for _, street := range quartier.Streets {
|
||||
if !allowed(street.PathPrefix) {
|
||||
continue
|
||||
}
|
||||
street.DetailHref = cityDetailHref("street", street.Name, quartier.Name, street.UUID)
|
||||
spots := []store.CitySpot{}
|
||||
for _, spot := range street.Spots {
|
||||
if !allowed(spot.PathPrefix) {
|
||||
continue
|
||||
}
|
||||
spot.DetailHref = cityDetailHref("spot", spot.Name, quartier.Name, spot.UUID)
|
||||
pages := []store.CityPagePlacement{}
|
||||
unique := map[string]bool{}
|
||||
spot.PortalPath, spot.PortalPageUUID = "", ""
|
||||
portalPriority := -1
|
||||
for _, page := range spot.Pages {
|
||||
if page.Path == "" || !allowed(page.Path) {
|
||||
continue
|
||||
}
|
||||
pages = append(pages, page)
|
||||
unique[page.PageUUID] = true
|
||||
priority := 0
|
||||
if page.EdgeType == "primary" {
|
||||
priority = 1
|
||||
}
|
||||
if page.Path == spot.PathPrefix {
|
||||
priority = 2
|
||||
}
|
||||
if priority > portalPriority {
|
||||
spot.PortalPath, spot.PortalPageUUID = page.Path, page.PageUUID
|
||||
portalPriority = priority
|
||||
}
|
||||
}
|
||||
spot.Pages, spot.PageCount = pages, len(unique)
|
||||
visible[spot.UUID] = true
|
||||
spots = append(spots, spot)
|
||||
}
|
||||
street.Spots = spots
|
||||
streets = append(streets, street)
|
||||
}
|
||||
quartier.Streets = streets
|
||||
result.Quartiers = append(result.Quartiers, quartier)
|
||||
}
|
||||
for _, relation := range city.Relations {
|
||||
if visible[relation.SourceSpotUUID] && visible[relation.TargetSpotUUID] {
|
||||
result.Relations = append(result.Relations, relation)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Server) cityProjectionForRequest(r *http.Request) (store.CityMap, error) {
|
||||
city, err := s.navigationCity(r.Context(), s.domainUUIDForRequest(r))
|
||||
if err != nil {
|
||||
return city, err
|
||||
}
|
||||
return filterCityProjection(city, s.pathReadFilter(r)), nil
|
||||
}
|
||||
|
||||
func (s *Server) spotPagesForRequest(r *http.Request, spotUUID string) ([]store.CityPagePlacement, error) {
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
spot, err := s.store.CityNodeContext(r.Context(), domain, "spot", "uuid:"+spotUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !canReadCityNode(spot, s.pathReadFilter(r)) {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
pages, err := s.store.ListSpotPagesContext(r.Context(), domain, spotUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []store.CityPagePlacement{}
|
||||
allowed := s.pathReadFilter(r)
|
||||
for _, page := range pages {
|
||||
if page.Path != "" && allowed(page.Path) {
|
||||
out = append(out, page)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func privateCityResponse(w http.ResponseWriter) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Add("Vary", "Cookie, Authorization")
|
||||
}
|
||||
|
||||
func (s *Server) pageSpotAppearancesForRequest(r *http.Request, objectUUID string) ([]store.CitySpotAppearance, error) {
|
||||
spots, err := s.store.ListPageSpotAppearances(s.domainUUIDForRequest(r), objectUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []store.CitySpotAppearance{}
|
||||
allowed := s.pathReadFilter(r)
|
||||
for _, spot := range spots {
|
||||
if allowed(spot.PathPrefix) {
|
||||
out = append(out, spot)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestCityReadErrorsDistinguishMissingFromResourceLimits(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
err error
|
||||
want int
|
||||
}{
|
||||
{store.ErrAccessDenied, http.StatusNotFound}, {sql.ErrNoRows, http.StatusNotFound},
|
||||
{store.ErrCityMapBudget, http.StatusServiceUnavailable},
|
||||
{context.DeadlineExceeded, http.StatusServiceUnavailable}, {context.Canceled, http.StatusServiceUnavailable},
|
||||
} {
|
||||
if got := cityReadErrorStatus(test.err); got != test.want {
|
||||
t.Fatalf("status(%v)=%d want=%d", test.err, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCityReadPortalPriorityAfterFiltering(t *testing.T) {
|
||||
spot := store.CitySpot{UUID: "spot", PathPrefix: "/q/s/p", Pages: []store.CityPagePlacement{
|
||||
{PageUUID: "guide", Path: "/q/s/p/guide", EdgeType: "guide"},
|
||||
{PageUUID: "primary", Path: "/q/s/p/primary", EdgeType: "primary"},
|
||||
{PageUUID: "boundary", Path: "/q/s/p", EdgeType: "path"},
|
||||
}}
|
||||
street := store.CityStreet{PathPrefix: "/q/s", Spots: []store.CitySpot{spot}}
|
||||
quartier := store.CityQuartier{PathPrefix: "/q", Streets: []store.CityStreet{street}}
|
||||
city := store.CityMap{Quartiers: []store.CityQuartier{quartier}}
|
||||
for _, test := range []struct{ excluded, want string }{{"", "/q/s/p"}, {"/q/s/p/primary", "/q/s/p"}} {
|
||||
projected := filterCityProjection(city, func(path string) bool { return path != test.excluded })
|
||||
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != test.want {
|
||||
t.Fatalf("portal=%s want=%s", got, test.want)
|
||||
}
|
||||
}
|
||||
city.Quartiers[0].Streets[0].Spots[0].Pages = city.Quartiers[0].Streets[0].Spots[0].Pages[:2]
|
||||
projected := filterCityProjection(city, func(string) bool { return true })
|
||||
if got := projected.Quartiers[0].Streets[0].Spots[0].PortalPath; got != "/q/s/p/primary" {
|
||||
t.Fatalf("primary Page lost to first visible guide: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCityReadProjectionHidesPagesLocalitiesPortalsAndRelations(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
s.mux.HandleFunc("/v/city/", s.handleCityMap)
|
||||
for _, path := range []string{"/life/home/alpha/good", "/life/home/alpha/aaa-secret", "/life/home/beta/private", "/secret/vault/lock/private"} {
|
||||
if _, err := s.store.CreatePageAtPathInDomain(domain, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.applyCityAutoPlan(domain, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user, err := s.store.CreatePasswordAccount(domain, "limited", "a long test password", "Limited", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := s.store.CreateAuthSession(domain, user.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var role string
|
||||
if err := s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=?`, domain, user.UUID).Scan(&role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range []string{"/life", "/life/home", "/life/home/alpha", "/life/home/alpha/good"} {
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,'read','path',?,1)`, "grant:"+p, role, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw, err := s.store.GetCityMap(domain)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var alpha, beta store.CitySpot
|
||||
for _, q := range raw.Quartiers {
|
||||
for _, street := range q.Streets {
|
||||
for _, p := range street.Spots {
|
||||
if p.Title == "alpha" {
|
||||
alpha = p
|
||||
}
|
||||
if p.Title == "beta" {
|
||||
beta = p
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := s.store.AddSpotRelation(domain, "test", alpha.UUID, beta.UUID, "related"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := func(route string) *http.Request {
|
||||
r := httptest.NewRequest("GET", "http://localhost"+route, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
return r
|
||||
}
|
||||
call := func(route string) *httptest.ResponseRecorder {
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, request(route))
|
||||
return w
|
||||
}
|
||||
w := call("/_/api/v1/city/map")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("map=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
for _, secret := range []string{"aaa-secret", "private", "beta", "vault", "secret"} {
|
||||
if strings.Contains(w.Body.String(), secret) {
|
||||
t.Fatalf("hidden metadata %q leaked: %s", secret, w.Body.String())
|
||||
}
|
||||
}
|
||||
var projected store.CityMap
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &projected); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spot := projected.Quartiers[0].Streets[0].Spots[0]
|
||||
if spot.PageCount != 1 || len(spot.Pages) != 1 || spot.PortalPath != "/life/home/alpha/good" || len(projected.Relations) != 0 {
|
||||
t.Fatalf("projection leaked count/portal/relation: %+v", spot)
|
||||
}
|
||||
if w.Header().Get("ETag") != "" || w.Header().Get("Cache-Control") != "private, no-store" {
|
||||
t.Fatal("unscoped City caching")
|
||||
}
|
||||
if w := call("/_/api/v1/city/spots/" + beta.UUID + "/pages"); w.Code != 404 {
|
||||
t.Fatalf("hidden Spot pages=%d", w.Code)
|
||||
}
|
||||
if w := call("/v/city/p/beta"); w.Code != 404 {
|
||||
t.Fatalf("hidden detail=%d", w.Code)
|
||||
}
|
||||
if w := call("/v/city/p/alpha"); w.Code != 200 || strings.Contains(w.Body.String(), "aaa-secret") {
|
||||
t.Fatalf("visible detail=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
r := cacheRequestDomain(withReadPolicyContext(request("/life/home/alpha/good")))
|
||||
macro := s.expandMacrosForRequest(r, `{{spot-pages}} {{nearby}}`, "/life/home/alpha/good", domain)
|
||||
if strings.Contains(macro, "aaa-secret") || strings.Contains(macro, "beta") || !strings.Contains(macro, "/good") {
|
||||
t.Fatalf("macro projection=%s", macro)
|
||||
}
|
||||
// A new request re-evaluates revoked grants; there is no cross-request cache.
|
||||
if _, err := s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = call("/_/api/v1/city/map")
|
||||
if w.Code != 200 || strings.Contains(w.Body.String(), "alpha") {
|
||||
t.Fatalf("revoked projection=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,24 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) compileMarkdownFromPath(
|
||||
r *http.Request,
|
||||
path string,
|
||||
) (string, error) {
|
||||
return s.transpileMarkdownFromPath(r, path, markdownAuthoring)
|
||||
}
|
||||
|
||||
type markdownTranspileMode string
|
||||
|
||||
const (
|
||||
markdownAuthoring markdownTranspileMode = "authoring"
|
||||
markdownProjection markdownTranspileMode = "projection"
|
||||
)
|
||||
|
||||
func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode markdownTranspileMode) (string, error) {
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
s.store.ResolvePathInDomain(
|
||||
s.domainUUIDForRequest(r),
|
||||
path,
|
||||
)
|
||||
|
||||
@@ -21,7 +34,8 @@ func (s *Server) compileMarkdownFromPath(
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
s.latestRevisionForRequest(
|
||||
r,
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
@@ -29,11 +43,10 @@ func (s *Server) compileMarkdownFromPath(
|
||||
return "", err
|
||||
}
|
||||
|
||||
html :=
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
)
|
||||
html := latest.Body
|
||||
if mode == markdownProjection {
|
||||
html = s.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r))
|
||||
}
|
||||
|
||||
return compiler.HTMLToMarkdown(
|
||||
html,
|
||||
@@ -61,6 +74,7 @@ func (s *Server) handleCompileMarkdown(
|
||||
|
||||
out, err :=
|
||||
s.compileMarkdownFromPath(
|
||||
r,
|
||||
path,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestConfigTableDensityChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
if _, _, err := s.store.PutAsset(domain, "asset:text:density", []byte("test"), "text/plain", strings.Repeat("long-filename-", 30)+".txt", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
event := store.EventDefinition{Name: `A "quoted" event`, EventType: "preview.sweep", EventKey: "density", ScheduleKind: "manual", Enabled: true, MaxAttempts: 1}
|
||||
if err := s.store.SaveEventDefinition(&event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/config-density.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.Method == "GET" && r.URL.Path == "/-/sys/config" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, panel := range []string{"auth", "campuses", "extensions"} {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/-/sys/config?panel=auth&capture="+panel, t.TempDir())
|
||||
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd.Args = append(cmd.Args, filepath.Join(dir, "dense-"+panel+".png"))
|
||||
}
|
||||
out, err := cmd.CombinedOutput()
|
||||
cancel()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v %s", panel, err, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
type configPanelLink struct{ ID, Label string }
|
||||
type configPanelGroup struct {
|
||||
ID, Label string
|
||||
Panels []configPanelLink
|
||||
}
|
||||
|
||||
// The registry is owned by Pages Wiki; adapter data supplies names and IDs,
|
||||
// never arbitrary templates or executable settings HTML.
|
||||
func configPanelGroups(locale string, plugins []adapterPlugin) []configPanelGroup {
|
||||
link := func(id, key string) configPanelLink { return configPanelLink{id, translate(locale, key)} }
|
||||
groups := []configPanelGroup{
|
||||
{"appearance", translate(locale, "config.group_appearance"), []configPanelLink{link("home", "config.home"), link("editor", "config.editor"), link("bookmarks", "home.bookmarks"), link("nav-home", "config.nav_home"), link("nav-system", "config.nav_system")}},
|
||||
{"content", translate(locale, "config.group_content"), []configPanelLink{link("objects", "config.objects"), link("assets", "config.assets"), link("external-resources", "config.external_resources"), link("broadcasts", "config.broadcasts")}},
|
||||
{"identity", translate(locale, "config.group_identity"), []configPanelLink{link("auth", "auth.accounts"), link("auth-providers", "config.auth_providers"), link("auth-sessions", "auth.sessions"), link("groups", "auth.groups")}},
|
||||
{"campus", translate(locale, "config.group_campus"), []configPanelLink{link("campuses", "config.campuses"), link("domains", "config.domains")}},
|
||||
{"adapters", translate(locale, "config.group_adapters"), []configPanelLink{link("adapters", "config.adapter_catalog"), link("adapters-builtin", "config.builtin_adapters")}},
|
||||
{"operations", translate(locale, "config.group_operations"), []configPanelLink{link("scheduler", "config.execution_status"), link("logs", "config.execution_log"), link("events", "config.event_definitions"), link("queue", "config.queue_settings")}},
|
||||
}
|
||||
for _, plugin := range plugins {
|
||||
groups[4].Panels = append(groups[4].Panels, configPanelLink{"adapter-" + plugin.ID, plugin.Name})
|
||||
}
|
||||
groups = append(groups, configPanelGroup{"extensions", translate(locale, "extension.title"), []configPanelLink{link("extensions", "extension.catalog"), link("diary", "periodical.diary"), link("blog", "periodical.blog")}})
|
||||
return groups
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func configFixture(t *testing.T) (*Server, string, string) {
|
||||
t.Helper()
|
||||
t.Chdir("../..")
|
||||
database, err := db.Open(t.TempDir() + "/config.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.NewWithDocumentRoot(database, t.TempDir()+"/documents")
|
||||
if err = st.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := st.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := st.BootstrapOwner(domain.UUID, "admin", "a long test password", "Test Admin", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := st.CreateAuthSession(domain.UUID, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = st.SetConfigInDomain(domain.UUID, "locale", "ja"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := &Server{store: st, mux: http.NewServeMux()}
|
||||
s.mustLoadTemplates()
|
||||
s.registerStaticRoutes()
|
||||
s.mux.HandleFunc("/-/sys/config", s.handleConfig)
|
||||
s.mux.HandleFunc("/-/sys/config/auth", s.handleAuthAdmin)
|
||||
s.mux.HandleFunc("/-/sys/config/adapters", s.handleAdapterPluginConfig)
|
||||
s.mux.HandleFunc("/_/api/v1/assets", s.handleAssetsAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/adapter", s.handleAdapterAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/queue", s.handleQueueAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/queue/", s.handleQueueAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/event", s.handleEventAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/event/", s.handleEventAPI)
|
||||
s.mux.HandleFunc("/o/path:/system/static/css/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, filepath.Join("static/css", filepath.Base(r.URL.Path)))
|
||||
})
|
||||
if err = s.saveAdapterPlugins(domain.UUID, []adapterPlugin{{ID: "hosted.test", Name: "Test Editor", Mode: "editor", Enabled: true, URLTemplate: "https://editor.example/open?ticket={ticket}"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err = st.PutAsset(domain.UUID, "asset:image:test", []byte("test"), "image/png", "test.png", "", owner.UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, token, domain.UUID
|
||||
}
|
||||
|
||||
func configRequest(s *Server, token, method, path string, form url.Values) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(form.Encode()))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if form != nil {
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
|
||||
func TestConfigOIDCAccountMatchingMode(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
w := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `name="oidc_match_by"`) || !strings.Contains(w.Body.String(), `value="email" selected`) {
|
||||
t.Fatalf("default OIDC matching selector: %d", w.Code)
|
||||
}
|
||||
form := url.Values{
|
||||
"action": {"save_oidc"},
|
||||
"enabled": {"on"},
|
||||
"issuer": {"https://id.example.test"},
|
||||
"client_id": {"pages"},
|
||||
"client_secret": {"secret"},
|
||||
"oidc_match_by": {"display_name"},
|
||||
}
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
|
||||
if w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "panel=auth-providers") {
|
||||
t.Fatalf("save OIDC matching mode: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
config, err := s.store.AuthProviderConfig(domain, "oidc")
|
||||
if err != nil || config.OIDCMatchBy != "display_name" {
|
||||
t.Fatalf("stored OIDC matching mode: %#v %v", config, err)
|
||||
}
|
||||
w = configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `value="display_name" selected`) {
|
||||
t.Fatalf("selected OIDC matching mode: %d", w.Code)
|
||||
}
|
||||
form.Set("oidc_match_by", "preferred_username")
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
|
||||
if w.Code != http.StatusSeeOther {
|
||||
t.Fatalf("save preferred_username matching mode: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
config, err = s.store.AuthProviderConfig(domain, "oidc")
|
||||
if err != nil || config.OIDCMatchBy != "preferred_username" {
|
||||
t.Fatalf("stored preferred_username matching mode: %#v %v", config, err)
|
||||
}
|
||||
w = configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `value="preferred_username" selected`) {
|
||||
t.Fatalf("selected preferred_username matching mode: %d", w.Code)
|
||||
}
|
||||
form.Set("oidc_match_by", "username")
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("invalid OIDC matching mode accepted: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigLDAPProviderPanelAndSecretPreservation(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
w := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=auth-providers", nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `name="ldap_search_filter"`) || !strings.Contains(w.Body.String(), `{{username}}`) {
|
||||
t.Fatalf("LDAP provider panel: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
form := url.Values{
|
||||
"action": {"save_ldap"},
|
||||
"enabled": {"on"},
|
||||
"ldap_url": {"ldaps://directory.example.test:636"},
|
||||
"ldap_bind_dn": {"cn=pageswiki,ou=services,dc=example,dc=test"},
|
||||
"ldap_bind_password": {"directory-secret"},
|
||||
"ldap_search_base": {"ou=people,dc=example,dc=test"},
|
||||
"ldap_search_filter": {"(&(objectClass=person)(uid={{username}}))"},
|
||||
"ldap_verify_tls": {"on"},
|
||||
"ldap_uid_attribute": {"entryUUID"},
|
||||
"ldap_username_attribute": {"uid"},
|
||||
"ldap_email_attribute": {"mail"},
|
||||
"ldap_display_attribute": {"displayName"},
|
||||
"ldap_match_by": {"username"},
|
||||
"ldap_auto_provision": {"on"},
|
||||
}
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
|
||||
if w.Code != http.StatusSeeOther || !strings.Contains(w.Header().Get("Location"), "panel=auth-providers") {
|
||||
t.Fatalf("save LDAP provider: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
config, err := s.store.AuthProviderConfig(domain, "ldap")
|
||||
if err != nil || config.LDAPBindPassword != "directory-secret" || config.LDAPSkipTLSVerify || !config.LDAPAutoProvision || config.UUID == "" {
|
||||
t.Fatalf("stored LDAP provider: %#v %v", config, err)
|
||||
}
|
||||
form.Set("ldap_bind_password", "")
|
||||
form.Set("ldap_search_filter", "(mail={{username}})")
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config/auth", form)
|
||||
if w.Code != http.StatusSeeOther {
|
||||
t.Fatalf("update LDAP provider: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
config, err = s.store.AuthProviderConfig(domain, "ldap")
|
||||
if err != nil || config.LDAPBindPassword != "directory-secret" || config.LDAPSearchFilter != "(mail={{username}})" {
|
||||
t.Fatalf("LDAP secret preservation: %#v %v", config, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetsListingAdminAllowedAndMemberDenied(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
w := configRequest(s, token, "GET", "/_/api/v1/assets", nil)
|
||||
var data map[string]json.RawMessage
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil || data["assets"] == nil {
|
||||
t.Fatalf("asset list: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
member, err := s.store.CreatePasswordAccount(domain, "reader", "another long password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
memberToken, err := s.store.CreateAuthSession(domain, member.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = configRequest(s, memberToken, "GET", "/_/api/v1/assets", nil)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("member can list assets: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBookmarksSaveAndPanelRegistry(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
form := url.Values{"section": {"bookmarks"}, "bookmark_label": {"Docs", "External"}, "bookmark_kind": {"path", "url"}, "bookmark_target": {"/docs/100% complete", "https://example.com/help"}}
|
||||
w := configRequest(s, token, "POST", "/-/sys/config", form)
|
||||
if w.Code != 302 {
|
||||
t.Fatalf("save bookmarks: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
items := s.homeBookmarks(domain)
|
||||
if len(items) != 2 || items[0].Href != "/docs/100%25%20complete" {
|
||||
t.Fatalf("bookmarks: %#v", items)
|
||||
}
|
||||
form["bookmark_target"][1] = "javascript:alert(1)"
|
||||
w = configRequest(s, token, "POST", "/-/sys/config", form)
|
||||
if w.Code != 400 || len(s.homeBookmarks(domain)) != 2 {
|
||||
t.Fatal("unsafe bookmark accepted or prior state lost")
|
||||
}
|
||||
w = configRequest(s, token, "GET", "/-/sys/config?panel=bookmarks", nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("config: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
root, err := html.Parse(strings.NewReader(w.Body.String()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids := map[string]bool{}
|
||||
var panels []string
|
||||
var scripts []string
|
||||
var walk func(*html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
if n.Data == "table" {
|
||||
var class, parentClass string
|
||||
for _, attr := range n.Attr {
|
||||
if attr.Key == "class" {
|
||||
class = attr.Val
|
||||
}
|
||||
}
|
||||
if n.Parent != nil {
|
||||
for _, attr := range n.Parent.Attr {
|
||||
if attr.Key == "class" {
|
||||
parentClass = attr.Val
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strings.Contains(class, "recent-table") || strings.Contains(class, "config-form-table")) && !strings.Contains(parentClass, "table-scroll") {
|
||||
t.Errorf("configuration table has no horizontal scroll container: %s", class)
|
||||
}
|
||||
}
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == "id" {
|
||||
if ids[a.Val] {
|
||||
t.Errorf("duplicate ID %s", a.Val)
|
||||
}
|
||||
ids[a.Val] = true
|
||||
}
|
||||
if a.Key == "data-panel" {
|
||||
panels = append(panels, a.Val)
|
||||
}
|
||||
}
|
||||
}
|
||||
if n.Type == html.ElementNode && n.Data == "script" && n.FirstChild != nil {
|
||||
scripts = append(scripts, n.FirstChild.Data)
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(root)
|
||||
for _, panel := range panels {
|
||||
if !ids["panel-"+panel] {
|
||||
t.Errorf("missing panel %s", panel)
|
||||
}
|
||||
}
|
||||
if len(panels) < 18 || !ids["panel-adapter-hosted.test"] {
|
||||
t.Fatalf("incomplete panel registry: %v", panels)
|
||||
}
|
||||
if node, err := exec.LookPath("node"); err == nil {
|
||||
for _, script := range scripts {
|
||||
cmd := exec.Command(node, "--check")
|
||||
cmd.Stdin = strings.NewReader(script)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("invalid inline JS: %s", out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegratedAdapterSettingsPersistAndRedirect(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
form := url.Values{"id": {"hosted.test"}, "connection_fields": {"1"}, "name": {"Edited"}, "mode": {"editor"},
|
||||
"url_template": {"https://editor.example/open?ticket={ticket}"}, "enabled": {"on"}, "new_tab": {"on"}, "html_markdown": {"on"}, "default_editor": {"on"}, "extensions": {".html, .txt"}}
|
||||
w := configRequest(s, token, "POST", "/-/sys/config/adapters?id=hosted.test", form)
|
||||
if w.Code != 302 || !strings.Contains(w.Header().Get("Location"), "panel=adapter-hosted.test") {
|
||||
t.Fatalf("save adapter: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
plugins, err := s.adapterPlugins(domain)
|
||||
if err != nil || len(plugins) != 1 || !plugins[0].NewTab || !plugins[0].HTMLMarkdown || !plugins[0].DefaultEditor || plugins[0].Name != "Edited" {
|
||||
t.Fatalf("plugin=%#v %v", plugins, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigChromiumSmoke(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium smoke test")
|
||||
}
|
||||
s, token, _ := configFixture(t)
|
||||
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.runEvent(&store.EventJob{UUID: "resource-browser", Type: store.ResourceEventType}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, panel := range []string{"assets", "events", "queue", "scheduler", "logs", "resources", "bookmarks", "adapter-hosted.test", "extensions"} {
|
||||
t.Run(panel, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=1500", server.URL+"/-/sys/config?panel="+panel)
|
||||
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd.Args = append(cmd.Args, "--window-size=1440,1000", "--screenshot="+filepath.Join(dir, panel+".png"))
|
||||
}
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("browser: %v %s", err, out)
|
||||
}
|
||||
body := string(out)
|
||||
target, classes := panel, "config-section config-panel active"
|
||||
if panel == "resources" || panel == "scheduler" {
|
||||
target, classes = "scheduler", "config-section config-panel execution-dashboard active"
|
||||
}
|
||||
if strings.Contains(body, "Uncaught ") || !strings.Contains(body, `id="panel-`+target+`" class="`+classes+`"`) {
|
||||
t.Fatalf("browser panel failed: %s", body)
|
||||
}
|
||||
if panel == "assets" && !strings.Contains(body, `data-asset-copy-hash=`) {
|
||||
t.Fatalf("assets did not load: %s", body)
|
||||
}
|
||||
if panel == "resources" && !strings.Contains(body, `class="chart-line"`) {
|
||||
t.Fatalf("resource chart did not load: %s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type projectionDelivery string
|
||||
|
||||
const (
|
||||
deliveryHTML projectionDelivery = "html"
|
||||
deliveryText projectionDelivery = "text"
|
||||
deliveryRawMedia projectionDelivery = "raw-media"
|
||||
deliveryRawFrame projectionDelivery = "raw-frame"
|
||||
deliveryDownload projectionDelivery = "download"
|
||||
)
|
||||
|
||||
type contentDescriptor struct {
|
||||
MediaType string
|
||||
Filename string
|
||||
SizeBytes int64
|
||||
}
|
||||
|
||||
type projectionAdapter struct {
|
||||
ID string
|
||||
Kind string
|
||||
Delivery projectionDelivery
|
||||
NeedsText bool
|
||||
WindowableText bool
|
||||
Match func(contentDescriptor) bool
|
||||
}
|
||||
|
||||
const inlineTextWindowThreshold = int64(2 << 20)
|
||||
|
||||
var projectionAdapters = []projectionAdapter{
|
||||
{
|
||||
ID: "html",
|
||||
Kind: "html",
|
||||
Delivery: deliveryHTML,
|
||||
Match: mediaTypes("text/html"),
|
||||
},
|
||||
{
|
||||
ID: "excalidraw",
|
||||
Kind: "excalidraw",
|
||||
Delivery: deliveryText,
|
||||
NeedsText: true,
|
||||
WindowableText: true,
|
||||
Match: extensions(".excalidraw"),
|
||||
},
|
||||
{
|
||||
ID: "csv",
|
||||
Kind: "csv",
|
||||
Delivery: deliveryText,
|
||||
NeedsText: true,
|
||||
WindowableText: true,
|
||||
Match: matchAny(
|
||||
mediaTypes("text/csv", "application/csv"),
|
||||
extensions(".csv"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "archive",
|
||||
Kind: "archive",
|
||||
Delivery: deliveryDownload,
|
||||
Match: matchAny(
|
||||
mediaTypes(
|
||||
"application/zip", "application/x-zip-compressed",
|
||||
"application/x-rar-compressed", "application/vnd.rar",
|
||||
"application/x-7z-compressed", "application/x-tar",
|
||||
"application/gzip", "application/x-xz",
|
||||
),
|
||||
extensions(".zip", ".rar", ".7z", ".tar", ".gz", ".tgz", ".xz", ".txz"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "debian-package",
|
||||
Kind: "metadata",
|
||||
Delivery: deliveryDownload,
|
||||
Match: matchAny(
|
||||
mediaTypes(
|
||||
"application/vnd.debian.binary-package",
|
||||
"application/x-debian-package",
|
||||
),
|
||||
extensions(".deb"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "eml",
|
||||
Kind: "email",
|
||||
Delivery: deliveryText,
|
||||
Match: matchAny(
|
||||
mediaTypes("message/rfc822"),
|
||||
extensions(".eml"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "pe",
|
||||
Kind: "metadata",
|
||||
Delivery: deliveryDownload,
|
||||
Match: extensions(".exe", ".dll", ".sys"),
|
||||
},
|
||||
{
|
||||
ID: "json",
|
||||
Kind: "json",
|
||||
Delivery: deliveryText,
|
||||
WindowableText: true,
|
||||
Match: matchAny(mediaTypes("application/json", "application/ld+json"), extensions(".json", ".jsonl")),
|
||||
},
|
||||
{
|
||||
ID: "text",
|
||||
Kind: "code",
|
||||
Delivery: deliveryText,
|
||||
NeedsText: true,
|
||||
WindowableText: true,
|
||||
Match: textMediaType,
|
||||
},
|
||||
{
|
||||
ID: "image",
|
||||
Kind: "image",
|
||||
Delivery: deliveryRawMedia,
|
||||
Match: matchAny(
|
||||
mediaTypePrefix("image/"),
|
||||
extensions(".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif", ".svg"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "audio",
|
||||
Kind: "audio",
|
||||
Delivery: deliveryRawMedia,
|
||||
Match: matchAny(
|
||||
mediaTypePrefix("audio/"),
|
||||
extensions(".mp3", ".flac", ".wav", ".ogg", ".m4a", ".aac", ".opus"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "video",
|
||||
Kind: "video",
|
||||
Delivery: deliveryRawMedia,
|
||||
Match: matchAny(
|
||||
mediaTypePrefix("video/"),
|
||||
extensions(".mp4", ".webm", ".mkv", ".mov", ".m4v", ".ogv"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "pdf",
|
||||
Kind: "pdf",
|
||||
Delivery: deliveryRawFrame,
|
||||
Match: matchAny(
|
||||
mediaTypes("application/pdf"),
|
||||
extensions(".pdf"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ID: "download",
|
||||
Kind: "download",
|
||||
Delivery: deliveryDownload,
|
||||
Match: func(contentDescriptor) bool { return true },
|
||||
},
|
||||
}
|
||||
|
||||
func selectProjectionAdapter(descriptor contentDescriptor) projectionAdapter {
|
||||
for _, adapter := range projectionAdapters {
|
||||
if adapter.Match(descriptor) {
|
||||
return adapter
|
||||
}
|
||||
}
|
||||
return projectionAdapters[len(projectionAdapters)-1]
|
||||
}
|
||||
|
||||
func mediaTypes(types ...string) func(contentDescriptor) bool {
|
||||
return func(descriptor contentDescriptor) bool {
|
||||
for _, candidate := range types {
|
||||
if descriptor.MediaType == candidate {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func mediaTypePrefix(prefix string) func(contentDescriptor) bool {
|
||||
return func(descriptor contentDescriptor) bool {
|
||||
return strings.HasPrefix(descriptor.MediaType, prefix)
|
||||
}
|
||||
}
|
||||
|
||||
func extensions(values ...string) func(contentDescriptor) bool {
|
||||
return func(descriptor contentDescriptor) bool {
|
||||
extension := strings.ToLower(filepath.Ext(descriptor.Filename))
|
||||
for _, candidate := range values {
|
||||
if extension == candidate {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func matchAny(matchers ...func(contentDescriptor) bool) func(contentDescriptor) bool {
|
||||
return func(descriptor contentDescriptor) bool {
|
||||
for _, matcher := range matchers {
|
||||
if matcher(descriptor) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func textMediaType(descriptor contentDescriptor) bool {
|
||||
switch descriptor.MediaType {
|
||||
case "application/json",
|
||||
"application/xml",
|
||||
"application/javascript",
|
||||
"application/x-javascript",
|
||||
"application/x-sh",
|
||||
"application/x-shellscript",
|
||||
"application/x-yaml",
|
||||
"application/yaml",
|
||||
"application/toml":
|
||||
return true
|
||||
default:
|
||||
return strings.HasPrefix(descriptor.MediaType, "text/")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/encoding/japanese"
|
||||
textunicode "golang.org/x/text/encoding/unicode"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
type detectedText struct {
|
||||
ContentType string
|
||||
Text string
|
||||
IsText bool
|
||||
}
|
||||
|
||||
func detectRevisionText(data []byte, filename string) detectedText {
|
||||
if len(data) == 0 {
|
||||
return detectedText{"text/plain; charset=utf-8", "", true}
|
||||
}
|
||||
magicType := http.DetectContentType(data)
|
||||
if magicType != "application/octet-stream" &&
|
||||
!strings.HasPrefix(magicType, "text/plain") {
|
||||
return detectedText{ContentType: magicType}
|
||||
}
|
||||
|
||||
decoded, charset, ok := decodeTextBytes(data)
|
||||
if !ok {
|
||||
return detectedText{ContentType: http.DetectContentType(data)}
|
||||
}
|
||||
|
||||
contentType := detectTextMediaType(decoded, filename)
|
||||
if strings.HasPrefix(contentType, "text/") ||
|
||||
contentType == "application/json" ||
|
||||
contentType == "application/xml" ||
|
||||
contentType == "application/javascript" ||
|
||||
contentType == "application/x-sh" {
|
||||
contentType += "; charset=" + charset
|
||||
}
|
||||
return detectedText{
|
||||
ContentType: contentType,
|
||||
Text: decoded,
|
||||
IsText: true,
|
||||
}
|
||||
}
|
||||
|
||||
func decodeTextBytes(data []byte) (string, string, bool) {
|
||||
if bytes.HasPrefix(data, []byte{0xef, 0xbb, 0xbf}) {
|
||||
return string(data[3:]), "utf-8", true
|
||||
}
|
||||
if bytes.HasPrefix(data, []byte{0xff, 0xfe}) {
|
||||
decoded, _, err := transform.Bytes(
|
||||
textunicode.UTF16(textunicode.LittleEndian, textunicode.ExpectBOM).NewDecoder(),
|
||||
data,
|
||||
)
|
||||
return string(decoded), "utf-16le", err == nil
|
||||
}
|
||||
if bytes.HasPrefix(data, []byte{0xfe, 0xff}) {
|
||||
decoded, _, err := transform.Bytes(
|
||||
textunicode.UTF16(textunicode.BigEndian, textunicode.ExpectBOM).NewDecoder(),
|
||||
data,
|
||||
)
|
||||
return string(decoded), "utf-16be", err == nil
|
||||
}
|
||||
if utf8.Valid(data) {
|
||||
text := string(data)
|
||||
return text, "utf-8", looksLikeText(text)
|
||||
}
|
||||
if bytes.Contains(data, []byte{0x1b, '$'}) {
|
||||
if decoded, _, err := transform.Bytes(japanese.ISO2022JP.NewDecoder(), data); err == nil {
|
||||
text := string(decoded)
|
||||
return text, "iso-2022-jp", looksLikeText(text)
|
||||
}
|
||||
}
|
||||
for _, candidate := range []struct {
|
||||
name string
|
||||
enc transform.Transformer
|
||||
}{
|
||||
{"shift_jis", japanese.ShiftJIS.NewDecoder()},
|
||||
{"euc-jp", japanese.EUCJP.NewDecoder()},
|
||||
} {
|
||||
decoded, _, err := transform.Bytes(candidate.enc, data)
|
||||
if err == nil && looksLikeText(string(decoded)) {
|
||||
return string(decoded), candidate.name, true
|
||||
}
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func looksLikeText(text string) bool {
|
||||
if text == "" {
|
||||
return true
|
||||
}
|
||||
var controls, total int
|
||||
for _, r := range text {
|
||||
total++
|
||||
if r == '\n' || r == '\r' || r == '\t' || r == '\f' {
|
||||
continue
|
||||
}
|
||||
if unicode.IsControl(r) || r == utf8.RuneError {
|
||||
controls++
|
||||
}
|
||||
}
|
||||
return controls*100 <= total*2
|
||||
}
|
||||
|
||||
func detectTextMediaType(text, filename string) string {
|
||||
firstLine := text
|
||||
if newline := strings.IndexByte(firstLine, '\n'); newline >= 0 {
|
||||
firstLine = firstLine[:newline]
|
||||
}
|
||||
lower := strings.ToLower(firstLine)
|
||||
switch {
|
||||
case strings.HasPrefix(lower, "#!") &&
|
||||
strings.Contains(lower, "python"):
|
||||
return "text/x-python"
|
||||
case strings.HasPrefix(lower, "#!") &&
|
||||
(strings.Contains(lower, "/sh") ||
|
||||
strings.Contains(lower, "bash") ||
|
||||
strings.Contains(lower, "zsh")):
|
||||
return "application/x-sh"
|
||||
case strings.HasPrefix(lower, "#!") &&
|
||||
(strings.Contains(lower, "node") || strings.Contains(lower, "deno")):
|
||||
return "application/javascript"
|
||||
}
|
||||
if json.Valid([]byte(text)) {
|
||||
return "application/json"
|
||||
}
|
||||
if strings.HasPrefix(strings.TrimSpace(text), "<?xml") {
|
||||
return "application/xml"
|
||||
}
|
||||
if extType := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))); extType != "" {
|
||||
if mediaType, _, err := mime.ParseMediaType(extType); err == nil {
|
||||
return mediaType
|
||||
}
|
||||
}
|
||||
return "text/plain"
|
||||
}
|
||||
|
||||
func codeLanguage(contentType, filename, text string) string {
|
||||
mediaType, _, _ := mime.ParseMediaType(contentType)
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
switch {
|
||||
case mediaType == "text/x-python" || ext == ".py":
|
||||
return "python"
|
||||
case mediaType == "application/x-sh" || mediaType == "application/x-shellscript" ||
|
||||
ext == ".sh" || ext == ".bash" || ext == ".zsh":
|
||||
return "shell"
|
||||
case mediaType == "application/json" || ext == ".json":
|
||||
return "json"
|
||||
case mediaType == "application/javascript" || mediaType == "application/x-javascript" ||
|
||||
ext == ".js" || ext == ".jsx" || ext == ".mjs" || ext == ".cjs":
|
||||
return "javascript"
|
||||
case ext == ".ts" || ext == ".tsx":
|
||||
return "typescript"
|
||||
case mediaType == "text/html" || ext == ".html" || ext == ".htm":
|
||||
return "html"
|
||||
case ext == ".css":
|
||||
return "css"
|
||||
case ext == ".go":
|
||||
return "go"
|
||||
case ext == ".rs":
|
||||
return "rust"
|
||||
case ext == ".java":
|
||||
return "java"
|
||||
case ext == ".c" || ext == ".h" || ext == ".cc" || ext == ".cpp" || ext == ".hpp":
|
||||
return "cpp"
|
||||
case mediaType == "application/x-yaml" || mediaType == "application/yaml" ||
|
||||
ext == ".yaml" || ext == ".yml":
|
||||
return "yaml"
|
||||
case mediaType == "application/toml" || ext == ".toml":
|
||||
return "toml"
|
||||
case ext == ".sql":
|
||||
return "sql"
|
||||
}
|
||||
detected := detectTextMediaType(text, filename)
|
||||
if detected != "text/plain" && detected != mediaType {
|
||||
return codeLanguage(detected, filename, text)
|
||||
}
|
||||
return "text"
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/text/encoding/japanese"
|
||||
)
|
||||
|
||||
func TestDetectRevisionTextWithoutExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
contentType string
|
||||
isText bool
|
||||
language string
|
||||
}{
|
||||
{
|
||||
name: "shell shebang",
|
||||
data: []byte("#!/bin/sh\necho hello\n"),
|
||||
contentType: "application/x-sh",
|
||||
isText: true,
|
||||
language: "shell",
|
||||
},
|
||||
{
|
||||
name: "json",
|
||||
data: []byte("{\"enabled\":true}\n"),
|
||||
contentType: "application/json",
|
||||
isText: true,
|
||||
language: "json",
|
||||
},
|
||||
{
|
||||
name: "PDF magic",
|
||||
data: []byte("%PDF-1.7\n%\x80\x81\x82\n"),
|
||||
contentType: "application/pdf",
|
||||
isText: false,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
detected := detectRevisionText(test.data, "")
|
||||
if detected.IsText != test.isText {
|
||||
t.Fatalf("IsText = %v, want %v", detected.IsText, test.isText)
|
||||
}
|
||||
if !strings.HasPrefix(detected.ContentType, test.contentType) {
|
||||
t.Fatalf(
|
||||
"ContentType = %q, want prefix %q",
|
||||
detected.ContentType,
|
||||
test.contentType,
|
||||
)
|
||||
}
|
||||
if test.language != "" {
|
||||
if got := codeLanguage(
|
||||
detected.ContentType,
|
||||
"",
|
||||
detected.Text,
|
||||
); got != test.language {
|
||||
t.Fatalf("language = %q, want %q", got, test.language)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectRevisionTextDecodesShiftJIS(t *testing.T) {
|
||||
data, err := japanese.ShiftJIS.NewEncoder().Bytes(
|
||||
[]byte("日本語のテキストです\n"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
detected := detectRevisionText(data, "")
|
||||
if !detected.IsText {
|
||||
t.Fatal("Shift_JIS text was classified as binary")
|
||||
}
|
||||
if detected.Text != "日本語のテキストです\n" {
|
||||
t.Fatalf("decoded text = %q", detected.Text)
|
||||
}
|
||||
if !strings.Contains(detected.ContentType, "charset=shift_jis") {
|
||||
t.Fatalf("ContentType = %q", detected.ContentType)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) windowedContentViewForRequest(r *http.Request, objectUUID, semanticPath string) (contentView, bool, error) {
|
||||
metadata, err := s.latestRevisionMetadataForRequest(r, objectUUID)
|
||||
if err != nil || metadata.UUID == "" {
|
||||
return contentView{}, false, err
|
||||
}
|
||||
size, err := s.store.DocumentSize(metadata.DocumentHash)
|
||||
if err != nil {
|
||||
return contentView{}, false, nil
|
||||
}
|
||||
mediaType := strings.ToLower(strings.TrimSpace(strings.Split(metadata.ContentType, ";")[0]))
|
||||
adapter := selectProjectionAdapter(contentDescriptor{MediaType: mediaType, Filename: metadata.Filename, SizeBytes: size})
|
||||
if adapter.ID == "pdf" || (adapter.ID == "archive" && !strings.EqualFold(filepath.Ext(metadata.Filename), ".zip")) {
|
||||
view := revisionContentView(metadata, semanticPath)
|
||||
view.SizeBytes = size
|
||||
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
|
||||
s.applyMetadataProjection(&view, semanticPath)
|
||||
return view, true, nil
|
||||
}
|
||||
if size <= inlineTextWindowThreshold || !adapter.WindowableText {
|
||||
return contentView{}, false, nil
|
||||
}
|
||||
window, err := s.store.ReadDocumentWindow(metadata.DocumentHash, 0, 128<<10)
|
||||
if err != nil {
|
||||
return contentView{}, false, err
|
||||
}
|
||||
view := revisionWindowedContentView(metadata, semanticPath, size, window.Data, window.NextOffset)
|
||||
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
|
||||
return view, true, nil
|
||||
}
|
||||
|
||||
type contentView struct {
|
||||
Kind string
|
||||
AdapterID string
|
||||
Delivery projectionDelivery
|
||||
ContentType string
|
||||
Filename string
|
||||
Text string
|
||||
RawURL string
|
||||
Language string
|
||||
SizeBytes int64
|
||||
Windowed bool
|
||||
ArchiveEntries []archiveEntryView
|
||||
ArchiveTruncated bool
|
||||
MetadataStatus string
|
||||
MetadataURL string
|
||||
Metadata []metadataView
|
||||
ParseError string
|
||||
EmailHTML bool
|
||||
PDFPages int
|
||||
CSVRows [][]string
|
||||
CSVTruncated bool
|
||||
ExcalidrawValid bool
|
||||
ExcalidrawElements int
|
||||
ObjectUUID string
|
||||
RevisionUUID string
|
||||
DocumentHash string
|
||||
WindowURL string
|
||||
NextOffset int64
|
||||
JSONURL string
|
||||
}
|
||||
|
||||
func revisionWindowedContentView(revision *store.Revision, semanticPath string, totalBytes int64, initial []byte, nextOffset int64) contentView {
|
||||
contentType := revision.ContentType
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
mediaType, contentType = "application/octet-stream", "application/octet-stream"
|
||||
}
|
||||
filename := strings.TrimSpace(revision.Filename)
|
||||
if filename == "" {
|
||||
filename = filepath.Base(semanticPath)
|
||||
}
|
||||
if mediaType == "application/octet-stream" {
|
||||
detected := detectRevisionText(initial, filename)
|
||||
if detected.ContentType != "" {
|
||||
contentType = detected.ContentType
|
||||
mediaType, _, _ = mime.ParseMediaType(contentType)
|
||||
}
|
||||
}
|
||||
adapter := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(mediaType), Filename: filename, SizeBytes: totalBytes})
|
||||
text, _, ok := decodeTextBytes(initial)
|
||||
if !ok {
|
||||
text = string(initial)
|
||||
}
|
||||
view := contentView{Kind: adapter.Kind, AdapterID: adapter.ID, Delivery: adapter.Delivery, ContentType: contentType, Filename: filename, Text: text, Language: codeLanguage(contentType, filename, text), SizeBytes: totalBytes, Windowed: true, ObjectUUID: revision.ObjectUUID, RevisionUUID: revision.UUID, DocumentHash: revision.DocumentHash, WindowURL: "/v/text/rev:" + revision.UUID, NextOffset: nextOffset}
|
||||
if adapter.ID == "json" {
|
||||
view.JSONURL = "/v/json/rev:" + revision.UUID
|
||||
view.Text = ""
|
||||
}
|
||||
if adapter.ID == "csv" {
|
||||
parseCSVView(&view, initial)
|
||||
view.CSVTruncated = view.CSVTruncated || nextOffset < totalBytes
|
||||
}
|
||||
if adapter.ID == "excalidraw" {
|
||||
// Large scenes keep bounded source delivery rather than parsing an
|
||||
// incomplete or unbounded graph during navigation.
|
||||
view.Kind = "code"
|
||||
view.Language = "json"
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
func revisionContentView(revision *store.Revision, semanticPath string) contentView {
|
||||
contentType := revision.ContentType
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
mediaType = "application/octet-stream"
|
||||
contentType = mediaType
|
||||
}
|
||||
mediaType = strings.ToLower(mediaType)
|
||||
filename := strings.TrimSpace(revision.Filename)
|
||||
if filename == "" {
|
||||
filename = filepath.Base(semanticPath)
|
||||
}
|
||||
view := contentView{
|
||||
ContentType: contentType,
|
||||
Filename: filename,
|
||||
SizeBytes: int64(len(revision.Body)),
|
||||
ObjectUUID: revision.ObjectUUID,
|
||||
RevisionUUID: revision.UUID,
|
||||
DocumentHash: revision.DocumentHash,
|
||||
}
|
||||
if mediaType == "application/octet-stream" {
|
||||
detected := detectRevisionText([]byte(revision.Body), filename)
|
||||
if detected.ContentType != "" {
|
||||
contentType = detected.ContentType
|
||||
mediaType, _, _ = mime.ParseMediaType(contentType)
|
||||
view.ContentType = contentType
|
||||
}
|
||||
if detected.IsText {
|
||||
view.Text = detected.Text
|
||||
}
|
||||
}
|
||||
adapter := selectProjectionAdapter(contentDescriptor{
|
||||
MediaType: mediaType,
|
||||
Filename: filename,
|
||||
SizeBytes: view.SizeBytes,
|
||||
})
|
||||
view.Kind = adapter.Kind
|
||||
view.AdapterID = adapter.ID
|
||||
view.Delivery = adapter.Delivery
|
||||
view.Windowed = adapter.WindowableText &&
|
||||
view.SizeBytes > inlineTextWindowThreshold
|
||||
if adapter.ID == "json" {
|
||||
view.JSONURL = "/v/json/rev:" + revision.UUID
|
||||
}
|
||||
|
||||
if adapter.NeedsText {
|
||||
if view.Text == "" {
|
||||
if decoded, _, ok := decodeTextBytes([]byte(revision.Body)); ok {
|
||||
view.Text = decoded
|
||||
} else {
|
||||
view.Text = revision.Body
|
||||
}
|
||||
}
|
||||
view.Language = codeLanguage(contentType, filename, view.Text)
|
||||
}
|
||||
structuredContentView(&view, []byte(revision.Body), semanticPath)
|
||||
if adapter.ID == "eml" {
|
||||
view.EmailHTML = extractEmailHTML([]byte(revision.Body)) != ""
|
||||
}
|
||||
return view
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestRevisionContentViewSelectsSafeAdapters(t *testing.T) {
|
||||
tests := []struct {
|
||||
contentType string
|
||||
kind string
|
||||
}{
|
||||
{"text/html; charset=utf-8", "html"},
|
||||
{"text/x-python; charset=utf-8", "code"},
|
||||
{"application/x-sh", "code"},
|
||||
{"application/json", "json"},
|
||||
{"image/png", "image"},
|
||||
{"audio/mpeg", "audio"},
|
||||
{"video/mp4", "video"},
|
||||
{"application/pdf", "pdf"},
|
||||
{
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"download",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
view := revisionContentView(
|
||||
&store.Revision{
|
||||
ContentType: test.contentType,
|
||||
Filename: "sample",
|
||||
Body: "<script>alert(1)</script>",
|
||||
},
|
||||
"/sample",
|
||||
)
|
||||
if view.Kind != test.kind {
|
||||
t.Errorf(
|
||||
"Content-Type %q kind = %q, want %q",
|
||||
test.contentType,
|
||||
view.Kind,
|
||||
test.kind,
|
||||
)
|
||||
}
|
||||
if test.kind == "code" && view.Text == "" {
|
||||
t.Errorf("code content was not passed to escaped template rendering")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevisionContentViewSniffsExtensionlessOctetStream(t *testing.T) {
|
||||
script := revisionContentView(
|
||||
&store.Revision{
|
||||
ContentType: "application/octet-stream",
|
||||
Body: "#!/usr/bin/env bash\necho hello\n",
|
||||
},
|
||||
"/run",
|
||||
)
|
||||
if script.Kind != "code" || script.Language != "shell" {
|
||||
t.Fatalf("script view = %#v", script)
|
||||
}
|
||||
if script.Text == "" {
|
||||
t.Fatal("extensionless script did not expose decoded text")
|
||||
}
|
||||
|
||||
pdf := revisionContentView(
|
||||
&store.Revision{
|
||||
ContentType: "application/octet-stream",
|
||||
Body: "%PDF-1.7\n%\x80\x81\x82\n",
|
||||
},
|
||||
"/manual",
|
||||
)
|
||||
if pdf.Kind != "pdf" {
|
||||
t.Fatalf("PDF kind = %q, want pdf", pdf.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevisionContentViewSelectsSpecializedAdapterBoundaries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
contentType string
|
||||
filename string
|
||||
adapterID string
|
||||
kind string
|
||||
}{
|
||||
{"CSV", "text/csv", "report.csv", "csv", "csv"},
|
||||
{"ZIP", "application/zip", "bundle.zip", "archive", "archive"},
|
||||
{
|
||||
"DEB",
|
||||
"application/octet-stream",
|
||||
"pageswiki.deb",
|
||||
"debian-package",
|
||||
"metadata",
|
||||
},
|
||||
{
|
||||
"Excalidraw",
|
||||
"application/json",
|
||||
"diagram.excalidraw",
|
||||
"excalidraw",
|
||||
"excalidraw",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
view := revisionContentView(
|
||||
&store.Revision{
|
||||
ContentType: test.contentType,
|
||||
Filename: test.filename,
|
||||
Body: "{}",
|
||||
},
|
||||
"/"+test.filename,
|
||||
)
|
||||
if view.AdapterID != test.adapterID || view.Kind != test.kind {
|
||||
t.Fatalf(
|
||||
"view adapter/kind = %q/%q, want %q/%q",
|
||||
view.AdapterID,
|
||||
view.Kind,
|
||||
test.adapterID,
|
||||
test.kind,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVProjectionIsBounded(t *testing.T) {
|
||||
body := "name,value\n" + strings.Repeat("row,1\n", maxCSVRows+10)
|
||||
view := revisionContentView(&store.Revision{ContentType: "text/csv", Filename: "report.csv", Body: body}, "/report.csv")
|
||||
if view.Kind != "csv" || len(view.CSVRows) != maxCSVRows {
|
||||
t.Fatalf("CSV projection kind/rows = %q/%d", view.Kind, len(view.CSVRows))
|
||||
}
|
||||
if !view.CSVTruncated {
|
||||
t.Fatal("bounded CSV projection did not report truncation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodeLanguageStopsWhenDetectionReturnsCurrentMediaType(t *testing.T) {
|
||||
if language := codeLanguage("text/csv; charset=utf-8", "report.csv", "name,value\na,1\n"); language != "text" {
|
||||
t.Fatalf("CSV fallback language = %q, want text", language)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcalidrawProjectionValidatesScene(t *testing.T) {
|
||||
valid := revisionContentView(&store.Revision{
|
||||
ContentType: "application/json", Filename: "map.excalidraw",
|
||||
Body: `{"type":"excalidraw","elements":[{"type":"rectangle","x":1,"y":2,"width":30,"height":20}]}`,
|
||||
}, "/map.excalidraw")
|
||||
if valid.Kind != "excalidraw" || !valid.ExcalidrawValid || valid.ExcalidrawElements != 1 {
|
||||
t.Fatalf("valid scene projection = %#v", valid)
|
||||
}
|
||||
|
||||
invalid := revisionContentView(&store.Revision{
|
||||
ContentType: "application/json", Filename: "map.excalidraw", Body: `{"elements":[]}`,
|
||||
}, "/map.excalidraw")
|
||||
if invalid.ExcalidrawValid || invalid.ParseError == "" {
|
||||
t.Fatalf("invalid scene was accepted: %#v", invalid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExcalidrawProjectionRejectsExcessivePointComplexity(t *testing.T) {
|
||||
points := strings.Repeat("[0,0],", maxExcalidrawPoints+1)
|
||||
points = strings.TrimSuffix(points, ",")
|
||||
view := contentView{}
|
||||
parseExcalidrawView(&view, []byte(`{"type":"excalidraw","elements":[{"type":"line","x":0,"y":0,"width":1,"height":1,"points":[`+points+`]}]}`))
|
||||
if view.ExcalidrawValid || view.ParseError == "" {
|
||||
t.Fatal("excessive Excalidraw point complexity was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextAdapterSignalsWindowingForLargeDocuments(t *testing.T) {
|
||||
view := revisionContentView(
|
||||
&store.Revision{
|
||||
ContentType: "text/plain; charset=utf-8",
|
||||
Filename: "large.txt",
|
||||
Body: strings.Repeat("x", int(inlineTextWindowThreshold+1)),
|
||||
},
|
||||
"/large.txt",
|
||||
)
|
||||
if view.AdapterID != "text" {
|
||||
t.Fatalf("AdapterID = %q, want text", view.AdapterID)
|
||||
}
|
||||
if !view.Windowed {
|
||||
t.Fatal("large text adapter did not request windowed delivery")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewUsesViewerAdapterForNonHTMLRevision(t *testing.T) {
|
||||
view := previewContentView(&store.Revision{
|
||||
UUID: "revision-1",
|
||||
ContentType: "text/plain; charset=utf-8",
|
||||
Filename: "payload.txt",
|
||||
Body: "<script>alert('preview')</script>",
|
||||
}, "/payload.txt")
|
||||
if view.Kind != "code" {
|
||||
t.Fatalf("preview kind = %q, want code", view.Kind)
|
||||
}
|
||||
if view.Text != "<script>alert('preview')</script>" {
|
||||
t.Fatalf("preview text = %q", view.Text)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// No cross-origin trust/bypass list: adapters and federation use explicit
|
||||
// server-to-server credentials, while browser editors are same-origin proxies.
|
||||
var browserOriginProtection http.CrossOriginProtection
|
||||
|
||||
func validBrowserMutationOrigin(r *http.Request) bool {
|
||||
// Legacy semantic editor entry may allocate an empty Page. Until creation
|
||||
// is moved entirely into save, subject that navigation to the mutation gate.
|
||||
navigation := (r.Method == http.MethodGet || r.Method == http.MethodHead) &&
|
||||
(strings.HasPrefix(r.URL.Path, "/e/") || strings.HasPrefix(r.URL.Path, "/o/e/") || r.URL.Path == "/:/new")
|
||||
if navigation {
|
||||
copy := r.Clone(r.Context())
|
||||
copy.Method = http.MethodPost
|
||||
r = copy
|
||||
}
|
||||
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
|
||||
return true
|
||||
}
|
||||
if browserOriginProtection.Check(r) != nil {
|
||||
return false
|
||||
}
|
||||
// The standard guard compares hosts for older clients without Fetch
|
||||
// Metadata. Also require the same scheme, especially an HTTP-origin form
|
||||
// targeting an HTTPS session. Trusted same-origin Fetch Metadata already
|
||||
// accounts for TLS termination at the reverse proxy.
|
||||
if origin := r.Header.Get("Origin"); origin != "" && r.Header.Get("Sec-Fetch-Site") != "same-origin" {
|
||||
u, err := url.Parse(origin)
|
||||
scheme := "http"
|
||||
if requestIsHTTPS(r) {
|
||||
scheme = "https"
|
||||
}
|
||||
if err != nil || u.User != nil || u.Scheme != scheme || u.Host == "" || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
// Check legacy clients too when ambient browser credentials are present.
|
||||
// A CLI must send an explicit Bearer credential, not reuse a browser cookie
|
||||
// without supplying an Origin. Do not trust X-Forwarded-Host for this test.
|
||||
_, session := r.Cookie(authCookieName)
|
||||
_, challenge := r.Cookie(mfaChallengeCookieName)
|
||||
if session != nil && challenge != nil || len(r.Header.Values("Authorization")) != 0 {
|
||||
return true
|
||||
}
|
||||
if r.Header.Get("Sec-Fetch-Site") == "same-origin" || r.Header.Get("Origin") != "" {
|
||||
return true // CrossOriginProtection already validated the supplied origin.
|
||||
}
|
||||
if navigation && r.Header.Get("Sec-Fetch-Site") == "none" && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
|
||||
return true // address-bar/bookmark navigation, not an external embedding
|
||||
}
|
||||
u, err := url.Parse(r.Referer())
|
||||
return err == nil && u.User == nil && (u.Scheme == "http" || u.Scheme == "https") && strings.EqualFold(u.Host, r.Host) && (!requestIsHTTPS(r) || u.Scheme == "https")
|
||||
}
|
||||
|
||||
func rejectBrowserMutation(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
if strings.HasPrefix(r.URL.Path, "/_/api/") {
|
||||
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "cross-origin or unverified browser mutation rejected"})
|
||||
return
|
||||
}
|
||||
http.Error(w, "forbidden: cross-origin or unverified browser mutation", http.StatusForbidden)
|
||||
}
|
||||
|
||||
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
if r.Method == http.MethodGet || r.Method == http.MethodHead {
|
||||
s.renderAuthPage(w, r, map[string]any{"Logout": true})
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", "GET, HEAD, POST")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if token := s.authTokenFromRequest(r); token != "" {
|
||||
if err := s.store.RevokeAuthSession(token); err != nil {
|
||||
http.Error(w, "session revocation failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(r.Header.Values("Authorization")) == 0 {
|
||||
http.SetCookie(w, &http.Cookie{Name: authCookieName, Path: "/", MaxAge: -1, HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode})
|
||||
} else {
|
||||
writeEventJSON(w, http.StatusOK, map[string]string{"status": "signed_out"})
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/_/auth/login", http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBrowserMutationOrigins(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, method, origin, site, referer, authorization string
|
||||
cookie, allow bool
|
||||
}{
|
||||
{"same_origin", "POST", "https://wiki.example", "same-origin", "", "", true, true},
|
||||
{"legacy_same_origin", "POST", "https://wiki.example", "", "", "", true, true},
|
||||
{"legacy_cross_scheme", "POST", "http://wiki.example", "", "", "", true, false},
|
||||
{"legacy_cross_scheme_login", "POST", "http://wiki.example", "", "", "", false, false},
|
||||
{"origin_is_not_url_path", "POST", "https://wiki.example/path", "", "", "", true, false},
|
||||
{"foreign_origin", "POST", "https://evil.example", "cross-site", "", "", true, false},
|
||||
{"sibling_is_not_trusted", "POST", "https://sibling.wiki.example", "same-site", "", "", true, false},
|
||||
{"null_origin", "POST", "null", "", "", "", true, false},
|
||||
{"no_origin_cookie", "POST", "", "", "", "", true, false},
|
||||
{"same_origin_metadata", "POST", "", "same-origin", "", "", true, true},
|
||||
{"old_same_origin_referrer", "POST", "", "", "https://wiki.example/form", "", true, true},
|
||||
{"foreign_referrer", "POST", "", "", "https://evil.example/form", "", true, false},
|
||||
{"cli_bearer", "POST", "", "", "", "Bearer explicit", false, true},
|
||||
{"explicit_bearer_with_cookie", "POST", "", "", "", "Bearer explicit", true, true},
|
||||
{"cross_origin_login", "POST", "https://evil.example", "cross-site", "", "", false, false},
|
||||
{"machine_enrollment", "POST", "", "", "", "", false, true},
|
||||
{"safe_read", "GET", "https://evil.example", "cross-site", "", "", true, true},
|
||||
{"cross_origin_delete", "DELETE", "https://evil.example", "cross-site", "", "", true, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := httptest.NewRequest(tc.method, "https://wiki.example/_/auth/login", nil)
|
||||
r.Header.Set("Origin", tc.origin)
|
||||
r.Header.Set("Sec-Fetch-Site", tc.site)
|
||||
r.Header.Set("Referer", tc.referer)
|
||||
if tc.authorization != "" {
|
||||
r.Header.Set("Authorization", tc.authorization)
|
||||
}
|
||||
if tc.cookie {
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "browser"})
|
||||
}
|
||||
called := false
|
||||
w := httptest.NewRecorder()
|
||||
(&Server{}).withAuthorization(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { called = true })).ServeHTTP(w, r)
|
||||
if called != tc.allow || (!tc.allow && w.Code != http.StatusForbidden) {
|
||||
t.Fatalf("allowed=%v status=%d, want %v", called, w.Code, tc.allow)
|
||||
}
|
||||
})
|
||||
}
|
||||
// Handler-owned JSON routes receive a parseable denial, before DB access.
|
||||
r := httptest.NewRequest("POST", "https://wiki.example/_/api/v1/assets", nil)
|
||||
r.Header.Set("Origin", "https://evil.example")
|
||||
w := httptest.NewRecorder()
|
||||
(&Server{}).withAuthorization(http.NotFoundHandler()).ServeHTTP(w, r)
|
||||
var result map[string]string
|
||||
if w.Code != 403 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result["error"] == "" {
|
||||
t.Fatalf("invalid CSRF JSON: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitCredentialNeverFallsBackToCookie(t *testing.T) {
|
||||
s := &Server{}
|
||||
for _, tc := range []struct{ header, want string }{
|
||||
{"Basic secret", ""}, {"Bearer", ""}, {"Bearer a b", ""}, {"Bearer a,b", ""}, {"bearer api-token", "api-token"},
|
||||
} {
|
||||
r := httptest.NewRequest("GET", "http://localhost/", nil)
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "cookie-token"})
|
||||
r.Header.Set("Authorization", tc.header)
|
||||
if got := s.authTokenFromRequest(r); got != tc.want {
|
||||
t.Fatalf("header=%q got=%q want=%q", tc.header, got, tc.want)
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/", nil)
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "one"})
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: "two"})
|
||||
if s.authTokenFromRequest(r) != "" {
|
||||
t.Fatal("ambiguous cookies accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRequiresPostAndRevokesSelectedCredential(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
||||
principal, err := s.store.PrincipalForSession(domain, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cookieToken, err := s.store.CreateBrowserAuthSession(domain, principal.UUID, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := func(method, origin, bearer string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "http://localhost/_/auth/logout", nil)
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: cookieToken})
|
||||
if origin != "" {
|
||||
r.Header.Set("Origin", origin)
|
||||
}
|
||||
if bearer != "" {
|
||||
r.Header.Set("Authorization", "Bearer "+bearer)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
if w := request("GET", "", ""); w.Code != 200 {
|
||||
t.Fatalf("confirmation: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, cookieToken); err != nil {
|
||||
t.Fatal("GET revoked session")
|
||||
}
|
||||
if w := request("POST", "http://evil.example", ""); w.Code != 403 {
|
||||
t.Fatalf("CSRF logout accepted: %d", w.Code)
|
||||
}
|
||||
if w := request("POST", "", token); w.Code != 200 {
|
||||
t.Fatalf("bearer logout: %d", w.Code)
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, token); err == nil {
|
||||
t.Fatal("Bearer token survived logout")
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, cookieToken); err != nil {
|
||||
t.Fatal("Bearer logout revoked unrelated cookie credential")
|
||||
}
|
||||
if w := request("POST", "http://localhost", ""); w.Code != 303 {
|
||||
t.Fatalf("cookie logout: %d", w.Code)
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, cookieToken); err == nil {
|
||||
t.Fatal("cookie token survived logout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnPathStaysLocal(t *testing.T) {
|
||||
for _, value := range []string{"https://evil.example", "//evil.example", "/%2fevil.example", "/\\evil.example", "/%00bad"} {
|
||||
if got := safeReturnPath(value); got != "/" {
|
||||
t.Fatalf("unsafe return %q -> %q", value, got)
|
||||
}
|
||||
}
|
||||
if got := safeReturnPath("/life/home?q=1"); got != "/life/home?q=1" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionPurposeAndEditorNavigation(t *testing.T) {
|
||||
s, apiToken, domain := configFixture(t)
|
||||
principal, err := s.store.PrincipalForSession(domain, apiToken)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
browserToken, err := s.store.CreateBrowserAuthSession(domain, principal.UUID, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
token, transport string
|
||||
allowed bool
|
||||
}{
|
||||
{apiToken, "bearer", true}, {apiToken, "cookie", false},
|
||||
{browserToken, "bearer", false}, {browserToken, "cookie", true},
|
||||
} {
|
||||
r := httptest.NewRequest("GET", "http://localhost/", nil)
|
||||
if tc.transport == "bearer" {
|
||||
r.Header.Set("Authorization", "Bearer "+tc.token)
|
||||
} else {
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: tc.token})
|
||||
}
|
||||
_, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
|
||||
if (err == nil) != tc.allowed {
|
||||
t.Fatalf("%s purpose mismatch: allowed=%v err=%v", tc.transport, tc.allowed, err)
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest("GET", "https://wiki.example/e/new-page", nil)
|
||||
r.AddCookie(&http.Cookie{Name: authCookieName, Value: browserToken})
|
||||
r.Header.Set("Sec-Fetch-Site", "cross-site")
|
||||
if validBrowserMutationOrigin(r) {
|
||||
t.Fatal("legacy editor GET accepted cross-site")
|
||||
}
|
||||
r.Header.Set("Sec-Fetch-Site", "none")
|
||||
r.Header.Set("Sec-Fetch-Mode", "navigate")
|
||||
if !validBrowserMutationOrigin(r) {
|
||||
t.Fatal("address-bar editor navigation rejected")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
// calendar-access normally asserts RFC 4791 conformance, which our limited
|
||||
// implementation does not provide. Only advertise it for an explicitly opted-in
|
||||
// compatibility credential (e.g. OneTask's OPTIONS gate); never infer from UA.
|
||||
func (s *Server) setDAVCapabilities(w http.ResponseWriter, r *http.Request, actor, credential string) {
|
||||
w.Header().Set("DAV", "1")
|
||||
if credential == "" {
|
||||
return
|
||||
}
|
||||
if enabled, err := s.store.DAVCalendarAccessCompat(r.Context(), s.domainUUIDForRequest(r), actor, credential); err == nil && enabled {
|
||||
w.Header().Set("DAV", "1, calendar-access")
|
||||
w.Header().Set("X-PagesWiki-CalDAV-Profile", "limited-compatibility")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// Reproduce the reported OPTIONS gate, not a claim to execute the OneTask app.
|
||||
// Never use real device credentials or imported calendar data in this fixture.
|
||||
func TestDAVCalendarAccessCompatibility(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
repo, err := s.calendars()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := repo.Create(ctx, domain, actor, "Synthetic calendar")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := func(auth, method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
|
||||
if auth != "" {
|
||||
r.Header.Set("Authorization", auth)
|
||||
}
|
||||
r.Header.Set("Depth", "1")
|
||||
r.Header.Set("Content-Type", "text/calendar")
|
||||
for k, v := range h {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
|
||||
}
|
||||
return w
|
||||
}
|
||||
issue := func(scope string, write, compat bool) (string, string) {
|
||||
t.Helper()
|
||||
b, _ := json.Marshal(map[string]any{"label": "Compatibility fixture", "scope": scope, "write": write, "calendar_access_compat": compat})
|
||||
w := request("Bearer "+token, "POST", davCredentialsAPI, string(b), nil, 201)
|
||||
var result struct {
|
||||
Username, Password string
|
||||
Credential store.TaskDAVCredential
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Credential.CalendarAccessCompat != compat {
|
||||
t.Fatal("compatibility not persisted")
|
||||
}
|
||||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(result.Username+":"+result.Password)), result.Username
|
||||
}
|
||||
strict, _ := issue("all", true, false)
|
||||
compat, id := issue("all", true, true)
|
||||
readOnly, _ := issue("tasks", false, true)
|
||||
paths := []string{calendarDAV, strings.TrimSuffix(calendarDAV, "/"), taskDAVRoot, strings.TrimSuffix(taskDAVRoot, "/"), taskDAVCollection, strings.TrimSuffix(taskDAVCollection, "/"), calendarDAV + c.ID + "/", calendarDAV + c.ID}
|
||||
for _, p := range paths {
|
||||
for _, auth := range []string{strict, "Bearer " + token} {
|
||||
w := request(auth, "OPTIONS", p, "", map[string]string{"User-Agent": "OneTask"}, 204)
|
||||
if w.Header().Get("DAV") != "1" || w.Header().Get("X-PagesWiki-CalDAV-Profile") != "" {
|
||||
t.Fatal("UA or session enabled compatibility", w.Header())
|
||||
}
|
||||
}
|
||||
w := request(compat, "OPTIONS", p, "", nil, 204)
|
||||
if w.Header().Get("DAV") != "1, calendar-access" || w.Header().Get("X-PagesWiki-CalDAV-Profile") != "limited-compatibility" || w.Header().Get("Location") != "" {
|
||||
t.Fatal(p, w.Header())
|
||||
}
|
||||
if strings.Contains(w.Header().Get("DAV"), "calendar-schedule") || strings.Contains(w.Header().Get("Allow"), "MKCALENDAR") {
|
||||
t.Fatal("unimplemented capability advertised")
|
||||
}
|
||||
}
|
||||
// Follow the accepted gate through authenticated discovery, fetch and writes.
|
||||
home := request(compat, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
|
||||
if !strings.Contains(home.Body.String(), "current-user-principal") || !strings.Contains(home.Body.String(), taskDAVCollection) {
|
||||
t.Fatal(home.Body)
|
||||
}
|
||||
col := request(readOnly, "PROPFIND", taskDAVCollection, commonDAVProps, map[string]string{"Depth": "0"}, 207)
|
||||
if !strings.Contains(col.Body.String(), "current-user-privilege-set") || strings.Contains(col.Body.String(), "<d:write/>") {
|
||||
t.Fatal("readonly privileges", col.Body)
|
||||
}
|
||||
col = request(compat, "PROPFIND", taskDAVCollection, commonDAVProps, map[string]string{"Depth": "0"}, 207)
|
||||
if !strings.Contains(col.Body.String(), "<d:write/>") {
|
||||
t.Fatal("missing write privilege", col.Body)
|
||||
}
|
||||
p := taskDAVCollection + "compat.ics"
|
||||
request(compat, "PUT", p, taskICALFixture, nil, 201)
|
||||
get := request(compat, "GET", p, "", nil, 200)
|
||||
request(compat, "PUT", p, taskICALFixture, nil, 204)
|
||||
get = request(compat, "GET", p, "", nil, 200)
|
||||
request(readOnly, "PUT", p, taskICALFixture, map[string]string{"If-Match": get.Header().Get("ETag")}, 403)
|
||||
changed := strings.Replace(taskICALFixture, "SUMMARY:CLI task", "SUMMARY:Compatibility update", 1)
|
||||
request(compat, "PUT", p, changed, map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
|
||||
query := strings.Replace(commonDAVQuery, `<c:comp-filter name="VCALENDAR"/>`, `<c:comp-filter name="VCALENDAR"><c:comp-filter name="VTODO"/></c:comp-filter>`, 1)
|
||||
result := request(compat, "REPORT", taskDAVCollection, query, nil, 207)
|
||||
if !strings.Contains(result.Body.String(), "Compatibility update") {
|
||||
t.Fatal(result.Body)
|
||||
}
|
||||
// Compatibility never strips unsupported recurrence to make a write succeed.
|
||||
bad := strings.Replace(changed, "END:VTODO", "RRULE:FREQ=DAILY\r\nEND:VTODO", 1)
|
||||
get = request(compat, "GET", p, "", nil, 200)
|
||||
request(compat, "PUT", p, bad, map[string]string{"If-Match": get.Header().Get("ETag")}, 403)
|
||||
request(compat, "DELETE", p, "", map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
|
||||
request(readOnly, "OPTIONS", calendarDAV+c.ID+"/", "", nil, 401)
|
||||
request(compat, "POST", davCredentialsAPI, `{"label":"escalate","calendar_access_compat":true}`, nil, 401)
|
||||
// Another principal/domain cannot pick up this credential's compatibility.
|
||||
if enabled, _ := s.store.DAVCalendarAccessCompat(ctx, domain, "someone-else", id); enabled {
|
||||
t.Fatal("cross-owner flag leaked")
|
||||
}
|
||||
if enabled, _ := s.store.DAVCalendarAccessCompat(ctx, "other-domain", actor, id); enabled {
|
||||
t.Fatal("cross-domain flag leaked")
|
||||
}
|
||||
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := request(compat, "OPTIONS", calendarDAV, "", nil, 401)
|
||||
if w.Header().Get("DAV") != "" {
|
||||
t.Fatal("revoked credential advertised support")
|
||||
}
|
||||
var count int
|
||||
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_dav_compat WHERE credential_id=?`, id).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("orphan compatibility row", count, err)
|
||||
}
|
||||
expired, expiredID := issue("all", true, true)
|
||||
if _, err = s.store.DB.Exec(`UPDATE organizer_dav_credentials SET expires_ms=? WHERE id=?`, time.Now().Add(-time.Minute).UnixMilli(), expiredID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request(expired, "OPTIONS", calendarDAV, "", nil, 401)
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
)
|
||||
|
||||
const commonDAVProps = `<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:current-user-principal/><c:calendar-home-set/><d:resourcetype/><d:getetag/><d:displayname/><d:current-user-privilege-set/></d:prop></d:propfind>`
|
||||
const commonDAVQuery = `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data><c:comp name="VCALENDAR"><c:allprop/><c:allcomp/></c:comp></c:calendar-data></d:prop><c:filter><c:comp-filter name="VCALENDAR"/></c:filter></c:calendar-query>`
|
||||
|
||||
func TestCommonDAVScopesAndCalendarWrites(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
repo, err := s.calendars()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := repo.Create(ctx, domain, actor, "Common fixture")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
col := calendarDAV + c.ID + "/"
|
||||
item, err := calendar.Build("", calendar.Event{UID: "common@example.test", Summary: "Synthetic event", Start: time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
makeAuth := func(scope string, write bool) (string, string) {
|
||||
t.Helper()
|
||||
c, p, e := s.store.CreateDAVCredential(ctx, domain, actor, scope, write, 1, scope)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+p)), c.ID
|
||||
}
|
||||
taskAuth, legacyID := makeAuth("tasks", true)
|
||||
// Simulate a credential from before scope storage existed.
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM organizer_dav_scopes WHERE credential_id=?`, legacyID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calAuth, _ := makeAuth("calendar", true)
|
||||
allAuth, allID := makeAuth("all", true)
|
||||
readAuth, _ := makeAuth("all", false)
|
||||
req := func(auth, method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
|
||||
r.Header.Set("Authorization", auth)
|
||||
r.Header.Set("Depth", "1")
|
||||
r.Header.Set("Content-Type", "text/calendar")
|
||||
for k, v := range h {
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
|
||||
}
|
||||
return w
|
||||
}
|
||||
for _, p := range []string{calendarDAV, strings.TrimSuffix(calendarDAV, "/")} {
|
||||
for _, test := range []struct {
|
||||
auth string
|
||||
tasks, cal bool
|
||||
}{{taskAuth, true, false}, {calAuth, false, true}, {allAuth, true, true}} {
|
||||
w := req(test.auth, "PROPFIND", p, commonDAVProps, nil, 207)
|
||||
if strings.Contains(w.Body.String(), taskDAVCollection) != test.tasks || strings.Contains(w.Body.String(), col) != test.cal || w.Header().Get("Location") != "" {
|
||||
t.Fatal(w.Body)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "current-user-principal") {
|
||||
t.Fatal(w.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
req(taskAuth, "PROPFIND", col, commonDAVProps, nil, 401)
|
||||
req(calAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 401)
|
||||
req(allAuth, "GET", davCredentialsAPI, "", nil, 401)
|
||||
req(allAuth, "POST", davCredentialsAPI, `{"label":"escalate","scope":"all"}`, nil, 401)
|
||||
unauth := req(allAuth, "GET", calendarAPI+"calendars", "", nil, 200)
|
||||
if !strings.Contains(unauth.Body.String(), `"actor":""`) || strings.Contains(unauth.Body.String(), c.ID) {
|
||||
t.Fatal("DAV credential escaped to JSON API", unauth.Body)
|
||||
}
|
||||
path := col + "arbitrary-client-name.ics"
|
||||
req(readAuth, "PUT", path, item.Data, nil, 403)
|
||||
req(calAuth, "PUT", path, item.Data, nil, 201)
|
||||
w := req(allAuth, "GET", path, "", nil, 200)
|
||||
tag := w.Header().Get("ETag")
|
||||
req(allAuth, "GET", path, "", map[string]string{"If-None-Match": tag}, 304)
|
||||
req(allAuth, "PUT", path, item.Data, nil, 428)
|
||||
req(allAuth, "PUT", col+"duplicate.ics", item.Data, nil, 412)
|
||||
req(allAuth, "PUT", path, item.Data, map[string]string{"If-Match": `"stale"`}, 412)
|
||||
updated, err := calendar.Build(item.Data, calendar.Event{UID: item.UID, Summary: "Edited through CLI", Start: item.Start, End: item.End})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req(allAuth, "PUT", path, updated.Data, map[string]string{"If-Match": tag}, 204)
|
||||
for _, method := range []string{"PROPFIND", "REPORT"} {
|
||||
body := commonDAVProps
|
||||
if method == "REPORT" {
|
||||
body = commonDAVQuery
|
||||
}
|
||||
w = req(allAuth, method, col, body, nil, 207)
|
||||
if !strings.Contains(w.Body.String(), path) {
|
||||
t.Fatal("lost client filename", w.Body)
|
||||
}
|
||||
}
|
||||
// Reject a partial filter instead of silently returning the entire collection.
|
||||
bad := strings.Replace(commonDAVQuery, `name="VCALENDAR"`, `name="VCALENDAR" test="anyof"`, 1)
|
||||
req(allAuth, "REPORT", col, bad, nil, 403)
|
||||
// Canonical URI used by the existing Web UI still resolves after CLI creation.
|
||||
req("Bearer "+token, "GET", calendarHref(c.ID, item.UID), "", nil, 200)
|
||||
projected := req("Bearer "+token, "GET", calendarAPI+"calendars/"+c.ID+"/events?resource=arbitrary-client-name.ics", "", nil, 200)
|
||||
if !strings.Contains(projected.Body.String(), "Edited through CLI") {
|
||||
t.Fatal("Web viewer projection failed", projected.Body)
|
||||
}
|
||||
// ACL revocation and membership checks are independent of the secret's scope.
|
||||
if _, err = s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req(allAuth, "GET", path, "", nil, 401)
|
||||
s.store.DB.Exec(`UPDATE principals SET state='active' WHERE uuid=?`, actor)
|
||||
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, allID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req(allAuth, "GET", path, "", nil, 401)
|
||||
// Permission recheck inside the calendar's locked write path.
|
||||
_, _, err = repo.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary-client-name.ics", updated.ETag, &updated, func() error { return s.store.CheckDAVCredential(ctx, domain, actor, allID, true, "calendar") })
|
||||
if err != calendar.ErrForbidden {
|
||||
t.Fatal("revoked credential wrote", err)
|
||||
}
|
||||
req(calAuth, "DELETE", path, "", map[string]string{"If-Match": updated.ETag}, 204)
|
||||
req(calAuth, "GET", path, "", nil, 404)
|
||||
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.calendar":false}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = req(taskAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
|
||||
if strings.Contains(w.Body.String(), col) || !strings.Contains(w.Body.String(), taskDAVCollection) {
|
||||
t.Fatal("disabled calendar exposed", w.Body)
|
||||
}
|
||||
req(calAuth, "PROPFIND", col, commonDAVProps, nil, 404)
|
||||
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w = req(calAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
|
||||
if strings.Contains(w.Body.String(), taskDAVCollection) || !strings.Contains(w.Body.String(), col) {
|
||||
t.Fatal("disabled tasks exposed", w.Body)
|
||||
}
|
||||
req(taskAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 404)
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
mw "pageswiki/internal/server/middleware"
|
||||
)
|
||||
|
||||
func (s *Server) davActor(w http.ResponseWriter, r *http.Request, requested string, anonymous bool) (actor string, write bool, scope, credential string, ok bool) {
|
||||
scope = "all"
|
||||
if len(r.Header.Values("Authorization")) > 1 {
|
||||
mw.SetDAVAuthOutcome(r, "multiple_authorization")
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
if id, secret, basic := r.BasicAuth(); basic {
|
||||
var err error
|
||||
actor, write, scope, err = s.store.AuthenticateDAV(r.Context(), s.domainUUIDForRequest(r), id, secret, requested)
|
||||
if err == nil {
|
||||
mw.SetDAVAuthOutcome(r, "basic_accepted")
|
||||
return actor, write, scope, id, true
|
||||
}
|
||||
mw.SetDAVAuthOutcome(r, "basic_rejected")
|
||||
} else {
|
||||
actor, write = s.calendarActor(r)
|
||||
if actor != "" || anonymous && r.Header.Get("Authorization") == "" {
|
||||
if actor != "" {
|
||||
mw.SetDAVAuthOutcome(r, "session_accepted")
|
||||
} else {
|
||||
mw.SetDAVAuthOutcome(r, "anonymous")
|
||||
}
|
||||
return actor, write, scope, "", true
|
||||
}
|
||||
if r.Header.Get("Authorization") == "" {
|
||||
mw.SetDAVAuthOutcome(r, "missing")
|
||||
} else {
|
||||
mw.SetDAVAuthOutcome(r, "invalid_authorization")
|
||||
}
|
||||
}
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="PagesWiki CalDAV", charset="UTF-8"`)
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) handleDAVHome(w http.ResponseWriter, r *http.Request) {
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
tasks := s.systemExtensionEnabled(domain, "organizer.tasks")
|
||||
calendars := s.systemExtensionEnabled(domain, "organizer.calendar")
|
||||
if !tasks && !calendars {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
actor, writable, scope, credential, ok := s.davActor(w, r, "", false)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
s.setDAVCapabilities(w, r, actor, credential)
|
||||
w.Header().Set("Allow", "OPTIONS, PROPFIND")
|
||||
if r.Method == "OPTIONS" {
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
if r.Method != "PROPFIND" {
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if depth := r.Header.Get("Depth"); depth != "0" && depth != "1" {
|
||||
http.Error(w, "Depth 0 or 1 required", 403)
|
||||
return
|
||||
}
|
||||
n, err := readDAV(w, r)
|
||||
if err != nil || n.XMLName.Local != "" && n.XMLName != (xml.Name{Space: davNS, Local: "propfind"}) {
|
||||
http.Error(w, "invalid PROPFIND", 400)
|
||||
return
|
||||
}
|
||||
var req []davNode
|
||||
if p := n.child(davNS, "prop"); p != nil {
|
||||
req = p.Children
|
||||
}
|
||||
rows := []davResponse{{href: calendarDAV, props: map[xml.Name]string{
|
||||
{Space: davNS, Local: "resourcetype"}: "<d:collection/><d:principal/>",
|
||||
{Space: davNS, Local: "displayname"}: "PagesWiki CalDAV",
|
||||
{Space: davNS, Local: "current-user-principal"}: "<d:href>" + calendarDAV + "</d:href>",
|
||||
{Space: davNS, Local: "principal-URL"}: "<d:href>" + calendarDAV + "</d:href>",
|
||||
{Space: calNS, Local: "calendar-home-set"}: "<d:href>" + calendarDAV + "</d:href>",
|
||||
}}}
|
||||
rows[0].props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(false)
|
||||
if writable && tasks && scope != "calendar" {
|
||||
rows[0].props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] += "<d:privilege><d:bind/></d:privilege>"
|
||||
}
|
||||
if r.Header.Get("Depth") == "1" {
|
||||
if calendars && scope != "tasks" {
|
||||
repo, err := s.calendars()
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
items, err := repo.List(domain, actor)
|
||||
if err != nil {
|
||||
calendarError(w, err)
|
||||
return
|
||||
}
|
||||
for _, c := range items {
|
||||
row := calendarDAVRow(c)
|
||||
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(writable && c.CanWrite(actor))
|
||||
rows = append(rows, row)
|
||||
}
|
||||
}
|
||||
if tasks && scope != "calendar" {
|
||||
row := taskDAVCollectionRow()
|
||||
title, err := s.store.TaskDAVDefaultTitle(r.Context(), domain, actor)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
row.props[xml.Name{Space: davNS, Local: "displayname"}] = xmlText(title)
|
||||
row.props[xml.Name{Space: davNS, Local: "current-user-privilege-set"}] = davPrivileges(writable)
|
||||
rows = append(rows, row)
|
||||
if !s.appendTaskDAVCollections(w, r, domain, actor, writable, &rows) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
writeDAV(w, rows, req)
|
||||
}
|
||||
|
||||
func davPrivileges(write bool) string {
|
||||
value := "<d:privilege><d:read/></d:privilege>"
|
||||
if write {
|
||||
value += "<d:privilege><d:write/></d:privilege>"
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (s *Server) handleDAVCredentials(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
if !s.systemExtensionEnabled(domain, "organizer.tasks") && !s.systemExtensionEnabled(domain, "organizer.calendar") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
actor, _ := s.calendarActor(r) // Basic DAV secrets cannot issue/revoke credentials.
|
||||
if actor == "" {
|
||||
s.authRequired(w, r)
|
||||
return
|
||||
}
|
||||
parts := []string{"dav-credentials"}
|
||||
if r.URL.Path != davCredentialsAPI {
|
||||
parts = append(parts, strings.Split(strings.TrimPrefix(r.URL.Path, davCredentialsAPI+"/"), "/")...)
|
||||
}
|
||||
s.handleTaskDAVCredentials(w, r, actor, parts)
|
||||
}
|
||||
@@ -4,21 +4,47 @@ import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var diagramSourceCache = map[string]diagramCacheEntry{}
|
||||
var diagramSourceMu sync.Mutex
|
||||
|
||||
type diagramCacheEntry struct {
|
||||
Type string
|
||||
Source string
|
||||
At time.Time
|
||||
Domain string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (s *Server) diagramCachePath(kind, hash string) string {
|
||||
root := s.diagramRoot
|
||||
if root == "" {
|
||||
root = "/data/cache/diagram"
|
||||
}
|
||||
return filepath.Join(root, kind, hash+".svg")
|
||||
}
|
||||
|
||||
func readDiagramCache(path string) ([]byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
b, err := io.ReadAll(io.LimitReader(f, (4<<20)+1))
|
||||
if len(b) > 4<<20 {
|
||||
return nil, errors.New("diagram cache exceeds limit")
|
||||
}
|
||||
return b, err
|
||||
}
|
||||
|
||||
func diagramHash(
|
||||
@@ -81,6 +107,8 @@ func krokiURL() string {
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramsInHTML(
|
||||
r *http.Request,
|
||||
path string,
|
||||
html string,
|
||||
) string {
|
||||
|
||||
@@ -109,20 +137,35 @@ func (s *Server) renderDiagramsInHTML(
|
||||
htmlUnescapeString(
|
||||
m[2],
|
||||
)
|
||||
if len(source) > 32<<10 || path == "" {
|
||||
return block
|
||||
}
|
||||
|
||||
hash :=
|
||||
diagramHash(
|
||||
diagramType,
|
||||
source,
|
||||
htmlProjectionVersion+"\x00"+s.domainUUIDForRequest(r)+"\x00"+path+"\x00"+source,
|
||||
"svg",
|
||||
)
|
||||
|
||||
diagramSourceMu.Lock()
|
||||
if len(diagramSourceCache) >= 128 {
|
||||
oldest, key := time.Now(), ""
|
||||
for k, v := range diagramSourceCache {
|
||||
if v.At.Before(oldest) {
|
||||
oldest, key = v.At, k
|
||||
}
|
||||
}
|
||||
delete(diagramSourceCache, key)
|
||||
}
|
||||
diagramSourceCache[hash] =
|
||||
diagramCacheEntry{
|
||||
Type: diagramType,
|
||||
Source: source,
|
||||
At: time.Now(),
|
||||
Domain: s.domainUUIDForRequest(r), Path: path,
|
||||
}
|
||||
diagramSourceMu.Unlock()
|
||||
|
||||
return `<figure class="diagram diagram-` + diagramType + `">` +
|
||||
`<img src="/:/compile/diagram/` + diagramType + `/` + hash + `.svg" loading="lazy" alt="` + diagramType + ` diagram">` +
|
||||
@@ -153,6 +196,10 @@ func (s *Server) handleCompileDiagram(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
// Apply also to disk-cache hits and direct navigation, not only new renders.
|
||||
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
s.handleCompileDiagramPost(
|
||||
@@ -208,14 +255,16 @@ func (s *Server) handleCompileDiagram(
|
||||
return
|
||||
}
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
cachePath := s.diagramCachePath(diagramType, hash)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
diagramSourceMu.Lock()
|
||||
entry, ok := diagramSourceCache[hash]
|
||||
diagramSourceMu.Unlock()
|
||||
if !ok || entry.Type != diagramType || entry.Domain != s.domainUUIDForRequest(r) || time.Since(entry.At) > 30*time.Minute || !s.pathReadFilter(r)(entry.Path) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if b, err := readDiagramCache(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
@@ -226,16 +275,9 @@ func (s *Server) handleCompileDiagram(
|
||||
return
|
||||
}
|
||||
|
||||
entry, ok :=
|
||||
diagramSourceCache[hash]
|
||||
|
||||
if !ok || entry.Type != diagramType {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
r,
|
||||
diagramType,
|
||||
entry.Source,
|
||||
cachePath,
|
||||
@@ -269,12 +311,12 @@ func (s *Server) handleCompileDiagramPost(
|
||||
io.ReadAll(
|
||||
io.LimitReader(
|
||||
r.Body,
|
||||
1024*1024,
|
||||
(1<<20)+1,
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if err != nil || len(sourceBytes) > 1<<20 {
|
||||
http.Error(w, "diagram source exceeds limit", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -288,14 +330,9 @@ func (s *Server) handleCompileDiagramPost(
|
||||
"svg",
|
||||
)
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
cachePath := s.diagramCachePath(diagramType, hash)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
if b, err := readDiagramCache(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
@@ -308,6 +345,7 @@ func (s *Server) handleCompileDiagramPost(
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
r,
|
||||
diagramType,
|
||||
source,
|
||||
cachePath,
|
||||
@@ -316,13 +354,15 @@ func (s *Server) handleCompileDiagramPost(
|
||||
|
||||
func (s *Server) renderDiagramSVG(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
diagramType string,
|
||||
source string,
|
||||
cachePath string,
|
||||
) {
|
||||
|
||||
req, err :=
|
||||
http.NewRequest(
|
||||
http.NewRequestWithContext(
|
||||
r.Context(),
|
||||
http.MethodPost,
|
||||
krokiURL()+"/"+diagramType+"/svg",
|
||||
bytes.NewBufferString(source),
|
||||
@@ -364,11 +404,11 @@ func (s *Server) renderDiagramSVG(
|
||||
|
||||
b, err :=
|
||||
io.ReadAll(
|
||||
resp.Body,
|
||||
io.LimitReader(resp.Body, (4<<20)+1),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if err != nil || len(b) > 4<<20 {
|
||||
http.Error(w, "diagram output unavailable or exceeds limit", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -392,7 +432,7 @@ func (s *Server) renderDiagramSVG(
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Security-Policy",
|
||||
"default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
)
|
||||
|
||||
_, _ =
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDiagramProjectionCacheIsolation(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.diagramRoot = t.TempDir()
|
||||
s.mux.HandleFunc("/:/compile/diagram/", s.handleCompileDiagram)
|
||||
var renders atomic.Int32
|
||||
renderer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
renders.Add(1)
|
||||
_, _ = io.WriteString(w, `<svg xmlns="http://www.w3.org/2000/svg"><script>SVG_SENTINEL</script><text>diagram</text></svg>`)
|
||||
}))
|
||||
defer renderer.Close()
|
||||
t.Setenv("KROKI_URL", renderer.URL)
|
||||
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r = cacheRequestDomain(withReadPolicyContext(r))
|
||||
input := `<pre><code class="language-mermaid">graph LR; A-->B;</code></pre>`
|
||||
out := s.safeHTMLProjection(r, input, "/guide", true)
|
||||
m := regexp.MustCompile(`src="([^"]+\.svg)"`).FindStringSubmatch(out)
|
||||
if len(m) != 2 {
|
||||
t.Fatalf("diagram formatting lost: %s", out)
|
||||
}
|
||||
other := s.safeHTMLProjection(r, input, "/other", true)
|
||||
if strings.Contains(other, m[1]) {
|
||||
t.Fatal("identical diagrams at different Paths share authority references")
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
w := configRequest(s, token, "GET", m[1], nil)
|
||||
if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Security-Policy"), "sandbox;") || w.Header().Get("Cache-Control") != "private, no-store" {
|
||||
t.Fatalf("diagram/cache=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if renders.Load() != 1 {
|
||||
t.Fatalf("cache missed: %d", renders.Load())
|
||||
}
|
||||
// Even a populated on-disk cache cannot bypass the requesting reader's
|
||||
// Path grant or a mismatching Domain provenance record.
|
||||
reader, err := s.store.CreatePasswordAccount(domain, "diagram-reader", "a long reader password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readerToken, err := s.store.CreateAuthSession(domain, reader.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var role string
|
||||
if err = s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.principal_uuid=? AND m.domain_uuid=?`, reader.UUID, domain).Scan(&role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
denied := httptest.NewRequest("GET", "http://localhost"+m[1], nil)
|
||||
denied.Header.Set("Authorization", "Bearer "+readerToken)
|
||||
s.handleCompileDiagram(w, denied)
|
||||
if w.Code != 404 {
|
||||
t.Fatalf("cached hidden diagram=%d", w.Code)
|
||||
}
|
||||
hash := strings.TrimSuffix(m[1][strings.LastIndex(m[1], "/")+1:], ".svg")
|
||||
diagramSourceMu.Lock()
|
||||
entry := diagramSourceCache[hash]
|
||||
entry.Domain = "foreign"
|
||||
diagramSourceCache[hash] = entry
|
||||
diagramSourceMu.Unlock()
|
||||
w = httptest.NewRecorder()
|
||||
s.handleCompileDiagram(w, httptest.NewRequest("GET", "http://localhost"+m[1], nil))
|
||||
if w.Code != 404 {
|
||||
t.Fatal("foreign cached diagram exposed")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); _ = s.safeHTMLProjection(r, input, "/guide", true) }()
|
||||
}
|
||||
wg.Wait()
|
||||
if strings.Contains(s.safeHTMLProjection(r, `<pre><code class="language-mermaid">`+strings.Repeat("A", 33<<10)+`</code></pre>`, "/guide", true), "/:/compile/diagram/") {
|
||||
t.Fatal("oversized diagram enqueued")
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) currentDistrict() *store.District {
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDefaultDistrict()
|
||||
|
||||
if err != nil {
|
||||
return &store.District{
|
||||
ID: 1,
|
||||
Name: "default",
|
||||
Title: "Default District",
|
||||
Enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
return sp
|
||||
}
|
||||
|
||||
func (s *Server) districtForRequest(
|
||||
r *http.Request,
|
||||
) *store.District {
|
||||
|
||||
if r == nil {
|
||||
return s.currentDistrict()
|
||||
}
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDistrictByHost(
|
||||
r.Host,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return s.currentDistrict()
|
||||
}
|
||||
|
||||
return sp
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package server
|
||||
|
||||
func (s *Server) getDomainConfig(key string, domainUUID ...string) string {
|
||||
var (
|
||||
value string
|
||||
err error
|
||||
)
|
||||
if len(domainUUID) > 0 && domainUUID[0] != "" {
|
||||
value, err = s.store.GetConfigInDomain(domainUUID[0], key)
|
||||
} else {
|
||||
value, err = s.store.GetConfig(key)
|
||||
}
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type requestDomainKey struct{}
|
||||
type requestDomainLookup struct {
|
||||
once sync.Once
|
||||
host string
|
||||
domain *store.Domain
|
||||
err error
|
||||
}
|
||||
|
||||
func cacheRequestDomain(r *http.Request) *http.Request {
|
||||
return r.WithContext(context.WithValue(r.Context(), requestDomainKey{}, &requestDomainLookup{host: r.Host}))
|
||||
}
|
||||
|
||||
func (s *Server) currentDomain() *store.Domain {
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDefaultDomain()
|
||||
|
||||
if err != nil {
|
||||
return &store.Domain{
|
||||
ID: 1,
|
||||
Name: "default",
|
||||
Title: "Default Domain",
|
||||
Enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
return sp
|
||||
}
|
||||
|
||||
func (s *Server) currentCampus() *store.Campus {
|
||||
campus, err :=
|
||||
s.store.GetDefaultCampus()
|
||||
|
||||
if err != nil {
|
||||
return &store.Campus{
|
||||
ID: 1,
|
||||
Name: "default",
|
||||
Title: "Default Campus",
|
||||
DefaultLang: "en",
|
||||
Enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
return campus
|
||||
}
|
||||
|
||||
func (s *Server) campusForDomain(
|
||||
domain *store.Domain,
|
||||
) *store.Campus {
|
||||
if domain == nil {
|
||||
return s.currentCampus()
|
||||
}
|
||||
|
||||
campus, err :=
|
||||
s.store.GetCampusByUUID(
|
||||
domain.CampusUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return s.currentCampus()
|
||||
}
|
||||
|
||||
return campus
|
||||
}
|
||||
|
||||
func (s *Server) domainForRequest(
|
||||
r *http.Request,
|
||||
) *store.Domain {
|
||||
domain, _ := s.domainForRequestWithError(r)
|
||||
return domain
|
||||
}
|
||||
|
||||
func (s *Server) domainForRequestWithError(
|
||||
r *http.Request,
|
||||
) (*store.Domain, error) {
|
||||
if r == nil {
|
||||
return s.currentDomain(), nil
|
||||
}
|
||||
if cached, ok := r.Context().Value(requestDomainKey{}).(*requestDomainLookup); ok && cached.host == r.Host {
|
||||
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHost(r.Host) })
|
||||
return cached.domain, cached.err
|
||||
}
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDomainByHost(
|
||||
r.Host,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sp, nil
|
||||
}
|
||||
|
||||
func logDomainLookupError(r *http.Request, err error) {
|
||||
log.Printf(
|
||||
"domain lookup failed host=%q: %v",
|
||||
r.Host,
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) domainUUIDForRequest(r *http.Request) string {
|
||||
domain := s.domainForRequest(r)
|
||||
if domain == nil {
|
||||
return ""
|
||||
}
|
||||
return domain.UUID
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDomainEntryDialogChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
for _, theme := range []string{"light", "nanoha-dark"} {
|
||||
for _, width := range []string{"1440", "390"} {
|
||||
t.Run(theme+"-"+width, func(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
if err := s.store.SetConfigInDomain(domain, "theme", theme); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/domain-entry-dialog.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/-/sys/config" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for k, v := range rec.Header() {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
||||
defer cancel()
|
||||
screenshot := ""
|
||||
if dir := os.Getenv("PAGESWIKI_PERIODICAL_SCREENSHOT_DIR"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
screenshot = filepath.Join(dir, "domain-entry-"+theme+"-"+width+".png")
|
||||
}
|
||||
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/-/sys/config?panel=domains", t.TempDir(), screenshot, width).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("browser %v %s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"pageswiki/internal/store"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDomainEntryConfig(t *testing.T) {
|
||||
s, token, domain, actor := periodicalFixture(t)
|
||||
var id int64
|
||||
if err := s.store.DB.QueryRow(`SELECT id FROM domains WHERE uuid=?`, domain).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
form := url.Values{"section": {"domain-entry"}, "domain_id": {strconv.FormatInt(id, 10)}, "entry_layout": {"diary"}, "diary_enabled": {"1"}, "diary_prefix": {"/"}, "blog_prefix": {"/blog"}}
|
||||
w := configRequest(s, token, "POST", "/-/sys/config", form)
|
||||
if w.Code != 302 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO federation_instances(uuid,campus_uuid,fqdn,fingerprint,status,last_seen_at,created_at) SELECT 'old-peer',campus_uuid,'old.example.test','sha256:fixture','approved',0,0 FROM domains WHERE uuid=?`, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if blocked := configRequest(s, token, "POST", "/-/sys/config", form); blocked.Code != 409 {
|
||||
t.Fatal("legacy peer not blocked", blocked.Code)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO federation_instance_capabilities VALUES('old-peer',2)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ready := configRequest(s, token, "POST", "/-/sys/config", form); ready.Code != 302 {
|
||||
t.Fatal("supported peer blocked", ready.Code)
|
||||
}
|
||||
if got, _ := s.store.GetConfigInDomain(domain, "home_layout"); got != "diary" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if w = periodicalRequest(s, token, "POST", "/_/api/v1/extension/content.diary/settings", `{"enabled":false}`); w.Code != 403 {
|
||||
t.Fatal("override allowed", w.Code)
|
||||
}
|
||||
w = configRequest(s, token, "GET", "/-/sys/config?panel=domains", nil)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "data-domain-entry-open") {
|
||||
t.Fatal("missing dialog", w.Code)
|
||||
}
|
||||
if err := s.store.SetDomainEntry(context.Background(), id, "unrelated", store.DomainEntrySettings{Layout: "classic", DiaryPrefix: "/diary", BlogPrefix: "/blog"}); err == nil {
|
||||
t.Fatal("unauthorized settings")
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`UPDATE campuses SET authority_mode='managed' WHERE uuid=(SELECT campus_uuid FROM domains WHERE uuid=?)`, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.store.SetDomainEntry(context.Background(), id, actor, store.DomainEntrySettings{Layout: "classic", DiaryPrefix: "/diary", BlogPrefix: "/blog"}); err == nil {
|
||||
t.Fatal("managed settings override")
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,9 @@
|
||||
package server
|
||||
|
||||
func (s *Server) getEditorKeymap() string {
|
||||
func (s *Server) getEditorKeymap(domainUUID ...string) string {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_keymap",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
v := s.getDomainConfig("editor_keymap", domainUUID...)
|
||||
if v == "" {
|
||||
return "default"
|
||||
}
|
||||
|
||||
@@ -19,30 +15,37 @@ func (s *Server) getEditorKeymap() string {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getEditorSyntaxHighlight() bool {
|
||||
func (s *Server) getEditorSyntaxHighlight(domainUUID ...string) bool {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_syntax_highlight",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
v := s.getDomainConfig("editor_syntax_highlight", domainUUID...)
|
||||
if v == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return v != "false"
|
||||
}
|
||||
|
||||
func (s *Server) getEditorAutocomplete() bool {
|
||||
func (s *Server) getEditorAutocomplete(domainUUID ...string) bool {
|
||||
|
||||
v, err :=
|
||||
s.store.GetConfig(
|
||||
"editor_autocomplete",
|
||||
)
|
||||
|
||||
if err != nil || v == "" {
|
||||
v := s.getDomainConfig("editor_autocomplete", domainUUID...)
|
||||
if v == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return v != "false"
|
||||
}
|
||||
|
||||
func (s *Server) getEditorFormat(domainUUID ...string) string {
|
||||
v := s.getDomainConfig("editor_format_default", domainUUID...)
|
||||
if v == "markdown" {
|
||||
return "markdown"
|
||||
}
|
||||
return "html"
|
||||
}
|
||||
|
||||
func (s *Server) getEditorPreviewDefault(domainUUID ...string) string {
|
||||
if s.getDomainConfig("editor_preview_default", domainUUID...) == "hidden" {
|
||||
return "hidden"
|
||||
}
|
||||
return "visible"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package server
|
||||
|
||||
import "strings"
|
||||
|
||||
// Both the compact /e/docs and the historic /e/-/docs edit /docs.
|
||||
func semanticEditorPath(route string) string {
|
||||
if route == "/e/-" || route == "/e/-/" {
|
||||
return "/"
|
||||
}
|
||||
if strings.HasPrefix(route, "/e/-/") {
|
||||
return "/" + strings.TrimPrefix(route, "/e/-/")
|
||||
}
|
||||
return "/" + strings.TrimPrefix(route, "/e/")
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleEmailHTML(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Query().Get("path")
|
||||
objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
revision, err := s.latestRevisionForRequest(r, objectUUID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
htmlBody := extractEmailHTML([]byte(revision.Body))
|
||||
if htmlBody == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
"default-src 'none'; img-src data: cid: https: http:; style-src 'unsafe-inline'")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
_, _ = io.WriteString(w, htmlBody)
|
||||
}
|
||||
|
||||
func extractEmailHTML(body []byte) string {
|
||||
message, err := mail.ReadMessage(bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
mediaType, params, _ := mime.ParseMediaType(message.Header.Get("Content-Type"))
|
||||
if mediaType == "text/html" {
|
||||
return readMailBody(message.Header.Get("Content-Transfer-Encoding"), message.Body)
|
||||
}
|
||||
if !strings.HasPrefix(mediaType, "multipart/") || params["boundary"] == "" {
|
||||
return ""
|
||||
}
|
||||
reader := multipart.NewReader(message.Body, params["boundary"])
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
partType, _, _ := mime.ParseMediaType(part.Header.Get("Content-Type"))
|
||||
if partType == "text/html" {
|
||||
return readMailBody(part.Header.Get("Content-Transfer-Encoding"), part)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type authorizationTargetKind string
|
||||
|
||||
const (
|
||||
targetPath authorizationTargetKind = "path"
|
||||
targetDomain authorizationTargetKind = "domain"
|
||||
)
|
||||
|
||||
type endpointPolicy struct {
|
||||
Name string
|
||||
Capability string
|
||||
Target authorizationTargetKind
|
||||
Resolver endpointTargetResolver
|
||||
}
|
||||
|
||||
type endpointTargetResolver string
|
||||
|
||||
const (
|
||||
resolveNone endpointTargetResolver = ""
|
||||
resolveHandler endpointTargetResolver = "handler"
|
||||
resolveObjectURL endpointTargetResolver = "object_url"
|
||||
resolveRevision endpointTargetResolver = "revision_url"
|
||||
resolveRaw endpointTargetResolver = "raw_address"
|
||||
resolveText endpointTargetResolver = "text_revision"
|
||||
resolvePDF endpointTargetResolver = "pdf_revision"
|
||||
resolvePreview endpointTargetResolver = "preview_document"
|
||||
resolveQueryPath endpointTargetResolver = "query_path"
|
||||
resolveViewPath endpointTargetResolver = "view_path"
|
||||
resolvePrint endpointTargetResolver = "print_address"
|
||||
resolveDiff endpointTargetResolver = "revision_diff"
|
||||
)
|
||||
|
||||
var endpointPolicies = map[string]endpointPolicy{
|
||||
"GET /_/api/v1/extension": {Name: "extension.catalog", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /-/sys/config/vim": {Name: "config.vim", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /:/new": {Name: "page.create_form", Capability: "create", Target: targetDomain},
|
||||
"GET /v/upload": {Name: "upload.form", Capability: "read", Target: targetDomain},
|
||||
"GET /_/api/v1/object/list": {Name: "object.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/path-picker": {Name: "path_picker.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/search/suggest": {Name: "search.suggest", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/browser/navigation": {Name: "browser.navigation.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"HEAD /_/api/v1/object/list": {Name: "object.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/revisions/list": {Name: "revision.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"HEAD /_/api/v1/revisions/list": {Name: "revision.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/gc/zombies/list": {Name: "gc.list", Capability: "manage", Target: targetDomain},
|
||||
"POST /:/new": {Name: "page.create", Capability: "create", Target: targetDomain},
|
||||
"POST /:/compile/markdown": {Name: "render.markdown", Capability: "read", Target: targetDomain},
|
||||
"POST /_/api/v1/object/create": {Name: "object.create", Capability: "create", Target: targetDomain},
|
||||
"POST /_/api/v1/object/delete": {Name: "object.delete", Capability: "delete", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/revisions/save": {Name: "revision.save", Capability: "write", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/revisions/restore": {Name: "revision.restore", Capability: "write", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/render/markdown": {Name: "render.markdown", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/tag/add": {Name: "tag.add", Capability: "write", Target: targetDomain},
|
||||
"POST /_/api/v1/path/bind": {Name: "path.bind", Capability: "link", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/path/rename": {Name: "path.rename", Capability: "rename", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/path/rename-preview": {Name: "path.rename_preview", Capability: "rename", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/path/remove": {Name: "path.remove", Capability: "delete", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/path/copy": {Name: "path.copy", Capability: "create", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/browser/navigation": {Name: "browser.navigation.add", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"PATCH /_/api/v1/browser/navigation": {Name: "browser.navigation.rename", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"DELETE /_/api/v1/browser/navigation": {Name: "browser.navigation.delete", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/gc/zombies/purge": {Name: "gc.purge", Capability: "delete", Target: targetDomain},
|
||||
"POST /v/upload": {Name: "document.upload", Capability: "upload", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/assets": {Name: "asset.list", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/assets": {Name: "asset.create", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"DELETE /_/api/v1/assets": {Name: "asset.delete", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/notifications": {Name: "notifications.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"DELETE /_/api/v1/notifications/push": {Name: "notifications.push", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/notifications/read": {Name: "notifications.read", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"PATCH /_/api/v1/notifications/read": {Name: "notifications.read", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/publication/requests": {Name: "publication.request", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/publication/requests/": {Name: "publication.decide", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
}
|
||||
|
||||
var mutationPrefixPolicies = []struct {
|
||||
Prefix string
|
||||
Policy endpointPolicy
|
||||
}{
|
||||
{Prefix: "/e/", Policy: endpointPolicy{Name: "document.edit", Capability: "write", Target: targetPath}},
|
||||
{Prefix: "/-/sys/", Policy: endpointPolicy{Name: "system.manage", Capability: "manage", Target: targetDomain}},
|
||||
{Prefix: "/_/api/v1/event", Policy: endpointPolicy{Name: "event.manage", Capability: "manage", Target: targetDomain}},
|
||||
{Prefix: "/_/api/v1/queue", Policy: endpointPolicy{Name: "queue.manage", Capability: "manage", Target: targetDomain}},
|
||||
{Prefix: "/_/api/v1/adapter", Policy: endpointPolicy{Name: "adapter.manage", Capability: "manage", Target: targetDomain}},
|
||||
{Prefix: "/_/api/v1/publication/requests/", Policy: endpointPolicy{Name: "publication.decide", Capability: "manage", Target: targetDomain, Resolver: resolveHandler}},
|
||||
{Prefix: "/_/api/v1/city/", Policy: endpointPolicy{Name: "city.mutate", Capability: "write", Target: targetDomain}},
|
||||
{Prefix: "/_/api/v1/import/", Policy: endpointPolicy{Name: "import.mutate", Capability: "write", Target: targetDomain}},
|
||||
{Prefix: "/v/import", Policy: endpointPolicy{Name: "import.mutate", Capability: "write", Target: targetDomain}},
|
||||
{Prefix: "/:/compile/diagram/", Policy: endpointPolicy{Name: "render.diagram", Capability: "read", Target: targetDomain}},
|
||||
{Prefix: "/l/", Policy: endpointPolicy{Name: "path.link", Capability: "link", Target: targetDomain, Resolver: resolveHandler}},
|
||||
}
|
||||
|
||||
func policyForRequest(r *http.Request) endpointPolicy {
|
||||
if taskRoute(r.URL.Path) {
|
||||
return endpointPolicy{Name: "tasks.handler", Capability: "read", Target: targetDomain, Resolver: resolveHandler}
|
||||
}
|
||||
if calendarRoute(r.URL.Path) {
|
||||
return endpointPolicy{Name: "calendar.handler", Capability: "read", Target: targetDomain, Resolver: resolveHandler}
|
||||
}
|
||||
if kind, operation := periodicalRoute(r.URL.Path); kind != "" {
|
||||
if (r.Method == "GET" && operation != "posts") || (r.Method == "POST" && operation != "view" && operation != "plan" && operation != "new") {
|
||||
capability := "read"
|
||||
if r.Method == "POST" {
|
||||
capability = "write"
|
||||
if operation == "settings" {
|
||||
capability = "manage"
|
||||
}
|
||||
}
|
||||
return endpointPolicy{Name: "extension." + kind + "." + operation, Capability: capability, Target: targetDomain, Resolver: resolveHandler}
|
||||
}
|
||||
return endpointPolicy{Name: "extension.method.undefined", Capability: "deny", Target: targetDomain}
|
||||
}
|
||||
if cityGovernanceRoute(r.URL.Path) {
|
||||
return endpointPolicy{Name: "city.governance", Capability: "manage", Target: targetDomain, Resolver: resolveHandler}
|
||||
}
|
||||
cleanPath := strings.TrimSuffix(r.URL.Path, "/")
|
||||
if policy, ok := endpointPolicies[r.Method+" "+cleanPath]; ok {
|
||||
return policy
|
||||
}
|
||||
// Exact routes are method-closed. This also protects older handlers that do
|
||||
// not yet carry their own explicit method guard.
|
||||
for route := range endpointPolicies {
|
||||
_, declaredPath, ok := strings.Cut(route, " ")
|
||||
if ok && declaredPath == cleanPath {
|
||||
return endpointPolicy{Name: "method.undefined", Capability: "deny", Target: targetDomain}
|
||||
}
|
||||
}
|
||||
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
|
||||
if resolver, capability := readEndpointResolver(r.URL.Path); resolver != resolveNone {
|
||||
return endpointPolicy{Name: "derived.read", Capability: capability, Target: targetPath, Resolver: resolver}
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/e/") {
|
||||
return endpointPolicy{Name: "document.editor", Capability: "write", Target: targetPath}
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/-/sys/") ||
|
||||
strings.HasPrefix(r.URL.Path, "/_/api/v1/event") ||
|
||||
strings.HasPrefix(r.URL.Path, "/_/api/v1/queue") {
|
||||
return endpointPolicy{Name: "system.read", Capability: "manage", Target: targetDomain}
|
||||
}
|
||||
return endpointPolicy{Name: "content.read", Capability: "read", Target: endpointTargetKind(r.URL.Path)}
|
||||
}
|
||||
|
||||
for _, rule := range mutationPrefixPolicies {
|
||||
if strings.HasPrefix(r.URL.Path, rule.Prefix) {
|
||||
return rule.Policy
|
||||
}
|
||||
}
|
||||
if isReservedEndpoint(r.URL.Path) {
|
||||
return endpointPolicy{Name: "reserved.undefined", Capability: "deny", Target: targetDomain}
|
||||
}
|
||||
return endpointPolicy{Name: "document.mutate", Capability: "write", Target: targetPath}
|
||||
}
|
||||
|
||||
func readEndpointResolver(path string) (endpointTargetResolver, string) {
|
||||
switch {
|
||||
case path == "/v/children", path == "/v/tree", strings.HasPrefix(path, "/v/tree/"):
|
||||
return resolveHandler, "read"
|
||||
case path == "/v/city", strings.HasPrefix(path, "/v/city/"), strings.HasPrefix(path, "/_/api/v1/city/"):
|
||||
return resolveHandler, "read"
|
||||
case path == "/v/bookmarks":
|
||||
return resolveHandler, "read"
|
||||
case strings.HasPrefix(path, "/_/obj/"):
|
||||
return resolveHandler, "read"
|
||||
case strings.HasPrefix(path, "/v/raw/"):
|
||||
return resolveRaw, "read"
|
||||
case strings.HasPrefix(path, "/v/text/"):
|
||||
return resolveText, "read"
|
||||
case path == "/v/pdf-page":
|
||||
return resolvePDF, "read"
|
||||
case strings.HasPrefix(path, "/v/preview-cache/"):
|
||||
return resolvePreview, "read"
|
||||
case path == "/v/archive", path == "/v/email-html":
|
||||
return resolveQueryPath, "read"
|
||||
case path == "/v/search", path == "/v/tree", strings.HasPrefix(path, "/v/tree/"):
|
||||
return resolveHandler, "read"
|
||||
case strings.HasPrefix(path, "/_/view/print/"):
|
||||
return resolvePrint, "read"
|
||||
case strings.HasPrefix(path, "/v/preview"), strings.HasPrefix(path, "/v/plain"), strings.HasPrefix(path, "/v/markdown"):
|
||||
return resolveViewPath, "read"
|
||||
case strings.HasPrefix(path, "/o/e/"):
|
||||
return resolveObjectURL, "write"
|
||||
case strings.HasPrefix(path, "/l/uuid:"):
|
||||
return resolveObjectURL, "read"
|
||||
case strings.HasPrefix(path, "/o/r/"), strings.HasPrefix(path, "/o/"):
|
||||
return resolveObjectURL, "read"
|
||||
case strings.HasPrefix(path, "/r/history/"):
|
||||
return resolveObjectURL, "read"
|
||||
case strings.HasPrefix(path, "/r/view/"):
|
||||
return resolveRevision, "read"
|
||||
case strings.HasPrefix(path, "/r/diff/"):
|
||||
return resolveDiff, "read"
|
||||
}
|
||||
return resolveNone, ""
|
||||
}
|
||||
|
||||
func endpointTargetKind(path string) authorizationTargetKind {
|
||||
if isReservedEndpoint(path) {
|
||||
return targetDomain
|
||||
}
|
||||
return targetPath
|
||||
}
|
||||
|
||||
func isReservedEndpoint(path string) bool {
|
||||
for _, prefix := range []string{"/_/", "/-/", "/:/", "/o/", "/r/", "/v/", "/l/", "/static/"} {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPolicyForRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
method, path, capability string
|
||||
target authorizationTargetKind
|
||||
}{
|
||||
{http.MethodGet, "/guide/delete-is-a-title", "read", targetPath},
|
||||
{http.MethodPost, "/guide/delete-is-a-title", "write", targetPath},
|
||||
{http.MethodGet, "/e/-/guide", "write", targetPath},
|
||||
{http.MethodGet, "/:/new", "create", targetDomain},
|
||||
{http.MethodPost, "/:/new", "create", targetDomain},
|
||||
{http.MethodGet, "/_/api/v1/object/create", "deny", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/path/rename", "rename", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/render/markdown", "read", targetDomain},
|
||||
{http.MethodDelete, "/_/api/v1/assets", "manage", targetDomain},
|
||||
{http.MethodGet, "/_/api/v1/assets", "manage", targetDomain},
|
||||
{http.MethodGet, "/_/api/v1/notifications", "read", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/notifications/read", "read", targetDomain},
|
||||
{http.MethodPatch, "/_/api/v1/notifications/read", "read", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/publication/requests", "manage", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/publication/requests/request-1/decisions", "manage", targetDomain},
|
||||
{http.MethodPut, "/_/api/v1/assets", "deny", targetDomain},
|
||||
{http.MethodGet, "/_/api/v1/event/jobs", "manage", targetDomain},
|
||||
{http.MethodGet, "/_/api/v1/queue", "manage", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/event/run", "manage", targetDomain},
|
||||
{http.MethodPost, "/_/api/v1/not-registered/delete", "deny", targetDomain},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.method+" "+tt.path, func(t *testing.T) {
|
||||
r := httptest.NewRequest(tt.method, tt.path, nil)
|
||||
got := policyForRequest(r)
|
||||
if got.Capability != tt.capability || got.Target != tt.target {
|
||||
t.Fatalf("policy = capability %q target %q; want %q %q", got.Capability, got.Target, tt.capability, tt.target)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEndpointResolvers(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
resolver endpointTargetResolver
|
||||
capability string
|
||||
}{
|
||||
{"/v/raw/rev:123", resolveRaw, "read"},
|
||||
{"/v/text/rev:123", resolveText, "read"},
|
||||
{"/v/pdf-page", resolvePDF, "read"},
|
||||
{"/v/preview-cache/hash.jpg", resolvePreview, "read"},
|
||||
{"/v/preview/docs/page", resolveViewPath, "read"},
|
||||
{"/o/e/uuid:123", resolveObjectURL, "write"},
|
||||
{"/r/view/revision", resolveRevision, "read"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, tt.path, nil)
|
||||
policy := policyForRequest(request)
|
||||
if policy.Resolver != tt.resolver || policy.Capability != tt.capability {
|
||||
t.Fatalf("resolver=%q capability=%q; want %q %q", policy.Resolver, policy.Capability, tt.resolver, tt.capability)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointPoliciesAreNamed(t *testing.T) {
|
||||
for route, policy := range endpointPolicies {
|
||||
if policy.Name == "" || policy.Capability == "" || policy.Target == "" {
|
||||
t.Errorf("incomplete policy for %s: %#v", route, policy)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) authorizeResolvedEndpoint(r *http.Request, principal *store.Principal, policy endpointPolicy) (bool, error) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
switch policy.Resolver {
|
||||
case resolveNone:
|
||||
return false, nil
|
||||
case resolveHandler:
|
||||
return true, nil
|
||||
case resolveRaw:
|
||||
address := strings.TrimPrefix(r.URL.Path, "/v/raw/")
|
||||
switch {
|
||||
case strings.HasPrefix(address, "path:"):
|
||||
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "path:"))
|
||||
case strings.HasPrefix(address, "uuid:"):
|
||||
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "uuid:"))
|
||||
case strings.HasPrefix(address, "rev:"):
|
||||
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
|
||||
case strings.HasPrefix(address, "asset:"):
|
||||
// Asset HEAD authorization is address-aware and remains in its handler.
|
||||
return true, nil
|
||||
default:
|
||||
return true, store.ErrAccessDenied
|
||||
}
|
||||
case resolveText:
|
||||
address := strings.TrimPrefix(r.URL.Path, "/v/text/")
|
||||
if !strings.HasPrefix(address, "rev:") {
|
||||
return true, store.ErrAccessDenied
|
||||
}
|
||||
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
|
||||
case resolvePDF:
|
||||
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("rev"))
|
||||
case resolvePreview:
|
||||
name := filepath.Base(strings.TrimPrefix(r.URL.Path, "/v/preview-cache/"))
|
||||
hash := strings.TrimSuffix(strings.TrimSuffix(name, ".jpg"), ".svg")
|
||||
return true, s.authorizePrincipalDocument(domainUUID, principal.UUID, policy.Capability, hash)
|
||||
case resolveQueryPath:
|
||||
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("path"))
|
||||
case resolveViewPath:
|
||||
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, semanticViewPath(r.URL.Path))
|
||||
case resolvePrint:
|
||||
address := strings.TrimPrefix(r.URL.Path, "/_/view/print/")
|
||||
return true, s.authorizePrincipalAddress(domainUUID, principal.UUID, policy.Capability, address)
|
||||
case resolveObjectURL:
|
||||
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, objectIDFromRoute(r.URL.Path))
|
||||
case resolveRevision:
|
||||
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(r.URL.Path, "/r/view/"))
|
||||
case resolveDiff:
|
||||
parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/r/diff/"), "/", 2)
|
||||
if len(parts) != 2 {
|
||||
return true, store.ErrAccessDenied
|
||||
}
|
||||
if err := s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[0]); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[1])
|
||||
default:
|
||||
return true, store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) authorizePrincipalAddress(domainUUID, principalUUID, capability, address string) error {
|
||||
switch {
|
||||
case strings.HasPrefix(address, "path:"):
|
||||
return s.authorizePrincipalPath(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "path:"))
|
||||
case strings.HasPrefix(address, "uuid:"):
|
||||
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "uuid:"))
|
||||
case strings.HasPrefix(address, "rev:"):
|
||||
return s.authorizePrincipalRevision(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "rev:"))
|
||||
default:
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) requestCanReadPath(r *http.Request, path string) bool {
|
||||
return s.pathReadFilter(r)(path)
|
||||
}
|
||||
|
||||
func (s *Server) authorizeObjectForRequest(r *http.Request, capability, objectUUID string) error {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mode != "enforcing" {
|
||||
return nil
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
return s.authorizePrincipalObject(domainUUID, principal.UUID, capability, objectUUID)
|
||||
}
|
||||
|
||||
func normalizeAuthorizationPath(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || !strings.HasPrefix(value, "/") {
|
||||
return ""
|
||||
}
|
||||
if value != "/" {
|
||||
value = strings.TrimRight(value, "/")
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// authorizedMutationSnapshot returns exactly the bindings whose source and
|
||||
// destination grants were checked. The mutation must pass this opaque snapshot
|
||||
// to the checked store method, never enumerate the tree for a second time.
|
||||
func (s *Server) authorizedMutationSnapshot(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) (*store.PathMutationSnapshot, error) {
|
||||
source = normalizeAuthorizationPath(source)
|
||||
if source == "" {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
if destinationCapability != "" {
|
||||
destination = normalizeAuthorizationPath(destination)
|
||||
if destination == "" {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var principal *store.Principal
|
||||
if mode == "enforcing" {
|
||||
principal, err = s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
snapshot, err := s.store.SnapshotPathsInDomain(domainUUID, source, recursive)
|
||||
if err != nil {
|
||||
// Do not expose the existence/size of a scope whose root is unreadable.
|
||||
if principal != nil && (errors.Is(err, store.ErrPathNotFound) || errors.Is(err, store.ErrPathMutationTooLarge)) && s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, source) != nil {
|
||||
return nil, store.ErrAccessDenied
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if principal == nil {
|
||||
return snapshot, nil
|
||||
}
|
||||
for _, sourcePath := range snapshot.Paths() {
|
||||
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, sourcePath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if destinationCapability != "" {
|
||||
destinationPath := destination + strings.TrimPrefix(sourcePath, snapshot.Root())
|
||||
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, destinationCapability, destinationPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (s *Server) authorizePathTreeForRequest(r *http.Request, capability, root string, recursive bool) error {
|
||||
_, err := s.authorizedMutationSnapshot(r, capability, "", root, "", recursive)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) authorizePathMoveForRequest(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) error {
|
||||
_, err := s.authorizedMutationSnapshot(r, sourceCapability, destinationCapability, source, destination, recursive)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) authorizePrincipalDocument(domainUUID, principalUUID, capability, hash string) error {
|
||||
if hash == "" {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
references, err := s.store.DocumentReferencesInDomain(domainUUID, hash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, reference := range references {
|
||||
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, reference.Path) == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
|
||||
func (s *Server) authorizePrincipalPath(domainUUID, principalUUID, capability, path string) error {
|
||||
if path == "" || !strings.HasPrefix(path, "/") {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
if store.IsUserSpacePath(path) {
|
||||
allowed, err := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, path)
|
||||
if err != nil || !allowed {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "path", TargetID: path})
|
||||
}
|
||||
|
||||
func (s *Server) authorizePrincipalObject(domainUUID, principalUUID, capability, objectUUID string) error {
|
||||
if objectUUID == "" {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
request := store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "object", TargetID: objectUUID}
|
||||
inScope, err := s.store.ObjectInDomainAuthorityScope(domainUUID, objectUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !inScope {
|
||||
return s.store.AuthorizeExactObject(request)
|
||||
}
|
||||
links, err := s.store.ListPathLinksInDomain(domainUUID, objectUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, link := range links {
|
||||
if !store.IsUserSpacePath(link.Path) {
|
||||
continue
|
||||
}
|
||||
allowed, audienceErr := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, link.Path)
|
||||
if audienceErr != nil || !allowed {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
}
|
||||
if err := s.store.CheckObjectLocalityAccessCaps(domainUUID, principalUUID, capability, objectUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.store.Authorize(request); err == nil {
|
||||
return nil
|
||||
} else if !errors.Is(err, store.ErrAccessDenied) {
|
||||
return err
|
||||
}
|
||||
if !s.store.ObjectBoundInDomain(domainUUID, objectUUID) {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
for _, link := range links {
|
||||
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, link.Path) == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
|
||||
func (s *Server) authorizePrincipalRevision(domainUUID, principalUUID, capability, revisionUUID string) error {
|
||||
if revisionUUID == "" {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
revision, err := s.store.GetRevisionMetadata(revisionUUID)
|
||||
if err != nil {
|
||||
return store.ErrAccessDenied
|
||||
}
|
||||
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, revision.ObjectUUID)
|
||||
}
|
||||
|
||||
func objectIDFromRoute(path string) string {
|
||||
for _, prefix := range []string{"/o/e/uuid:", "/o/r/uuid:", "/o/uuid:", "/l/uuid:", "/r/history/"} {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
return strings.TrimPrefix(path, prefix)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func semanticViewPath(route string) string {
|
||||
for _, prefix := range []string{"/v/preview", "/v/plain", "/v/markdown", "/v/browser"} {
|
||||
if strings.HasPrefix(route, prefix) {
|
||||
path := strings.TrimPrefix(route, prefix)
|
||||
if path == "" {
|
||||
return "/"
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
return "/" + path
|
||||
}
|
||||
return path
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestObjectAndRevisionAuthorizationFollowReadableBindings(t *testing.T) {
|
||||
database, err := db.Open(t.TempDir() + "/endpoint-target.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
if err := db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.New(database)
|
||||
if err := st.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := st.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
principal, err := st.BootstrapOwner(domain.UUID, "scoped", "correct horse battery staple", "Scoped", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var roleUUID string
|
||||
if err := database.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? LIMIT 1`, domain.UUID, principal.UUID).Scan(&roleUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := database.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, roleUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-read',?,'read','path','/allowed',1)`, roleUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
allowedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
allowedRevision, err := st.SaveRevision(allowedObject, "allowed", "test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deniedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/denied")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := &Server{store: st}
|
||||
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", allowedObject); err != nil {
|
||||
t.Fatalf("object through readable binding: %v", err)
|
||||
}
|
||||
if err := server.authorizePrincipalRevision(domain.UUID, principal.UUID, "read", allowedRevision); err != nil {
|
||||
t.Fatalf("revision through readable binding: %v", err)
|
||||
}
|
||||
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", deniedObject); !errors.Is(err, store.ErrAccessDenied) {
|
||||
t.Fatalf("unreadable object authorization = %v", err)
|
||||
}
|
||||
|
||||
if _, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed/child"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := st.CreateAuthSession(domain.UUID, principal.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "http://localhost/_/api/v1/path/copy", nil)
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); !errors.Is(err, store.ErrAccessDenied) {
|
||||
t.Fatalf("recursive operation crossed an ungranted child: %v", err)
|
||||
}
|
||||
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-child-read',?,'read','path','/allowed/child',1)`, roleUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); err != nil {
|
||||
t.Fatalf("fully granted recursive operation: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func writeEventJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func decodeEventJSON(w http.ResponseWriter, r *http.Request, value any) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||
if err := json.NewDecoder(r.Body).Decode(value); err != nil {
|
||||
writeEventJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) handleEventAPI(w http.ResponseWriter, r *http.Request) {
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/event"), "/")
|
||||
switch action {
|
||||
case "":
|
||||
if r.Method == http.MethodGet {
|
||||
definitions, err := s.store.ListEventDefinitions()
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"events": definitions})
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
var definition store.EventDefinition
|
||||
if !decodeEventJSON(w, r, &definition) {
|
||||
return
|
||||
}
|
||||
if err := s.store.SaveEventDefinition(&definition); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
writeEventJSON(w, http.StatusCreated, definition)
|
||||
return
|
||||
}
|
||||
case "run":
|
||||
if r.Method == http.MethodPost {
|
||||
var input struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
queued, err := s.store.RunEventDefinition(input.UUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
writeEventJSON(w, 202, map[string]any{"queued": queued})
|
||||
return
|
||||
}
|
||||
case "delete":
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodDelete {
|
||||
var input struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if err := s.store.DeleteEventDefinition(input.UUID); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]bool{"deleted": true})
|
||||
return
|
||||
}
|
||||
case "logs":
|
||||
if r.Method == http.MethodGet {
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
before, err := strconv.ParseInt(r.URL.Query().Get("before_id"), 10, 64)
|
||||
if r.URL.Query().Has("before_id") && (err != nil || before < 0) {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid log cursor"})
|
||||
return
|
||||
}
|
||||
logs, err := s.store.ListEventLogsBefore(r.URL.Query().Get("job_uuid"), limit, before)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"logs": logs})
|
||||
return
|
||||
}
|
||||
case "logs/trends":
|
||||
if r.Method == http.MethodGet {
|
||||
hours, _ := strconv.Atoi(r.URL.Query().Get("hours"))
|
||||
if hours < 1 || hours > 24*365 {
|
||||
hours = 24
|
||||
}
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
trends, err := s.store.ListEventLogTrends(r.Context(), r.URL.Query().Get("event_type"), time.Now().Add(-time.Duration(hours)*time.Hour).UnixMilli(), limit)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"trends": trends, "hours": hours})
|
||||
return
|
||||
}
|
||||
case "policies":
|
||||
if r.Method == http.MethodGet {
|
||||
policies, err := s.store.ListEventLogPolicies()
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"policies": policies})
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodPut {
|
||||
var policy store.EventLogPolicy
|
||||
if !decodeEventJSON(w, r, &policy) {
|
||||
return
|
||||
}
|
||||
if err := s.store.SetEventLogPolicy(policy); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, policy)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.Header().Set("Allow", "GET, POST, DELETE")
|
||||
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported event operation"})
|
||||
}
|
||||
|
||||
func (s *Server) handleQueueAPI(w http.ResponseWriter, r *http.Request) {
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/queue"), "/")
|
||||
switch action {
|
||||
case "jobs":
|
||||
if r.Method == http.MethodGet {
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
jobs, next, err := s.store.EventJobPage(r.Context(), r.URL.Query().Get("status"), r.URL.Query().Get("cursor"), limit)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
if errors.Is(err, store.ErrEventJobFilter) {
|
||||
code = http.StatusBadRequest
|
||||
}
|
||||
writeEventJSON(w, code, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"jobs": jobs, "next_cursor": next})
|
||||
return
|
||||
}
|
||||
case "":
|
||||
if r.Method == http.MethodGet {
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
jobs, err := s.store.ListEventJobs(limit)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
stats, err := s.store.EventStats()
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
settings, err := s.store.GetQueueSettings()
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{
|
||||
"stats": stats, "settings": settings, "jobs": jobs,
|
||||
"active_workers": s.schedulerRunning.Load(),
|
||||
})
|
||||
return
|
||||
}
|
||||
case "settings":
|
||||
if r.Method == http.MethodGet {
|
||||
settings, err := s.store.GetQueueSettings()
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, settings)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost || r.Method == http.MethodPut {
|
||||
var settings store.QueueSettings
|
||||
if !decodeEventJSON(w, r, &settings) {
|
||||
return
|
||||
}
|
||||
if err := s.store.SetQueueSettings(settings); err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
writeEventJSON(w, 200, settings)
|
||||
return
|
||||
}
|
||||
case "cancel", "retry":
|
||||
if r.Method == http.MethodPost {
|
||||
var input struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
if !decodeEventJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
if action == "cancel" {
|
||||
err = s.store.CancelEventJob(input.UUID)
|
||||
} else {
|
||||
err = s.store.RetryEventJob(input.UUID)
|
||||
}
|
||||
if err != nil {
|
||||
writeEventJSON(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
writeEventJSON(w, 200, map[string]bool{"ok": true})
|
||||
return
|
||||
}
|
||||
case "resources":
|
||||
if r.Method == http.MethodGet {
|
||||
writeEventJSON(w, 200, s.resourceStatus())
|
||||
return
|
||||
}
|
||||
case "resources/history":
|
||||
if r.Method == http.MethodGet {
|
||||
hours, _ := strconv.Atoi(r.URL.Query().Get("hours"))
|
||||
if hours != 1 && hours != 24 && hours != 168 {
|
||||
hours = 24
|
||||
}
|
||||
samples, err := s.store.ResourceHistory(r.Context(), hours, time.Now())
|
||||
if err != nil {
|
||||
writeEventJSON(w, 500, map[string]string{"error": "could not load resource history"})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"samples": samples, "hours": hours, "retention_days": 7})
|
||||
return
|
||||
}
|
||||
}
|
||||
w.Header().Set("Allow", "GET, POST, PUT")
|
||||
writeEventJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "unsupported queue operation"})
|
||||
}
|
||||
|
||||
func (s *Server) resourceStatus() map[string]any {
|
||||
var memory runtime.MemStats
|
||||
runtime.ReadMemStats(&memory)
|
||||
root := s.store.DocumentRoot
|
||||
if root == "" {
|
||||
root = "."
|
||||
}
|
||||
root, _ = filepath.Abs(root)
|
||||
var fs syscall.Statfs_t
|
||||
_ = syscall.Statfs(root, &fs)
|
||||
total := fs.Blocks * uint64(fs.Bsize)
|
||||
available := fs.Bavail * uint64(fs.Bsize)
|
||||
loadAverage := ""
|
||||
if data, err := os.ReadFile("/proc/loadavg"); err == nil {
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) >= 3 {
|
||||
loadAverage = strings.Join(fields[:3], " ")
|
||||
}
|
||||
}
|
||||
stats, _ := s.store.ActiveEventStats()
|
||||
settings, _ := s.store.GetQueueSettings()
|
||||
return map[string]any{
|
||||
"timestamp_ms": time.Now().UnixMilli(),
|
||||
"io": readProcessIO(),
|
||||
"cpu": map[string]any{"logical_cpus": runtime.NumCPU(), "load_average": loadAverage},
|
||||
"memory": map[string]any{
|
||||
"heap_bytes": memory.HeapAlloc, "go_system_bytes": memory.Sys,
|
||||
"gc_cycles": memory.NumGC, "goroutines": runtime.NumGoroutine(),
|
||||
},
|
||||
"storage": map[string]any{
|
||||
"path": root, "total_bytes": total, "used_bytes": total - available,
|
||||
"available_bytes": available,
|
||||
},
|
||||
"queue": map[string]any{"stats": map[string]int{"queued": stats.Queued, "running": stats.Running}, "active_workers": s.schedulerRunning.Load(), "paused": settings.Paused},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,641 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const previewEventType = "preview.generate"
|
||||
const previewSweepEventType = "preview.sweep"
|
||||
const cityReconcileEventType = "city.reconcile"
|
||||
|
||||
var cityActivitySubscription = store.ActivitySubscription{ConsumerID: "city", EventTypes: []string{"page.created", "revision.created", "path.renamed", "path.unlinked", "object.deleted"}}
|
||||
|
||||
type previewEventPayload struct {
|
||||
RevisionUUID string `json:"revision_uuid"`
|
||||
DocumentHash string `json:"document_hash"`
|
||||
ContentType string `json:"content_type"`
|
||||
Filename string `json:"filename"`
|
||||
Kind string `json:"kind"`
|
||||
Page int `json:"page,omitempty"`
|
||||
}
|
||||
|
||||
type cityReconcilePayload struct {
|
||||
DomainUUID string `json:"domain_uuid"`
|
||||
}
|
||||
|
||||
func (s *Server) initializePreviewScheduler() {
|
||||
if s.store == nil || s.store.DocumentRoot == "" {
|
||||
return
|
||||
}
|
||||
s.previewRoot = filepath.Join(filepath.Dir(s.store.DocumentRoot), "previews")
|
||||
if err := os.MkdirAll(s.previewRoot, 0755); err != nil {
|
||||
log.Printf("preview cache initialization failed: %v", err)
|
||||
s.previewRoot = ""
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startEventScheduler() {
|
||||
if s.store == nil {
|
||||
return
|
||||
}
|
||||
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
|
||||
log.Printf("resource monitor initialization failed: %v", err)
|
||||
}
|
||||
if err := s.ensureCalendarSweep(); err != nil {
|
||||
log.Printf("calendar scheduler initialization failed: %v", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskActivityBridgeEvent(); err != nil {
|
||||
log.Printf("task journal bridge initialization failed: %v", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskReminderEvent(); err != nil {
|
||||
log.Printf("task reminder initialization failed: %v", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskFocusEvent(); err != nil {
|
||||
log.Printf("focus timer initialization failed: %v", err)
|
||||
}
|
||||
if err := s.store.EnsureNotificationPushEvent(); err != nil {
|
||||
log.Printf("notification push initialization failed: %v", err)
|
||||
}
|
||||
go func() {
|
||||
initialPreviewSweep := time.NewTimer(5 * time.Second)
|
||||
defer initialPreviewSweep.Stop()
|
||||
previewSweep := time.NewTicker(15 * time.Minute)
|
||||
defer previewSweep.Stop()
|
||||
dispatch := time.NewTicker(time.Second)
|
||||
defer dispatch.Stop()
|
||||
cleanup := time.NewTicker(time.Hour)
|
||||
defer cleanup.Stop()
|
||||
activitySweep := time.NewTicker(10 * time.Second)
|
||||
defer activitySweep.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-initialPreviewSweep.C:
|
||||
s.enqueueAllPreviewEvents()
|
||||
case <-previewSweep.C:
|
||||
s.enqueueAllPreviewEvents()
|
||||
case <-dispatch.C:
|
||||
case <-cleanup.C:
|
||||
if settings, err := s.store.GetQueueSettings(); err == nil {
|
||||
if err := s.store.PurgeEventHistory(settings.RetentionHours); err != nil {
|
||||
log.Printf("event history cleanup failed: %v", err)
|
||||
}
|
||||
}
|
||||
case <-activitySweep.C:
|
||||
if _, err := s.store.ExpireApprovalRequests(context.Background(), time.Now().UnixMilli(), 100); err != nil {
|
||||
log.Printf("approval request expiry failed: %v", err)
|
||||
}
|
||||
s.enqueueCityActivityEvents()
|
||||
case <-s.schedulerWake:
|
||||
if _, err := s.store.ExpireApprovalRequests(context.Background(), time.Now().UnixMilli(), 100); err != nil {
|
||||
log.Printf("approval request expiry failed: %v", err)
|
||||
}
|
||||
s.enqueueCityActivityEvents()
|
||||
}
|
||||
if _, err := s.store.EnqueueDueDefinitions(time.Now()); err != nil {
|
||||
log.Printf("event schedule evaluation failed: %v", err)
|
||||
}
|
||||
s.runDueEvents()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Server) wakeEventScheduler() {
|
||||
if s.schedulerWake == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case s.schedulerWake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) runDueEvents() {
|
||||
for {
|
||||
settings, err := s.store.GetQueueSettings()
|
||||
if err != nil {
|
||||
log.Printf("event scheduler settings failed: %v", err)
|
||||
return
|
||||
}
|
||||
if settings.Paused || s.schedulerRunning.Load() >= int64(settings.Concurrency) {
|
||||
return
|
||||
}
|
||||
// Keep one reserved background slot while foreground work is active.
|
||||
// A global stop lets long-lived editor/WebSocket requests starve jobs.
|
||||
if s.foregroundRequests.Load() > 0 && s.schedulerRunning.Load() > 0 {
|
||||
return
|
||||
}
|
||||
job, err := s.store.ClaimDueEvent()
|
||||
if errors.Is(err, sql.ErrNoRows) || errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("event scheduler claim failed: %v", err)
|
||||
return
|
||||
}
|
||||
s.schedulerRunning.Add(1)
|
||||
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", "execution started")
|
||||
go func(job *store.EventJob) {
|
||||
defer func() {
|
||||
s.schedulerRunning.Add(-1)
|
||||
s.wakeEventScheduler()
|
||||
}()
|
||||
var runErr error
|
||||
if s.store.EventCancellationRequested(job.UUID) {
|
||||
runErr = errors.New("cancelled before execution")
|
||||
} else {
|
||||
runErr = s.runEvent(job)
|
||||
if runErr == nil && s.store.EventCancellationRequested(job.UUID) {
|
||||
runErr = errors.New("cancelled during execution")
|
||||
}
|
||||
}
|
||||
if finishErr := s.store.FinishEvent(job.UUID, runErr); finishErr != nil {
|
||||
log.Printf("event scheduler finish failed job=%s: %v", job.UUID, finishErr)
|
||||
}
|
||||
if runErr != nil {
|
||||
log.Printf("event job failed type=%s key=%s: %v", job.Type, job.Key, runErr)
|
||||
}
|
||||
}(job)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) runEvent(job *store.EventJob) error {
|
||||
switch job.Type {
|
||||
case store.NotificationPushEventType:
|
||||
return s.deliverNotificationPush(job)
|
||||
case store.TaskFocusEventType:
|
||||
processed, err := s.store.CompleteTaskFocus(context.Background(), time.Now(), 100)
|
||||
if err == nil && processed > 0 {
|
||||
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("focus timers completed=%d", processed))
|
||||
}
|
||||
return err
|
||||
case store.TaskReminderEventType:
|
||||
checked, sent, err := s.store.DeliverTaskReminders(context.Background(), time.Now(), 100)
|
||||
if err == nil && checked > 0 {
|
||||
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("task reminders checked=%d delivered=%d", checked, sent))
|
||||
}
|
||||
return err
|
||||
case store.TaskActivityBridgeEventType:
|
||||
processed, published, err := s.store.PublishTaskActivity(context.Background(), 100)
|
||||
if err == nil && processed > 0 {
|
||||
_ = s.store.AddEventLog(job.UUID, job.EventUUID, "info", fmt.Sprintf("journal entries checked=%d published=%d", processed, published))
|
||||
}
|
||||
return err
|
||||
case calendarSweepType:
|
||||
return s.sweepCalendarSources()
|
||||
case calendarSyncType:
|
||||
return s.runCalendarSource(job)
|
||||
case metadataProjectionEventType:
|
||||
var payload metadataProjectionPayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.generateMetadataProjection(payload)
|
||||
case store.ResourceEventType:
|
||||
snapshot := s.resourceStatus()
|
||||
payload, err := json.Marshal(snapshot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.SaveResourceSample(job.UUID, snapshot["timestamp_ms"].(int64), payload)
|
||||
case previewSweepEventType:
|
||||
s.enqueueAllPreviewEvents()
|
||||
return nil
|
||||
case previewEventType:
|
||||
var payload previewEventPayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.generatePreview(payload)
|
||||
case cityReconcileEventType:
|
||||
var payload cityReconcilePayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.reconcileCityActivity(payload.DomainUUID)
|
||||
case "pdf.page.generate":
|
||||
var payload previewEventPayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.generatePDFPage(payload)
|
||||
case importAnalyzeEventType:
|
||||
var payload importAnalyzePayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.analyzeImport(payload)
|
||||
case importCommitEventType:
|
||||
var payload importCommitPayload
|
||||
if err := json.Unmarshal([]byte(job.Payload), &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.commitImport(payload)
|
||||
default:
|
||||
return fmt.Errorf("unknown event type %q", job.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) enqueueCityActivityEvents() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
domains, err := s.store.PendingActivityConsumerDomains(ctx, cityActivitySubscription, 100)
|
||||
if err != nil {
|
||||
log.Printf("activity outbox domain scan failed: %v", err)
|
||||
return
|
||||
}
|
||||
for _, domain := range domains {
|
||||
payload, err := json.Marshal(cityReconcilePayload{DomainUUID: domain})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
_, err = s.store.EnqueueEventWithOptions("", cityReconcileEventType, "city:"+domain, string(payload), time.Now(), -2, 3)
|
||||
if err != nil {
|
||||
log.Printf("city activity enqueue failed domain=%s: %v", domain, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) reconcileCityActivity(domainUUID string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
events, err := s.store.ActivityOutboxForConsumer(ctx, cityActivitySubscription, domainUUID, 500)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(events) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.applyCityAutoPlan(domainUUID, "system:city"); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.CompleteActivityOutboxThrough("city", domainUUID, events[len(events)-1].Sequence)
|
||||
}
|
||||
|
||||
func (s *Server) generatePDFPage(payload previewEventPayload) error {
|
||||
if payload.Page < 1 || payload.DocumentHash == "" {
|
||||
return fmt.Errorf("invalid PDF page job")
|
||||
}
|
||||
target := filepath.Join(s.previewRoot, fmt.Sprintf("%s-p%d.jpg", payload.DocumentHash, payload.Page))
|
||||
if info, err := os.Stat(target); err == nil && info.Size() > 0 {
|
||||
return nil
|
||||
}
|
||||
source, err := s.store.DocumentPath(payload.DocumentHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workDir, err := os.MkdirTemp(s.previewRoot, ".pdf-page-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(workDir)
|
||||
base := filepath.Join(workDir, "page")
|
||||
if err := runPreviewCommand("pdftoppm", "-f", strconv.Itoa(payload.Page), "-l", strconv.Itoa(payload.Page), "-singlefile", "-jpeg", "-scale-to", "1600", source, base); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(base+".jpg", target)
|
||||
}
|
||||
|
||||
func (s *Server) previewCachePath(hash, kind string) string {
|
||||
extension := ".jpg"
|
||||
if kind == "excalidraw" {
|
||||
extension = ".svg"
|
||||
}
|
||||
return filepath.Join(s.previewRoot, hash+extension)
|
||||
}
|
||||
|
||||
func (s *Server) previewCacheURL(hash, kind string) string {
|
||||
extension := ".jpg"
|
||||
if kind == "excalidraw" {
|
||||
extension = ".svg"
|
||||
}
|
||||
return "/v/preview-cache/" + hash + extension
|
||||
}
|
||||
|
||||
func (s *Server) handlePreviewCache(w http.ResponseWriter, r *http.Request) {
|
||||
if s.previewRoot == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
name := filepath.Base(strings.TrimPrefix(r.URL.Path, "/v/preview-cache/"))
|
||||
if name == "." || name == "" ||
|
||||
(!strings.HasSuffix(name, ".jpg") && !strings.HasSuffix(name, ".svg")) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
path := filepath.Join(s.previewRoot, name)
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
|
||||
w.Header().Set("Vary", "Authorization, Cookie")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
if strings.HasSuffix(name, ".svg") {
|
||||
w.Header().Set("Content-Security-Policy", "sandbox")
|
||||
}
|
||||
http.ServeFile(w, r, path)
|
||||
}
|
||||
|
||||
func (s *Server) enqueuePreview(entry store.BrowserEntry) (string, bool) {
|
||||
if s.previewRoot == "" || entry.DocumentHash == "" {
|
||||
return "", false
|
||||
}
|
||||
filename := entry.Filename
|
||||
if filename == "" {
|
||||
filename = filepath.Base(entry.Path)
|
||||
}
|
||||
adapter := selectProjectionAdapter(contentDescriptor{
|
||||
MediaType: strings.ToLower(strings.Split(entry.ContentType, ";")[0]),
|
||||
Filename: filename,
|
||||
})
|
||||
kind := adapter.ID
|
||||
if kind != "image" && kind != "pdf" &&
|
||||
kind != "video" && kind != "excalidraw" && kind != "archive" {
|
||||
return "", false
|
||||
}
|
||||
cachePath := s.previewCachePath(entry.DocumentHash, kind)
|
||||
cacheURL := s.previewCacheURL(entry.DocumentHash, kind)
|
||||
if info, err := os.Stat(cachePath); err == nil && info.Size() > 0 {
|
||||
return cacheURL, true
|
||||
}
|
||||
payload, err := json.Marshal(previewEventPayload{
|
||||
RevisionUUID: entry.RevisionUUID,
|
||||
DocumentHash: entry.DocumentHash,
|
||||
ContentType: entry.ContentType,
|
||||
Filename: filename,
|
||||
Kind: kind,
|
||||
})
|
||||
if err == nil {
|
||||
if queued, queueErr := s.store.EnqueueEvent(
|
||||
previewEventType,
|
||||
entry.DocumentHash,
|
||||
string(payload),
|
||||
time.Now(),
|
||||
); queueErr == nil && queued {
|
||||
s.wakeEventScheduler()
|
||||
}
|
||||
}
|
||||
return cacheURL, false
|
||||
}
|
||||
|
||||
func (s *Server) enqueueAllPreviewEvents() int {
|
||||
domains, err := s.store.ListDomains()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
count := 0
|
||||
for _, domain := range domains {
|
||||
if !domain.Enabled || domain.Placement != "local" {
|
||||
continue
|
||||
}
|
||||
entries, err := s.store.ListPreviewCandidatesInDomain(domain.UUID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if _, cached := s.enqueuePreview(entry); !cached {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
s.wakeEventScheduler()
|
||||
return count
|
||||
}
|
||||
|
||||
func (s *Server) generatePreview(payload previewEventPayload) error {
|
||||
target := s.previewCachePath(payload.DocumentHash, payload.Kind)
|
||||
if info, err := os.Stat(target); err == nil && info.Size() > 0 {
|
||||
return nil
|
||||
}
|
||||
source, err := s.store.DocumentPath(payload.DocumentHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workDir, err := os.MkdirTemp(s.previewRoot, ".job-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(workDir)
|
||||
|
||||
var generated string
|
||||
switch payload.Kind {
|
||||
case "image":
|
||||
generated = filepath.Join(workDir, "preview.jpg")
|
||||
err = generateImageThumbnail(source, generated)
|
||||
case "pdf":
|
||||
generated = filepath.Join(workDir, "preview.jpg")
|
||||
err = runPreviewCommand(
|
||||
"pdftoppm",
|
||||
"-f", "1", "-singlefile", "-jpeg",
|
||||
"-scale-to", "480",
|
||||
source,
|
||||
strings.TrimSuffix(generated, ".jpg"),
|
||||
)
|
||||
case "video":
|
||||
generated = filepath.Join(workDir, "preview.jpg")
|
||||
err = runPreviewCommand(
|
||||
"ffmpeg", "-hide_banner", "-loglevel", "error",
|
||||
"-ss", "1", "-i", source,
|
||||
"-frames:v", "1", "-vf", "scale=480:-2",
|
||||
"-y", generated,
|
||||
)
|
||||
case "excalidraw":
|
||||
generated = filepath.Join(workDir, "preview.svg")
|
||||
err = generateExcalidrawSVG(source, generated)
|
||||
case "archive":
|
||||
generated = filepath.Join(workDir, "preview.jpg")
|
||||
err = generateArchiveThumbnail(source, generated, payload.Filename)
|
||||
default:
|
||||
return fmt.Errorf("unsupported preview kind %q", payload.Kind)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(generated, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(generated, target)
|
||||
}
|
||||
|
||||
func runPreviewCommand(name string, args ...string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
command := exec.CommandContext(ctx, "nice", append([]string{"-n", "15", name}, args...)...)
|
||||
output, err := command.CombinedOutput()
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s failed: %w: %s", name, err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateImageThumbnail(source, target string) error {
|
||||
file, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
return generateImageThumbnailFromReader(file, target)
|
||||
}
|
||||
|
||||
func generateArchiveThumbnail(source, target, filename string) error {
|
||||
if strings.EqualFold(filepath.Ext(filename), ".zip") {
|
||||
return generateZIPThumbnail(source, target)
|
||||
}
|
||||
return store.ErrEventSkipped
|
||||
}
|
||||
|
||||
func generateZIPThumbnail(source, target string) error {
|
||||
reader, err := zip.OpenReader(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
imageCount, fileCount := 0, 0
|
||||
for _, file := range reader.File {
|
||||
if file.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
fileCount++
|
||||
if archiveImageName(file.Name) {
|
||||
imageCount++
|
||||
}
|
||||
}
|
||||
if imageCount < 3 || imageCount*2 < fileCount {
|
||||
return fmt.Errorf("%w: archive is not image-dominant", store.ErrEventSkipped)
|
||||
}
|
||||
for _, file := range reader.File {
|
||||
if file.FileInfo().IsDir() || !archiveImageName(file.Name) {
|
||||
continue
|
||||
}
|
||||
input, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = generateImageThumbnailFromReader(input, target)
|
||||
input.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%w: archive has no supported comic images", store.ErrEventSkipped)
|
||||
}
|
||||
|
||||
func archiveImageName(name string) bool {
|
||||
switch strings.ToLower(filepath.Ext(name)) {
|
||||
case ".jpg", ".jpeg", ".png", ".gif":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func generateImageThumbnailFromReader(input io.Reader, target string) error {
|
||||
decoded, _, err := image.Decode(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bounds := decoded.Bounds()
|
||||
width, height := bounds.Dx(), bounds.Dy()
|
||||
if width <= 0 || height <= 0 {
|
||||
return errors.New("invalid image dimensions")
|
||||
}
|
||||
scale := min(480.0/float64(width), 300.0/float64(height), 1)
|
||||
outWidth := max(1, int(float64(width)*scale))
|
||||
outHeight := max(1, int(float64(height)*scale))
|
||||
thumbnail := image.NewRGBA(image.Rect(0, 0, outWidth, outHeight))
|
||||
for y := 0; y < outHeight; y++ {
|
||||
for x := 0; x < outWidth; x++ {
|
||||
sourceX := bounds.Min.X + x*width/outWidth
|
||||
sourceY := bounds.Min.Y + y*height/outHeight
|
||||
thumbnail.Set(x, y, decoded.At(sourceX, sourceY))
|
||||
}
|
||||
}
|
||||
output, err := os.Create(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer output.Close()
|
||||
return jpeg.Encode(output, thumbnail, &jpeg.Options{Quality: 78})
|
||||
}
|
||||
|
||||
func generateExcalidrawSVG(source, target string) error {
|
||||
data, err := os.ReadFile(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var scene struct {
|
||||
Elements []struct {
|
||||
Type string `json:"type"`
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
Width float64 `json:"width"`
|
||||
Height float64 `json:"height"`
|
||||
Text string `json:"text"`
|
||||
StrokeColor string `json:"strokeColor"`
|
||||
BackgroundColor string `json:"backgroundColor"`
|
||||
IsDeleted bool `json:"isDeleted"`
|
||||
} `json:"elements"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &scene); err != nil {
|
||||
return err
|
||||
}
|
||||
var body strings.Builder
|
||||
body.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 500">`)
|
||||
body.WriteString(`<rect width="100%" height="100%" fill="#fff"/>`)
|
||||
for _, element := range scene.Elements {
|
||||
if element.IsDeleted {
|
||||
continue
|
||||
}
|
||||
stroke := element.StrokeColor
|
||||
if stroke == "" {
|
||||
stroke = "#1b1b1f"
|
||||
}
|
||||
fill := element.BackgroundColor
|
||||
if fill == "" || fill == "transparent" {
|
||||
fill = "none"
|
||||
}
|
||||
switch element.Type {
|
||||
case "ellipse":
|
||||
fmt.Fprintf(&body,
|
||||
`<ellipse cx="%g" cy="%g" rx="%g" ry="%g" fill="%s" stroke="%s"/>`,
|
||||
element.X+element.Width/2, element.Y+element.Height/2,
|
||||
element.Width/2, element.Height/2, html.EscapeString(fill), html.EscapeString(stroke))
|
||||
case "text":
|
||||
fmt.Fprintf(&body,
|
||||
`<text x="%g" y="%g" fill="%s" font-family="sans-serif" font-size="20">%s</text>`,
|
||||
element.X, element.Y+20, html.EscapeString(stroke), html.EscapeString(element.Text))
|
||||
default:
|
||||
fmt.Fprintf(&body,
|
||||
`<rect x="%g" y="%g" width="%g" height="%g" fill="%s" stroke="%s"/>`,
|
||||
element.X, element.Y, element.Width, element.Height,
|
||||
html.EscapeString(fill), html.EscapeString(stroke))
|
||||
}
|
||||
}
|
||||
body.WriteString("</svg>")
|
||||
return os.WriteFile(target, []byte(body.String()), 0644)
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestExecutionMonitorChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, _ := configFixture(t)
|
||||
seedMonitorJobs(t, s)
|
||||
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.runEvent(&store.EventJob{UUID: "resource-browser", Type: store.ResourceEventType}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/monitor-interactions.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.Header.Get("Origin") != "" {
|
||||
r.Header.Set("Origin", "http://localhost")
|
||||
}
|
||||
if r.Method == "GET" && r.URL.Path == "/-/sys/config" {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
||||
defer cancel()
|
||||
profile := t.TempDir()
|
||||
for _, view := range []string{"monitor-interactions", "monitor-dialog"} {
|
||||
path := server.URL + "/-/sys/config?panel=scheduler"
|
||||
if view == "monitor-dialog" {
|
||||
path += "&monitor_dialog=1"
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", path, profile)
|
||||
if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd.Args = append(cmd.Args, filepath.Join(dir, view+".png"))
|
||||
}
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%s browser: %v %s", view, err, out)
|
||||
}
|
||||
}
|
||||
var interval int
|
||||
if err := s.store.DB.QueryRow(`SELECT interval_seconds FROM event_definitions WHERE uuid=?`, store.ResourceEventID).Scan(&interval); err != nil || interval != 120 {
|
||||
t.Fatalf("monitor dialog setting not saved: %d %v", interval, err)
|
||||
}
|
||||
}
|
||||
|
||||
func seedMonitorJobs(t *testing.T, s *Server) {
|
||||
t.Helper()
|
||||
now := time.Now().UnixMilli()
|
||||
for i := 0; i < 65; i++ {
|
||||
status, at := "succeeded", now
|
||||
if i == 0 {
|
||||
status, at = "running", now-3600000
|
||||
}
|
||||
if i == 1 {
|
||||
status = "queued"
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO event_jobs(uuid,event_type,event_key,payload,status,run_at_ms,started_at_ms,created_at_ms,updated_at_ms) VALUES(?, 'test.monitor', ?, '{}', ?, ?, ?, ?, ?)`, fmt.Sprintf("monitor-%03d", i), fmt.Sprintf("job-%03d", i), status, at, at, at, at); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for i := 0; i < 105; i++ {
|
||||
if err := s.store.AddEventLog("monitor-064", "", "info", fmt.Sprintf("entry-%03d <unsafe>", i)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionMonitorFilteringPaginationAndPolicy(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
seedMonitorJobs(t, s)
|
||||
if _, err := s.store.DB.Exec(`UPDATE event_jobs SET payload=? WHERE uuid='monitor-000'`, strings.Repeat("large-input", 1000)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page := func(status, cursor string) (jobs []store.EventJob, next string) {
|
||||
t.Helper()
|
||||
w := configRequest(s, token, "GET", "/_/api/v1/queue/jobs?limit=10&status="+status+"&cursor="+url.QueryEscape(cursor), nil)
|
||||
var data struct {
|
||||
Jobs []store.EventJob `json:"jobs"`
|
||||
Next string `json:"next_cursor"`
|
||||
}
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &data) != nil {
|
||||
t.Fatalf("page: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
return data.Jobs, data.Next
|
||||
}
|
||||
jobs, next := page("running", "")
|
||||
if len(jobs) != 1 || jobs[0].UUID != "monitor-000" || jobs[0].Payload != "" || next != "" {
|
||||
t.Fatalf("long running hidden: %#v %s", jobs, next)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for cursor := ""; ; {
|
||||
jobs, next = page("history", cursor)
|
||||
for _, job := range jobs {
|
||||
if seen[job.UUID] || job.Status != "succeeded" {
|
||||
t.Fatalf("duplicate or nonterminal job: %#v", job)
|
||||
}
|
||||
seen[job.UUID] = true
|
||||
}
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
cursor = next
|
||||
}
|
||||
if len(seen) != 63 {
|
||||
t.Fatalf("history lost jobs: %d", len(seen))
|
||||
}
|
||||
rows, err := s.store.DB.Query(`EXPLAIN QUERY PLAN SELECT uuid FROM event_jobs INDEXED BY idx_event_jobs_history WHERE status IN ('succeeded','skipped','failed','cancelled') AND (created_at_ms,uuid)<(?,?) ORDER BY created_at_ms DESC,uuid DESC LIMIT 51`, time.Now().UnixMilli()+1, "z")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var id, parent, unused int
|
||||
var plan string
|
||||
if err := rows.Scan(&id, &parent, &unused, &plan); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(plan, "TEMP B-TREE") {
|
||||
t.Errorf("history sorted entire retention window: %s", plan)
|
||||
}
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, query := range []string{"status=bogus", "cursor=broken"} {
|
||||
if w := configRequest(s, token, "GET", "/_/api/v1/queue/jobs?"+query, nil); w.Code != 400 {
|
||||
t.Fatalf("invalid query accepted: %d", w.Code)
|
||||
}
|
||||
}
|
||||
var logs struct {
|
||||
Logs []store.EventLog `json:"logs"`
|
||||
}
|
||||
w := configRequest(s, token, "GET", "/_/api/v1/event/logs?job_uuid=monitor-064&limit=100", nil)
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &logs) != nil || len(logs.Logs) != 100 {
|
||||
t.Fatalf("logs: %s", w.Body.String())
|
||||
}
|
||||
last := logs.Logs[99].ID
|
||||
w = configRequest(s, token, "GET", fmt.Sprintf("/_/api/v1/event/logs?job_uuid=monitor-064&limit=100&before_id=%d", last), nil)
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &logs) != nil || len(logs.Logs) != 5 || logs.Logs[0].ID >= last {
|
||||
t.Fatalf("older logs: %s", w.Body.String())
|
||||
}
|
||||
member, err := s.store.CreatePasswordAccount(domain, "observer", "long observer password", "Observer", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
memberToken, err := s.store.CreateAuthSession(domain, member.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []string{"/_/api/v1/queue/jobs?status=running", "/_/api/v1/event/logs?job_uuid=monitor-064", "/_/api/v1/queue/resources"} {
|
||||
if w := configRequest(s, memberToken, "GET", path, nil); w.Code != 403 {
|
||||
t.Fatalf("member got %s: %d", path, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ type ExplorerNode struct {
|
||||
}
|
||||
|
||||
func (s *Server) buildExplorerTree(
|
||||
domainUUID string,
|
||||
path string,
|
||||
depth int,
|
||||
) []ExplorerNode {
|
||||
@@ -19,7 +20,7 @@ func (s *Server) buildExplorerTree(
|
||||
}
|
||||
|
||||
children, err :=
|
||||
s.store.ListChildren(path)
|
||||
s.store.ListChildrenInDomain(domainUUID, path)
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -35,6 +36,7 @@ func (s *Server) buildExplorerTree(
|
||||
|
||||
node.Children =
|
||||
s.buildExplorerTree(
|
||||
domainUUID,
|
||||
child,
|
||||
depth+1,
|
||||
)
|
||||
@@ -52,12 +54,14 @@ func (s *Server) handleExplorer(
|
||||
|
||||
tree :=
|
||||
s.buildExplorerTree(
|
||||
s.domainUUIDForRequest(r),
|
||||
"/",
|
||||
0,
|
||||
)
|
||||
|
||||
s.render(
|
||||
s.renderWithContext(
|
||||
w,
|
||||
r,
|
||||
"explorer.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Explorer",
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
systemext "pageswiki/internal/extensions"
|
||||
)
|
||||
|
||||
const systemExtensionStateKey = "system_extension_states"
|
||||
|
||||
func (s *Server) systemExtensionEnabled(domainUUID, id string) bool {
|
||||
raw, err := s.store.GetConfigInDomain(domainUUID, systemExtensionStateKey)
|
||||
if err != nil || strings.TrimSpace(raw) == "" {
|
||||
return true
|
||||
}
|
||||
var states map[string]bool
|
||||
if json.Unmarshal([]byte(raw), &states) != nil {
|
||||
return true
|
||||
}
|
||||
enabled, ok := states[id]
|
||||
return !ok || enabled
|
||||
}
|
||||
|
||||
func (s *Server) setSystemExtensionEnabled(domainUUID, id string, enabled bool) error {
|
||||
raw, _ := s.store.GetConfigInDomain(domainUUID, systemExtensionStateKey)
|
||||
states := map[string]bool{}
|
||||
_ = json.Unmarshal([]byte(raw), &states)
|
||||
states[id] = enabled
|
||||
payload, err := json.Marshal(states)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.SetConfigInDomain(domainUUID, systemExtensionStateKey, string(payload))
|
||||
}
|
||||
|
||||
func (s *Server) systemExtensionCatalog(domainUUID string) []systemext.Manifest {
|
||||
items := systemext.Catalog()
|
||||
for i := range items {
|
||||
items[i].Enabled = s.systemExtensionEnabled(domainUUID, items[i].ID)
|
||||
}
|
||||
return items
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const externalResourceApprovalsConfig = "external_resource_approvals_v1"
|
||||
const maxExternalResourceApprovals = 128
|
||||
|
||||
type externalResourceApproval struct {
|
||||
Kind string `json:"kind"`
|
||||
URL string `json:"url"`
|
||||
Integrity string `json:"integrity,omitempty"`
|
||||
}
|
||||
|
||||
type externalResourcePolicy struct {
|
||||
items []externalResourceApproval
|
||||
byKind map[string]map[string]externalResourceApproval
|
||||
}
|
||||
|
||||
func normalizeExternalResourceURL(raw string) (string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || len(raw) > 2048 || strings.ContainsAny(raw, " \\\r\n\t") {
|
||||
return "", errors.New("invalid external resource URL")
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || !u.IsAbs() || !strings.EqualFold(u.Scheme, "https") || u.Host == "" || u.User != nil || u.Fragment != "" {
|
||||
return "", errors.New("external resources require an absolute HTTPS URL without credentials or fragments")
|
||||
}
|
||||
u.Scheme = "https"
|
||||
u.Host = strings.ToLower(u.Host)
|
||||
u.RawFragment = ""
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func normalizeExternalResourceIntegrity(raw string) (string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if len(raw) == 64 {
|
||||
decoded, err := hex.DecodeString(raw)
|
||||
if err == nil && len(decoded) == 32 {
|
||||
return "sha256-" + base64.StdEncoding.EncodeToString(decoded), nil
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(raw, "sha256-") {
|
||||
return "", errors.New("SHA-256 integrity is required")
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(raw, "sha256-"))
|
||||
if err != nil || len(decoded) != 32 {
|
||||
return "", errors.New("invalid SHA-256 integrity")
|
||||
}
|
||||
return "sha256-" + base64.StdEncoding.EncodeToString(decoded), nil
|
||||
}
|
||||
|
||||
func normalizeExternalResourceApproval(item externalResourceApproval) (externalResourceApproval, error) {
|
||||
item.Kind = strings.ToLower(strings.TrimSpace(item.Kind))
|
||||
if item.Kind != "image" && item.Kind != "stylesheet" && item.Kind != "script" {
|
||||
return externalResourceApproval{}, errors.New("invalid external resource kind")
|
||||
}
|
||||
var err error
|
||||
item.URL, err = normalizeExternalResourceURL(item.URL)
|
||||
if err != nil {
|
||||
return externalResourceApproval{}, err
|
||||
}
|
||||
if item.Kind == "image" {
|
||||
item.Integrity = ""
|
||||
return item, nil
|
||||
}
|
||||
item.Integrity, err = normalizeExternalResourceIntegrity(item.Integrity)
|
||||
if err != nil {
|
||||
return externalResourceApproval{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func newExternalResourcePolicy(items []externalResourceApproval) externalResourcePolicy {
|
||||
p := externalResourcePolicy{byKind: map[string]map[string]externalResourceApproval{}}
|
||||
seen := map[string]bool{}
|
||||
for _, raw := range items {
|
||||
item, err := normalizeExternalResourceApproval(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
key := item.Kind + "\x00" + item.URL
|
||||
if seen[key] || len(p.items) >= maxExternalResourceApprovals {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
if p.byKind[item.Kind] == nil {
|
||||
p.byKind[item.Kind] = map[string]externalResourceApproval{}
|
||||
}
|
||||
p.byKind[item.Kind][item.URL] = item
|
||||
p.items = append(p.items, item)
|
||||
}
|
||||
sort.Slice(p.items, func(i, j int) bool {
|
||||
if p.items[i].Kind == p.items[j].Kind {
|
||||
return p.items[i].URL < p.items[j].URL
|
||||
}
|
||||
return p.items[i].Kind < p.items[j].Kind
|
||||
})
|
||||
return p
|
||||
}
|
||||
|
||||
func (p externalResourcePolicy) approved(kind, rawURL string) (externalResourceApproval, bool) {
|
||||
normalized, err := normalizeExternalResourceURL(rawURL)
|
||||
if err != nil {
|
||||
return externalResourceApproval{}, false
|
||||
}
|
||||
item, ok := p.byKind[kind][normalized]
|
||||
return item, ok
|
||||
}
|
||||
|
||||
func (s *Server) externalResourcePolicy(domainUUID string) externalResourcePolicy {
|
||||
var items []externalResourceApproval
|
||||
raw := strings.TrimSpace(s.getDomainConfig(externalResourceApprovalsConfig, domainUUID))
|
||||
if raw != "" {
|
||||
_ = json.Unmarshal([]byte(raw), &items)
|
||||
}
|
||||
return newExternalResourcePolicy(items)
|
||||
}
|
||||
|
||||
func (s *Server) saveExternalResourceApprovals(domainUUID string, items []externalResourceApproval) error {
|
||||
policy := newExternalResourcePolicy(items)
|
||||
if len(policy.items) != len(items) {
|
||||
return errors.New("invalid or duplicate external resource approval")
|
||||
}
|
||||
raw, err := json.Marshal(policy.items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.SetConfigInDomain(domainUUID, externalResourceApprovalsConfig, string(raw))
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const externalResourceTestSRI = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
||||
|
||||
func TestExternalResourceApprovalNormalization(t *testing.T) {
|
||||
item, err := normalizeExternalResourceApproval(externalResourceApproval{
|
||||
Kind: " SCRIPT ", URL: "https://CDN.Example.test/app.js?v=1", Integrity: strings.Repeat("00", 32),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if item.Kind != "script" || item.URL != "https://cdn.example.test/app.js?v=1" || item.Integrity != externalResourceTestSRI {
|
||||
t.Fatalf("unexpected normalization: %+v", item)
|
||||
}
|
||||
for _, raw := range []string{"http://cdn.example.test/app.js", "https://user@cdn.example.test/app.js", "https://cdn.example.test/app.js#fragment", "//cdn.example.test/app.js"} {
|
||||
if _, err := normalizeExternalResourceURL(raw); err == nil {
|
||||
t.Fatalf("unsafe URL accepted: %s", raw)
|
||||
}
|
||||
}
|
||||
if _, err := normalizeExternalResourceApproval(externalResourceApproval{Kind: "script", URL: "https://cdn.example.test/app.js"}); err == nil {
|
||||
t.Fatal("script without SRI accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLProjectionAllowsOnlyApprovedExternalResources(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
items := []externalResourceApproval{
|
||||
{Kind: "image", URL: "https://media.example.test/avatar.png"},
|
||||
{Kind: "stylesheet", URL: "https://cdn.example.test/profile.css", Integrity: externalResourceTestSRI},
|
||||
{Kind: "script", URL: "https://cdn.example.test/profile.js", Integrity: externalResourceTestSRI},
|
||||
}
|
||||
if err := s.saveExternalResourceApprovals(domain, items); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := `<img src="https://media.example.test/avatar.png" onerror="alert(1)">` +
|
||||
`<img src="https://media.example.test/not-approved.png">` +
|
||||
`<link rel="stylesheet" href="https://cdn.example.test/profile.css" integrity="sha256-bogus">` +
|
||||
`<link rel="stylesheet" href="https://cdn.example.test/other.css">` +
|
||||
`<script src="https://cdn.example.test/profile.js" integrity="sha256-bogus">alert(1)</script>` +
|
||||
`<script src="https://cdn.example.test/other.js"></script><script>alert(1)</script>`
|
||||
got := s.safeHTMLProjection(httptest.NewRequest(http.MethodGet, "http://localhost/u/anya", nil), body, "/u/anya", true)
|
||||
for _, want := range []string{
|
||||
`src="https://media.example.test/avatar.png"`,
|
||||
`href="https://cdn.example.test/profile.css"`,
|
||||
`src="https://cdn.example.test/profile.js"`,
|
||||
`integrity="` + externalResourceTestSRI + `"`,
|
||||
`crossorigin="anonymous"`,
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("approved resource missing %q: %s", want, got)
|
||||
}
|
||||
}
|
||||
for _, forbidden := range []string{"not-approved.png", "other.css", "other.js", "sha256-bogus", "onerror", ">alert(1)<"} {
|
||||
if strings.Contains(got, forbidden) {
|
||||
t.Fatalf("unapproved resource survived %q: %s", forbidden, got)
|
||||
}
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
if _, err := setProjectionCSP(w, s.externalResourcePolicy(domain)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
csp := w.Header().Get("Content-Security-Policy")
|
||||
for _, want := range []string{"https://media.example.test", "https://cdn.example.test", "'" + externalResourceTestSRI + "'", "connect-src 'self'"} {
|
||||
if !strings.Contains(csp, want) {
|
||||
t.Fatalf("CSP missing %q: %s", want, csp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalResourceApprovalConfig(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
page := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=external-resources", nil)
|
||||
if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), `id="panel-external-resources"`) {
|
||||
t.Fatalf("config page status=%d body=%s", page.Code, page.Body.String())
|
||||
}
|
||||
w := configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
||||
"section": {"external-resource-add"}, "kind": {"image"}, "url": {"https://media.example.test/avatar.png"},
|
||||
})
|
||||
if w.Code != http.StatusFound {
|
||||
t.Fatalf("add status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if _, ok := s.externalResourcePolicy(domain).approved("image", "https://media.example.test/avatar.png"); !ok {
|
||||
t.Fatal("approval was not saved")
|
||||
}
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
||||
"section": {"external-resource-delete"}, "kind": {"image"}, "url": {"https://media.example.test/avatar.png"},
|
||||
})
|
||||
if w.Code != http.StatusFound || len(s.externalResourcePolicy(domain).items) != 0 {
|
||||
t.Fatalf("delete status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
||||
"section": {"external-resource-add"}, "kind": {"script"}, "url": {"https://cdn.example.test/app.js"}, "integrity": {externalResourceTestSRI},
|
||||
})
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("script risk confirmation bypassed: %d", w.Code)
|
||||
}
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
||||
"section": {"external-resource-add"}, "kind": {"script"}, "url": {"https://cdn.example.test/app.js"}, "integrity": {externalResourceTestSRI}, "confirm_script_risk": {"1"},
|
||||
})
|
||||
if w.Code != http.StatusFound {
|
||||
t.Fatalf("confirmed script add status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"pageswiki/internal/store"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type federationEnrollRequest struct {
|
||||
FQDN string `json:"fqdn"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
PublicKey string `json:"public_key"`
|
||||
Signature string `json:"signature"`
|
||||
ManifestVersion int `json:"manifest_version"`
|
||||
CapabilitySignature string `json:"capability_signature"`
|
||||
}
|
||||
|
||||
func (s *Server) handleFederationEnrollAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
authorization := strings.TrimSpace(r.Header.Get("Authorization"))
|
||||
const bearer = "Bearer "
|
||||
if !strings.HasPrefix(authorization, bearer) {
|
||||
http.Error(w, "bearer token is required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
var request federationEnrollRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
campus, instance, err := s.store.RegisterFederationInstance(
|
||||
strings.TrimSpace(strings.TrimPrefix(authorization, bearer)),
|
||||
request.FQDN,
|
||||
request.Fingerprint,
|
||||
request.PublicKey,
|
||||
request.Signature,
|
||||
store.FederationCapabilities{ManifestVersion: request.ManifestVersion, Signature: request.CapabilitySignature},
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
domains, err := s.store.ListDomains()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
filtered := domains[:0]
|
||||
if instance.Status == "approved" {
|
||||
for _, domain := range domains {
|
||||
if domain.CampusUUID == campus.UUID {
|
||||
filtered = append(filtered, domain)
|
||||
}
|
||||
}
|
||||
}
|
||||
manifest, err := s.store.BuildSignedCampusManifest(*campus, filtered)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"campus": campus,
|
||||
"instance": instance,
|
||||
"domains": filtered,
|
||||
"manifest": manifest,
|
||||
})
|
||||
}
|
||||
+54
-101
@@ -4,115 +4,68 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleZombieListAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
graceSeconds := int64(86400)
|
||||
|
||||
if v := r.URL.Query().Get("grace_seconds"); v != "" {
|
||||
|
||||
n, err :=
|
||||
strconv.ParseInt(
|
||||
v,
|
||||
10,
|
||||
64,
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
graceSeconds = n
|
||||
func zombieAPIOptions(r *http.Request) (int64, int, error) {
|
||||
grace := store.ZombieGCDefaultGraceSeconds
|
||||
limit := store.ZombieGCDefaultLimit
|
||||
if raw := r.URL.Query().Get("grace_seconds"); raw != "" {
|
||||
n, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || n < 0 {
|
||||
return 0, 0, store.ErrZombieGCOptions
|
||||
}
|
||||
grace = n
|
||||
}
|
||||
|
||||
zombies, err :=
|
||||
s.store.ListZombieObjects(
|
||||
graceSeconds,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
if raw := r.URL.Query().Get("limit"); raw != "" {
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 || n > store.ZombieGCMaxLimit {
|
||||
return 0, 0, store.ErrZombieGCOptions
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
map[string]any{
|
||||
"zombies": zombies,
|
||||
"count": len(zombies),
|
||||
},
|
||||
)
|
||||
return grace, limit, nil
|
||||
}
|
||||
|
||||
func (s *Server) handleZombiePurgeAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
graceSeconds := int64(86400)
|
||||
limit := 100
|
||||
|
||||
if v := r.URL.Query().Get("grace_seconds"); v != "" {
|
||||
|
||||
n, err :=
|
||||
strconv.ParseInt(
|
||||
v,
|
||||
10,
|
||||
64,
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
graceSeconds = n
|
||||
}
|
||||
}
|
||||
|
||||
if v := r.URL.Query().Get("limit"); v != "" {
|
||||
|
||||
n, err :=
|
||||
strconv.Atoi(v)
|
||||
|
||||
if err == nil {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
|
||||
deleted, err :=
|
||||
s.store.PurgeZombieObjects(
|
||||
graceSeconds,
|
||||
limit,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
grace, limit, err := zombieAPIOptions(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
zombies, err := s.store.ListZombieObjectsInDomain(r.Context(), s.domainUUIDForRequest(r), grace, limit)
|
||||
if err != nil {
|
||||
http.Error(w, "unable to list zombie objects", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
json.NewEncoder(w).Encode(map[string]any{"zombies": zombies, "count": len(zombies), "limit": limit})
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
map[string]any{
|
||||
"deleted": deleted,
|
||||
},
|
||||
)
|
||||
func (s *Server) handleZombiePurgeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
grace, limit, err := zombieAPIOptions(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
deleted, err := s.store.PurgeZombieObjectsInDomain(r.Context(), s.domainUUIDForRequest(r), grace, limit)
|
||||
if err != nil {
|
||||
http.Error(w, "unable to purge zombie objects", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
json.NewEncoder(w).Encode(map[string]any{"deleted": deleted})
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func seedServerZombie(t *testing.T, s *Server, id, domain string) {
|
||||
t.Helper()
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',1)`, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if domain != "" {
|
||||
if _, err := s.store.DB.Exec(`INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES(?,?,1)`, id, domain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func requireServerZombie(t *testing.T, s *Server, id string, want bool) {
|
||||
t.Helper()
|
||||
var exists bool
|
||||
if err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM objects WHERE uuid=?)`, id).Scan(&exists); err != nil || exists != want {
|
||||
t.Fatalf("Object %s exists=%v want=%v err=%v", id, exists, want, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZombieHTTPAndConfigAreCurrentDomainOnly(t *testing.T) {
|
||||
for _, operation := range []string{"api", "config"} {
|
||||
t.Run(operation, func(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/gc/zombies/list", s.handleZombieListAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/gc/zombies/purge", s.handleZombiePurgeAPI)
|
||||
seedServerZombie(t, s, "gc-own", domain)
|
||||
seedServerZombie(t, s, "gc-foreign", "other-domain")
|
||||
seedServerZombie(t, s, "gc-unknown", "")
|
||||
listURL := "/_/api/v1/gc/zombies/list?domain_uuid=other-domain&grace_seconds=0"
|
||||
if operation == "config" {
|
||||
listURL = "/-/sys/config?panel=objects"
|
||||
}
|
||||
w := configRequest(s, token, http.MethodGet, listURL, nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "gc-own") || strings.Contains(w.Body.String(), "gc-foreign") || strings.Contains(w.Body.String(), "gc-unknown") {
|
||||
t.Fatalf("%s scoped listing: %d %s", operation, w.Code, w.Body.String())
|
||||
}
|
||||
if operation == "api" {
|
||||
w = configRequest(s, token, http.MethodPost, "/_/api/v1/gc/zombies/purge?domain_uuid=other-domain&grace_seconds=0", nil)
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"deleted":1`) {
|
||||
t.Fatalf("purge API: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
} else {
|
||||
w = configRequest(s, token, http.MethodPost, "/-/sys/config?panel=objects", url.Values{"section": {"zombie-purge"}, "domain_uuid": {"other-domain"}})
|
||||
if w.Code != http.StatusFound {
|
||||
t.Fatalf("config purge: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
requireServerZombie(t, s, "gc-own", false)
|
||||
requireServerZombie(t, s, "gc-foreign", true)
|
||||
requireServerZombie(t, s, "gc-unknown", true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestZombieHTTPRejectsUnboundedOptionsAndNonAdmin(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/gc/zombies/list", s.handleZombieListAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/gc/zombies/purge", s.handleZombiePurgeAPI)
|
||||
seedServerZombie(t, s, "gc-kept", domain)
|
||||
for _, query := range []string{"limit=0", "limit=-1", "limit=1001", "limit=all", "grace_seconds=-1", "grace_seconds=invalid", "grace_seconds=9223372036854775808"} {
|
||||
for _, endpoint := range []struct{ method, path string }{{http.MethodGet, "list"}, {http.MethodPost, "purge"}} {
|
||||
w := configRequest(s, token, endpoint.method, "/_/api/v1/gc/zombies/"+endpoint.path+"?"+query, nil)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("invalid %s %s accepted: %d %s", endpoint.path, query, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
member, err := s.store.CreatePasswordAccount(domain, "gc-reader", "a long reader password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
memberToken, err := s.store.CreateAuthSession(domain, member.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, endpoint := range []struct{ method, path string }{{http.MethodGet, "list"}, {http.MethodPost, "purge"}} {
|
||||
w := configRequest(s, memberToken, endpoint.method, "/_/api/v1/gc/zombies/"+endpoint.path, nil)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("non-admin %s: %d %s", endpoint.path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
requireServerZombie(t, s, "gc-kept", true)
|
||||
}
|
||||
|
||||
func TestManualEventsCannotInvokeGlobalZombieGC(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
seedServerZombie(t, s, "gc-own", domain)
|
||||
seedServerZombie(t, s, "gc-foreign", "foreign")
|
||||
seedServerZombie(t, s, "gc-unknown", "")
|
||||
post := func(path string, payload any) *httptest.ResponseRecorder {
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodPost, "http://localhost"+path, strings.NewReader(string(raw)))
|
||||
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
|
||||
}
|
||||
for _, eventType := range []string{"gc.zombies.purge", "zombie.purge", "gc.purge"} {
|
||||
definition := store.EventDefinition{Name: "Attempt global GC", EventType: eventType, EventKey: eventType, Payload: `{"grace_seconds":0,"domain_uuid":"foreign"}`, ScheduleKind: "manual", Enabled: true}
|
||||
w := post("/_/api/v1/event", definition)
|
||||
if w.Code != http.StatusCreated || json.Unmarshal(w.Body.Bytes(), &definition) != nil {
|
||||
t.Fatalf("definition: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
w = post("/_/api/v1/event/run", map[string]string{"uuid": definition.UUID})
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("manual trigger: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
job, err := s.store.ClaimDueEvent()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runErr := s.runEvent(job)
|
||||
if runErr == nil || !strings.Contains(runErr.Error(), "unknown event type") {
|
||||
t.Fatalf("manual GC type became executable: %v", runErr)
|
||||
}
|
||||
if err := s.store.FinishEvent(job.UUID, runErr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{"gc-own", "gc-foreign", "gc-unknown"} {
|
||||
requireServerZombie(t, s, id, true)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZombieMaintenanceRetainsGraceAcrossDomainChangesAndRestart(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
|
||||
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "72"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != 72*3600 {
|
||||
t.Fatalf("legacy grace shortened: %d %v", grace, err)
|
||||
}
|
||||
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if grace, err := (&Server{store: s.store}).zombieMaintenanceGrace(); err != nil || grace != 72*3600 {
|
||||
t.Fatalf("Domain changed instance retention: %d %v", grace, err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "168")
|
||||
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != 168*3600 {
|
||||
t.Fatalf("operator extension: %d %v", grace, err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "24")
|
||||
if grace, err := (&Server{store: s.store}).zombieMaintenanceGrace(); err != nil || grace != 168*3600 {
|
||||
t.Fatalf("restart shortened retention: %d %v", grace, err)
|
||||
}
|
||||
for _, value := range []string{"invalid", "-1", strconv.FormatInt(math.MaxInt64, 10)} {
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", value)
|
||||
if _, err := s.zombieMaintenanceGrace(); err == nil {
|
||||
t.Fatalf("invalid operator grace %s accepted", value)
|
||||
}
|
||||
}
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
|
||||
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", strconv.FormatInt(math.MaxInt64, 10)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if grace := s.getZombieGraceSeconds(domain); grace != math.MaxInt64 {
|
||||
t.Fatalf("hours overflowed: %d", grace)
|
||||
}
|
||||
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != math.MaxInt64 {
|
||||
t.Fatalf("legacy long grace overflow: %d %v", grace, err)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`DROP TABLE config`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.zombieMaintenanceGrace(); err == nil {
|
||||
t.Fatal("config failure silently shortened retention")
|
||||
}
|
||||
}
|
||||
|
||||
func TestZombieMaintenanceDefaultIsStill24Hours(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
|
||||
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != store.ZombieGCDefaultGraceSeconds {
|
||||
t.Fatalf("default retention changed: %d %v", grace, err)
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,51 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// This is deliberately not exposed as a user-editable EventDefinition type.
|
||||
// Instance maintenance retains the previous grace period (at least 24h), even
|
||||
// after Domain config changes or a restart. Only the operator environment can
|
||||
// explicitly increase the persistent retention high-water mark.
|
||||
func (s *Server) zombieMaintenanceGrace() (int64, error) {
|
||||
raw, err := s.store.GetConfig("zombie_grace_hours")
|
||||
if err != nil {
|
||||
return 0, err // Never shorten retention on a failed legacy config read.
|
||||
}
|
||||
grace := store.ZombieGCDefaultGraceSeconds
|
||||
if hours, err := strconv.ParseInt(raw, 10, 64); err == nil && hours > 0 {
|
||||
if hours > math.MaxInt64/3600 {
|
||||
grace = math.MaxInt64
|
||||
} else {
|
||||
grace = max(grace, hours*3600)
|
||||
}
|
||||
}
|
||||
if raw := strings.TrimSpace(os.Getenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS")); raw != "" {
|
||||
hours, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || hours < 0 || hours > math.MaxInt64/3600 {
|
||||
return 0, fmt.Errorf("invalid PAGESWIKI_ZOMBIE_GC_GRACE_HOURS")
|
||||
}
|
||||
grace = max(grace, hours*3600)
|
||||
}
|
||||
return s.store.RaiseZombieMaintenanceGrace(context.Background(), grace)
|
||||
}
|
||||
|
||||
func (s *Server) startGCWorker() {
|
||||
grace, err := s.zombieMaintenanceGrace()
|
||||
if err != nil {
|
||||
log.Printf("zombie GC maintenance disabled: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
||||
@@ -17,10 +57,11 @@ func (s *Server) startGCWorker() {
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
<-ticker.C
|
||||
|
||||
deleted, err :=
|
||||
s.store.PurgeZombieObjects(
|
||||
s.getZombieGraceSeconds(),
|
||||
grace,
|
||||
100,
|
||||
)
|
||||
|
||||
@@ -39,7 +80,6 @@ func (s *Server) startGCWorker() {
|
||||
)
|
||||
}
|
||||
|
||||
<-ticker.C
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) requestPrincipal(r *http.Request) *store.Principal {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return principal
|
||||
}
|
||||
|
||||
func (s *Server) canManageGroup(domainUUID string, principal *store.Principal, group *store.Group, ownerOnly bool) bool {
|
||||
if principal == nil {
|
||||
return false
|
||||
}
|
||||
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) == nil {
|
||||
return true
|
||||
}
|
||||
if s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
||||
return true
|
||||
}
|
||||
role := s.store.GroupMemberRole(group.UUID, principal.UUID)
|
||||
return role == "owner" || (!ownerOnly && role == "manager")
|
||||
}
|
||||
|
||||
func (s *Server) handleGroups(w http.ResponseWriter, r *http.Request) {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
remainder := ""
|
||||
if r.URL.Path != "/g" {
|
||||
remainder = strings.Trim(strings.TrimPrefix(r.URL.Path, "/g/"), "/")
|
||||
}
|
||||
if remainder == "" {
|
||||
if r.Method == http.MethodPost {
|
||||
if principal == nil || !s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
group, err := s.store.CreateGroup(domainUUID, principal.UUID, r.FormValue("name"), r.FormValue("display_name"), r.FormValue("description"), r.FormValue("visibility"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/g/"+group.Name+"/_/edit", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
groups, err := s.store.ListGroups(domainUUID, func() string {
|
||||
if principal != nil {
|
||||
return principal.UUID
|
||||
}
|
||||
return ""
|
||||
}())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
canCreate := principal != nil && s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin")
|
||||
s.renderWithContext(w, r, "groups.gohtml", map[string]any{"Groups": groups, "CanCreate": canCreate, "Locale": s.getLocale(domainUUID)})
|
||||
return
|
||||
}
|
||||
parts := strings.Split(remainder, "/")
|
||||
group, err := s.store.GroupByName(domainUUID, parts[0])
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
memberRole := ""
|
||||
if principal != nil {
|
||||
memberRole = s.store.GroupMemberRole(group.UUID, principal.UUID)
|
||||
}
|
||||
visible := group.Visibility == "public" || (group.Visibility == "domain" && principal != nil) || memberRole != "" || s.canManageGroup(domainUUID, principal, group, false)
|
||||
if !visible {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if len(parts) >= 3 && parts[1] == "_" && parts[2] == "edit" {
|
||||
if !s.canManageGroup(domainUUID, principal, group, false) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
action := r.FormValue("action")
|
||||
ownerAuthority := s.canManageGroup(domainUUID, principal, group, true)
|
||||
switch action {
|
||||
case "add_member":
|
||||
if r.FormValue("member_role") == "owner" && !ownerAuthority {
|
||||
err = store.ErrAccessDenied
|
||||
break
|
||||
}
|
||||
err = s.store.AddGroupMemberByUsername(domainUUID, group.UUID, r.FormValue("username"), r.FormValue("member_role"))
|
||||
case "remove_member":
|
||||
if role := s.store.GroupMemberRoleByMembership(group.UUID, r.FormValue("membership_uuid")); role != "member" && !ownerAuthority {
|
||||
err = store.ErrAccessDenied
|
||||
break
|
||||
}
|
||||
err = s.store.RemoveGroupMemberWithAuthority(group.UUID, r.FormValue("membership_uuid"), ownerAuthority)
|
||||
case "set_permission":
|
||||
if !ownerAuthority {
|
||||
err = store.ErrAccessDenied
|
||||
break
|
||||
}
|
||||
err = s.store.SetGroupPermission(group.UUID, r.FormValue("scope_path"), r.FormValue("permission"))
|
||||
case "set_campus_inheritance":
|
||||
domain := s.domainForRequest(r)
|
||||
if group.BuiltinKey != "campus_admin" || domain == nil || !s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID) {
|
||||
err = store.ErrAccessDenied
|
||||
break
|
||||
}
|
||||
err = s.store.SetCampusAdminInheritance(group.UUID, r.FormValue("inherit_members") == "on")
|
||||
default:
|
||||
err = errors.New("invalid group operation")
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, r.URL.Path+"?saved=1", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
members, _ := s.store.ListGroupMembers(group.UUID)
|
||||
permissions, _ := s.store.ListGroupPermissions(group.UUID)
|
||||
s.renderWithContext(w, r, "group.gohtml", map[string]any{"Group": group, "Editing": true, "Members": members, "Permissions": permissions, "CanOwn": s.canManageGroup(domainUUID, principal, group, true), "Saved": r.URL.Query().Get("saved") == "1", "Locale": s.getLocale(domainUUID)})
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
s.renderWithContext(w, r, "group.gohtml", map[string]any{"Group": group, "MemberRole": memberRole, "CanManage": s.canManageGroup(domainUUID, principal, group, false), "Locale": s.getLocale(domainUUID)})
|
||||
return
|
||||
}
|
||||
if principal == nil || s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "read", TargetType: "path", TargetID: r.URL.Path}) != nil {
|
||||
mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
||||
if mode != "enforcing" {
|
||||
s.handleSemanticRoute(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
s.handleSemanticRoute(w, r)
|
||||
}
|
||||
+42
-14
@@ -10,14 +10,26 @@ func (s *Server) handleHome(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
if s.servePublicPeriodical(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path != "/" {
|
||||
s.handleSemanticRoute(w, r)
|
||||
return
|
||||
}
|
||||
if layout := s.homeLayout(s.domainUUIDForRequest(r)); layout != "classic" {
|
||||
if layout == "diary" || layout == "blog" {
|
||||
s.handlePeriodicalView(w, r, layout, "view")
|
||||
return
|
||||
}
|
||||
s.renderSearchHome(w, r, layout)
|
||||
return
|
||||
}
|
||||
|
||||
homePath, err :=
|
||||
s.store.GetConfig(
|
||||
s.store.GetConfigInDomain(
|
||||
s.domainUUIDForRequest(r),
|
||||
"home_path",
|
||||
)
|
||||
|
||||
@@ -38,7 +50,8 @@ func (s *Server) handleHome(
|
||||
}
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(
|
||||
s.store.ResolvePathInDomain(
|
||||
s.domainUUIDForRequest(r),
|
||||
homePath,
|
||||
)
|
||||
|
||||
@@ -52,9 +65,14 @@ func (s *Server) handleHome(
|
||||
|
||||
return
|
||||
}
|
||||
if !s.requestCanReadPath(r, homePath) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
s.latestRevisionForRequest(
|
||||
r,
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
@@ -69,18 +87,19 @@ func (s *Server) handleHome(
|
||||
return
|
||||
}
|
||||
|
||||
rendered :=
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
homePath,
|
||||
)
|
||||
contentView := revisionContentView(latest, homePath)
|
||||
rendered := ""
|
||||
if contentView.Kind == "html" {
|
||||
rendered = s.safeHTMLProjection(r, latest.Body, homePath, true)
|
||||
}
|
||||
|
||||
s.render(
|
||||
s.renderWithContext(
|
||||
w,
|
||||
r,
|
||||
"page.gohtml",
|
||||
map[string]any{
|
||||
"NavItems": resolveNavItems(
|
||||
s.getHomeNavItems(),
|
||||
s.getHomeNavItems(s.domainUUIDForRequest(r)),
|
||||
NavContext{
|
||||
Path: homePath,
|
||||
ObjectUUID: objectUUID,
|
||||
@@ -92,6 +111,13 @@ func (s *Server) handleHome(
|
||||
"Path": homePath,
|
||||
"Breadcrumbs": buildBreadcrumbs(homePath),
|
||||
"ObjectUUID": objectUUID,
|
||||
"ContentView": contentView,
|
||||
"SocialSource": func() string {
|
||||
if contentView.Kind == "html" {
|
||||
return latest.Body
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
"RenderedHTML": template.HTML(
|
||||
rendered,
|
||||
),
|
||||
@@ -102,17 +128,19 @@ func (s *Server) handleHome(
|
||||
}
|
||||
|
||||
rendered :=
|
||||
s.expandMacros(
|
||||
"{{children:/ exclude=/draft,/-,/_}}",
|
||||
s.expandMacrosForRequest(r,
|
||||
"{{children:/ exclude=/draft,/-,/_,/u}}",
|
||||
"/",
|
||||
s.domainUUIDForRequest(r),
|
||||
)
|
||||
|
||||
s.render(
|
||||
s.renderWithContext(
|
||||
w,
|
||||
r,
|
||||
"home.gohtml",
|
||||
map[string]any{
|
||||
"NavItems": resolveNavItems(
|
||||
s.getHomeNavItems(),
|
||||
s.getHomeNavItems(s.domainUUIDForRequest(r)),
|
||||
NavContext{},
|
||||
),
|
||||
"RenderedHTML": template.HTML(
|
||||
|
||||
@@ -17,7 +17,8 @@ func (s *Server) handleNewPage(
|
||||
)
|
||||
|
||||
_, err :=
|
||||
s.store.CreateObject(
|
||||
s.store.CreatePageAtPathInDomain(
|
||||
s.domainUUIDForRequest(r),
|
||||
draftPath,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const defaultHomeGlassTransparency = 42
|
||||
|
||||
func normalizeHomeGlassTransparency(raw string) (int, error) {
|
||||
value, err := strconv.Atoi(strings.TrimSpace(raw))
|
||||
if err != nil || value < 0 || value > 85 {
|
||||
return 0, errors.New("invalid home glass transparency")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (s *Server) homeGlassTransparency(domainUUID string) int {
|
||||
value, err := normalizeHomeGlassTransparency(s.getDomainConfig("home_glass_transparency", domainUUID))
|
||||
if err != nil {
|
||||
return defaultHomeGlassTransparency
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (s *Server) homeLayout(domainUUID string) string {
|
||||
switch value := s.getDomainConfig("home_layout", domainUUID); value {
|
||||
case "search", "directory", "diary", "blog":
|
||||
return value
|
||||
default:
|
||||
return "classic"
|
||||
}
|
||||
}
|
||||
|
||||
func parseHomeBookmarks(raw string) ([]NavItem, error) {
|
||||
items := []NavItem{}
|
||||
for _, line := range strings.Split(raw, "\n") {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
label, href, ok := strings.Cut(line, "|")
|
||||
label, href = strings.TrimSpace(label), strings.TrimSpace(href)
|
||||
if !ok || label == "" || !validHomeBookmarkURL(href) || len(items) >= 64 {
|
||||
return nil, errors.New("invalid home bookmark")
|
||||
}
|
||||
items = append(items, NavItem{Label: label, Href: href})
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func validHomeBookmarkURL(href string) bool {
|
||||
u, err := url.Parse(href)
|
||||
if err != nil || strings.ContainsAny(href, "\\\r\n") || u.User != nil {
|
||||
return false
|
||||
}
|
||||
if u.Scheme == "http" || u.Scheme == "https" {
|
||||
return u.Hostname() != ""
|
||||
}
|
||||
return u.Scheme == "" && u.Host == "" && strings.HasPrefix(href, "/") && !strings.HasPrefix(href, "//")
|
||||
}
|
||||
|
||||
func (s *Server) homeBookmarks(domainUUID string) []NavItem {
|
||||
var items []NavItem
|
||||
_ = json.Unmarshal([]byte(s.getDomainConfig("home_bookmarks", domainUUID)), &items)
|
||||
return items
|
||||
}
|
||||
|
||||
func (s *Server) homeBookmarksText(domainUUID string) string {
|
||||
var lines []string
|
||||
for _, item := range s.homeBookmarks(domainUUID) {
|
||||
lines = append(lines, item.Label+" | "+item.Href)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (s *Server) renderSearchHome(w http.ResponseWriter, r *http.Request, layout string) {
|
||||
s.renderWithContext(w, r, "search_home.gohtml", map[string]any{"HomeLayout": layout})
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/server/routes"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestHomeLayoutsAndScopedTreeRoutes(t *testing.T) {
|
||||
t.Chdir("../..")
|
||||
database, err := db.Open(t.TempDir() + "/navigation.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
if err := db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.New(database)
|
||||
if err := st.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := st.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := &Server{store: st}
|
||||
s.mustLoadTemplates()
|
||||
for _, path := range []string{"/top", "/docs/child", "/unrelated"} {
|
||||
object, err := st.CreatePageAtPathInDomain(domain.UUID, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.SaveRevisionContent(object, []byte("<p>Home content</p>"), "test", "und", "text/html", "page.html"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := st.SetConfigInDomain(domain.UUID, "home_path", "/top"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.SetConfigInDomain(domain.UUID, "home_bookmarks", `[{"label":"Documentation","href":"/docs"}]`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
routes.RegisterUIRoutes(mux, routes.UIHandlers{Home: s.handleHome, Tree: s.handleTree, Browser: s.handleBrowser})
|
||||
for _, layout := range []string{"classic", "search", "directory"} {
|
||||
if err := st.SetConfigInDomain(domain.UUID, "home_layout", layout); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/", nil))
|
||||
body := rr.Body.String()
|
||||
if rr.Code != 200 || strings.Contains(body, "can't evaluate") || !strings.Contains(body, `name="scope" value="/"`) {
|
||||
t.Fatalf("%s: %d %s", layout, rr.Code, body)
|
||||
}
|
||||
if layout == "classic" && !strings.Contains(body, `class="topbar"`) {
|
||||
t.Fatal("classic topbar missing")
|
||||
}
|
||||
if layout != "classic" && strings.Contains(body, `class="topbar"`) {
|
||||
t.Fatal("search home has topbar")
|
||||
}
|
||||
if layout == "directory" && !strings.Contains(body, "Documentation") {
|
||||
t.Fatal("bookmarks missing")
|
||||
}
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/v/tree/docs", nil))
|
||||
body := rr.Body.String()
|
||||
if rr.Code != 200 || !strings.Contains(body, `data-path="/docs/child"`) || strings.Contains(body, `data-path="/unrelated"`) || strings.Contains(body, `data-path="/docs"`) {
|
||||
t.Fatalf("scoped tree: %d %s", rr.Code, body)
|
||||
}
|
||||
policy := policyForRequest(httptest.NewRequest(http.MethodGet, "http://localhost/v/tree/docs", nil))
|
||||
if policy.Resolver != resolveHandler {
|
||||
t.Fatalf("scoped tree authorization: %#v", policy)
|
||||
}
|
||||
// Directly visiting /top remains a folder-scoped Viewer, not a home layout.
|
||||
rr = httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/top", nil))
|
||||
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `name="scope" value="/top"`) {
|
||||
t.Fatalf("semantic viewer: %d %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHomeBookmarksRejectUnsafeLinks(t *testing.T) {
|
||||
for _, raw := range []string{"bad | javascript:alert(1)", "bad | //evil.test/path", "bad | /\\evil.test", "missing separator"} {
|
||||
if _, err := parseHomeBookmarks(raw); err == nil {
|
||||
t.Fatalf("accepted %q", raw)
|
||||
}
|
||||
}
|
||||
items, err := parseHomeBookmarks("Docs | /docs\nExternal | https://example.com/\n")
|
||||
if err != nil || len(items) != 2 {
|
||||
t.Fatalf("bookmarks=%#v err=%v", items, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"mime"
|
||||
"net/http"
|
||||
pathpkg "path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const defaultHomeWallpaperOpacity = 28
|
||||
|
||||
type homeWallpaperView struct {
|
||||
Path string
|
||||
URL string
|
||||
Opacity string
|
||||
}
|
||||
|
||||
// configuredLoginWallpaper returns only the image selected by the Domain
|
||||
// administrator as the home/login wallpaper. It intentionally does not use
|
||||
// the requesting principal's document permissions: choosing an image as the
|
||||
// login wallpaper makes that one image public on the sign-in surface.
|
||||
func (s *Server) configuredLoginWallpaper(domainUUID string) *store.Revision {
|
||||
configured, _ := s.homeWallpaperConfig(domainUUID)
|
||||
if configured == "" {
|
||||
return nil
|
||||
}
|
||||
if objectUUID, err := s.store.ResolvePathInDomain(domainUUID, configured); err == nil {
|
||||
revision, revisionErr := s.store.GetLatestRevisionForLanguages(objectUUID, []string{s.getLocale(domainUUID), "und"})
|
||||
if revisionErr == nil && imageContentType(revision.ContentType) {
|
||||
return revision
|
||||
}
|
||||
}
|
||||
entries, err := s.store.ListBrowserEntriesInDomain(domainUUID, configured)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
candidates := make([]store.BrowserEntry, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir && entry.RevisionUUID != "" && imageContentType(entry.ContentType) {
|
||||
candidates = append(candidates, entry)
|
||||
}
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
return nil
|
||||
}
|
||||
index := 0
|
||||
if value, randomErr := rand.Int(rand.Reader, big.NewInt(int64(len(candidates)))); randomErr == nil {
|
||||
index = int(value.Int64())
|
||||
}
|
||||
revision, err := s.store.GetRevision(candidates[index].RevisionUUID)
|
||||
if err != nil || !imageContentType(revision.ContentType) {
|
||||
return nil
|
||||
}
|
||||
return revision
|
||||
}
|
||||
|
||||
func (s *Server) handleAuthWallpaper(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
revision := s.configuredLoginWallpaper(s.domainUUIDForRequest(r))
|
||||
if revision == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
contentType := revision.ContentType
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || !strings.HasPrefix(strings.ToLower(mediaType), "image/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
filename := strings.TrimSpace(revision.Filename)
|
||||
if filename == "" {
|
||||
filename = "wallpaper"
|
||||
}
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filename}))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("ETag", `"`+revision.DocumentHash+`"`)
|
||||
if strings.EqualFold(mediaType, "image/svg+xml") {
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; sandbox")
|
||||
}
|
||||
http.ServeContent(w, r, filename, time.UnixMilli(revision.CreatedAtMS), bytes.NewReader([]byte(revision.Body)))
|
||||
}
|
||||
|
||||
func normalizePickerPath(raw string) (string, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return "", true
|
||||
}
|
||||
if len(raw) > 4096 || !strings.HasPrefix(raw, "/") || strings.ContainsAny(raw, "\x00\r\n\\") {
|
||||
return "", false
|
||||
}
|
||||
for _, part := range strings.Split(raw, "/") {
|
||||
if part == "." || part == ".." {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
clean := pathpkg.Clean(raw)
|
||||
if clean == "." || !strings.HasPrefix(clean, "/") {
|
||||
return "", false
|
||||
}
|
||||
return clean, true
|
||||
}
|
||||
|
||||
func imageContentType(raw string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(raw)
|
||||
return err == nil && strings.HasPrefix(strings.ToLower(mediaType), "image/")
|
||||
}
|
||||
|
||||
func (s *Server) homeWallpaperConfig(domainUUID string) (string, int) {
|
||||
path, ok := normalizePickerPath(s.getDomainConfig("home_wallpaper_path", domainUUID))
|
||||
if !ok {
|
||||
path = ""
|
||||
}
|
||||
opacity := defaultHomeWallpaperOpacity
|
||||
if raw := strings.TrimSpace(s.getDomainConfig("home_wallpaper_opacity", domainUUID)); raw != "" {
|
||||
if parsed, err := strconv.Atoi(raw); err == nil && parsed >= 0 && parsed <= 100 {
|
||||
opacity = parsed
|
||||
}
|
||||
}
|
||||
return path, opacity
|
||||
}
|
||||
|
||||
func (s *Server) homeWallpaperForRequest(r *http.Request) homeWallpaperView {
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
configured, opacity := s.homeWallpaperConfig(domainUUID)
|
||||
view := homeWallpaperView{Path: configured, Opacity: strconv.FormatFloat(float64(opacity)/100, 'f', 2, 64)}
|
||||
if configured == "" {
|
||||
return view
|
||||
}
|
||||
|
||||
httpRequest := r
|
||||
if objectUUID, err := s.store.ResolvePathInDomain(domainUUID, configured); err == nil && s.requestCanReadPath(httpRequest, configured) {
|
||||
if revision, err := s.latestRevisionMetadataForRequest(httpRequest, objectUUID); err == nil && imageContentType(revision.ContentType) {
|
||||
view.URL = rawProjectionURL("path:" + configured)
|
||||
return view
|
||||
}
|
||||
}
|
||||
|
||||
entries, err := s.store.ListBrowserEntriesInDomain(domainUUID, configured)
|
||||
if err != nil {
|
||||
return view
|
||||
}
|
||||
candidates := make([]store.BrowserEntry, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir && imageContentType(entry.ContentType) && s.requestCanReadPath(httpRequest, entry.Path) {
|
||||
candidates = append(candidates, entry)
|
||||
}
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
return view
|
||||
}
|
||||
index := 0
|
||||
if value, err := rand.Int(rand.Reader, big.NewInt(int64(len(candidates)))); err == nil {
|
||||
index = int(value.Int64())
|
||||
}
|
||||
view.URL = rawProjectionURL("path:" + candidates[index].Path)
|
||||
return view
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func saveWallpaperFixture(t *testing.T, s *Server, domain, path, contentType string) {
|
||||
t.Helper()
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.SaveRevisionContent(object, []byte("fixture"), "test", "und", contentType, strings.TrimPrefix(path, "/wallpapers/")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthPageUsesConfiguredWallpaperAndGlass(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
||||
saveWallpaperFixture(t, s, domain, "/wallpapers/login.jpg", "image/jpeg")
|
||||
for key, value := range map[string]string{
|
||||
"home_wallpaper_path": "/wallpapers/login.jpg",
|
||||
"home_wallpaper_opacity": "41",
|
||||
"home_glass_transparency": "47",
|
||||
} {
|
||||
if err := s.store.SetConfigInDomain(domain, key, value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "oidc", Enabled: true, Issuer: "https://id.example.test", ClientID: "pages"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://localhost/_/auth/login", nil)
|
||||
response := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(response, request)
|
||||
body := response.Body.String()
|
||||
for _, expected := range []string{
|
||||
`class="auth-page"`,
|
||||
`--home-glass-opacity:53%`,
|
||||
`class="home-wallpaper" src="/_/auth/wallpaper"`,
|
||||
`style="opacity:0.41"`,
|
||||
`/static/css/auth.css?v=1`,
|
||||
`class="auth-provider-button"`,
|
||||
`OpenID Connectでログイン`,
|
||||
} {
|
||||
if response.Code != http.StatusOK || !strings.Contains(body, expected) {
|
||||
t.Fatalf("auth page missing %q: status=%d body=%s", expected, response.Code, body)
|
||||
}
|
||||
}
|
||||
|
||||
wallpaper := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(wallpaper, httptest.NewRequest(http.MethodGet, "http://localhost/_/auth/wallpaper?path=/private/ignored", nil))
|
||||
if wallpaper.Code != http.StatusOK || wallpaper.Body.String() != "fixture" || wallpaper.Header().Get("Content-Type") != "image/jpeg" || wallpaper.Header().Get("X-Content-Type-Options") != "nosniff" {
|
||||
t.Fatalf("login wallpaper: status=%d type=%q body=%q", wallpaper.Code, wallpaper.Header().Get("Content-Type"), wallpaper.Body.String())
|
||||
}
|
||||
|
||||
method := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(method, httptest.NewRequest(http.MethodPost, "http://localhost/_/auth/wallpaper", nil))
|
||||
if method.Code != http.StatusMethodNotAllowed || method.Header().Get("Allow") != "GET, HEAD" {
|
||||
t.Fatalf("login wallpaper accepted mutation: %d", method.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHomeWallpaperFolderPickerAndTheme(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
|
||||
saveWallpaperFixture(t, s, domain, "/wallpapers/a.jpg", "image/jpeg")
|
||||
saveWallpaperFixture(t, s, domain, "/wallpapers/b.png", "image/png")
|
||||
saveWallpaperFixture(t, s, domain, "/wallpapers/readme.txt", "text/plain")
|
||||
|
||||
form := url.Values{
|
||||
"section": {"home"},
|
||||
"home_layout": {"search"},
|
||||
"home_wallpaper_path": {"/wallpapers"},
|
||||
"home_wallpaper_opacity": {"41"},
|
||||
"home_glass_transparency": {"47"},
|
||||
"locale": {"ja"},
|
||||
"timezone": {"Asia/Tokyo"},
|
||||
"theme": {"fate-t-dark"},
|
||||
"zombie_grace_hours": {"24"},
|
||||
"virtual_folder_pages": {"1"},
|
||||
}
|
||||
response := configRequest(s, token, http.MethodPost, "/-/sys/config", form)
|
||||
if response.Code != http.StatusFound {
|
||||
t.Fatalf("save: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
if got := s.getTheme(domain); got != "fate-t-dark" {
|
||||
t.Fatalf("theme=%q", got)
|
||||
}
|
||||
path, opacity := s.homeWallpaperConfig(domain)
|
||||
if path != "/wallpapers" || opacity != 41 {
|
||||
t.Fatalf("wallpaper=%q opacity=%d", path, opacity)
|
||||
}
|
||||
if got := s.homeGlassTransparency(domain); got != 47 {
|
||||
t.Fatalf("glass transparency=%d", got)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
view := s.homeWallpaperForRequest(r)
|
||||
if view.Opacity != "0.41" || (view.URL != "/v/raw/path:/wallpapers/a.jpg" && view.URL != "/v/raw/path:/wallpapers/b.png") {
|
||||
t.Fatalf("view=%#v", view)
|
||||
}
|
||||
homeResponse := httptest.NewRecorder()
|
||||
s.handleHome(homeResponse, r)
|
||||
homeBody := homeResponse.Body.String()
|
||||
if homeResponse.Code != http.StatusOK || !strings.Contains(homeBody, `class="home-wallpaper"`) || !strings.Contains(homeBody, `style="opacity:0.41"`) || !strings.Contains(homeBody, `--home-glass-opacity:53%`) || !strings.Contains(homeBody, `/static/css/theme/fate-t-dark.css`) {
|
||||
t.Fatalf("home wallpaper render: %d %s", homeResponse.Code, homeBody)
|
||||
}
|
||||
settings := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=home", nil)
|
||||
if settings.Code != http.StatusOK || !strings.Contains(settings.Body.String(), `data-picker-mode="image-or-folder"`) || !strings.Contains(settings.Body.String(), `value="fate-t-dark"`) {
|
||||
t.Fatalf("wallpaper settings UI: %d", settings.Code)
|
||||
}
|
||||
|
||||
picker := configRequest(s, token, http.MethodGet, "/_/api/v1/path-picker?path=%2Fwallpapers&mode=image-or-folder", nil)
|
||||
var listing struct {
|
||||
Path string `json:"path"`
|
||||
Entries []pathPickerEntry `json:"entries"`
|
||||
}
|
||||
if picker.Code != http.StatusOK || json.Unmarshal(picker.Body.Bytes(), &listing) != nil {
|
||||
t.Fatalf("picker: %d %s", picker.Code, picker.Body.String())
|
||||
}
|
||||
if listing.Path != "/wallpapers" || len(listing.Entries) != 2 {
|
||||
t.Fatalf("listing=%#v", listing)
|
||||
}
|
||||
|
||||
direct := configRequest(s, token, http.MethodGet, "/_/api/v1/path-picker?path=%2Fwallpapers%2Fa.jpg&mode=image-or-folder", nil)
|
||||
var selected struct {
|
||||
Path string `json:"path"`
|
||||
Selected string `json:"selected"`
|
||||
}
|
||||
if direct.Code != http.StatusOK || json.Unmarshal(direct.Body.Bytes(), &selected) != nil || selected.Path != "/wallpapers" || selected.Selected != "/wallpapers/a.jpg" {
|
||||
t.Fatalf("direct picker: %d %s", direct.Code, direct.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHomeWallpaperValidation(t *testing.T) {
|
||||
for _, value := range []string{"relative", "/wallpapers/../private", "/bad\\path", "/bad\npath"} {
|
||||
if _, ok := normalizePickerPath(value); ok {
|
||||
t.Fatalf("accepted %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
stdhtml "html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
pathpkg "path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/net/html/atom"
|
||||
)
|
||||
|
||||
const htmlProjectionVersion = "controlled-v2"
|
||||
const maxProjectionInput = 1 << 20
|
||||
const maxProjectionOutput = 4 << 20
|
||||
|
||||
var projectionMacroRE = regexp.MustCompile(`\{\{(?:children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`)
|
||||
var languageClassRE = regexp.MustCompile(`^language-[a-zA-Z0-9_+-]{1,40}$`)
|
||||
|
||||
func projectionPolicy(generated bool) *bluemonday.Policy {
|
||||
p := bluemonday.NewPolicy()
|
||||
p.AllowStandardURLs()
|
||||
p.RequireNoReferrerOnLinks(true)
|
||||
p.AllowElements("p", "br", "hr", "div", "span", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "pre", "code", "b", "strong", "i", "em", "u", "s", "del", "ins", "sub", "sup", "small", "mark", "abbr", "kbd", "samp", "var", "ul", "ol", "li", "dl", "dt", "dd", "table", "thead", "tbody", "tfoot", "tr", "th", "td", "caption", "colgroup", "col", "figure", "figcaption", "details", "summary", "section", "article")
|
||||
p.AllowAttrs("href").OnElements("a")
|
||||
p.AllowAttrs("title", "lang", "dir").Globally()
|
||||
p.AllowAttrs("id").Matching(regexp.MustCompile(`^[^\s]{1,256}$`)).Globally()
|
||||
p.AllowAttrs("class").Matching(languageClassRE).OnElements("code")
|
||||
p.AllowAttrs("src", "alt").OnElements("img")
|
||||
p.AllowAttrs("width", "height", "colspan", "rowspan", "span", "start", "value").Matching(regexp.MustCompile(`^[0-9]{1,4}$`)).OnElements("img", "td", "th", "col", "colgroup", "ol", "li")
|
||||
p.AllowAttrs("open").Matching(regexp.MustCompile(`^(open)?$`)).OnElements("details")
|
||||
if generated {
|
||||
// Only the server's macro renderer can introduce these attributes. The
|
||||
// authored pass does not allow data-*, arbitrary classes, forms or IDs
|
||||
// outside the document namespace after tree normalization.
|
||||
p.AllowElements("nav")
|
||||
p.AllowAttrs("class", "role", "aria-label").Globally()
|
||||
p.AllowAttrs("data-children-path", "data-children-exclude", "data-children-src").OnElements("section")
|
||||
p.AllowAttrs("loading", "referrerpolicy").OnElements("img")
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
var authoredHTMLPolicy = projectionPolicy(false)
|
||||
var generatedHTMLPolicy = projectionPolicy(true)
|
||||
|
||||
func projectionFallback(body, message string) string {
|
||||
if len(body) > 64<<10 {
|
||||
body = body[:64<<10]
|
||||
}
|
||||
return `<p class="projection-warning" role="status">` + stdhtml.EscapeString(message) + `</p><pre>` + stdhtml.EscapeString(body) + `</pre>`
|
||||
}
|
||||
|
||||
func projectionInputOK(ctx context.Context, body string) bool {
|
||||
if len(body) > maxProjectionInput {
|
||||
return false
|
||||
}
|
||||
z := html.NewTokenizer(strings.NewReader(body))
|
||||
z.SetMaxBuf(maxProjectionInput)
|
||||
depth := 0
|
||||
for count := 0; count < 16384; count++ {
|
||||
if count%128 == 0 && ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
switch z.Next() {
|
||||
case html.ErrorToken:
|
||||
return z.Err() == io.EOF
|
||||
case html.StartTagToken:
|
||||
name, _ := z.TagName()
|
||||
switch string(name) {
|
||||
case "br", "hr", "img", "meta", "link", "input", "wbr", "source", "area", "base", "embed", "param", "track", "col":
|
||||
default:
|
||||
depth++
|
||||
}
|
||||
if depth > 128 {
|
||||
return false
|
||||
}
|
||||
case html.EndTagToken:
|
||||
if depth > 0 {
|
||||
depth--
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func documentAnchor(id string) string { return fmt.Sprintf("pw-doc-%x", sha256.Sum256([]byte(id))) }
|
||||
|
||||
// extractApprovedExternalActiveResources runs before the authored allowlist,
|
||||
// which intentionally knows nothing about script or link elements. An
|
||||
// approved element becomes unpredictable text; its canonical server-authored
|
||||
// tag is restored only after the generated-output allowlist has also run.
|
||||
func extractApprovedExternalActiveResources(body string, policy externalResourcePolicy) (string, map[string]string) {
|
||||
var seed [18]byte
|
||||
if _, err := rand.Read(seed[:]); err != nil {
|
||||
return body, nil
|
||||
}
|
||||
prefix := "PWEXT-" + base64.RawURLEncoding.EncodeToString(seed[:]) + "-"
|
||||
fragments := map[string]string{}
|
||||
var output strings.Builder
|
||||
z := html.NewTokenizer(strings.NewReader(body))
|
||||
z.SetMaxBuf(maxProjectionInput)
|
||||
skippingApprovedScript := false
|
||||
for {
|
||||
tokenType := z.Next()
|
||||
if tokenType == html.ErrorToken {
|
||||
break
|
||||
}
|
||||
if skippingApprovedScript {
|
||||
if tokenType == html.EndTagToken {
|
||||
token := z.Token()
|
||||
if strings.EqualFold(token.Data, "script") {
|
||||
skippingApprovedScript = false
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if tokenType != html.StartTagToken && tokenType != html.SelfClosingTagToken {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
token := z.Token()
|
||||
name := strings.ToLower(token.Data)
|
||||
attrs := map[string]string{}
|
||||
for _, attr := range token.Attr {
|
||||
attrs[strings.ToLower(attr.Key)] = attr.Val
|
||||
}
|
||||
kind, resourceURL := "", ""
|
||||
switch name {
|
||||
case "script":
|
||||
kind, resourceURL = "script", attrs["src"]
|
||||
case "link":
|
||||
if strings.EqualFold(strings.TrimSpace(attrs["rel"]), "stylesheet") {
|
||||
kind, resourceURL = "stylesheet", attrs["href"]
|
||||
}
|
||||
}
|
||||
approval, approved := policy.approved(kind, resourceURL)
|
||||
if !approved {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
placeholder := prefix + fmt.Sprint(len(fragments))
|
||||
var node *html.Node
|
||||
if kind == "script" {
|
||||
node = &html.Node{Type: html.ElementNode, Data: "script", DataAtom: atom.Script, Attr: []html.Attribute{
|
||||
{Key: "src", Val: approval.URL}, {Key: "integrity", Val: approval.Integrity},
|
||||
{Key: "crossorigin", Val: "anonymous"}, {Key: "defer", Val: "defer"},
|
||||
}}
|
||||
if tokenType == html.StartTagToken {
|
||||
skippingApprovedScript = true
|
||||
}
|
||||
} else {
|
||||
node = &html.Node{Type: html.ElementNode, Data: "link", DataAtom: atom.Link, Attr: []html.Attribute{
|
||||
{Key: "rel", Val: "stylesheet"}, {Key: "href", Val: approval.URL},
|
||||
{Key: "integrity", Val: approval.Integrity}, {Key: "crossorigin", Val: "anonymous"},
|
||||
}}
|
||||
}
|
||||
var fragment strings.Builder
|
||||
if html.Render(&fragment, node) != nil {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
fragments[placeholder] = fragment.String()
|
||||
output.WriteString(placeholder)
|
||||
}
|
||||
return output.String(), fragments
|
||||
}
|
||||
|
||||
// safeHTMLProjection is the only authored-HTML display boundary. It never
|
||||
// writes Documents/Revisions and never fetches an external resource. Canonical
|
||||
// Markdown conversion deliberately does not call it.
|
||||
func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand bool) string {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
fallback := func() string {
|
||||
return projectionFallback(body, translate(s.getLocale(s.domainUUIDForRequest(r)), "viewer.html_projection_unavailable"))
|
||||
}
|
||||
if !projectionInputOK(r.Context(), body) {
|
||||
return fallback()
|
||||
}
|
||||
resourcePolicy := s.externalResourcePolicy(s.domainUUIDForRequest(r))
|
||||
body, externalFragments := extractApprovedExternalActiveResources(body, resourcePolicy)
|
||||
safe := authoredHTMLPolicy.Sanitize(body)
|
||||
root := &html.Node{Type: html.ElementNode, Data: "div", DataAtom: atom.Div}
|
||||
nodes, err := html.ParseFragment(strings.NewReader(safe), root)
|
||||
if err != nil {
|
||||
return fallback()
|
||||
}
|
||||
var output strings.Builder
|
||||
macros, count := 0, 0
|
||||
var emit func(*html.Node, int, bool) bool
|
||||
emit = func(n *html.Node, depth int, literal bool) bool {
|
||||
count++
|
||||
if count > 16384 || depth > 128 || output.Len() > maxProjectionOutput || r.Context().Err() != nil {
|
||||
return false
|
||||
}
|
||||
if n.Type == html.TextNode {
|
||||
if !expand || literal {
|
||||
output.WriteString(stdhtml.EscapeString(n.Data))
|
||||
return true
|
||||
}
|
||||
at := 0
|
||||
for _, loc := range projectionMacroRE.FindAllStringIndex(n.Data, 33) {
|
||||
if ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
macros++
|
||||
if macros > 32 {
|
||||
return false
|
||||
}
|
||||
output.WriteString(stdhtml.EscapeString(n.Data[at:loc[0]]))
|
||||
macro := n.Data[loc[0]:loc[1]]
|
||||
expanded := macro
|
||||
// Dispatch exactly one macro. A failed City macro must never
|
||||
// be reparsed as a nested children request on its raw text.
|
||||
if isAnonymousProjection(r) {
|
||||
expanded = s.expandPublicMacro(r, macro, path)
|
||||
} else if strings.HasPrefix(macro, "{{children") {
|
||||
expanded = s.expandChildrenMacros(macro, path)
|
||||
} else {
|
||||
expanded = s.expandCityMacrosContext(ctx, macro, path, s.domainUUIDForRequest(r), s.pathReadFilter(r))
|
||||
}
|
||||
if expanded == macro {
|
||||
// Failed/unresolved macros are still authored text. Do not
|
||||
// promote decoded entities into trusted generated elements.
|
||||
output.WriteString(stdhtml.EscapeString(macro))
|
||||
} else {
|
||||
output.WriteString(expanded)
|
||||
}
|
||||
at = loc[1]
|
||||
}
|
||||
output.WriteString(stdhtml.EscapeString(n.Data[at:]))
|
||||
return true
|
||||
}
|
||||
if n.Type != html.ElementNode {
|
||||
return true
|
||||
}
|
||||
attrs := n.Attr[:0]
|
||||
for _, a := range n.Attr {
|
||||
switch a.Key {
|
||||
case "id":
|
||||
a.Val = documentAnchor(a.Val)
|
||||
case "href":
|
||||
if strings.HasPrefix(a.Val, "#") {
|
||||
a.Val = "#" + documentAnchor(strings.TrimPrefix(a.Val, "#"))
|
||||
}
|
||||
case "src":
|
||||
u, e := url.Parse(a.Val)
|
||||
if e != nil || strings.ContainsAny(a.Val, "\\\r\n\t") {
|
||||
continue
|
||||
}
|
||||
if u.IsAbs() || u.Host != "" {
|
||||
approval, ok := resourcePolicy.approved("image", a.Val)
|
||||
if !ok || n.Data != "img" {
|
||||
continue
|
||||
}
|
||||
a.Val = approval.URL
|
||||
attrs = append(attrs, a)
|
||||
continue
|
||||
}
|
||||
u.Path = pathpkg.Clean(u.Path)
|
||||
// Images are passive read projections, not arbitrary authenticated
|
||||
// GETs against editors, login flows or management endpoints.
|
||||
if !strings.HasPrefix(u.Path, "/v/raw/") && !strings.HasPrefix(u.Path, "/_/obj/") && !strings.HasPrefix(u.Path, "/static/") && !strings.HasPrefix(u.Path, "/o/path:/system/static/") {
|
||||
continue
|
||||
}
|
||||
u.RawPath = ""
|
||||
a.Val = u.String()
|
||||
}
|
||||
attrs = append(attrs, a)
|
||||
}
|
||||
n.Attr = attrs
|
||||
if n.Data == "img" {
|
||||
n.Attr = append(n.Attr, html.Attribute{Key: "loading", Val: "lazy"}, html.Attribute{Key: "referrerpolicy", Val: "no-referrer"})
|
||||
}
|
||||
// Render a shallow element to let net/html escape its attributes; never
|
||||
// interpolate authored attributes into the macro output context.
|
||||
shallow := &html.Node{Type: html.ElementNode, Data: n.Data, DataAtom: n.DataAtom, Attr: n.Attr}
|
||||
var tag strings.Builder
|
||||
if html.Render(&tag, shallow) != nil {
|
||||
return false
|
||||
}
|
||||
opening := tag.String()
|
||||
closing := "</" + n.Data + ">"
|
||||
void := !strings.HasSuffix(opening, closing)
|
||||
if !void {
|
||||
opening = strings.TrimSuffix(opening, closing)
|
||||
}
|
||||
output.WriteString(opening)
|
||||
for child := n.FirstChild; child != nil; child = child.NextSibling {
|
||||
if !emit(child, depth+1, literal || n.Data == "code" || n.Data == "pre" || n.Data == "a") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !void {
|
||||
output.WriteString(closing)
|
||||
}
|
||||
return true
|
||||
}
|
||||
for _, n := range nodes {
|
||||
if !emit(n, 0, false) {
|
||||
return fallback()
|
||||
}
|
||||
}
|
||||
if output.Len() > maxProjectionOutput {
|
||||
return fallback()
|
||||
}
|
||||
projected := output.String()
|
||||
if expand && !isAnonymousProjection(r) {
|
||||
projected = s.renderDiagramsInHTML(r, path, projected)
|
||||
}
|
||||
// A second allowlist pass guards all generated URL/HTML output too. Active
|
||||
// external resources only exist as random text at this boundary, so macro
|
||||
// output cannot manufacture an approved script or stylesheet tag.
|
||||
projected = generatedHTMLPolicy.Sanitize(projected)
|
||||
for token, fragment := range externalFragments {
|
||||
projected = strings.ReplaceAll(projected, token, fragment)
|
||||
}
|
||||
return projected
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHTMLProjectionChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
s.mux.HandleFunc("/o/", s.handleObjectView)
|
||||
s.mux.HandleFunc("/r/view/", s.handleRevisionView)
|
||||
s.mux.HandleFunc("/v/preview/", s.handlePreview)
|
||||
s.mux.HandleFunc("/_/view/print/", s.handlePrint)
|
||||
s.mux.HandleFunc("/u/", s.handleUserProfile)
|
||||
s.mux.HandleFunc("/e/", s.handleEditorRoute)
|
||||
s.mux.HandleFunc("/_/api/v1/render/markdown", s.handleMarkdownRenderAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/revisions/save", s.handleSaveRevisionAPI)
|
||||
s.mux.HandleFunc("/v/children", s.handleChildrenProjection)
|
||||
body := projectionAttack + `<p>{{children:/guide}}</p><pre><code>literal code</code></pre>`
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/guide")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.CreatePageAtPathInDomain(domain, "/guide/child"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// A path-scoped author writes via the real API; a stronger administrator
|
||||
// subsequently renders those exact bytes in every normal display surface.
|
||||
author, err := s.store.CreatePasswordAccount(domain, "writer", "a long writer password", "Writer", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authorToken, err := s.store.CreateAuthSession(domain, author.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var role string
|
||||
if err = s.store.DB.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=?`, domain, author.UUID).Scan(&role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, capability := range []string{"read", "write"} {
|
||||
if _, err = s.store.DB.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,?,'path','/guide',1)`, "writer-"+capability, role, capability); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]any{"object_uuid": object, "body": body, "content_type": "text/html", "filename": "guide.html", "lang": "en"})
|
||||
request := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(payload))
|
||||
request.Header.Set("Authorization", "Bearer "+authorToken)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(response, request)
|
||||
if w := response; w.Code != 200 {
|
||||
t.Fatalf("author save=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
rev, err := s.store.GetLatestRevisionForLanguages(object, []string{"en"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userObject, err := s.store.CreatePageAtPathInDomain(domain, "/u/admin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(userObject, []byte(body), "", "en", "text/html", "profile.html"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "home_path", "/guide"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
routes := []string{"/guide", "/", "/o/uuid:" + object, "/r/view/" + rev.UUID, "/v/preview/guide", "/_/view/print/path:/guide", "/u/admin", "/e/-/guide?edittype=html", "/e/-/guide?edittype=markdown"}
|
||||
script, err := os.ReadFile("internal/server/testdata/html-projection.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var hits atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
switch r.URL.Path {
|
||||
case "/fixture":
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
_, _ = io.WriteString(w, `<!doctype html><body><h1>HTML isolation regression</h1><script>`+string(script)+`</script></body>`)
|
||||
return
|
||||
case "/fixture-state":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"routes": routes, "body": rev.Body, "hits": hits.Load()})
|
||||
return
|
||||
case "/sentinel":
|
||||
hits.Add(1)
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
case "/static/attack.js":
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
_, _ = io.WriteString(w, `document.documentElement.dataset.attack='external';fetch('/sentinel',{method:'POST'})`)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodGet && (r.URL.Path == "/" || r.URL.Path == "/guide" || strings.HasPrefix(r.URL.Path, "/o/") || strings.HasPrefix(r.URL.Path, "/r/view/") || strings.HasPrefix(r.URL.Path, "/v/preview/") || strings.HasPrefix(r.URL.Path, "/_/view/print/") || strings.HasPrefix(r.URL.Path, "/u/") || strings.HasPrefix(r.URL.Path, "/e/")) {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
// Parser-inserted, deliberately unnonced probes test CSP separately
|
||||
// from sanitizer correctness, without disabling the real headers.
|
||||
probe := `<script>document.documentElement.dataset.cspAttack='script'</script><script src="/static/attack.js"></script><img src="/static/not-found.png" onerror="document.documentElement.dataset.cspAttack='event'">`
|
||||
_, _ = io.WriteString(w, strings.Replace(rec.Body.String(), "</body>", probe+"</body>", 1))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/fixture", t.TempDir())
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("browser: %v %s", err, out)
|
||||
}
|
||||
if hits.Load() != 0 {
|
||||
t.Fatalf("authored content issued %d privileged requests", hits.Load())
|
||||
}
|
||||
after, err := s.store.GetRevision(rev.UUID)
|
||||
if err != nil || after.DocumentHash != rev.DocumentHash || after.Body != rev.Body {
|
||||
t.Fatal("browser projection altered canonical source")
|
||||
}
|
||||
t.Log(fmt.Sprintf("verified %d display/editor routes as a stronger reader", len(routes)))
|
||||
}
|
||||
|
||||
// Match CSP when installing only fixture-owned code. This does not disable CSP
|
||||
// and authored corpus nodes never receive the application's nonce.
|
||||
func TestHTMLProjectionCSPRejectsUnnoncedMarkup(t *testing.T) {
|
||||
s, token, _ := configFixture(t)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
w := configRequest(s, token, "GET", "/", nil)
|
||||
if w.Code != 200 || strings.Contains(w.Header().Get("Content-Security-Policy"), "script-src 'self'") {
|
||||
t.Fatal("unsafe script origin allowlist")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var trustedScriptTagRE = regexp.MustCompile(`(?i)<script([\s>])`)
|
||||
|
||||
// Only application-owned template files are processed here, before any request
|
||||
// data or authored HTML exists. Never add nonces by rewriting rendered HTML.
|
||||
func parseTrustedTemplates(t *template.Template) (*template.Template, error) {
|
||||
paths, err := filepath.Glob("templates/**/*.gohtml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, path := range paths {
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body = trustedScriptTagRE.ReplaceAll(body, []byte(`<script nonce="{{ cspNonce }}"$1`))
|
||||
if _, err = t.New(filepath.Base(path)).Parse(string(body)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func safeProjectionTemplate(name string) bool {
|
||||
switch name {
|
||||
case "periodical.gohtml", "public_periodical.gohtml", "periodical_new.gohtml":
|
||||
return true
|
||||
case "city_map.gohtml", "public_tree.gohtml", "anonymous_view.gohtml", "page.gohtml", "home.gohtml", "search_home.gohtml", "object_view.gohtml", "revision_view.gohtml", "preview.gohtml", "print.gohtml", "profile.gohtml", "editor.gohtml", "object_editor.gohtml":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func externalResourceCSPSources(policy externalResourcePolicy) (scriptHashes, styleOrigins, imageOrigins []string) {
|
||||
styles := map[string]bool{}
|
||||
images := map[string]bool{}
|
||||
hashes := map[string]bool{}
|
||||
for _, item := range policy.items {
|
||||
u, err := url.Parse(item.URL)
|
||||
if err != nil || u.Scheme != "https" || u.Host == "" {
|
||||
continue
|
||||
}
|
||||
origin := "https://" + u.Host
|
||||
switch item.Kind {
|
||||
case "script":
|
||||
hashes["'"+item.Integrity+"'"] = true
|
||||
case "stylesheet":
|
||||
styles[origin] = true
|
||||
case "image":
|
||||
images[origin] = true
|
||||
}
|
||||
}
|
||||
for value := range hashes {
|
||||
scriptHashes = append(scriptHashes, value)
|
||||
}
|
||||
for value := range styles {
|
||||
styleOrigins = append(styleOrigins, value)
|
||||
}
|
||||
for value := range images {
|
||||
imageOrigins = append(imageOrigins, value)
|
||||
}
|
||||
sort.Strings(scriptHashes)
|
||||
sort.Strings(styleOrigins)
|
||||
sort.Strings(imageOrigins)
|
||||
return
|
||||
}
|
||||
|
||||
func setProjectionCSP(w http.ResponseWriter, policy externalResourcePolicy) (string, error) {
|
||||
var token [24]byte
|
||||
if _, err := rand.Read(token[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := base64.RawStdEncoding.EncodeToString(token[:])
|
||||
scriptHashes, styleOrigins, imageOrigins := externalResourceCSPSources(policy)
|
||||
csp := "default-src 'self'; script-src 'nonce-" + nonce + "' 'strict-dynamic'"
|
||||
if len(scriptHashes) > 0 {
|
||||
csp += " " + strings.Join(scriptHashes, " ")
|
||||
}
|
||||
csp += "; script-src-attr 'none'; object-src 'none'; base-uri 'none'; frame-ancestors 'self'; frame-src 'self'; form-action 'self'; style-src 'self' 'unsafe-inline'"
|
||||
if len(styleOrigins) > 0 {
|
||||
csp += " " + strings.Join(styleOrigins, " ")
|
||||
}
|
||||
csp += "; img-src 'self' data: blob:"
|
||||
if len(imageOrigins) > 0 {
|
||||
csp += " " + strings.Join(imageOrigins, " ")
|
||||
}
|
||||
csp += "; media-src 'self' blob:; font-src 'self'; connect-src 'self'; worker-src 'self' blob:"
|
||||
w.Header().Set("Content-Security-Policy", csp)
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("X-PagesWiki-HTML-Policy", htmlProjectionVersion)
|
||||
return nonce, nil
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
const projectionAttack = `<h1 id="chapter">Readable heading</h1><p><strong>Formatting</strong> <a href="#chapter">chapter</a></p>
|
||||
<script>document.documentElement.dataset.attack='script';fetch('/sentinel',{method:'POST'})</script>
|
||||
<script src="/static/attack.js"></script><img src="/v/raw/path:/missing.png" onerror="document.documentElement.dataset.attack='event'">
|
||||
<a href="java	script:document.documentElement.dataset.attack='url'">bad URL</a>
|
||||
<svg onload="document.documentElement.dataset.attack='svg'"><a><animate attributeName="href" values="javascript:alert(1)"/></a></svg>
|
||||
<math><mtext><table><mglyph><style><!--</style><img title="--><img src=x onerror=alert(1)>"></math>
|
||||
<iframe srcdoc="<script>parent.document.documentElement.dataset.attack='frame'</script>"></iframe>
|
||||
<form id="editor" name="PagesEditor" action="/sentinel"><input name="saveNow"><button>attack</button></form>
|
||||
<div class="children-async" data-children-src="/sentinel" style="position:fixed;inset:0">forged controls</div>
|
||||
<style>body{display:none}</style><base href="https://invalid.example/"><meta http-equiv="refresh" content="0;url=/sentinel">
|
||||
<img src="https://invalid.example/tracker"><img src="/e/accidental-write"><img src="/static/%2e%2e/e/accidental-write"><object data="/v/raw/path:/attack.html"></object>`
|
||||
|
||||
func checkInertProjection(t *testing.T, body string) {
|
||||
t.Helper()
|
||||
doc, err := html.Parse(strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var walk func(*html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
switch n.Data {
|
||||
case "script", "style", "svg", "math", "iframe", "form", "input", "button", "object", "embed", "base", "meta":
|
||||
t.Fatalf("active element %s: %s", n.Data, body)
|
||||
}
|
||||
for _, a := range n.Attr {
|
||||
if strings.HasPrefix(a.Key, "on") || a.Key == "style" || a.Key == "name" || a.Key == "srcdoc" {
|
||||
t.Fatalf("active attr %+v", a)
|
||||
}
|
||||
if a.Key == "id" && !strings.HasPrefix(a.Val, "pw-doc-") {
|
||||
t.Fatalf("unscoped ID %s", a.Val)
|
||||
}
|
||||
if (a.Key == "href" || a.Key == "src") && strings.Contains(strings.ToLower(a.Val), "javascript") {
|
||||
t.Fatalf("active URL %+v", a)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(doc)
|
||||
}
|
||||
|
||||
func TestHTMLProjectionCorpusAndMacros(t *testing.T) {
|
||||
s, _, _ := configFixture(t)
|
||||
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
for _, body := range []string{projectionAttack, `<noscript><p title="</noscript><img src=x onerror=alert(1)>">`, `<a href="javascript:alert(1)">bad</a><img src="//invalid.example/x">`, `<svg><foreignObject><iframe srcdoc="oops"></iframe></foreignObject></svg>`} {
|
||||
got := s.safeHTMLProjection(r, body, "/guide", true)
|
||||
checkInertProjection(t, got)
|
||||
}
|
||||
got := s.safeHTMLProjection(r, `<p>{{children:/guide}}</p><code>{{children:/literal}}</code><img alt="{{children:/attribute}}" src="/static/test.png"><section data-children-src="/sentinel" class="children-async">fake</section>`, "/guide", true)
|
||||
checkInertProjection(t, got)
|
||||
if strings.Count(got, `data-children-src=`) != 1 || strings.Contains(got, `data-children-src="/sentinel"`) || !strings.Contains(got, `{{children:/literal}}`) {
|
||||
t.Fatalf("macro trust=%s", got)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, projectionAttack, "/guide", false)
|
||||
if !strings.Contains(got, "<strong>Formatting</strong>") || !strings.Contains(got, `href="#`+documentAnchor("chapter")) || strings.Contains(got, "https://invalid.example") || strings.Contains(got, `src="/e/`) {
|
||||
t.Fatalf("format/resources=%s", got)
|
||||
}
|
||||
for _, body := range []string{strings.Repeat("x", maxProjectionInput+1), strings.Repeat("<div>", 129), strings.Repeat("{{children}}", 33)} {
|
||||
got = s.safeHTMLProjection(r, body, "/guide", true)
|
||||
if !strings.Contains(got, "projection-warning") {
|
||||
t.Fatal("limit did not fail closed")
|
||||
}
|
||||
checkInertProjection(t, got)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
cancel()
|
||||
if got = s.safeHTMLProjection(r.WithContext(ctx), projectionAttack, "/guide", true); !strings.Contains(got, "projection-warning") {
|
||||
t.Fatal("cancellation ignored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLProjectionFailedMacroRemainsText(t *testing.T) {
|
||||
s, _, _ := configFixture(t)
|
||||
// Force a locality lookup failure in this temporary fixture. Decoded text
|
||||
// must not acquire the final policy's application-owned macro attributes.
|
||||
if _, err := s.store.DB.Exec(`ALTER TABLE zones RENAME TO unavailable_zones`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
for _, tail := range []string{"", "{{children:/guide}}"} {
|
||||
body := `<p>{{spot ignored="<section class='children-async' data-children-src='/sentinel'>fake</section>` + tail + `"}}</p>`
|
||||
got := s.safeHTMLProjection(r, body, "/guide", true)
|
||||
if strings.Contains(got, "<section") || !strings.Contains(got, "<section") {
|
||||
t.Fatalf("unresolved macro promoted authored markup: %s", got)
|
||||
}
|
||||
checkInertProjection(t, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLProjectionAPIKeepsCanonicalSource(t *testing.T) {
|
||||
s, _, domain := configFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/guide")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revID, err := s.store.SaveRevisionContent(object, []byte(projectionAttack), "", "en", "text/html", "guide.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := s.store.GetRevision(revID.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, mode := range []string{"html", "markdown"} {
|
||||
payload := map[string]any{"projection": true, "path": "/guide", mode: projectionAttack}
|
||||
b, _ := json.Marshal(payload)
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/render/markdown", bytes.NewReader(b))
|
||||
s.handleMarkdownRenderAPI(w, r)
|
||||
var response struct {
|
||||
HTML string `json:"html"`
|
||||
}
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checkInertProjection(t, response.HTML)
|
||||
if w.Header().Get("Cache-Control") != "private, no-store" {
|
||||
t.Fatal("preview cache policy")
|
||||
}
|
||||
}
|
||||
// The canonical conversion remains an authoring API, not a display result.
|
||||
b, _ := json.Marshal(map[string]any{"markdown": projectionAttack})
|
||||
w := httptest.NewRecorder()
|
||||
s.handleMarkdownRenderAPI(w, httptest.NewRequest("POST", "http://localhost/_/api/v1/render/markdown", bytes.NewReader(b)))
|
||||
if !strings.Contains(w.Body.String(), "dataset.attack") {
|
||||
t.Fatal("canonical conversion was sanitized")
|
||||
}
|
||||
after, err := s.store.GetRevision(revID.UUID)
|
||||
if err != nil || after.Body != before.Body || after.DocumentHash != before.DocumentHash {
|
||||
t.Fatalf("canonical changed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTMLProjectionEndpointsAndCSP(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
s.mux.HandleFunc("/o/", s.handleObjectView)
|
||||
s.mux.HandleFunc("/r/view/", s.handleRevisionView)
|
||||
s.mux.HandleFunc("/v/preview/", s.handlePreview)
|
||||
s.mux.HandleFunc("/_/view/print/", s.handlePrint)
|
||||
s.mux.HandleFunc("/u/", s.handleUserProfile)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/guide")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rev, err := s.store.SaveRevisionContent(object, []byte(projectionAttack), "", "en", "text/html", "guide.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userObject, err := s.store.CreatePageAtPathInDomain(domain, "/u/admin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(userObject, []byte(projectionAttack), "", "en", "text/html", "profile.html"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "home_path", "/guide"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, route := range []string{"/guide", "/", "/o/uuid:" + object, "/r/view/" + rev.UUID, "/v/preview/guide", "/_/view/print/path:/guide", "/u/admin"} {
|
||||
w := configRequest(s, token, "GET", route, nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("%s=%d %s", route, w.Code, w.Body.String())
|
||||
}
|
||||
csp := w.Header().Get("Content-Security-Policy")
|
||||
if !strings.Contains(csp, "script-src-attr 'none'") || !strings.Contains(csp, "'strict-dynamic'") || !strings.Contains(csp, "object-src 'none'") {
|
||||
t.Fatalf("CSP %s=%s", route, csp)
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `<script>document.documentElement.dataset.attack`) || strings.Contains(w.Body.String(), `onerror="document.documentElement.dataset.attack`) {
|
||||
t.Fatalf("unsafe source in %s", route)
|
||||
}
|
||||
for _, m := range regexp.MustCompile(`<script\b[^>]*>`).FindAllString(w.Body.String(), -1) {
|
||||
if !strings.Contains(m, `nonce="`) {
|
||||
t.Fatalf("untrusted application script: %s", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte(`<script>PLAIN_SENTINEL</script>`), "", "en", "text/plain", "code.txt"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := configRequest(s, token, "GET", "/o/uuid:"+object, nil)
|
||||
if w.Code != 200 || strings.Contains(w.Body.String(), `<script>PLAIN_SENTINEL`) || !strings.Contains(w.Body.String(), "<script>PLAIN_SENTINEL") {
|
||||
t.Fatal("object MIME confusion")
|
||||
}
|
||||
}
|
||||
|
||||
func browserFixtureScript(headers http.Header, script []byte) string {
|
||||
nonce := ""
|
||||
if m := regexp.MustCompile(`'nonce-([^']+)'`).FindStringSubmatch(headers.Get("Content-Security-Policy")); len(m) == 2 {
|
||||
nonce = m[1]
|
||||
}
|
||||
return `<script nonce="` + nonce + `">` + string(script) + `</script>`
|
||||
}
|
||||
Loaded 100 of 699 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user