C#-CWE-306ΒΆ
Missing authentication for critical functions (CWE-306)
Required inputs: CSharpAST
[Authorize]
[HttpPost]
public IActionResult DeleteUser(int id)
{
_userService.DeleteUser(id);
return Ok();
}
Intentional public action (explicit opt-out):
[AllowAnonymous]
[HttpGet]
public IActionResult PublicInfo()
{
return Ok("This endpoint is intentionally public.");
}
Non-controller critical method that should be protected (example pattern):
public class BankService
{
// This public transfer method is critical β analyzer will flag it if it lacks [Authorize]
public void TransferFunds(string from, string to, decimal amount)
{
// perform withdraw/deposit
}
}
**Recommendations / Remediation**
* Add `[Authorize]` on controller actions or on controller classes to require authentication.
* If an action is intentionally public, mark it `[AllowAnonymous]` so the rule does not report it.
* For non-controller public APIs that are critical (e.g., services that perform transfers, payments, file deletions), consider:
* Making them internal where possible, or
* Ensuring callers perform appropriate authentication/authorization checks, or
* Adding a custom `[Authorize]`-like attribute if those methods are entry points in hosted services.
* Tune the heuristic lists (verb prefixes, sensitive API types) in the analyser to match your codebase and reduce false positives.
**Notes**
* This rule is conservative and explainable β it uses deterministic heuristics rather than ML. Teams may wish to:
* Add a custom `[Critical]` attribute to explicitly mark methods as critical (and modify the analyser to check for it).
* Provide configuration to adjust the verb list or sensitive API patterns.Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
missing_authentication |
Public critical method is missing authentication/authorization attributes. Protect critical functions with [Authorize] or explicitly mark them [AllowAnonymous] if they are intentionally public. |
None |
False |
Options
This rule shares the following common options: excludes, includes, justification_checker, post_processing, provider, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.