feat: support mailing-list and monthly mail capture paths
Build and publish container images / images (push) Successful in 11m59s
Release container images / release (push) Successful in 22s

This commit is contained in:
Anya Tokugawa committed 2026-11-06 09:51:45 +09:00
1 parent 543814cbd6
commit 1a451023f0
7 files changed
+165 -5

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func TestTaskMailLayouts(t *testing.T) {
s, token, domain, actor := taskFixture(t)
for _, suffix := range []string{"hogehoge/123", "v/0", "2026/01/" + uuid.NewString(), "2026/12/" + strings.Repeat("a", 64)} {
id := uuid.NewString()
input := map[string]any{"request_id": id, "title": "日本語の件名", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": suffix}
w := taskRequest(s, token, "POST", "capture", input, "")
if w.Code != 201 {
t.Fatal(suffix, w.Code, w.Body)
}
var response struct {
Path string `json:"page_path"`
Item store.TaskRecord `json:"item"`
}
if e := json.Unmarshal(w.Body.Bytes(), &response); e != nil {
t.Fatal(e)
}
if response.Path != "/mail/"+suffix {
t.Fatal(response.Path)
}
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 {
t.Fatal("replay", w.Code, w.Body)
}
// A different Message-ID produces a different request UUID, never an overwrite.
other := uuid.NewString()
input["request_id"] = other
input["wiki_body"] = "Different mail"
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 {
t.Fatal("collision", w.Code, w.Body)
}
if _, e := s.store.GetTask(context.Background(), domain, actor, other); !errors.Is(e, sql.ErrNoRows) {
t.Fatal("partial task", e)
}
rev, e := s.store.GetLatestRevision(response.Item.PageUUID)
if e != nil || strings.Contains(rev.Body, "Different") {
t.Fatal("page overwritten", e)
}
}
for _, suffix := range []string{"", "../123", "/list/123", "a/../../123", "list/001", "list/-1", "list/1?x", "list%2f/1", "list/1/2", "_/123", "2026/13/" + uuid.NewString(), "0000/01/" + uuid.NewString(), "2026/01/not-an-id"} {
input := map[string]any{"request_id": uuid.NewString(), "title": "Bad", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": suffix}
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 {
t.Fatal(suffix, w.Code, w.Body)
}
}
input := map[string]any{"request_id": uuid.NewString(), "title": "No body", "wiki_suffix": "list/123"}
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 {
t.Fatal(w.Code, w.Body)
}
other, e := s.store.CreatePasswordAccount(domain, "mail-layout-other", "fixture password long", "Other", "")
if e != nil {
t.Fatal(e)
}
otherToken, e := s.store.CreateAuthSession(domain, other.UUID)
if e != nil {
t.Fatal(e)
}
input = map[string]any{"request_id": uuid.NewString(), "title": "No Wiki permission", "wiki_body": "Body", "wiki_prefix": "/mail", "wiki_suffix": "private/123"}
if w := taskRequest(s, otherToken, "POST", "capture", input, ""); w.Code != 403 {
t.Fatal(w.Code, w.Body)
}
}
+9 -1
View File
@@ -69,6 +69,7 @@ type taskCaptureInput struct {
Complexity *int `json:"complexity"`
WikiBody *string `json:"wiki_body,omitempty"`
WikiPrefix *string `json:"wiki_prefix,omitempty"`
WikiSuffix *string `json:"wiki_suffix,omitempty"`
}
func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request, actor string, active bool, kind string) {
@@ -260,6 +261,13 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
return
}
target = store.TaskCaptureWikiPath(prefix, item.Title, input.RequestID)
if input.WikiSuffix != nil {
if !store.ValidTaskMailSuffix(*input.WikiSuffix) {
taskError(w, store.ErrTaskInvalid)
return
}
target = strings.TrimRight(prefix, "/") + "/" + *input.WikiSuffix
}
for _, capability := range []string{"read", "create", "write"} {
if s.authorizePathRequest(r, capability, target) != nil {
taskError(w, store.ErrAccessDenied)
@@ -267,7 +275,7 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
}
}
body = *input.WikiBody
} else if input.WikiPrefix != nil {
} else if input.WikiPrefix != nil || input.WikiSuffix != nil {
taskError(w, store.ErrTaskInvalid)
return
}
+9
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"html"
"path"
"regexp"
"strings"
"time"
"unicode"
@@ -40,6 +41,14 @@ func TaskCaptureWikiPath(prefix, title, request string) string {
return strings.TrimRight(prefix, "/") + "/mail-" + request
}
var mailListSuffix = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,99}/(0|[1-9][0-9]{0,19})$`)
var mailMonthSuffix = regexp.MustCompile(`^[0-9]{4}/(0[1-9]|1[0-2])/([0-9a-f]{64}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$`)
// Only the two mail layouts are accepted; no arbitrary relative path traversal.
func ValidTaskMailSuffix(s string) bool {
return mailListSuffix.MatchString(s) || (mailMonthSuffix.MatchString(s) && !strings.HasPrefix(s, "0000/"))
}
func (s *Store) createCapturedTaskWiki(ctx context.Context, tx *sql.Tx, domain, actor string, item *TaskRecord, target, body string) error {
if target == "" || !ValidTaskWikiPrefix(path.Dir(target)) || path.Clean(target) != target || item.PageUUID != "" || len(body) > 256<<10 || !utf8.ValidString(body) || strings.IndexByte(body, 0) >= 0 {
return ErrTaskInvalid