Skip to content

fix: prevent user ID spoofing by stripping client-supplied 'g' header in withMobileAuth - #1927

Open
MehranKhn wants to merge 2 commits into
code100x:mainfrom
MehranKhn:fix/mobile-auth-validation
Open

MehranKhn wants to merge 2 commits into
code100x:mainfrom
MehranKhn:fix/mobile-auth-validation

Conversation

@MehranKhn

Copy link
Copy Markdown

Problem

The withMobileAuth middleware in /src/middleware.ts had two vulnerabilities:

Vulnerability 1 — Auth-Key not validated:
The original code only checked if Auth-Key header existed, not its value:

if (req.headers.get('Auth-Key')) {  // ← any value passed!
  return NextResponse.next();
}

Vulnerability 2 — Client-supplied g header not stripped:
When Auth-Key matched, the middleware called NextResponse.next()
without stripping the incoming headers. This meant an attacker could:

  1. Send a valid Auth-Key
  2. Attach their own g: {"id": "admin-user-id"} header
  3. The endpoint blindly trusted it:
// route.ts — trusted client-supplied g as if server set it
const user: { id: string } = JSON.parse(request.headers.get('g') || '');

Attack looked like this:

curl -H "Auth-Key: validkey" \
     -H 'g: {"id": "some-admin-id"}' \
     https://site.com/api/mobile/courses/123

Fix

const authKey = req.headers.get('Auth-Key');

if (authKey && authKey === process.env.APPX_AUTH_KEY) {
  const newHeaders =