66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
// 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
|
|
}
|