Fix cross-origin policy accepts any origin in Astro

The CORS configuration accepts requests from any origin. Combined with credentials this lets any site a logged-in user visits make authenticated calls to the API and read the responses. Without credentials it may be intentional for a public API - the finding says which case it found.

medium likely Astro CWE-942 / OWASP A05:2021

The vulnerable pattern in Astro

MEDIUM likely Cross-origin policy accepts any origin A05:2021 src/pages/api/users.ts:34:3 32 │ headers: { 'Content-Type': 'application/json' }, 33 │ }) 34 │ response.headers.set('Access-Control-Allow-Origin', '*') │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ accepts requests from any origin 35 │ response.headers.set('Access-Control-Allow-Credentials', 'true') 36 │ return response

This finding comes from the Astro fixture in the owlwarden test suite, in /api/users. Any origin may call this API and read the response. That is a deliberate choice for a public endpoint and a mistake for anything behind a session - nothing in the source says which this is, so it is reported for you to decide.

The corrected handler

Set the header explicitly in the endpoint rather than reflecting the caller's origin.

// src/pages/api/data.ts
const ALLOWED_ORIGIN = 'https://app.example.com'

export async function GET({ request }: APIContext) {
  const origin = request.headers.get('origin')
  const headers = new Headers()
  if (origin === ALLOWED_ORIGIN) {
    headers.set('Access-Control-Allow-Origin', ALLOWED_ORIGIN)
    headers.set('Vary', 'Origin')
  }
  return new Response(JSON.stringify({ ok: true }), { headers })
}

If you are not using Astro

Replace the wildcard with the origins that actually need access, and only send credentials to those.

Check your own repository

npx owlwarden scan
npx owlwarden explain cors-permissive

Runs on your machine. No account, no telemetry, no network unless you ask. In CI, SARIF uploads to code scanning and the exit code is the gate.

Other Astro checks

Rules with a tested Astro example.

cors-permissive for every framework / All rules / owlwarden