@@ -15,6 +15,7 @@ import csharp
1515import semmle.code.csharp.frameworks.system.Web
1616import semmle.code.csharp.frameworks.system.web.Helpers
1717import semmle.code.csharp.frameworks.system.web.Mvc
18+ import semmle.code.csharp.frameworks.microsoft.AspNetCore as AspNetCore
1819
1920private Method getAValidatingMethod ( ) {
2021 result = any ( AntiForgeryClass a ) .getValidateMethod ( )
@@ -35,6 +36,8 @@ private Method getAStartedMethod() {
3536
3637/**
3738 * Holds if the project has a global anti forgery filter.
39+ *
40+ * No AspNetCore case here as the corresponding class doesn't seem to exist.
3841 */
3942predicate hasGlobalAntiForgeryFilter ( ) {
4043 // A global filter added
@@ -48,16 +51,30 @@ predicate hasGlobalAntiForgeryFilter() {
4851 )
4952}
5053
51- from Controller c , Method postMethod
54+ predicate isUnvalidatedPostMethod ( Class c , Method m ) {
55+ c .( Controller ) .getAPostActionMethod ( ) = m and
56+ not m .getAnAttribute ( ) instanceof ValidateAntiForgeryTokenAttribute and
57+ not c .getAnAttribute ( ) instanceof ValidateAntiForgeryTokenAttribute
58+ or
59+ c .( AspNetCore:: MicrosoftAspNetCoreMvcController ) .getAnActionMethod ( ) = m and
60+ m .getAnAttribute ( ) instanceof AspNetCore:: MicrosoftAspNetCoreMvcHttpPostAttribute and
61+ not m .getAnAttribute ( ) instanceof AspNetCore:: ValidateAntiForgeryAttribute and
62+ not c .getAnAttribute ( ) instanceof AspNetCore:: ValidateAntiForgeryAttribute
63+ }
64+
65+ Element getAValidatedElement ( ) {
66+ any ( ValidateAntiForgeryTokenAttribute a ) .getTarget ( ) = result
67+ or
68+ any ( AspNetCore:: ValidateAntiForgeryAttribute a ) .getTarget ( ) = result
69+ }
70+
71+ from Class c , Method postMethod
5272where
53- postMethod = c .getAPostActionMethod ( ) and
54- // The method is not protected by a validate anti forgery token attribute
55- not postMethod .getAnAttribute ( ) instanceof ValidateAntiForgeryTokenAttribute and
56- not c .getAnAttribute ( ) instanceof ValidateAntiForgeryTokenAttribute and
73+ isUnvalidatedPostMethod ( c , postMethod ) and
5774 // Verify that validate anti forgery token attributes are used somewhere within this project, to
5875 // avoid reporting false positives on projects that use an alternative approach to mitigate CSRF
5976 // issues.
60- exists ( ValidateAntiForgeryTokenAttribute a , Element e | e = a . getTarget ( ) ) and
77+ exists ( getAValidatedElement ( ) ) and
6178 // Also ignore cases where a global anti forgery filter is in use.
6279 not hasGlobalAntiForgeryFilter ( )
6380select postMethod ,
0 commit comments