abuse reports: signed reports, gossip and sync, a local hide rule, visitor tips
A report is signed by a node operator (device key and certificate) and gossiped on one global topic. Each node decides whose reports count: the operator's own report hides a listing on that node, one trusted report of illegal content hides it, and --hide-after trusted scam reports hide it (--trust-reporter names others). Hidden listings stay stored and reachable. Visitors can tip the node's operator off with a proof-of-work request that hides nothing. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -38,6 +38,7 @@ import (
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/message"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/report"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
@@ -48,6 +49,10 @@ const (
|
||||
messageTopicPrefix = "/allmende/1/messages/"
|
||||
// RevocationTopic carries identity revocations for the whole network.
|
||||
RevocationTopic = "/allmende/1/revocations"
|
||||
// ReportTopic carries signed abuse reports for the whole network. They are
|
||||
// few (each needs a reporter's certificate and proof-of-work), and a node
|
||||
// decides for itself which ones count.
|
||||
ReportTopic = "/allmende/1/reports"
|
||||
|
||||
mdnsService = "allmende"
|
||||
maintenanceTick = time.Hour
|
||||
@@ -124,6 +129,7 @@ type Node struct {
|
||||
syncing map[peer.ID]bool
|
||||
oldSync bool // tests: answer like a node from before digests existed
|
||||
revTopic *pubsub.Topic
|
||||
repTopic *pubsub.Topic
|
||||
}
|
||||
|
||||
// New starts a node. Cancel ctx or call Close to stop it.
|
||||
@@ -236,6 +242,10 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
n.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := n.startReports(); err != nil {
|
||||
n.Close()
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range cfg.Regions {
|
||||
if err := n.subscribeRegion(r); err != nil {
|
||||
n.Close()
|
||||
@@ -356,6 +366,18 @@ func (n *Node) PublishRevocation(ctx context.Context, r *identity.Revocation) er
|
||||
return n.revTopic.Publish(ctx, data)
|
||||
}
|
||||
|
||||
// PublishReport stores r locally and gossips it to the whole network.
|
||||
func (n *Node) PublishReport(ctx context.Context, r *report.Report) error {
|
||||
if _, err := n.cfg.Store.PutReport(ctx, r); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := r.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return n.repTopic.Publish(ctx, data)
|
||||
}
|
||||
|
||||
// Close stops the node.
|
||||
func (n *Node) Close() error {
|
||||
n.cancel()
|
||||
@@ -401,6 +423,36 @@ func (n *Node) startRevocations() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Node) startReports() error {
|
||||
if err := n.ps.RegisterTopicValidator(ReportTopic, n.reportValidator,
|
||||
pubsub.WithValidatorTimeout(5*time.Second)); err != nil {
|
||||
return err
|
||||
}
|
||||
t, err := n.ps.Join(ReportTopic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sub, err := t.Subscribe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n.repTopic = t
|
||||
n.wg.Add(1)
|
||||
go func() {
|
||||
defer n.wg.Done()
|
||||
n.consume(n.ctx, sub, func(msg *pubsub.Message) {
|
||||
r, ok := msg.ValidatorData.(*report.Report)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, err := n.cfg.Store.PutReport(n.ctx, r); err != nil && !errors.Is(err, store.ErrRevoked) {
|
||||
n.log.Warn("store report", "err", err)
|
||||
}
|
||||
})
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// regionRun is what a followed region keeps running; dropping the region stops it.
|
||||
type regionRun struct {
|
||||
ctx context.Context
|
||||
@@ -689,6 +741,31 @@ func (n *Node) revocationValidator(_ context.Context, _ peer.ID, msg *pubsub.Mes
|
||||
return res
|
||||
}
|
||||
|
||||
func (n *Node) reportValidator(ctx context.Context, _ peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
|
||||
if len(msg.Data) > report.MaxSize {
|
||||
return pubsub.ValidationReject
|
||||
}
|
||||
var r report.Report
|
||||
if err := json.Unmarshal(msg.Data, &r); err != nil {
|
||||
return pubsub.ValidationReject
|
||||
}
|
||||
// Only the canonical encoding, so every node derives the same message ID.
|
||||
if canon, err := r.Bytes(); err != nil || !bytes.Equal(canon, msg.Data) {
|
||||
return pubsub.ValidationReject
|
||||
}
|
||||
if err := r.Check(n.clock(), n.cfg.Difficulty); err != nil {
|
||||
if errors.Is(err, report.ErrTimeWindow) {
|
||||
return pubsub.ValidationIgnore // clocks differ; not the relay's fault
|
||||
}
|
||||
return pubsub.ValidationReject
|
||||
}
|
||||
if revoked, err := n.cfg.Store.IsRevoked(ctx, r.Reporter, r.Device, r.Cert.IssuedAt); err != nil || revoked {
|
||||
return pubsub.ValidationIgnore
|
||||
}
|
||||
msg.ValidatorData = &r
|
||||
return pubsub.ValidationAccept
|
||||
}
|
||||
|
||||
func (n *Node) validateRevocation(data []byte) (*identity.Revocation, pubsub.ValidationResult) {
|
||||
if len(data) > 4096 {
|
||||
return nil, pubsub.ValidationReject
|
||||
|
||||
@@ -101,6 +101,14 @@ func (tn *tnode) listing(t *testing.T, geohash string) *listing.Listing {
|
||||
return l
|
||||
}
|
||||
|
||||
// store puts l straight into the node's store, failing the test if it is refused.
|
||||
func (tn *tnode) store(t *testing.T, l *listing.Listing) {
|
||||
t.Helper()
|
||||
if _, err := tn.s.Put(context.Background(), l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func connect(t *testing.T, from, to *tnode) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
@@ -449,8 +457,8 @@ func TestIncrementalSyncFetchesOnlyTheDifference(t *testing.T) {
|
||||
var shared []*listing.Listing
|
||||
for i := 0; i < 5; i++ { // page size is 3: two inventory pages
|
||||
l := a.listing(t, "u1xjhx")
|
||||
a.s.Put(ctx, l)
|
||||
b.s.Put(ctx, l)
|
||||
a.store(t, l)
|
||||
b.store(t, l)
|
||||
shared = append(shared, l)
|
||||
}
|
||||
update := &listing.Listing{
|
||||
@@ -463,8 +471,8 @@ func TestIncrementalSyncFetchesOnlyTheDifference(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fresh := a.listing(t, "u1xjhx")
|
||||
a.s.Put(ctx, update)
|
||||
a.s.Put(ctx, fresh)
|
||||
a.store(t, update)
|
||||
a.store(t, fresh)
|
||||
|
||||
connect(t, b, a)
|
||||
eventually(t, "b has the update and the new listing", func() bool {
|
||||
@@ -484,9 +492,8 @@ func TestIncrementalSyncFetchesOnlyTheDifference(t *testing.T) {
|
||||
func TestSyncWithANodeThatKnowsNoDigests(t *testing.T) {
|
||||
a := startNode(t, nil, "u1x")
|
||||
a.oldSync = true
|
||||
ctx := context.Background()
|
||||
for i := 0; i < 4; i++ {
|
||||
a.s.Put(ctx, a.listing(t, "u1xjhx"))
|
||||
a.store(t, a.listing(t, "u1xjhx"))
|
||||
}
|
||||
b := startNode(t, nil, "u1x")
|
||||
connect(t, b, a)
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||
pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/report"
|
||||
)
|
||||
|
||||
// report makes a signed report by this node's operator about a listing.
|
||||
func (tn *tnode) report(t *testing.T, author, id, category string) *report.Report {
|
||||
t.Helper()
|
||||
r := &report.Report{
|
||||
Listing: report.Ref{Author: author, ID: id},
|
||||
Category: category,
|
||||
Note: "Verlangt Vorkasse",
|
||||
CreatedAt: time.Now().Unix(),
|
||||
ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(),
|
||||
}
|
||||
if err := r.Seal(tn.dev, difficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func reportCount(tn *tnode) int {
|
||||
rs, _ := tn.s.Reports(context.Background(), 100)
|
||||
return len(rs)
|
||||
}
|
||||
|
||||
func TestReportsAreGossipedToEveryone(t *testing.T) {
|
||||
// Not even in the same region: reports travel on a topic of their own.
|
||||
a := startNode(t, nil, "u1x")
|
||||
b := startNode(t, nil, "u30")
|
||||
connect(t, a, b)
|
||||
eventually(t, "the report topic has a peer", func() bool { return len(a.ps.ListPeers(ReportTopic)) == 1 })
|
||||
|
||||
l := a.listing(t, "u1xjhx")
|
||||
if err := a.PublishReport(context.Background(), a.report(t, l.Author, l.ID, report.Scam)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventually(t, "b holds the report", func() bool { return reportCount(b) == 1 })
|
||||
}
|
||||
|
||||
func TestALateJoinerGetsTheReportsBySync(t *testing.T) {
|
||||
a := startNode(t, nil, "u1x")
|
||||
l := a.listing(t, "u1xjhx")
|
||||
if _, err := a.s.PutReport(context.Background(), a.report(t, l.Author, l.ID, report.Illegal)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := startNode(t, nil, "u1x") // joins after the report was made, never hears the gossip
|
||||
connect(t, b, a)
|
||||
eventually(t, "b caught up with the reports", func() bool { return reportCount(b) == 1 })
|
||||
}
|
||||
|
||||
// What a relay does with a report it is handed, checked on the validator itself
|
||||
// (as the listing validator is): it must be the exact thing the reporter signed,
|
||||
// from a device that is still valid.
|
||||
func TestTheReportValidatorJudgesWhatArrives(t *testing.T) {
|
||||
a := startNode(t, nil, "u1x")
|
||||
l := a.listing(t, "u1xjhx")
|
||||
rep := a.report(t, l.Author, l.ID, report.Scam)
|
||||
data, _ := rep.Bytes()
|
||||
|
||||
validate := func(data []byte) pubsub.ValidationResult {
|
||||
return a.reportValidator(context.Background(), "", &pubsub.Message{Message: &pb.Message{Data: data}})
|
||||
}
|
||||
if r := validate(data); r != pubsub.ValidationAccept {
|
||||
t.Fatalf("a good report: %v", r)
|
||||
}
|
||||
if r := validate([]byte(`{"v":1`)); r != pubsub.ValidationReject {
|
||||
t.Fatalf("garbage: %v", r)
|
||||
}
|
||||
|
||||
tampered := *rep
|
||||
tampered.Category = report.Illegal // the signature no longer fits
|
||||
tb, _ := tampered.Bytes()
|
||||
if r := validate(tb); r != pubsub.ValidationReject {
|
||||
t.Fatalf("a tampered report: %v", r)
|
||||
}
|
||||
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(data, &m)
|
||||
pretty, _ := json.MarshalIndent(m, "", " ")
|
||||
if r := validate(pretty); r != pubsub.ValidationReject {
|
||||
t.Fatalf("a report that is not in canonical form: %v", r)
|
||||
}
|
||||
|
||||
// Clocks differ: ignored, and the relay is not penalised for it.
|
||||
a.clock = func() time.Time { return time.Now().Add(31 * 24 * time.Hour) }
|
||||
if r := validate(data); r != pubsub.ValidationIgnore {
|
||||
t.Fatalf("an expired report: %v", r)
|
||||
}
|
||||
a.clock = time.Now
|
||||
|
||||
// The reporter's device was revoked: ignored, so it is not passed on.
|
||||
rev, err := identity.Revoke(a.root, a.dev.Cert, time.Now(), difficulty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := a.s.PutRevocation(context.Background(), rev); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r := validate(data); r != pubsub.ValidationIgnore {
|
||||
t.Fatalf("a report from a revoked device: %v", r)
|
||||
}
|
||||
}
|
||||
+44
-1
@@ -14,6 +14,7 @@ import (
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/message"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/report"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
@@ -30,12 +31,14 @@ const (
|
||||
maxSyncPages = 200 // bounds work per region against a misbehaving peer
|
||||
maxRevocations = 5000
|
||||
maxFetch = 100 // records asked for in one fetch
|
||||
maxReportsSync = 5000
|
||||
)
|
||||
|
||||
// digestReply is the answer to a "digest" request. Listings and Messages are
|
||||
// left empty when no region was asked about.
|
||||
type digestReply struct {
|
||||
Revocations store.Digest `json:"revocations"`
|
||||
Reports store.Digest `json:"reports"`
|
||||
Listings store.Digest `json:"listings"`
|
||||
Messages store.Digest `json:"messages"`
|
||||
}
|
||||
@@ -65,7 +68,7 @@ func (n *Node) handleSync(s network.Stream) {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
if n.oldSync && (req.Kind == "digest" || req.Kind == "inventory" || req.Kind == "fetch") {
|
||||
if n.oldSync && (req.Kind == "digest" || req.Kind == "inventory" || req.Kind == "fetch" || req.Kind == "reports") {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
@@ -137,6 +140,18 @@ func (n *Node) handleSync(s network.Stream) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case "reports":
|
||||
reps, err := n.cfg.Store.Reports(n.ctx, maxReportsSync)
|
||||
if err != nil {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
for _, r := range reps {
|
||||
if !write(r.Bytes()) {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
}
|
||||
case "digest":
|
||||
var d digestReply
|
||||
var err error
|
||||
@@ -144,6 +159,10 @@ func (n *Node) handleSync(s network.Stream) {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
if d.Reports, err = n.cfg.Store.ReportDigest(n.ctx); err != nil {
|
||||
_ = s.Reset()
|
||||
return
|
||||
}
|
||||
if req.Region != "" {
|
||||
if !listing.ValidRegion(req.Region) {
|
||||
_ = s.Reset()
|
||||
@@ -294,6 +313,16 @@ func (n *Node) SyncFrom(ctx context.Context, p peer.ID) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Reports after the revocations, so a revoked reporter's are refused.
|
||||
oursReports, err := n.cfg.Store.ReportDigest(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oursReports != theirs.Reports {
|
||||
if err := n.syncReports(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, region := range regions {
|
||||
if err := n.digest(ctx, p, region, &theirs); err != nil {
|
||||
return err
|
||||
@@ -492,6 +521,20 @@ func (n *Node) syncMessages(ctx context.Context, p peer.ID, region string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// syncReports pulls every live report p holds. They are few: each needs a
|
||||
// reporter's certificate and proof-of-work, and they last at most 30 days.
|
||||
func (n *Node) syncReports(ctx context.Context, p peer.ID) error {
|
||||
return n.request(ctx, p, syncRequest{Kind: "reports"}, func(line []byte) {
|
||||
var r report.Report
|
||||
if json.Unmarshal(line, &r) != nil {
|
||||
return
|
||||
}
|
||||
if _, err := n.cfg.Store.PutReport(ctx, &r); err != nil && !errors.Is(err, store.ErrRevoked) {
|
||||
n.log.Debug("sync: report rejected", "peer", p, "err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (n *Node) syncRevocations(ctx context.Context, p peer.ID) error {
|
||||
return n.request(ctx, p, syncRequest{Kind: "revocations"}, func(line []byte) {
|
||||
var r identity.Revocation
|
||||
|
||||
Reference in New Issue
Block a user