logo

Database

Javascript Cors Wildcard Origin

Description

Detects insecure CORS (Cross-Origin Resource Sharing) configurations in JavaScript code where wildcard (*) or overly permissive origins are specified. This creates a security risk by allowing any external domain to make cross-origin requests to your application, potentially exposing sensitive data to malicious sites.

Weakness:

134 - Insecure or unset HTTP headers - CORS

Category: Protocol Manipulation

Detection Strategy

    Check for CORS configuration settings in JavaScript code

    Look for wildcard (*) or overly permissive values being set as allowed origins

    Identify cases where CORS origins are not properly restricted to specific trusted domains

    Flag configurations that allow arbitrary cross-origin access from any domain

Vulnerable code example

const express = require('express');
const cors = require('cors');
const app = express();

// Vulnerable: Allows requests from any origin (*)
app.use(cors());

// Vulnerable: Explicitly allowing all origins...

✅ Secure code example

const express = require('express');
const cors = require('cors');
const app = express();

// Define allowed origins explicitly for security
const corsOptions = {
    origin: ['https://trusted-domain.com', 'https://api.trusted-domain.com'], // Only allow specific domains
    methods: ['GET', 'POST', 'PUT', 'DELETE'], // Restrict HTTP methods...