Skip to content

fix(security): enforce org ownership on internal API routes - #246

Merged
gkhngyk merged 1 commit into
mainfrom
fix/server-route-ownership-idor
Jun 12, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/server-route-ownership-idor

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Summary

All DB access in the server goes through supabaseAdmin, which bypasses Row Level Security. That means resource ownership has to be enforced in each route handler — and several authenticated routes were querying by a :brandId / :id / :jobId path (or body) param without checking the resource belongs to the caller's organization. Any user with a valid token could read or mutate another tenant's data (an IDOR class of bug).

Some routes already did this correctly (POST /tracking/check, /analyze-new, GET /tracking/results, content /generate, /:id/brief, prompts.js via assertBrandAccess); the gaps were inconsistent omissions, not intentional public endpoints.

Change

Add a shared server/src/lib/access.js with ownership guards that throw 404/403 (mirroring the existing routes/prompts.js pattern):

  • assertBrandAccess(brandId, userId)
  • assertOpportunityAccess(id, userId) / assertOpportunitiesAccess(ids, userId)
  • assertPromptAccess(promptIds, userId) (prompts → prompt_sets → brands)

Apply them to every route that was missing a check:

File Routes hardened
tracking.js GET unanalyzed/:brandId, GET status/:brandId, GET job/:jobId, DELETE job/:jobId
content.js GET job/:jobId, GET brand/:brandId, POST bulk/status, POST bulk/send-webhook, PATCH :id/status, POST :id/send-webhook, GET :id
volumes.js POST analyze, POST analyze-batch, POST refresh, GET brand/:brandId

Route catch blocks now honor error.status, so the guards surface as 403/404 instead of 500.

Compatibility notes

  • Self-hosted / single-tenant: org comparison is a plain !==, not a null-reject. Installs with a null organization_id on both profile and resource keep working (null !== null is false → allowed). In cloud every org id is non-null, so cross-tenant access is blocked.
  • MCP / per-user flows: checks are scoped to req.user, so the web MCP layer (which calls these endpoints with the user's own JWT) keeps working — only cross-tenant calls are rejected.
  • not_found contract: job routes still return { status: 'not_found' } for a missing job; the ownership check only runs once the job exists.

Verification

Tested the guard against the local DB with real rows:

  • own brand → allowed
  • non-existent brand → 404
  • brand in another org → 403

yarn/npm lint, prettier, and node --check all pass on the changed files.

Type of change

  • fix — security (IDOR / missing authorization)

All DB access in the server goes through supabaseAdmin, which bypasses Row
Level Security, so resource ownership must be checked in each route handler.
Several authenticated routes queried by a :brandId / :id / :jobId path param
without verifying the resource belonged to the caller's organization, letting
any valid token read or mutate another tenant's data.

Add a shared lib/access.js with assertBrandAccess / assertOpportunityAccess /
assertOpportunitiesAccess / assertPromptAccess (throwing 404/403, mirroring the
existing prompts.js pattern), and apply them to the routes that were missing a
check:

- tracking: GET unanalyzed/:brandId, GET status/:brandId, GET+DELETE job/:jobId
- content:  GET job/:jobId, GET brand/:brandId, POST bulk/status,
            POST bulk/send-webhook, PATCH :id/status, POST :id/send-webhook,
            GET :id
- volumes:  POST analyze, POST analyze-batch, POST refresh, GET brand/:brandId

Org comparison uses a plain !== (not a null-reject) so self-hosted single-tenant
installs with a null organization_id keep working; in cloud every org id is
non-null so cross-tenant access is blocked. Route catch blocks now honor
error.status so the guards surface as 403/404 instead of 500.
@gkhngyk
gkhngyk merged commit 783ed1a into main Jun 12, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/server-route-ownership-idor branch June 12, 2026 13:04
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.

1 participant