Skip to content

Commit 0a27da0

Browse files
Minor changes from review suggestions to shared logic between this and missing access control
Use case insensitive regex, factor out page load to improve possible bad joins make needsAuth not a member predicate
1 parent a022893 commit 0a27da0

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

csharp/ql/lib/semmle/code/csharp/security/auth/ActionMethods.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ abstract class ActionMethod extends Method {
2020
str =
2121
this.getADescription()
2222
// separate camelCase words
23-
.regexpReplaceAll("([a-z])([A-Z])", "$1_$2")
24-
.toLowerCase() and
25-
str.regexpMatch(".*(edit|delete|modify|change).*") and
26-
not str.regexpMatch(".*(on_?change|changed).*")
23+
.regexpReplaceAll("([a-z])([A-Z])", "$1_$2") and
24+
str.regexpMatch("(?i).*(edit|delete|modify|change).*") and
25+
not str.regexpMatch("(?i).*(on_?change|changed).*")
2726
)
2827
}
2928

@@ -32,13 +31,9 @@ abstract class ActionMethod extends Method {
3231
this.getADescription()
3332
// separate camelCase words
3433
.regexpReplaceAll("([a-z])([A-Z])", "$1_$2")
35-
.toLowerCase()
36-
.regexpMatch(".*(admin|superuser).*")
34+
.regexpMatch("(?i).*(admin|superuser).*")
3735
}
3836

39-
/** Holds if this method may need an authorization check. */
40-
predicate needsAuth() { this.isEdit() or this.isAdmin() }
41-
4237
/** Gets a callable for which if it contains an auth check, this method should be considered authenticated. */
4338
Callable getAnAuthorizingCallable() { result = this }
4439

@@ -64,8 +59,7 @@ private class WebFormActionMethod extends ActionMethod {
6459
override Callable getAnAuthorizingCallable() {
6560
result = super.getAnAuthorizingCallable()
6661
or
67-
result.getDeclaringType() = this.getDeclaringType() and
68-
result.getName() = "Page_Load"
62+
pageLoad(result, this.getDeclaringType())
6963
}
7064

7165
override string getARoute() {
@@ -80,6 +74,12 @@ private class WebFormActionMethod extends ActionMethod {
8074
}
8175
}
8276

77+
pragma[nomagic]
78+
private predicate pageLoad(Callable c, Type decl) {
79+
c.getName() = "Page_Load" and
80+
decl = c.getDeclaringType()
81+
}
82+
8383
/**
8484
* Holds if `virtualRoute` is a URL path
8585
* that can map to the corresponding `physicalRoute` filepath

csharp/ql/lib/semmle/code/csharp/security/auth/MissingFunctionLevelAccessControlQuery.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import semmle.code.csharp.frameworks.system.web.UI
66
import semmle.code.asp.WebConfig
77
import ActionMethods
88

9+
/** Holds if the method `m` may need an authorization check. */
10+
predicate needsAuth(ActionMethod m) { m.isEdit() or m.isAdmin() }
11+
912
/** An expression that indicates that some authorization/authentication check is being performed. */
1013
class AuthExpr extends Expr {
1114
AuthExpr() {
@@ -25,7 +28,7 @@ class AuthExpr extends Expr {
2528

2629
/** Holds if `m` is a method that should have an auth check, and does indeed have one. */
2730
predicate hasAuthViaCode(ActionMethod m) {
28-
m.needsAuth() and
31+
needsAuth(m) and
2932
exists(Callable caller, AuthExpr auth |
3033
m.getAnAuthorizingCallable().calls*(caller) and
3134
auth.getEnclosingCallable() = caller
@@ -86,7 +89,7 @@ predicate hasAuthViaAttribute(ActionMethod m) {
8689

8790
/** Holds if `m` is a method that should have an auth check, but is missing it. */
8891
predicate missingAuth(ActionMethod m) {
89-
m.needsAuth() and
92+
needsAuth(m) and
9093
not hasAuthViaCode(m) and
9194
not hasAuthViaXml(m) and
9295
not hasAuthViaAttribute(m) and

0 commit comments

Comments
 (0)