feat(sources): enforce supported source allowlist
Assign stable keys to the three supported sources, preserve excluded historical rows, and expose history read-only in admin. Add migration, coverage, and responsive browser spec. Refs #13 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -31,6 +31,7 @@ type UserAsset struct {
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
SourceKey *string `json:"source_key"`
|
||||
ArticleLimit int `json:"article_limit"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@ -130,7 +130,7 @@ func (r *Repository) CountAdmins() (int, error) {
|
||||
// ── Sources ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (r *Repository) ListSources() ([]Source, error) {
|
||||
rows, err := r.db.Query(`SELECT id, name, type, enabled, created_at, article_limit FROM sources ORDER BY name`)
|
||||
rows, err := r.db.Query(`SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -138,35 +138,44 @@ func (r *Repository) ListSources() ([]Source, error) {
|
||||
var sources []Source
|
||||
for rows.Next() {
|
||||
var s Source
|
||||
if err := rows.Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit); err != nil {
|
||||
if err := rows.Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit, &s.SourceKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sources = append(sources, s)
|
||||
}
|
||||
return sources, nil
|
||||
return sources, rows.Err()
|
||||
}
|
||||
|
||||
func (r *Repository) GetSourceByType(sourceType string) (*Source, error) {
|
||||
s := &Source{}
|
||||
err := r.db.QueryRow(`
|
||||
SELECT id, name, type, enabled, created_at, article_limit FROM sources WHERE type=$1`, sourceType,
|
||||
).Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit)
|
||||
SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources WHERE type=$1 AND source_key IN ('yahoo_finance','bloomberg','watcher_guru')`, sourceType,
|
||||
).Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit, &s.SourceKey)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
|
||||
var ErrSourceNotSupported = errors.New("source_not_supported")
|
||||
|
||||
func (r *Repository) UpdateSource(id string, enabled bool, articleLimit *int) error {
|
||||
result, err := r.db.Exec(`UPDATE sources SET enabled=$1, article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, articleLimit, id)
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
count, err := result.RowsAffected()
|
||||
if err == nil && count == 0 {
|
||||
return sql.ErrNoRows
|
||||
defer tx.Rollback()
|
||||
var key *string
|
||||
if err = tx.QueryRow(`SELECT source_key FROM sources WHERE id=$1 FOR UPDATE`, id).Scan(&key); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
if key == nil || !SupportedSourceKey(*key) {
|
||||
return ErrSourceNotSupported
|
||||
}
|
||||
if _, err = tx.Exec(`UPDATE sources SET enabled=$1,article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, articleLimit, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ── Scrape Credentials ─────────────────────────────────────────────────────
|
||||
|
||||
26
backend/internal/models/source_support.go
Normal file
26
backend/internal/models/source_support.go
Normal file
@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
const (
|
||||
SourceYahooFinance = "yahoo_finance"
|
||||
SourceBloomberg = "bloomberg"
|
||||
SourceWatcherGuru = "watcher_guru"
|
||||
)
|
||||
|
||||
func SupportedSourceKey(key string) bool {
|
||||
switch key {
|
||||
case SourceYahooFinance, SourceBloomberg, SourceWatcherGuru:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (s Source) Supported() bool { return s.SourceKey != nil && SupportedSourceKey(*s.SourceKey) }
|
||||
func (s Source) Operational() bool { return s.Enabled && s.Supported() }
|
||||
func OperationalSources(sources []Source) []Source {
|
||||
result := []Source{}
|
||||
for _, source := range sources {
|
||||
if source.Operational() {
|
||||
result = append(result, source)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
24
backend/internal/models/source_support_test.go
Normal file
24
backend/internal/models/source_support_test.go
Normal file
@ -0,0 +1,24 @@
|
||||
package models
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSourceSupportUsesOnlyCanonicalKeyAndPersistedEnabled(t *testing.T) {
|
||||
yahoo := SourceYahooFinance
|
||||
unknown := "reuters"
|
||||
sources := []Source{
|
||||
{ID: "supported", Type: "stocktwits", SourceKey: &yahoo, Enabled: true, ArticleLimit: 83},
|
||||
{ID: "disabled", SourceKey: &yahoo, Enabled: false, ArticleLimit: 77},
|
||||
{ID: "legacy", Type: "stocktwits", Name: "Yahoo Finance", Enabled: true, ArticleLimit: 42},
|
||||
{ID: "unknown", SourceKey: &unknown, Enabled: true, ArticleLimit: 31},
|
||||
}
|
||||
filtered := OperationalSources(sources)
|
||||
if len(filtered) != 1 || filtered[0].ID != "supported" || filtered[0].ArticleLimit != 83 {
|
||||
t.Fatalf("filtered=%v", filtered)
|
||||
}
|
||||
if !sources[2].Enabled || sources[2].Supported() || sources[2].Operational() {
|
||||
t.Fatal("legacy enabled must remain persisted, without operational support")
|
||||
}
|
||||
if !sources[1].Supported() || sources[1].Operational() {
|
||||
t.Fatal("disabled target support/operational mismatch")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user