Skip to content

filters: index generic tag criteria instead of scanning every criterion per tag #1256

Done
saket0187/nostream
#4

Description

@chappie-daemon

From the 2026-11-01 scan: F-026 (low, denial of service, CWE-407), src/utils/event.ts. Reachability was not established by the scan.

Current code (main 49f51378): generic-tag matching is event.tags.some(tag => criteria.some(criterion => isMatchingGenericTagCriterion(key, criterion)(tag))). Every event tag is compared against every criterion, so cost is |tags| × |criteria| predicate calls per filter — and both sides are attacker-sized: the event comes from the wire, the criteria come from a subscription the attacker opened. Several subscriptions per connection multiply it, all on the worker's event loop.

Fix shape

  • Compile exact-match criteria into a map keyed by tag name with value sets; walk event tags once and look up membership.
  • Handle prefix criteria separately, with a bounded comparison count.
  • Enforce an aggregate matching budget per connection's subscriptions. Out of scope (2026-11-29): admission already bounds this — maxSubscriptions, maxFilters and maxFilterValues cap what a subscription can carry, and a connection-wide budget has no default in this issue. File it separately if profiling shows the caps are not enough.

Acceptance

  • A test asserts predicate calls grow with |tags| + |criteria|, not with their product.
  • A filter with 10k criteria rejects a non-matching event in bounded time.

Activity

  1. added
    security
    on Nov 26, 2026
  2. saket0187 commented on Nov 29, 2026

    @saket0187
    Contributor

    Hi! I'd like to take this one. Fork PR: saket0187#4 (commit 5f460c6b, on current main).

    • Filters are compiled once per subscription (and once per REQ for the stored read), and each event's single-letter tags are indexed once per broadcast and shared by every filter and client. A tag name now costs set lookups on the smaller side plus a binary search per #g prefix: 5,000 p tags vs a 2,500-value #p filter went from 221 ms to 1.7 ms.
    • Same lines: a tag without a value (["g"]) made #g prefix filters throw, which cut the live broadcast short and stopped a REQ before EOSE. It's skipped now; a unit test and two cucumber scenarios fail on main and pass with the fix.
    • I did not add a per-connection matching budget (point 3): the existing maxSubscriptions/maxFilters/maxFilterValues still govern admission. If you'd like an explicit cap, tell me the default you want and I'll add it to this PR.

    Could you assign it to me and cherry-pick 5f460c6b if it looks good? @cameri

  3. chappie-daemon commented on Nov 29, 2026

    @chappie-daemon
    CollaboratorAuthor

    Assigned — it is yours.

    On your three points:

    • The tag-with-no-value crash is the part that is not optional: src/utils/event.ts:52 calls tag[1].startsWith(...) for a #g prefix criterion, so one event carrying a bare ["g"] tag throws and takes the broadcast down with it. Good catch, and worth its own scenario.
    • Point 3, the per-connection budget: dropped. I edited the issue to say so — maxSubscriptions, maxFilters and maxFilterValues already bound what a subscription can carry, and a connection-wide budget has no default anyone has specified. Do not add one to this PR. Your index plus the existing caps is the answer, and the 10k-criteria bounded-time case stays in acceptance to prove it.
    • Your 221 ms → 1.7 ms is your measurement; I have not reproduced it, so keep it in the PR body as measured-by-you rather than as a claim the repo makes.

    I am landing this as a PR in cameri/nostream carrying your commit with -x, the same way your last fix went in, so authorship and message stay yours. Review is against your current head (5f460c6b); if I want anything changed I will commit on top rather than ask you to re-push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

security

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions