Permissive Cross-domain Policy with Untrusted Domains
The product uses a cross-domain policy file that includes domains that should not be trusted.
A cross-domain policy file specifies the permissions for a web client to handle data across multiple domains. When overly permissive settings are used, malicious sites can abuse these permissions to access sensitive data or perform unauthorized actions on behalf of the user.
Comment corriger cette vulnérabilité
Restrict CORS to specific trusted origins instead of wildcard '*'
from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() app.add_middleware( CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], + allow_origins=["https://example.com", "https://app.example.com"], + allow_credentials=True, + allow_methods=["GET", "POST"], )
Restrict Flask-CORS to specific trusted origins instead of wildcard '*'
from flask import Flask from flask_cors import CORS app = Flask(__name__) - CORS(app, resources={r"/api/*": {"origins": "*"}}) + CORS(app, resources={ + r"/api/*": { + "origins": ["https://example.com", "https://app.example.com"], + "supports_credentials": True + } + })
Use exact string matching against an allowlist instead of regex for origin validation
- import re - from flask import request - - @app.after_request - def cors(response): - origin = request.headers.get('Origin', '') - if re.match(r'.*example\.com', origin): + ALLOWED_ORIGINS = { + "https://app.example.com", + "https://api.example.com", + } + + @app.after_request + def cors(response): + origin = request.headers.get('Origin', '') + if origin in ALLOWED_ORIGINS: response.headers['Access-Control-Allow-Origin'] = origin return response
Configure specific allowed origins in Chi CORS middleware
package main import ( "github.com/go-chi/chi/v5" "github.com/go-chi/cors" ) func main() { r := chi.NewRouter() r.Use(cors.Handler(cors.Options{ - AllowedOrigins: []string{"*"}, + AllowedOrigins: []string{"https://example.com"}, + AllowCredentials: true, })) }
Configure specific allowed origins in Echo CORS middleware
package main import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) func main() { e := echo.New() e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ - AllowOrigins: []string{"*"}, + AllowOrigins: []string{ + "https://example.com", + "https://app.example.com", + }, + AllowCredentials: true, })) e.Start(":8080") }
Configure specific allowed origins in Fiber CORS middleware
package main import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" ) func main() { app := fiber.New() app.Use(cors.New(cors.Config{ - AllowOrigins: "*", + AllowOrigins: "https://example.com,https://app.example.com", + AllowCredentials: true, })) app.Listen(":3000") }
Trouvez les vulnérabilités dans votre code
Utilisez Shoulder pour scanner votre code à la recherche de patterns Permissive Cross-domain Policy with Untrusted Domains. 9 règles.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=942 # Or scan entire project npx @shoulderdev/cli trust .
Règles de Détection (9)
Ce qu'il faut surveiller lors des revues de code
Ces patterns indiquent des vulnérabilités potentielles Permissive Cross-domain Policy with Untrusted Domains. Recherchez-les lors des revues de code et des audits de sécurité.
Scannez votre base de code pour Permissive Cross-domain Policy with Untrusted Domains
Shoulder CLI trouve les motifs vulnérables dans toute votre base de code.