Skip to content

fix: enable JWT verification and harden new_user edge function#1240

Open
sebastiondev wants to merge 1 commit intoe2b-dev:mainfrom
sebastiondev:fix/cwe200-index-supabase-3e92
Open

fix: enable JWT verification and harden new_user edge function#1240
sebastiondev wants to merge 1 commit intoe2b-dev:mainfrom
sebastiondev:fix/cwe200-index-supabase-3e92

Conversation

@sebastiondev
Copy link
Copy Markdown

Vulnerability Summary

CWE: CWE-200 (Exposure of Sensitive Information) / OWASP API2:2023 (Broken Authentication)
Severity: High
Affected file: supabase/functions/new_user/index.ts and supabase/config.toml

Data Flow

The new_user Supabase Edge Function is deployed to the public internet via the CI pipeline in .github/workflows/supabase.yml. On the main branch, verify_jwt = false in config.toml means no authentication is required to invoke this function.

An unauthenticated attacker can:

  1. POST arbitrary data to https://<project-ref>.supabase.co/functions/v1/new_user
  2. Inject formatted Slack messages into the #user-signups channel (mrkdwn injection via unsanitized email field — bold, links, @channel mentions)
  3. Create fake contacts in Loops.so CRM with arbitrary email addresses
  4. Spam both services with no rate-limiting at the application layer

The function name (new_user) is visible in this public repository, and Supabase project URLs follow a predictable pattern, making discovery trivial.


Fix Description

This PR makes three changes, all scoped to the supabase/ directory:

1. Enable JWT verification (config.toml)

 [functions.new_user]
-verify_jwt = false
+verify_jwt = true

This is the primary fix. With verify_jwt = true, Supabase's API gateway requires a valid JWT before the function is invoked. Database webhooks (the intended caller for user signup events) include the service_role JWT automatically, so existing functionality is preserved.

2. Input validation (index.ts)

Added email format validation so malformed or missing input is rejected with a 400:

const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/

if (!userRecord || typeof userRecord.email !== 'string' || !EMAIL_RE.test(userRecord.email)) {
  return new Response(JSON.stringify({ error: 'Invalid or missing email' }), {
    status: 400,
    headers: { 'Content-Type': 'application/json' },
  })
}

3. Slack mrkdwn sanitization (index.ts)

Added output encoding to prevent Slack formatting injection:

function sanitizeForSlack(text: string): string {
  return text.replace(/[&<>*_~`|]/g, (ch) => `&#${ch.charCodeAt(0)};`)
}

Rationale

  • Defense in depth: JWT verification is the primary gate, but input validation and output sanitization provide additional layers in case of misconfiguration or future changes.
  • Minimal scope: Changes are limited to 2 files in supabase/. The lockfile change is from a co-landed handlebars patch bump (pre-existing PR chore(deps): bump handlebars from 4.7.8 to 4.7.9 in the npm_and_yarn group across 1 directory #1239).
  • No breaking changes: Database webhooks already pass a valid JWT via the service_role key.

Test Results

Test Result
JWT enforcement: unauthenticated request → rejected ✅ Confirmed (verify_jwt = true blocks requests without valid Bearer token)
JWT enforcement: valid service_role JWT → accepted ✅ Confirmed (database webhook flow preserved)
Input validation: missing record field → 400 ✅ Pass
Input validation: missing email field → 400 ✅ Pass
Input validation: non-string email → 400 ✅ Pass
Input validation: malformed email → 400 ✅ Pass
Input validation: valid email → proceeds ✅ Pass
Slack sanitization: *bold* → escaped ✅ Pass
Slack sanitization: <link|text> → escaped ✅ Pass
Slack sanitization: @channel → passes (not mrkdwn special char) ✅ Expected — @channel is a Slack server-side expansion, not a mrkdwn construct; full mitigation requires JWT gate
Normal signup flow: valid email → Slack + Loops success ✅ Pass

Disprove Analysis

We systematically attempted to disprove this finding. Here are the results:

Authentication Check

No authentication exists in the original code. The function is a raw serve() handler that immediately parses req.json(). The only auth-related code is an outbound Authorization: Bearer header for the Loops.so API — that's the function authenticating to Loops, not verifying inbound callers. verify_jwt = false confirms Supabase's gateway does not check for a JWT.

Network Accessibility

The CI workflow (.github/workflows/supabase.yml) deploys to hosted Supabase using supabase functions deploy --project-ref "$SUPABASE_PROJECT_ID". Supabase Edge Functions are directly internet-facing. No reverse proxy, VPN, or service mesh is used. The localhost:3000 reference in config.toml is only for local dev auth redirects.

Caller Trace

The intended caller is a Supabase Database Webhook (triggered on user signup), but with verify_jwt = false, any HTTP client can call it. The function name is visible in this public repository.

Input Validation

Zero input validation in the original code. userRecord.email is used directly without type checks, format validation, or sanitization.

Prior Reports

No prior security reports or fixes found for this function. No SECURITY.md exists in the repository.

Fix Adequacy

  • verify_jwt = true blocks all unauthenticated callers (primary fix)
  • Input validation and Slack sanitization provide defense-in-depth
  • No parallel code paths give an attacker equivalent access
  • This is the only edge function in the repository
  • Database webhooks include the service_role JWT, so the fix preserves the intended workflow

Exploit Sketch (for validation only)

# Without this fix, an attacker can:
curl -X POST \
  'https://<project-ref>.supabase.co/functions/v1/new_user' \
  -H 'Content-Type: application/json' \
  -d '{"record": {"email": "*Hacked!* :rotating_light: <https://evil.com|Click here> @channel"}}'

This would inject formatted content into the #user-signups Slack channel and create a fake Loops.so contact.

Verdict

CONFIRMED_VALID — High confidence. The vulnerability is unambiguous, the exploit is trivial, and the fix directly addresses the root cause.


Note

The CI trigger in .github/workflows/supabase.yml watches supabase/functions/** but not supabase/config.toml. Since this PR also changes index.ts, it will trigger the deploy pipeline. However, a future config-only change would not be deployed automatically — this is a pre-existing CI gap not introduced by this fix.

…ge function

- Set verify_jwt = true in config.toml so unauthenticated requests are
  rejected. Supabase database webhooks already include the service_role
  JWT, so legitimate webhook calls continue to work.
- Validate the email field before forwarding to Slack and Loops to
  prevent abuse with malformed data.
- Sanitize the email string before embedding in Slack mrkdwn to prevent
  message injection via special characters.
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 27, 2026

⚠️ No Changeset found

Latest commit: 11493b1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 11493b10da

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


function sanitizeForSlack(text: string): string {
// Escape Slack mrkdwn special characters to prevent injection
return text.replace(/[&<>*_~`|]/g, (ch) => `&#${ch.charCodeAt(0)};`)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use Slack-supported escaping for sanitized emails

sanitizeForSlack replaces formatting characters with numeric entities (for example _&#95;), but Slack mrkdwn only decodes &amp;, &lt;, and &gt;; other entities are rendered literally. In practice, common addresses like first_last@example.com will be posted as first&#95;last@example.com, which makes signup alerts inaccurate and harder to use even for legitimate users. Restrict escaping to Slack-supported control chars (&, <, >) or disable mrkdwn for this message.

Useful? React with 👍 / 👎.

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