# Permissive Cross-domain Policy with Untrusted Domains (CWE-942) The product uses a cross-domain policy file that includes domains that should not be trusted. **Stack:** Go - Prevalence: 高 频繁被利用 - Impact: 高 1 条严重级别为高的规则 - Prevention: 已记录 9 个修复示例 **OWASP:** Security Misconfiguration (A05:2021-Security Misconfiguration) - #5 ## Description 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. ## Prevention ### Go Configure specific allowed origins in Chi CORS middleware Configure specific allowed origins in Echo CORS middleware Configure specific allowed origins in Fiber CORS middleware ## Warning Signs - [MEDIUM] Gin CORS middleware configured with wildcard origin - [MEDIUM] CORS policy allows untrusted origins ## Consequences - 读取应用程序数据 - 绕过保护机制 - 修改应用程序数据 ## Mitigations - 仔细评估访问策略,并在跨域策略文件中限制域名 - 不要使用通配符 (*) 允许所有域名 - 审查并将 CORS 响应头限制为仅信任的来源 ## Detection - Total rules: 9 - Languages: python, go ## Rules by Language ### Go (5 rules) - **Chi Permissive CORS** [MEDIUM]: Wildcard CORS allows any origin to access resources. - Remediation: Specify allowed origins instead of wildcard. ```go r.Use(cors.Handler(cors.Options{ AllowedOrigins: []string{ "https://example.com", "https://app.example.com", }, })) ``` Learn more: https://shoulder.dev/learn/go/cwe-942/cors - **Echo Permissive CORS** [MEDIUM]: Wildcard CORS allows any origin to access resources. - Remediation: Specify allowed origins instead of wildcard. ```go e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{ "https://example.com", "https://app.example.com", }, })) ``` Learn more: https://shoulder.dev/learn/go/cwe-942/cors - **Fiber Permissive CORS** [MEDIUM]: Wildcard CORS allows any origin to access resources. - Remediation: Specify allowed origins instead of wildcard. ```go app.Use(cors.New(cors.Config{ AllowOrigins: "https://example.com,https://app.example.com", })) ``` Learn more: https://shoulder.dev/learn/go/cwe-942/cors - **Gin Permissive CORS** [MEDIUM]: Wildcard CORS allows any origin to access resources. - Remediation: Specify allowed origins instead of wildcard. ```go config := cors.DefaultConfig() config.AllowOrigins = []string{ "https://example.com", "https://app.example.com", } r.Use(cors.New(config)) ``` Learn more: https://shoulder.dev/learn/go/cwe-942/cors - **Permissive CORS Configuration** [MEDIUM]: CORS allows wildcard origin or reflects Origin header without validation. - Remediation: Whitelist specific allowed origins instead of using wildcards. ```go allowedOrigins := map[string]bool{ "https://app.example.com": true, } origin := r.Header.Get("Origin") if allowedOrigins[origin] { w.Header().Set("Access-Control-Allow-Origin", origin) } ``` Learn more: https://shoulder.dev/learn/go/cwe-942/permissive-cors