feat(sources): add managed RSS feeds (#41)

This commit is contained in:
2026-11-25 01:42:27 +01:00
parent 12acb36612
commit 136d9b3b59
38 changed files with 2103 additions and 67 deletions

View File

@ -0,0 +1,65 @@
// Package sourcefeeds defines the closed, shared admin/runtime RSS allowlist.
package sourcefeeds
import (
"errors"
"net/url"
"strings"
)
const Bloomberg = "bloomberg"
const Investing = "investing_com"
const Host = "feed.quanwenrss.com"
const MaxFeeds = 5
var ErrInvalid = errors.New("source_feeds_invalid")
var ErrUnsupported = errors.New("source_feeds_not_supported")
func Supported(source string) bool { return source == Bloomberg || source == Investing }
func Defaults(source string) []string {
var paths []string
switch source {
case Bloomberg:
paths = []string{"bloomberg-latest.xml", "bloomberg-economics.xml", "bloomberg-technology.xml", "bloomberg-politics.xml", "bloomberg-technology-ai.xml"}
case Investing:
paths = []string{"investing-news.xml"}
}
out := make([]string, 0, len(paths))
for _, path := range paths {
out = append(out, "https://"+Host+"/feed/"+path)
}
return out
}
func Validate(source string, values []string) ([]string, error) {
if !Supported(source) {
return nil, ErrUnsupported
}
allowed := Defaults(source)
if len(values) == 0 || len(values) > len(allowed) {
return nil, ErrInvalid
}
seen := map[string]bool{}
out := make([]string, 0, len(values))
for _, value := range values {
if len(value) > 256 || value != strings.TrimSpace(value) || strings.Contains(value, "#") {
return nil, ErrInvalid
}
u, err := url.Parse(value)
if err != nil || u.Scheme != "https" || (!strings.EqualFold(u.Host, Host) && !strings.EqualFold(u.Host, Host+":443")) || u.User != nil || u.Opaque != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || u.RawPath != "" || (u.Port() != "" && u.Port() != "443") {
return nil, ErrInvalid
}
normalized := "https://" + Host + u.Path
matched := false
for _, known := range allowed {
if normalized == known {
matched = true
}
}
if !matched || seen[normalized] {
return nil, ErrInvalid
}
seen[normalized] = true
out = append(out, normalized)
}
return out, nil
}

View File

@ -0,0 +1,28 @@
package sourcefeeds
import "testing"
func TestAdminAndRuntimeUseClosedSourceSpecificFeedAllowlist(t *testing.T) {
for _, source := range []string{Bloomberg, Investing} {
defaults := Defaults(source)
if _, err := Validate(source, defaults); err != nil {
t.Fatal("approved defaults rejected")
}
for _, bad := range [][]string{nil, {""}, {"http://" + Host + "/feed/investing-news.xml"}, {"https://127.0.0.1/feed/investing-news.xml"}, {"https://" + Host + ".evil.invalid/feed/investing-news.xml"}, {"https://user:secret@" + Host + "/feed/investing-news.xml"}, {"https://" + Host + ":444/feed/investing-news.xml"}, {"https://" + Host + "/feed/unknown.xml"}, {defaults[0] + "?key=secret"}, {defaults[0] + "#"}, {defaults[0], defaults[0]}, {"https://" + Host + "/feed/%69nvesting-news.xml"}} {
if _, err := Validate(source, bad); err != ErrInvalid {
t.Fatal("invalid source feed configuration accepted")
}
}
}
if _, err := Validate(Bloomberg, Defaults(Investing)); err != ErrInvalid {
t.Fatal("cross-source feed accepted")
}
if _, err := Validate("yahoo_finance", Defaults(Investing)); err != ErrUnsupported {
t.Fatal("non-RSS source accepted feed mutation")
}
original := Defaults(Bloomberg)
original[0] = "private"
if Defaults(Bloomberg)[0] == "private" {
t.Fatal("mutable allowlist")
}
}