Skip to content

user-repository: stop write paths from clobbering admission and vanish state #1253

In review
saket0187/nostream
#3

Description

@chappie-daemon

From the 2026-11-01 scan: F-012 (authorization bypass, CWE-863), F-033 (CWE-862), F-036 (CWE-367), all in src/repositories/user-repository.ts. Reachability was not established by the scan.

Current code (main 49f51378)

  • F-033 — an invoice write can clear another account's flags. The upsert is insert(row).onConflict('pubkey').merge(omit(['pubkey','balance','created_at'])(row)). A row prepared for a caller-selected beneficiary carries admitted and isVanished as false, so an anonymous invoice request for an existing pubkey merges those defaults over live state.
  • F-012 — a replica-derived negative becomes authoritative. isVanished() hydrates a user missing from the users table with the value the event repository reports, then writes it; a vanish request missed under replication lag is stored as a permanent false.
  • F-036 — lazy initialisation races a real vanish. The same hydrate-update writes the earlier snapshot, so a true committed between the read and the write is overwritten with false.

Fix shape

  • Insert-if-absent, then read back: initialisation must never update an existing row (onConflict().ignore(), then findByPubkey).
  • Prepare invoice beneficiaries with insert-if-absent; leave admission and vanish changes to their dedicated methods.
  • Make general upserts merge only fields the caller explicitly supplied.

Acceptance

  • An invoice for an existing admitted, vanished pubkey leaves both flags unchanged.
  • Initialisation after a true is committed returns and stores true.
  • A replica-derived false never overwrites primary state.

Activity

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

    @saket0187
    Contributor

    Could you assign it to me?

    The PR is on my fork: saket0187#3 (commit). Invoices and the vanish check no longer overwrite a user's admission or vanish state.

    • An invoice for an existing pubkey is now insert-if-absent, and a new row takes its vanish state from the stored events; the vanish check never overwrites a row another writer created and reads the primary instead of the replica.
    • Trade-off: any undeleted stored kind-62 still counts, whatever relay it names, as on the first-event path today.
    • 10 unit cases and 2 integration scenarios (a renewal invoice on an admitted, vanished subscriber; an invoice for a pubkey with only a stored kind-62), all failing on 75b375c.

    Could you cherry-pick it when you get a chance? @cameri

  3. cameri commented on Nov 29, 2026

    @cameri
    Owner

    @saket0187 assigned to you

  4. saket0187 commented on Nov 29, 2026

    @saket0187
    Contributor

    @cameri Could you review my pr and let me know if any changes are needed?

  5. chappie-daemon commented on Nov 29, 2026

    @chappie-daemon
    CollaboratorAuthor

    Reviewed the fork submission (saket0187#3) against main 5d295d73: applied the diff to a clean tree, 3 644 unit tests pass, and reverting only the src/ change makes 8 of the new cases fail. The three findings are genuinely fixed — F-033 by insert-if-absent on the invoice path, F-012 by reading the primary, F-036 because the vanish writer merges unconditionally, so a concurrent vanish wins either ordering.

    Three non-blocking notes are on the PR: an extra events query on every invoice, the benchmark description at src/scripts/benchmark-queries.ts:206 which now describes a query that no longer runs per message, and a missing contract comment on IUserRepository.upsert. The two new integration scenarios are provable only in CI.

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