Skip to content

fix(sybil): enforce ownership and admin-only guards on mutation routes - #934

Open
timmyp10111 wants to merge 2 commits into
DigiNodes:mainfrom
timmyp10111:fix/sybil-authorization-controls
Open

timmyp10111 wants to merge 2 commits into
DigiNodes:mainfrom
timmyp10111:fix/sybil-authorization-controls

Conversation

@timmyp10111

Copy link
Copy Markdown

Linked task

Closes #852

Summary

Problem
The Sybil-resistance controller had no route-level authorization guards. Any authenticated user could:

POST /sybil/users/:userId/score — record a score for any arbitrary user ID
POST /sybil/users/:userId/recompute — recompute scores with pinned versions/fail-closed options for any user
POST /sybil/users/:userId/appeal — submit appeals targeting other users' accounts (IDOR)
POST /sybil/users/:userId/verify-worldcoin — set Worldcoin verification state to any boolean value without proof verification, for any user
POST /sybil/recalculate-all — trigger bulk score recalculation across the entire user table
The GlobalAuthGuard only requires a valid JWT for POST/PUT/PATCH/DELETE — it does not enforce ownership or role.

Fix
New SybilOwnershipGuard
Enforces request.params.userId === JWT subject. Prevents IDOR: user A cannot trigger score writes or appeal submissions targeting user B. Fails closed (401 when no principal, 403 on mismatch).

Authorization matrix applied per route
Route Before After
POST /users/:userId/score JWT only JwtAuthGuard + AdminGuard + @adminonly()
POST /users/:userId/recompute JWT only JwtAuthGuard + AdminGuard + @adminonly()
POST /recalculate-all JWT only JwtAuthGuard + AdminGuard + @adminonly()
POST /users/:userId/appeal JWT only JwtAuthGuard + SybilOwnershipGuard
GET /users/:userId/appeals PUBLIC JwtAuthGuard + SybilOwnershipGuard
GET /users/:userId/score PUBLIC JwtAuthGuard
GET /users/:userId/public-score PUBLIC PUBLIC (unchanged, privacy-safe)
GET /history, /voting, /models PUBLIC PUBLIC (unchanged, advisory reads)
POST /users/:userId/verify-worldcoin — permanently removed
This endpoint accepted a caller-supplied { verified: boolean } body that mutated Worldcoin verification state directly, completely bypassing proof verification. It now returns ForbiddenException (410 Gone semantics). The only authoritative Worldcoin verification path is POST /identity/worldcoin/verify, which requires a ZK proof and is guarded by JwtAuthGuard + WorldcoinVerificationGuard.

Service: one-active-appeal rule
submitAppeal now rejects a new submission when the user already has a SUBMITTED (pending) appeal. Enforced at the service/persistence boundary, not only at the controller, so it holds regardless of call site.

Service: bulk recalculation audit logging
recalculateAllScores accepts an optional actorId and emits structured log entries (sybil.recalculate_all started/completed [actor=…]) with total/succeeded/failed counts.

Module
SybilResistanceModule now imports AuthModule so JwtAuthGuard, AdminGuard, and RolesGuard are injectable by the controller's per-route guard instances.

Tests (40 total, all passing)
SybilOwnershipGuard unit: 401 on missing principal, 403 on ID mismatch, every cross-user IDOR pair.
AdminGuard unit: anonymous and non-admin roles rejected; admin role allowed.
Route metadata assertions: @adminonly() decorator confirmed present on recordScore, recomputeScore, recalculateAll via Reflect.getMetadata.
Controller input validation: empty userId, missing/empty body on every route.
setWorldcoinVerified removal: ForbiddenException for every caller × boolean value combination; service.setWorldcoinVerified confirmed never called.
One-active-appeal propagation from service rejection to controller response.
verify-worldcoin unreachability: 6-combination matrix (3 actors × 2 boolean values).
Related
Closes the IDOR and Worldcoin boolean-mutation issues described in the security report.
#804 — appeal persistence; this PR enforces the one-active-appeal rule at the service boundary.
#653 — wallet identity binding; ownership guard pattern follows the same IDOR model.
#611 — World ID proof privacy; this PR removes the boolean shortcut that circumvented proof verification.

The Sybil-resistance controller had no route-level authorization guards — any authenticated user could record or recompute scores for arbitrary user IDs, set Worldcoin verification state via a caller-supplied boolean, and invoke bulk recalculation.

Changes:
- New SybilOwnershipGuard: enforces :userId route param === JWT subject. Prevents IDOR on appeal submission and appeal-history reads.
- Authorization matrix applied per route:
  POST /users/:userId/score        -> JwtAuthGuard + AdminGuard + @adminonly()
  POST /users/:userId/recompute    -> JwtAuthGuard + AdminGuard + @adminonly()
  POST /recalculate-all            -> JwtAuthGuard + AdminGuard + @adminonly()
  POST /users/:userId/appeal       -> JwtAuthGuard + SybilOwnershipGuard (self-service)
  GET  /users/:userId/appeals      -> JwtAuthGuard + SybilOwnershipGuard (own account)
  GET  /users/:userId/score        -> JwtAuthGuard (authenticated read)
  GET  public-score/history/voting -> PUBLIC (advisory read-only signals)
- POST /users/:userId/verify-worldcoin removed: accepted a caller-supplied boolean to mutate Worldcoin verification state without proof verification. Endpoint returns ForbiddenException (410 Gone semantics). Service method retained for internal use; not callable from the API.
- Service: one-active-appeal rule — rejects new submissions while a SUBMITTED appeal is pending.
- Service: recalculateAllScores accepts actorId for audit logging of bulk operations.
- Module: imports AuthModule so JwtAuthGuard, AdminGuard, and RolesGuard are injectable.
- Tests: 40 tests covering guard unit tests (SybilOwnershipGuard, AdminGuard), route metadata assertions (AdminOnly decorator presence), controller method logic, verify-worldcoin removal, and service propagation of appeal eligibility.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PREWAVE-BE-149 — Enforce ownership and privileged access on Sybil mutations

1 participant