Files
Anya Tokugawa 7fe64113c7
Build and publish container images / images (push) Failing after 7m31s
feat: add previewed atomic OPML subscription import and export
2026-11-08 04:39:54 +09:00

276 lines
8.0 KiB
Go

package server
import (
"encoding/xml"
"io"
"net/http"
"strings"
"pageswiki/internal/store"
)
const feedOPMLMaxBytes = 512 << 10
type feedOPMLRow struct {
store.FeedSubscription
Index int `json:"index"`
Status string `json:"status"`
}
// Parse only the uploaded document. OPML includes and HTML links are never
// fetched. Every token participates in the depth/outline/DTD checks.
func parseFeedOPML(raw string) ([]feedOPMLRow, error) {
if len(raw) > feedOPMLMaxBytes {
return nil, store.ErrTaskDAVLimit
}
d := xml.NewDecoder(strings.NewReader(strings.TrimPrefix(raw, "\ufeff")))
// JSON and the browser's File.text() carry UTF-8, not the original legacy
// bytes. Reject other XML encodings instead of silently corrupting names.
type frame struct {
name xml.Name
folder string
commented bool
}
stack := []frame{}
rows := []feedOPMLRow{}
root, head, body, outlines := 0, 0, 0, 0
for {
token, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, store.ErrTaskInvalid
}
switch t := token.(type) {
case xml.Directive:
return nil, store.ErrTaskInvalid
case xml.CharData:
if len(stack) == 0 && strings.TrimSpace(string(t)) != "" {
return nil, store.ErrTaskInvalid
}
case xml.StartElement:
if len(stack) >= 16 {
return nil, store.ErrTaskDAVLimit
}
attrs := map[string]string{}
seen := map[xml.Name]bool{}
for _, a := range t.Attr {
if seen[a.Name] {
return nil, store.ErrTaskInvalid
}
seen[a.Name] = true
if a.Name.Space == "" {
attrs[a.Name.Local] = a.Value
}
}
f := frame{name: t.Name}
if len(stack) == 0 {
root++
if root != 1 || t.Name.Space != "" || t.Name.Local != "opml" || (attrs["version"] != "1.0" && attrs["version"] != "1.1" && attrs["version"] != "2.0") {
return nil, store.ErrTaskInvalid
}
} else {
parent := stack[len(stack)-1]
f.folder = parent.folder
f.commented = parent.commented
if len(stack) == 1 && t.Name.Space == "" {
switch t.Name.Local {
case "head":
head++
if head != 1 || body != 0 {
return nil, store.ErrTaskInvalid
}
case "body":
body++
if head != 1 || body != 1 {
return nil, store.ErrTaskInvalid
}
default:
return nil, store.ErrTaskInvalid
}
}
inBody := len(stack) >= 2 && stack[1].name == (xml.Name{Local: "body"})
if inBody && t.Name.Space == "" && parent.name.Space == "" && (parent.name.Local == "body" || parent.name.Local == "outline") && t.Name.Local != "outline" {
return nil, store.ErrTaskInvalid
}
if inBody && t.Name.Space == "" && t.Name.Local == "outline" && parent.name.Space == "" && (parent.name.Local == "body" || parent.name.Local == "outline") {
outlines++
if outlines > 1000 {
return nil, store.ErrTaskDAVLimit
}
f.commented = f.commented || strings.EqualFold(attrs["isComment"], "true")
label := attrs["text"]
if label == "" {
label = attrs["title"]
}
label = feedText(label, 1024)
kind := strings.ToLower(attrs["type"])
if attrs["xmlUrl"] != "" || kind != "" {
if len(rows) >= 500 {
return nil, store.ErrTaskDAVLimit
}
row := feedOPMLRow{FeedSubscription: store.FeedSubscription{Title: label, Folder: f.folder, URL: strings.TrimSpace(attrs["xmlUrl"])}, Index: len(rows), Status: "add"}
switch {
case f.commented:
row.Status = "commented"
case kind != "" && kind != "rss" && kind != "atom":
row.Status = "unsupported_type"
case !validCalendarSourceURL(row.URL):
row.Status = "unsupported_url"
case label == "" || len(label) > 200 || len(f.folder) > 200:
row.Status = "invalid_fields"
}
rows = append(rows, row)
} else if label != "" {
if f.folder != "" {
f.folder += " / "
}
f.folder += label
}
}
}
stack = append(stack, f)
case xml.EndElement:
if len(stack) == 0 {
return nil, store.ErrTaskInvalid
}
stack = stack[:len(stack)-1]
}
}
if root != 1 || head != 1 || body != 1 || len(stack) != 0 {
return nil, store.ErrTaskInvalid
}
return rows, nil
}
func classifyFeedOPML(rows []feedOPMLRow, sources []store.FeedSource) {
existing := map[string]bool{}
seen := map[string]bool{}
for _, s := range sources {
if s.Provider == "local" {
existing[store.FeedURLKey(s.URL)] = true
}
}
for i := range rows {
row := &rows[i]
if row.Status != "add" {
continue
}
key := store.FeedURLKey(row.URL)
if existing[key] {
row.Status = "existing"
} else if seen[key] {
row.Status = "duplicate"
}
seen[key] = true
}
}
type feedOPMLOutline struct {
Text string `xml:"text,attr"`
Title string `xml:"title,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
URL string `xml:"xmlUrl,attr,omitempty"`
Children []feedOPMLOutline `xml:"outline"`
}
func encodeFeedOPML(sources []store.FeedSource) ([]byte, error) {
var doc struct {
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
Title string `xml:"head>title"`
Outlines []feedOPMLOutline `xml:"body>outline"`
}
doc.Version = "2.0"
doc.Title = "PagesWiki RSS"
groups := map[string]int{}
for _, source := range sources {
if source.Provider != "local" {
continue
}
entry := feedOPMLOutline{Text: source.Title, Title: source.Title, Type: "rss", URL: source.URL}
if source.Folder == "" {
doc.Outlines = append(doc.Outlines, entry)
continue
}
i, ok := groups[source.Folder]
if !ok {
i = len(doc.Outlines)
groups[source.Folder] = i
doc.Outlines = append(doc.Outlines, feedOPMLOutline{Text: source.Folder})
}
doc.Outlines[i].Children = append(doc.Outlines[i].Children, entry)
}
raw, err := xml.MarshalIndent(doc, "", " ")
return append([]byte(xml.Header), raw...), err
}
func (s *Server) handleFeedOPML(w http.ResponseWriter, r *http.Request, domain, actor, key string) {
if key == "opml/export" && r.Method == http.MethodGet {
sources, err := s.store.FeedSources(r.Context(), domain, actor)
if err != nil {
taskError(w, err)
return
}
raw, err := encodeFeedOPML(sources)
if err != nil {
taskError(w, err)
return
}
w.Header().Set("Content-Type", "text/x-opml; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="pageswiki-feeds.opml"`)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Write(raw)
return
}
if r.Method != http.MethodPost || (key != "opml/preview" && key != "opml/import") {
http.NotFound(w, r)
return
}
var input struct {
OPML string `json:"opml"`
Selected []int `json:"selected"`
RequestID string `json:"request_id"`
Expected string `json:"expected"`
Enabled bool `json:"enabled"`
Interval int `json:"interval_seconds"`
}
if !taskJSONLimit(w, r, &input, 2<<20) {
return
}
rows, err := parseFeedOPML(input.OPML)
if err != nil {
taskError(w, err)
return
}
if key == "opml/preview" {
sources, err := s.store.FeedSources(r.Context(), domain, actor)
if err != nil {
taskError(w, err)
return
}
classifyFeedOPML(rows, sources)
writeEventJSON(w, 200, map[string]any{"rows": rows, "expected": store.FeedSourcesSnapshot(sources), "available": 32 - len(sources)})
return
}
// Reparse on apply; client-supplied titles, URLs or row eligibility are not trusted.
classifyFeedOPML(rows, nil)
seen := map[int]bool{}
plan := store.FeedImport{RequestID: input.RequestID, Expected: input.Expected, Enabled: input.Enabled, Interval: input.Interval}
for _, index := range input.Selected {
if index < 0 || index >= len(rows) || seen[index] || rows[index].Status != "add" {
taskError(w, store.ErrTaskInvalid)
return
}
seen[index] = true
plan.Entries = append(plan.Entries, rows[index].FeedSubscription)
}
ids, err := s.store.ImportFeedSubscriptions(r.Context(), domain, actor, plan)
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 201, map[string]any{"source_ids": ids})
}