False positive: "Missing cross-site request forgery token validation" should not apply to Web API Controller Actions sharing a project with Browser-based actions

Aperta
#22,215 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
csharp

Direzione di ricerca

Iniziare leggendo csharp/ql/src/Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql, in particolare il controllo CSRF a livello di progetto descritto intorno alla riga 74. Tracciare il modo in cui vengono modellate le azioni dei controller e i relativi schemi di autenticazione, quindi esaminare la copertura esistente della query. Il lavoro deve considerarsi completato quando le azioni autenticate tramite browser continuano a essere controllate, mentre le azioni API autenticate tramite bearer nello stesso progetto non vengono segnalate solo perché un’altra azione usa la validazione CSRF.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

false-positive

Description of the false positive

  • ASP.NET (and ASP.NET Core) projects can have two sets of endpoint handlers (controller actions) which separately handle...
  1. Requests originating from web-browsers, which are authenticated using browser cookies or browser-managed HTTP Basic/Digest authentication; including XHR/fetch-based requests, as well as ordinary document navigation. These are the kinds of requests that are vulnerable to CSRF attacks and so should use a CSRF validation token or other approach.
  2. Requests originating from non-browser-based clients (e.g. daemon processes; cron jobs running curl, etc); these are authenticated using HTTP Authorization header (e.g. Bearer tokens). It is not possible for a CSRF attack to succeed in this case (see https://security.stackexchange.com/questions/170388/do-i-need-csrf-token-if-im-using-bearer-jwt ).

Assuming that this code is the actual CodeQL analysis rule for this alert (CWE-352/MissingAntiForgeryTokenValidation.ql), then the problem is...

  1. The rule is only activated if the project uses CSRF at least once, anywhere (see the comment where it says "Verify that validate anti forgery token attributes are used somewhere within this project").
  2. So it assumes that if at least one controller-action in a project uses CSRF, then all controller-actions in the same project should also use CSRF...
    • This assumption is incorrect: as mentioned above, it's possible for a project to serve both browser-based requests and non-browser requests - with entirely different authentication schemes and policies such that non-browser-based endpoint-actions cannot be invoked in a browser-based CSRF scenario.

Code samples or links to source code

If the two controller-classes are built in a single project, then the fact BrowserAjaxController uses [ValidateAntiForgeryToken] will cause MissingAntiForgeryTokenValidation.q to think that WebServiceController should also use [ValidateAntiForgeryToken] even though it doesn't use browser-cookies based authentication (due to the different Scheme value).

class BrowserAjaxController : Controller
{
    [HttpPost("/ajax/exec-rm-rf-root" )]
    [Authorize( AuthenticationSchemes = MySchemeNames.BrowserCookiesScheme,  Policy = "SomePolicy1" )]
    [ValidateAntiForgeryToken]
    public IActionResult DoTheThing()
    {
        return this.Ok();
    }
}

class WebServiceController : Controller
{
    [HttpPost("/api/arbitrary-operation" )]
    [Authorize( AuthenticationSchemes = MySchemeNames.BearerTokenScheme,  Policy = "SomePolicy2" )]
    public IActionResult DoTheOtherThing()
    {
        return this.Ok();
    }
}
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 11h
PR unite (30g)
129

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di github/codeql

Tutte le issue di github/codeql

Issue simili

Altre issue su Security

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.