T09 · Insecure Skill Coding Practices
Error
- Location
- SKILL.md:311
- Finding
- Credentialed CORS Requests Allowed from Arbitrary Origins<![CDATA[ ## Vulnerability Details **File Location**: `SKILL.md:311-323` **Vulnerability Type**: Insecure CORS configuration **Risk Level**: High ### Vulnerable Code ```csharp builder.Services.AddCors(options => { options.AddPolicy("CorsPolicy", policy => { var withOrigins = builder.Configuration["WithOrigins"]; if (!string.IsNullOrWhiteSpace(withOrigins) && withOrigins.Split(';').Length > 0) policy.WithOrigins(withOrigins.Split(';')); else policy.SetIsOriginAllowed((host) => true); policy.AllowAnyMethod(); policy.AllowAnyHeader(); policy.AllowCredentials(); }); }); ``` ### Technical Analysis The policy falls back to trusting every requesting origin when `WithOrigins` is absent or empty. It simultaneously enables credentials, arbitrary HTTP methods, and arbitrary request headers. This configuration permits an attacker-controlled website to make cross-origin requests through a victim's browser and read the responses when the browser attaches credentials accepted by the generated service. The effective exploitability depends on the application's authentication mechanism and cookie attributes, but this configuration removes the browser's normal origin-based response isolation. The fallback also fails open: an omitted or misspelled production setting silently produces the least restrictive policy. ### Attack Path 1. The service is deployed without a valid `WithOrigins` value. 2. A victim authenticates to the service using browser-managed credentials that can be attached to cross-origin requests. 3. The victim visits an attacker-controlled website. 4. Malicious JavaScript sends a credentialed request to the service. 5. `SetIsOriginAllowed((host) => true)` approves the attacker's origin. 6. `AllowCredentials()`, `AllowAnyMethod()`, and `AllowAnyHeader()` permit the credentialed operation. 7. The attacker reads sensitive responses or performs authenticated state-changing acti ...[truncated 357 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Fail closed when `WithOrigins` is absent, malformed, or empty; do not start the application with an unrestricted fallback. - Require an explicit allowlist of trusted HTTPS origins for every deployed environment. - Remove `AllowCredentials()` unless browser-managed cross-origin credentials are a documented requirement. - Restrict allowed methods and headers to those required by the API. - Validate and normalize configured origins during startup. - Add automated tests that reject unknown origins and verify that missing configuration causes startup failure. A safer pattern is: ```csharp var configuredOrigins = builder.Configuration .GetSection("Cors:AllowedOrigins") .Get<string[]>(); if (configuredOrigins is null || configuredOrigins.Length == 0) { throw new InvalidOperationException( "At least one trusted CORS origin must be configured."); } builder.Services.AddCors(options => { options.AddPolicy("CorsPolicy", policy => { policy.WithOrigins(configuredOrigins) .WithMethods("GET", "POST", "PUT", "DELETE") .WithHeaders("Authorization", "Content-Type"); // Add AllowCredentials() only if it is explicitly required. }); }); ``` ]]>
