From c5c17029ba0e02baab2bf7f81bc0b7148ef8de93 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 13 Mar 2024 12:40:54 -0300 Subject: [PATCH] basic kind validation policy. --- README.md | 9 +++++++++ examples/readme-demo/main.go | 10 ++++++++++ policies/kind_validation.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 policies/kind_validation.go diff --git a/README.md b/README.md index 8974535..9cd3f8b 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,11 @@ func main() { // there are many other configurable things you can set relay.RejectEvent = append(relay.RejectEvent, + // built-in policies + policies.ValidateKind, + + // define your own policies + policies.PreventLargeTags(80), func(ctx context.Context, event *nostr.Event) (reject bool, msg string) { if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" { return true, "we don't allow this person to write here" @@ -79,6 +84,10 @@ func main() { // you can request auth by rejecting an event or a request with the prefix "auth-required: " relay.RejectFilter = append(relay.RejectFilter, + // built-in policies + policies.NoComplexFilters, + + // define your own policies func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) { if pubkey := khatru.GetAuthed(ctx); pubkey != "" { log.Printf("request from %s\n", pubkey) diff --git a/examples/readme-demo/main.go b/examples/readme-demo/main.go index c5ec340..1561956 100644 --- a/examples/readme-demo/main.go +++ b/examples/readme-demo/main.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/fiatjaf/khatru" + "github.com/fiatjaf/khatru/policies" "github.com/nbd-wtf/go-nostr" ) @@ -53,6 +54,11 @@ func main() { // there are many other configurable things you can set relay.RejectEvent = append(relay.RejectEvent, + // built-in policies + policies.ValidateKind, + + // define your own policies + policies.PreventLargeTags(80), func(ctx context.Context, event *nostr.Event) (reject bool, msg string) { if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" { return true, "we don't allow this person to write here" @@ -63,6 +69,10 @@ func main() { // you can request auth by rejecting an event or a request with the prefix "auth-required: " relay.RejectFilter = append(relay.RejectFilter, + // built-in policies + policies.NoComplexFilters, + + // define your own policies func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) { if pubkey := khatru.GetAuthed(ctx); pubkey != "" { log.Printf("request from %s\n", pubkey) diff --git a/policies/kind_validation.go b/policies/kind_validation.go new file mode 100644 index 0000000..e3506ce --- /dev/null +++ b/policies/kind_validation.go @@ -0,0 +1,29 @@ +package policies + +import ( + "context" + "encoding/json" + + "github.com/nbd-wtf/go-nostr" +) + +func ValidateKind(ctx context.Context, evt *nostr.Event) (bool, string) { + switch evt.Kind { + case 0: + var m struct { + Name string `json:"name"` + } + json.Unmarshal([]byte(evt.Content), &m) + if m.Name == "" { + return true, "missing json name in kind 0" + } + case 1: + return false, "" + case 2: + return true, "this kind has been deprecated" + } + + // TODO: all other kinds + + return false, "" +}