648 lines
26 KiB
Go
648 lines
26 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func feedFixture(t *testing.T) (*Server, string, string, string, store.FeedSource) {
|
|
t.Helper()
|
|
s, token, domain, actor := taskFixture(t)
|
|
source, e := s.store.SaveFeedSource(context.Background(), domain, actor, store.FeedSource{UUID: uuid.NewString(), Provider: "local", Title: "Synthetic news", Folder: "Tech", URL: "https://example.org/feed.xml", Enabled: true, Interval: 300})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
sources, e := s.store.FeedSources(context.Background(), domain, actor)
|
|
if e != nil || len(sources) != 1 {
|
|
t.Fatal(e)
|
|
}
|
|
source = sources[0]
|
|
return s, token, domain, actor, source
|
|
}
|
|
func feedRequestTest(s *Server, token, method, key string, body any) *httptest.ResponseRecorder {
|
|
raw, _ := json.Marshal(body)
|
|
r := httptest.NewRequest(method, "http://localhost"+feedsAPI+key, strings.NewReader(string(raw)))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if token != "" {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
func TestFeedReaderStateIsolationAndFencing(t *testing.T) {
|
|
s, token, domain, actor, source := feedFixture(t)
|
|
ctx := context.Background()
|
|
claim, e := s.store.ClaimFeed(ctx, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.ClaimFeed(ctx, source); !errors.Is(e, store.ErrTaskConflict) {
|
|
t.Fatal("double claim", e)
|
|
}
|
|
item := store.FeedItem{ID: "one", Title: "日本語 news", Body: "safe text", URL: "https://example.org/one", PublishedMS: 1}
|
|
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "e1", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if w := feedRequestTest(s, token, "GET", "items", nil); w.Code != 200 || !strings.Contains(w.Body.String(), "日本語") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w := feedRequestTest(s, "", "GET", "items", nil); w.Code == 200 {
|
|
t.Fatal("anonymous")
|
|
}
|
|
if _, e = s.store.FeedItem(ctx, domain, uuid.NewString(), source.UUID, "one"); !errors.Is(e, store.ErrAccessDenied) {
|
|
t.Fatal(e)
|
|
}
|
|
state := map[string]any{"source": source.UUID, "id": "one", "read": true, "starred": true, "version": 0}
|
|
if w := feedRequestTest(s, token, "POST", "state", state); w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w := feedRequestTest(s, token, "POST", "state", state); w.Code != 412 {
|
|
t.Fatal("stale state", w.Code, w.Body)
|
|
}
|
|
got, e := s.store.FeedItem(ctx, domain, actor, source.UUID, "one")
|
|
if e != nil || !got.Read || !got.Starred || got.Version != 1 {
|
|
t.Fatal(got, e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, nil, "", "", "", false); !errors.Is(e, store.ErrTaskConflict) {
|
|
t.Fatal("old completion", e)
|
|
}
|
|
// Re-fetch must preserve local read/star; config edits invalidate in-flight work.
|
|
source.Version = 1
|
|
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
sources, _ := s.store.FeedSources(ctx, domain, actor)
|
|
claim, e = s.store.ClaimFeed(ctx, sources[0])
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
got, _ = s.store.FeedItem(ctx, domain, actor, source.UUID, "one")
|
|
if !got.Read || !got.Starred {
|
|
t.Fatal("lost state")
|
|
}
|
|
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
sources, _ = s.store.FeedSources(ctx, domain, actor)
|
|
claim, e = s.store.ClaimFeed(ctx, sources[0])
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
source.Enabled = false
|
|
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "", "", "", false); !errors.Is(e, store.ErrTaskConflict) {
|
|
t.Fatal("disabled late commit", e)
|
|
}
|
|
if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.FeedItems(ctx, domain, actor, "", "", "", -1, ""); !errors.Is(e, store.ErrAccessDenied) {
|
|
t.Fatal(e)
|
|
}
|
|
if w := feedRequestTest(s, token, "GET", "items", nil); w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
}
|
|
func TestFeedParserSafetyAndFormats(t *testing.T) {
|
|
rss := `<?xml version="1.0"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>News</title><item><guid>id</guid><title>日本語</title><link>/one</link><content:encoded><![CDATA[<p>Hello</p><script>alert(1)</script><img src="https://evil.invalid/pixel"><b>world</b>]]></content:encoded><pubDate>Fri, 06 Nov 2026 10:00:00 +0000</pubDate></item></channel></rss>`
|
|
items, e := parseFeed([]byte(rss), "https://example.org/feed")
|
|
if e != nil || len(items) != 1 || items[0].URL != "https://example.org/one" || strings.Contains(items[0].Body, "alert") || strings.Contains(items[0].Body, "pixel") || items[0].PublishedMS == 0 {
|
|
t.Fatal(items, e)
|
|
}
|
|
atom := `<feed xmlns="http://www.w3.org/2005/Atom"><title>Atom</title><entry><id>urn:one</id><title>One</title><link href="https://example.org/one"/><updated>2026-11-01T10:00:00Z</updated><content type="html"><p>Body</p></content></entry></feed>`
|
|
items, e = parseFeed([]byte(atom), "https://example.org")
|
|
if e != nil || len(items) != 1 || items[0].Body != "Body" {
|
|
t.Fatal(items, e)
|
|
}
|
|
for _, raw := range []string{`<!DOCTYPE rss [<!ENTITY x SYSTEM "file:///etc/passwd">]><rss/>`, `<html>error</html>`, `<rss/><rss/>`, `<rss><channel><item><guid>x</guid></item><item><guid>x</guid></item></channel></rss>`, strings.Repeat("x", (4<<20)+1)} {
|
|
if _, e = parseFeed([]byte(raw), "https://example.org"); e == nil {
|
|
t.Fatal("accepted malformed feed")
|
|
}
|
|
}
|
|
if feedLink("https://example.org", "javascript:alert(1)") != "" || feedLink("https://example.org", "https://user:pass@example.org") != "" {
|
|
t.Fatal("unsafe link")
|
|
}
|
|
items, e = parseFeed([]byte(`<rss><channel><item><title>No ID or link</title><description>First body</description></item><item><title>No ID or link</title><description>Second body</description></item></channel></rss>`), "https://example.org/feed")
|
|
if e != nil || len(items) != 2 || items[0].ID == items[1].ID || items[0].URL != "" {
|
|
t.Fatal("missing IDs must not collapse to the feed URL", items, e)
|
|
}
|
|
}
|
|
func TestFreshRSSReadOnlyContract(t *testing.T) {
|
|
requests := []string{}
|
|
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requests = append(requests, r.Method+" "+r.URL.Path)
|
|
switch r.URL.Path {
|
|
case "/api/greader.php/accounts/ClientLogin":
|
|
if r.Method != "POST" {
|
|
t.Error("login method")
|
|
}
|
|
r.ParseForm()
|
|
if r.Form.Get("Email") != "reader" || r.Form.Get("Passwd") != "api-secret" {
|
|
t.Error("login form")
|
|
}
|
|
fmt.Fprint(w, "SID=x\nAuth=reader/token\n")
|
|
case "/api/greader.php/reader/api/0/stream/contents/reading-list":
|
|
if r.Header.Get("Authorization") != "GoogleLogin auth=reader/token" || r.URL.Query().Get("n") != "200" {
|
|
t.Error("request contract")
|
|
}
|
|
fmt.Fprint(w, `{"items":[{"id":"tag:1","title":"Title","summary":{"content":"<p>Text</p>"},"published":1793500000,"categories":["user/reader/state/com.google/read","user/reader/state/com.google/starred","user/reader/label/Tech"],"origin":{"title":"Feed"},"alternate":[{"href":"https://example.org/article"}]}]}`)
|
|
default:
|
|
t.Error("unexpected mutation", r.URL.Path)
|
|
w.WriteHeader(404)
|
|
}
|
|
}))
|
|
defer remote.Close()
|
|
items, e := fetchFreshRSS(context.Background(), remote.Client(), store.FeedSource{URL: remote.URL + "/api/greader.php"}, "reader", "api-secret")
|
|
if e != nil || len(items) != 1 || !items[0].Read || !items[0].Starred || items[0].Folder != "Tech" || items[0].Body != "Text" || items[0].BodyHTML != "<p>Text</p>" || len(requests) != 2 {
|
|
t.Fatal(items, e, requests)
|
|
}
|
|
for _, bad := range []string{"http://example.org/feed", "https://127.0.0.1/feed", "https://10.0.0.1/feed", "https://example.org:8443/feed", "https://user:pass@example.org/feed"} {
|
|
if validCalendarSourceURL(bad) {
|
|
t.Fatal("SSRF URL", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFeedHTMLFormattingAndIsolation(t *testing.T) {
|
|
raw := `<h2>Title</h2><p class="x" style="position:fixed" onclick="bad()"><strong>Bold</strong></p><table><tr><td colspan="2">Cell</td></tr></table><a href="../story" target="_top">Story</a><a href="javascript:bad()">bad</a><script>bad()</script><style>@import '/__feed_leak';</style><form action="/__feed_leak"><input autofocus></form><img src="/__feed_leak"><iframe src="/__feed_leak"></iframe><svg onload="bad()"></svg>`
|
|
clean := feedHTML(raw, "https://example.org/news/feed.xml")
|
|
for _, want := range []string{`<h2>Title</h2>`, `<strong>Bold</strong>`, `<table>`, `colspan="2"`, `href="https://example.org/story"`, `target="_blank"`, `noopener`} {
|
|
if !strings.Contains(clean, want) {
|
|
t.Fatal("format lost", want, clean)
|
|
}
|
|
}
|
|
for _, bad := range []string{"<script", "<style", "<form", "<input", "<img", "<iframe", "<svg", "onclick", "javascript:", "position:", "_top", "__feed_leak"} {
|
|
if strings.Contains(clean, bad) {
|
|
t.Fatal("unsafe markup", bad, clean)
|
|
}
|
|
}
|
|
if feedHTML(clean, "https://example.org/news/feed.xml") != clean {
|
|
t.Fatal("sanitizer not stable")
|
|
}
|
|
if feedHTML(strings.Repeat("<p>x</p>", 17000), "https://example.org") != "" {
|
|
t.Fatal("HTML cap")
|
|
}
|
|
for _, kind := range []string{"text", "html", "xhtml"} {
|
|
content := `<b>bold</b>`
|
|
if kind == "xhtml" {
|
|
content = `<div xmlns="http://www.w3.org/1999/xhtml"><b>bold</b></div>`
|
|
}
|
|
items, e := parseFeed([]byte(`<feed xmlns="http://www.w3.org/2005/Atom"><entry><id>x</id><content type="`+kind+`">`+content+`</content></entry></feed>`), "https://example.org")
|
|
if e != nil || len(items) != 1 {
|
|
t.Fatal(e)
|
|
}
|
|
if kind == "text" {
|
|
if items[0].BodyHTML != "" || items[0].Body != "<b>bold</b>" {
|
|
t.Fatal("literal Atom text", items)
|
|
}
|
|
} else if !strings.Contains(items[0].BodyHTML, "<b>bold</b>") {
|
|
t.Fatal("Atom markup", kind, items)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFeedPaginationFailureAndDelete(t *testing.T) {
|
|
s, token, domain, actor, source := feedFixture(t)
|
|
ctx := context.Background()
|
|
claim, e := s.store.ClaimFeed(ctx, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
items := []store.FeedItem{}
|
|
for i := 0; i < 205; i++ {
|
|
items = append(items, store.FeedItem{ID: fmt.Sprintf("id-%03d", i), Title: "Title", Body: "Body", PublishedMS: 100})
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, items, "old", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
seen := map[string]bool{}
|
|
before := int64(-1)
|
|
key := ""
|
|
for {
|
|
batch, e := s.store.FeedItems(ctx, domain, actor, "", "", "", before, key)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
more := len(batch) > 100
|
|
if more {
|
|
batch = batch[:100]
|
|
}
|
|
for _, item := range batch {
|
|
if seen[item.ID] {
|
|
t.Fatal("duplicate")
|
|
}
|
|
seen[item.ID] = true
|
|
}
|
|
if !more {
|
|
break
|
|
}
|
|
last := batch[len(batch)-1]
|
|
before = last.PublishedMS
|
|
key = last.Source + ":" + last.ID
|
|
}
|
|
if len(seen) != 205 {
|
|
t.Fatal(len(seen))
|
|
}
|
|
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
sources, _ := s.store.FeedSources(ctx, domain, actor)
|
|
claim, e = s.store.ClaimFeed(ctx, sources[0])
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, nil, "", "", "network", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.FeedItem(ctx, domain, actor, source.UUID, "id-001"); e != nil {
|
|
t.Fatal("cache lost on failure", e)
|
|
}
|
|
if w := feedRequestTest(s, token, "POST", "sources/"+source.UUID+"/delete", map[string]int64{"version": source.Version}); w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
var count int
|
|
if e = s.store.DB.QueryRow(`SELECT count(*) FROM feed_items`).Scan(&count); e != nil || count != 0 {
|
|
t.Fatal("cascade", count, e)
|
|
}
|
|
var integrity string
|
|
if e = s.store.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); e != nil || integrity != "ok" {
|
|
t.Fatal(integrity, e)
|
|
}
|
|
}
|
|
func TestFeedSourceAPIAndCredentialBinding(t *testing.T) {
|
|
s, token, domain, actor, _ := feedFixture(t)
|
|
input := map[string]any{"uuid": uuid.NewString(), "provider": "freshrss", "title": "Remote", "url": "https://rss.example.org/api/greader.php", "interval_seconds": 300, "enabled": true, "username": "reader", "password": "top-secret"}
|
|
w := feedRequestTest(s, token, "POST", "sources", input)
|
|
if w.Code != 200 || strings.Contains(w.Body.String(), "secret") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
// Each HTTP retry seals a different ciphertext, but identical credentials and
|
|
// request UUID must still return the same source, not create a duplicate.
|
|
if retry := feedRequestTest(s, token, "POST", "sources", input); retry.Code != 200 || retry.Body.String() != w.Body.String() {
|
|
t.Fatal("source replay", retry.Code, retry.Body)
|
|
}
|
|
input["password"] = "changed-secret"
|
|
if retry := feedRequestTest(s, token, "POST", "sources", input); retry.Code != 412 {
|
|
t.Fatal("changed source replay", retry.Code, retry.Body)
|
|
}
|
|
input["password"] = "top-secret"
|
|
sources, e := s.store.FeedSources(context.Background(), domain, actor)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if len(sources) != 2 {
|
|
t.Fatal("duplicate source", len(sources))
|
|
}
|
|
var remote store.FeedSource
|
|
for _, v := range sources {
|
|
if v.Provider == "freshrss" {
|
|
remote = v
|
|
}
|
|
}
|
|
u, p, e := s.store.OpenFeedCredential(remote)
|
|
if e != nil || u != "reader" || p != "top-secret" {
|
|
t.Fatal("credential", e)
|
|
}
|
|
remote.Owner = uuid.NewString()
|
|
if _, _, e = s.store.OpenFeedCredential(remote); e == nil {
|
|
t.Fatal("credential rebound")
|
|
}
|
|
if w = feedRequestTest(s, token, "GET", "sources", nil); w.Code != 200 || strings.Contains(w.Body.String(), "top-secret") || strings.Contains(w.Body.String(), "credential") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
input["url"] = "https://127.0.0.1/api/greader.php"
|
|
if w = feedRequestTest(s, token, "POST", "sources", input); w.Code != 422 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
|
|
func TestFeedFreshRSSAuthorityAndRevocation(t *testing.T) {
|
|
s, token, domain, actor, local := feedFixture(t)
|
|
ctx := context.Background()
|
|
v, e := s.store.SaveFeedSource(ctx, domain, actor, store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
claim := func() store.FeedSource {
|
|
t.Helper()
|
|
// Simulate the next scheduler tick without a wall-clock wait.
|
|
if _, err := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, v.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
all, err := s.store.FeedSources(ctx, domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, source := range all {
|
|
if source.UUID == v.UUID {
|
|
v = source
|
|
}
|
|
}
|
|
c, err := s.store.ClaimFeed(ctx, v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
c := claim()
|
|
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "removed", Title: "Removed"}, {ID: "kept", Title: "Kept", Read: true, Starred: true}}, "", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.SetFeedState(ctx, domain, actor, v.UUID, "kept", false, false, 0); !errors.Is(e, store.ErrAccessDenied) {
|
|
t.Fatal("remote state mutation", e)
|
|
}
|
|
c = claim()
|
|
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "kept", Title: "Changed elsewhere", Read: false}}, "", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
items, e := s.store.FeedItems(ctx, domain, actor, v.UUID, "", "", -1, "")
|
|
if e != nil || len(items) != 1 || items[0].Read || items[0].Starred || items[0].Title != "Changed elsewhere" {
|
|
t.Fatal("non-authoritative cache", items, e)
|
|
}
|
|
c = claim()
|
|
if e = s.store.SetPrincipalState(domain, actor, "readonly"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Changing principal state invalidates the previous session.
|
|
token, e = s.store.CreateAuthSession(domain, actor)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.FeedItems(ctx, domain, actor, "", "", "", -1, ""); e != nil {
|
|
t.Fatal("readonly read", e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, c, nil, "", "", "", false); !errors.Is(e, store.ErrAccessDenied) {
|
|
t.Fatal("late revoked worker", e)
|
|
}
|
|
if w := feedRequestTest(s, token, "POST", "sources/"+local.UUID+"/delete", map[string]int64{"version": 1}); w.Code != 403 {
|
|
t.Fatal("readonly API", w.Code, w.Body)
|
|
}
|
|
if due, err := s.store.DueFeedSources(ctx); err != nil || len(due) != 0 {
|
|
t.Fatal("inactive source starves queue", due, err)
|
|
}
|
|
if e = s.store.SetPrincipalState(domain, actor, "active"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if due, err := s.store.DueFeedSources(ctx); err != nil || len(due) != 0 {
|
|
t.Fatal("disabled extension starves queue", due, err)
|
|
}
|
|
}
|
|
|
|
func TestFeedNoteAtomicReceiptAndPermissions(t *testing.T) {
|
|
s, token, domain, _, source := feedFixture(t)
|
|
ctx := context.Background()
|
|
claim, e := s.store.ClaimFeed(ctx, source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{{ID: "note", Title: "Title", Body: "Body"}}, "", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
id := uuid.NewString()
|
|
input := map[string]any{"request_id": id, "source": source.UUID, "id": "note", "path": "/notes/rss-" + id, "title": "RSS title", "body": "Source: https://example.org\n{{children}} <script>bad()</script>", "confirm_publication": true}
|
|
w := feedRequestTest(s, token, "POST", "note", input)
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
original := w.Body.String()
|
|
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 201 || w.Body.String() != original {
|
|
t.Fatal("replay", w.Code, w.Body)
|
|
}
|
|
var response map[string]string
|
|
json.Unmarshal(w.Body.Bytes(), &response)
|
|
rev, e := s.store.GetLatestRevision(response["page_uuid"])
|
|
if e != nil || strings.Contains(rev.Body, "{{children}}") || strings.Contains(rev.Body, "<script>") {
|
|
t.Fatal(rev, e)
|
|
}
|
|
input["body"] = "changed"
|
|
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 412 {
|
|
t.Fatal("changed replay", w.Code, w.Body)
|
|
}
|
|
input["request_id"] = uuid.NewString()
|
|
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 409 {
|
|
t.Fatal("path exists", w.Code, w.Body)
|
|
}
|
|
input["path"] = "/u/other/rss"
|
|
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 403 {
|
|
t.Fatal("reserved user space", w.Code, w.Body)
|
|
}
|
|
// Roll back page/path when the durable receipt cannot be committed.
|
|
if _, e = s.store.DB.Exec(`CREATE TRIGGER fail_feed_receipt BEFORE INSERT ON feed_note_receipts BEGIN SELECT RAISE(ABORT,'synthetic failure'); END`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
input["path"] = "/notes/rollback"
|
|
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 500 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
var n int
|
|
if e = s.store.DB.QueryRow(`SELECT count(*) FROM paths WHERE domain_uuid=? AND path='/notes/rollback'`, domain).Scan(&n); e != nil || n != 0 {
|
|
t.Fatal("partial note", n, e)
|
|
}
|
|
}
|
|
|
|
type feedTransportFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f feedTransportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
|
|
|
func TestFeedBackgroundConditionalFetch(t *testing.T) {
|
|
s, _, domain, actor, source := feedFixture(t)
|
|
ctx := context.Background()
|
|
step := 0
|
|
client := &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) {
|
|
step++
|
|
if r.URL.String() != source.URL || r.Method != "GET" {
|
|
t.Fatal("unexpected request", r.Method, r.URL)
|
|
}
|
|
deadline, ok := r.Context().Deadline()
|
|
if !ok || time.Until(deadline) > 8*time.Second {
|
|
t.Fatal("unbounded fetch")
|
|
}
|
|
switch step {
|
|
case 1:
|
|
return &http.Response{StatusCode: 200, Header: http.Header{"Etag": {`"feed-1"`}}, Body: io.NopCloser(strings.NewReader(`<rss><channel><item><guid>id</guid><title>First</title></item></channel></rss>`))}, nil
|
|
case 2:
|
|
if r.Header.Get("If-None-Match") != `"feed-1"` {
|
|
t.Fatal("missing conditional GET")
|
|
}
|
|
return &http.Response{StatusCode: 304, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(""))}, nil
|
|
default:
|
|
return nil, errors.New("synthetic failure containing a secret URL; must not be persisted")
|
|
}
|
|
})}
|
|
for tick := 1; tick <= 3; tick++ {
|
|
if _, e := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, source.UUID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{UUID: uuid.NewString(), Type: store.FeedSyncEventType}, client); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if step != tick {
|
|
t.Fatal("wrong fetch count", step, tick)
|
|
}
|
|
item, e := s.store.FeedItem(ctx, domain, actor, source.UUID, "id")
|
|
if e != nil || item.Title != "First" {
|
|
t.Fatal("cache not preserved", item, e)
|
|
}
|
|
all, e := s.store.FeedSources(ctx, domain, actor)
|
|
if e != nil || len(all) != 1 || all[0].ETag != `"feed-1"` || all[0].Lease != "" || all[0].NextMS <= time.Now().UnixMilli() {
|
|
t.Fatal("worker metadata", all, e)
|
|
}
|
|
if tick == 3 && all[0].Error != "network" {
|
|
t.Fatal("secret error leaked", all[0].Error)
|
|
}
|
|
if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{UUID: uuid.NewString()}, client); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if step != tick {
|
|
t.Fatal("cooldown ignored")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFeedsChromium(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
s, token, domain, actor, source := feedFixture(t)
|
|
prefs, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prefs.WikiPrefix = "/notes/{YYYY}/{MM}/{DD}/{Subject}"
|
|
if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, prefs, time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claim, e := s.store.ClaimFeed(context.Background(), source)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
items := []store.FeedItem{}
|
|
for i := 0; i < 120; i++ {
|
|
items = append(items, store.FeedItem{ID: fmt.Sprint(i), Title: fmt.Sprintf("%03d 日本語のRSS記事 — リーダーの密度とキーボード操作", i), Body: "安全な本文です。\n\n<script>display as text</script> {{children}}\n\nSource content stays inert.", Feed: "技術ニュース", URL: "https://example.org/article", PublishedMS: int64(1793500000000 + i)})
|
|
}
|
|
items[119].BodyHTML = `<h2>HTMLの記事本文</h2><p><strong>強調</strong>や<a href="/article">リンク</a>をそのまま読めます。</p><ul><li>見出しと箇条書き</li><li>テーマに沿った表示</li></ul><table><tr><th>項目</th><th>内容</th></tr><tr><td>本文</td><td>HTML表示</td></tr></table><script>window.rssAttack=1</script><img src="/__feed_leak" onerror="window.rssAttack=1"><style>body{display:none}</style>`
|
|
if e = s.store.CompleteFeed(context.Background(), claim, items, "", "", "", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
fresh := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS fixture", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300}
|
|
fresh.Credential, e = s.store.SealFeedCredential(domain, actor, fresh.UUID, "fixture", "synthetic")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
fresh, e = s.store.SaveFeedSource(context.Background(), domain, actor, fresh)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
all, e := s.store.FeedSources(context.Background(), domain, actor)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, v := range all {
|
|
if v.UUID == fresh.UUID {
|
|
fresh = v
|
|
}
|
|
}
|
|
claim, e = s.store.ClaimFeed(context.Background(), fresh)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
catalog, e := parseFreshRSSCatalog([]byte(testFeedCatalog))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "catalog-a", Title: "Catalog first", RemoteFeed: "feed/1", RemoteCategories: []string{"user/alice/label/Tech"}}, {ID: "catalog-b", Title: "Catalog second", RemoteFeed: "feed/2", RemoteCategories: []string{"user/alice/label/Other"}}}, "", "", "", false, store.FeedCatalogSnapshot{Subscriptions: catalog, Folders: []store.FeedCategory{{ID: "user/-/label/Empty/Folder", Label: "Empty/Folder"}}}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
s.store.BackgroundJobsDisabled = true
|
|
s = New(s.store)
|
|
script, e := os.ReadFile("internal/server/testdata/feeds.js")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
handler := s.handler()
|
|
web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
if r.URL.Path == "/fixture/run-feed-actions" && r.Method == "POST" {
|
|
fake := &feedRemoteFake{id: "catalog-b"}
|
|
if e := s.syncFeedActions(r.Context(), fake.client(t)); e != nil {
|
|
http.Error(w, "test worker failed", 500)
|
|
return
|
|
}
|
|
w.WriteHeader(204)
|
|
return
|
|
}
|
|
if r.URL.Path == "/fixture/feed-history" && r.Method == "POST" {
|
|
missing, orphan := uuid.NewString(), uuid.NewString()
|
|
for _, entry := range []struct{ request, source, item string }{{missing, fresh.UUID, "history-evicted"}, {orphan, uuid.NewString(), `<img src=x onerror=alert(1)>`}} {
|
|
if _, e := s.store.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,'read',1,1,'unknown',?)`, domain, actor, entry.request, entry.source, entry.item, time.Now().UnixMilli()); e != nil {
|
|
t.Error(e)
|
|
http.Error(w, "fixture insert failed", 500)
|
|
return
|
|
}
|
|
}
|
|
writeEventJSON(w, 200, map[string]string{"missing": missing, "orphan": orphan})
|
|
return
|
|
}
|
|
if r.URL.Path == "/fixture/check-feed-history" && r.Method == "POST" {
|
|
fake := &feedRemoteFake{id: "history-evicted", read: true}
|
|
if e := s.syncFeedActions(r.Context(), fake.client(t)); e != nil || fake.edits != 0 {
|
|
t.Errorf("history read-only check: %v edits=%d", e, fake.edits)
|
|
http.Error(w, "test recheck failed", 500)
|
|
return
|
|
}
|
|
w.WriteHeader(204)
|
|
return
|
|
}
|
|
if r.URL.Path == "/fixture" {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
fmt.Fprint(w, "<!doctype html><body style='margin:0'><script>"+string(script)+"</script></body>")
|
|
return
|
|
}
|
|
handler.ServeHTTP(w, r)
|
|
}))
|
|
defer web.Close()
|
|
for _, width := range []string{"1440", "390"} {
|
|
if _, e = s.store.DB.Exec(`DELETE FROM feed_actions`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.DB.Exec(`UPDATE feed_items SET is_read=0 WHERE source_uuid=?`, fresh.UUID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
|
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", web.URL + "/fixture?width=" + url.QueryEscape(width), t.TempDir(), "/tmp/pageswiki-feeds-" + width + ".png", width, "850"}
|
|
out, e := exec.CommandContext(ctx, "node", args...).CombinedOutput()
|
|
cancel()
|
|
if e != nil {
|
|
t.Fatalf("browser %s: %v %s", width, e, out)
|
|
}
|
|
}
|
|
}
|