Repository navigation
fix(security): enforce org ownership on internal API routes - #246
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/:jobIdpath (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.jsviaassertBrandAccess); the gaps were inconsistent omissions, not intentional public endpoints.Change
Add a shared
server/src/lib/access.jswith ownership guards that throw404/403(mirroring the existingroutes/prompts.jspattern):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:
tracking.jsGET unanalyzed/:brandId,GET status/:brandId,GET job/:jobId,DELETE job/:jobIdcontent.jsGET job/:jobId,GET brand/:brandId,POST bulk/status,POST bulk/send-webhook,PATCH :id/status,POST :id/send-webhook,GET :idvolumes.jsPOST analyze,POST analyze-batch,POST refresh,GET brand/:brandIdRoute catch blocks now honor
error.status, so the guards surface as403/404instead of500.Compatibility notes
!==, not a null-reject. Installs with a nullorganization_idon both profile and resource keep working (null !== nullis false → allowed). In cloud every org id is non-null, so cross-tenant access is blocked.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_foundcontract: 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:
404403yarn/npmlint, prettier, andnode --checkall pass on the changed files.Type of change
fix— security (IDOR / missing authorization)