3. Write Policy Engine & Grant Enforcement

Nostr relays facing public networks require strict filtering to defend against denial-of-service attacks, spam flooding, invalid cryptographic claims, and timestamp spoofing.

fleetmesh-strfry implements a comprehensive policy enforcement engine (StrfryPolicyEngine) that acts as both an internal validation pipeline and an external strfry write-policy plugin.

3.1. The strfry Plugin Protocol

[strfry write-policy plugins](https://github.com/hoytech/strfry/blob/master/docs/plugins.md) communicate with the strfry relay daemon over standard input and standard output using a line-oriented JSON protocol:

strfry Relay Daemon                                 fleetmesh-strfry plugin
───────────────────                                 ───────────────────────
{"type": "lookahead", ...}                  ───►    (optional cache prep)
{"type": "new", "event": {"id": ...}}       ───►
                                            ◄───    {"id": "...", "action": "accept"}
                                               or   {"id": "...", "action": "reject", "msg": "blocked: ..."}
                                               or   {"id": "...", "action": "shadowReject"}

3.1.1. Configuring strfry to Use the Plugin

In strfry.conf, configure the writePolicy block to invoke the fleetmesh_strfry plugin runner:

relay {
    writePolicy {
        plugin = "/usr/bin/env PYTHONPATH=/opt/fleetmesh/strfry-bridge:/opt/fleetmesh/meshnode:/opt/fleetmesh/core:/opt/fleetmesh/schema python3 -m fleetmesh_strfry plugin --dir /var/lib/fleetmesh"
    }
}

When strfry starts, it spawns this subprocess. For every submitted event, strfry streams the event JSON to the plugin’s stdin, and the plugin returns its verdict on stdout.

3.2. Policy Verification Pipeline

The StrfryPolicyEngine evaluates every submitted event through a sequence of validation gates:

Validation Gate

Description

Event ID Recomputation

Computes the SHA-256 hash of the canonical NIP-01 serialized event array [0, pubkey, created_at, kind, tags, content] and ensures it matches event.id.

Cryptographic Signature

Verifies the 64-byte Schnorr signature (Secp256k1) against the author’s public key.

Clock Skew Enforcement

Compares created_at against the local monotonic clock. Rejects events drifting further into the future than max_future_drift_sec (default 300s) or further into the past than max_past_drift_sec (default 86400s).

NIP-40 Expiration

Evaluates expiration tags. If an event has already expired relative to current wall time, it is rejected immediately.

Payload Size Limits

Rejects any event exceeding max_event_bytes (default 128 KiB).

Kind Whitelisting & Blacklisting

Enforces allowed_kinds, blocked_kinds, and quarantine_kinds filters.

Rate Limiting

Tracks sliding-window submission velocity per pubkey, capping submissions at rate_limit_per_min.

3.3. Bilateral Peering Grant Enforcement

A unique capability of fleetmesh-strfry is bilateral peering grant enforcement. In a private or federated mesh deployment, an operator may wish to restrict relay storage solely to authorized peering partners holding active capacity grants.

When enforce_grants: true is configured:

  1. The policy engine queries the node’s SQLite grant store (grants.sqlite).

  2. It checks whether the author’s pubkey matches an active, unexpired peering grant (Kind 30801).

  3. If no active grant exists for that identity, or if the peer has exceeded their agreed bandwidth credit ceiling, the event is rejected:

{"id": "...", "action": "reject", "msg": "blocked: author has no active peering grant"}

3.4. Shadow Rejections

To analyze spam patterns without alerting malicious actors, the engine supports shadow_reject: true. In shadow mode, the relay responds to the client with an OK confirmation, but internally drops the event from LMDB storage, preserving disk space and downstream bandwidth.

3.5. Egress Policy

In addition to filtering inbound events entering strfry, the engine provides symmetric egress filtering:

  • Inspects events generated by the local node before publishing them out to the strfry relay.

  • Can redact sensitive internal telemetry tags or drop restricted kinds, ensuring that private mesh communications never leak onto public relays unintentionally.