fix ephemeral handling to not save
Some checks failed
Go / build (push) Has been cancelled

This commit is contained in:
2025-10-10 09:35:15 +01:00
parent 2637f4b85c
commit 075838150d
5 changed files with 89 additions and 4 deletions

View File

@@ -108,6 +108,12 @@ func (d *D) SaveEvent(c context.Context, ev *event.E) (kc, vc int, err error) {
err = errors.New("nil event")
return
}
// Check if the event is ephemeral (kinds 20000-29999) and reject if so
if kind.IsEphemeral(ev.Kind) {
err = errors.New("blocked: ephemeral events (kinds 20000-29999) are not stored")
return
}
// check if the event already exists
var ser *types.Uint40
if ser, err = d.GetSerialById(ev.ID); err == nil && ser != nil {

View File

@@ -215,7 +215,7 @@ func TestSaveExistingEvent(t *testing.T) {
}
// Verify the error message
expectedErrorPrefix := "event already exists: "
expectedErrorPrefix := "blocked: event already exists"
if !bytes.HasPrefix([]byte(err.Error()), []byte(expectedErrorPrefix)) {
t.Fatalf(
"Expected error message to start with '%s', got '%s'",
@@ -223,3 +223,70 @@ func TestSaveExistingEvent(t *testing.T) {
)
}
}
// TestEphemeralEventRejection tests that ephemeral events (kinds 20000-29999) are rejected.
func TestEphemeralEventRejection(t *testing.T) {
// Create a temporary directory for the database
tempDir, err := os.MkdirTemp("", "test-db-*")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after the test
// Create a context and cancel function for the database
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize the database
db, err := New(ctx, cancel, tempDir, "info")
if err != nil {
t.Fatalf("Failed to create database: %v", err)
}
defer db.Close()
// Create a signer
sign := new(p256k.Signer)
if err := sign.Generate(); chk.E(err) {
t.Fatal(err)
}
// Test different ephemeral event kinds
ephemeralKinds := []uint16{
20000, // EphemeralStart
21000, // LightningPubRPC
22242, // ClientAuthentication
23194, // NWCWalletRequest
23195, // NWCWalletResponse
23196, // NWCNotification
23197, // WalletNotification
24133, // NostrConnect
27235, // HTTPAuth
29998, // Just before EphemeralEnd
}
for _, kindValue := range ephemeralKinds {
// Create an ephemeral event
ev := event.New()
ev.Kind = kindValue
ev.Pubkey = sign.Pub()
ev.CreatedAt = timestamp.Now().V
ev.Content = []byte("Ephemeral event")
ev.Tags = tag.NewS()
ev.Sign(sign)
// Try to save the ephemeral event, it should be rejected
_, _, err = db.SaveEvent(ctx, ev)
if err == nil {
t.Fatalf("Expected ephemeral event with kind %d to be rejected, but it was accepted", kindValue)
}
// Verify the error message
expectedError := "blocked: ephemeral events (kinds 20000-29999) are not stored"
if err.Error() != expectedError {
t.Fatalf(
"Expected error message '%s', got '%s' for kind %d",
expectedError, err.Error(), kindValue,
)
}
}
}