Skip to content

Commit 39019b3

Browse files
committed
C#: Simplify logic in JsonWebTokenHandlerLib.qll
1 parent 358c741 commit 39019b3

File tree

2 files changed

+4
-47
lines changed

2 files changed

+4
-47
lines changed

csharp/ql/src/experimental/Security Features/JsonWebTokenHandler/JsonWebTokenHandlerLib.qll

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -125,39 +125,24 @@ class TokenValidationParametersProperty extends Property {
125125
predicate callableHasAReturnStmtAndAlwaysReturnsTrue(Callable c) {
126126
c.getReturnType() instanceof BoolType and
127127
not callableMayThrowException(c) and
128-
forall(ReturnStmt rs | rs.getEnclosingCallable() = c |
128+
forex(ReturnStmt rs | rs.getEnclosingCallable() = c |
129129
rs.getNumberOfChildren() = 1 and
130130
isExpressionAlwaysTrue(rs.getChildExpr(0))
131-
) and
132-
exists(ReturnStmt rs | rs.getEnclosingCallable() = c)
131+
)
133132
}
134133

135134
/**
136135
* Holds if the lambda expression `le` always returns true
137136
*/
138137
predicate lambdaExprReturnsOnlyLiteralTrue(AnonymousFunctionExpr le) {
139-
le.getExpressionBody().(BoolLiteral).getBoolValue() = true
140-
or
141-
// special scenarios where the expression is not a `BoolLiteral`, but it will evaluatue to `true`
142-
exists(Expr e | le.getExpressionBody() = e |
143-
not e instanceof Call and
144-
not e instanceof Literal and
145-
e.getType() instanceof BoolType and
146-
e.getValue() = "true"
147-
)
138+
isExpressionAlwaysTrue(le.getExpressionBody())
148139
}
149140

150141
class CallableAlwaysReturnsTrue extends Callable {
151142
CallableAlwaysReturnsTrue() {
152143
callableHasAReturnStmtAndAlwaysReturnsTrue(this)
153144
or
154145
lambdaExprReturnsOnlyLiteralTrue(this)
155-
or
156-
exists(AnonymousFunctionExpr le, Call call, Callable callable | this = le |
157-
callable.getACall() = call and
158-
call = le.getExpressionBody() and
159-
callableHasAReturnStmtAndAlwaysReturnsTrue(callable)
160-
)
161146
}
162147
}
163148

@@ -171,32 +156,6 @@ predicate callableOnlyThrowsArgumentNullException(Callable c) {
171156
)
172157
}
173158

174-
/**
175-
* A specialization of `CallableAlwaysReturnsTrue` that takes into consideration exceptions being thrown for higher precision.
176-
*/
177-
class CallableAlwaysReturnsTrueHigherPrecision extends CallableAlwaysReturnsTrue {
178-
CallableAlwaysReturnsTrueHigherPrecision() {
179-
callableOnlyThrowsArgumentNullException(this) and
180-
(
181-
forall(Call call, Callable callable | call.getEnclosingCallable() = this |
182-
callable.getACall() = call and
183-
callable instanceof CallableAlwaysReturnsTrueHigherPrecision
184-
)
185-
or
186-
exists(AnonymousFunctionExpr le, Call call, CallableAlwaysReturnsTrueHigherPrecision cat |
187-
this = le
188-
|
189-
le.canReturn(call) and
190-
cat.getACall() = call
191-
)
192-
or
193-
exists(LambdaExpr le | le = this |
194-
le.getBody() instanceof CallableAlwaysReturnsTrueHigherPrecision
195-
)
196-
)
197-
}
198-
}
199-
200159
/**
201160
* A callable that returns a `string` and has a `string` as 1st argument
202161
*/

csharp/ql/src/experimental/Security Features/JsonWebTokenHandler/delegated-security-validations-always-return-true.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import DataFlow
1717
import JsonWebTokenHandlerLib
1818
import semmle.code.csharp.commons.QualifiedName
1919

20-
from
21-
TokenValidationParametersProperty p, CallableAlwaysReturnsTrueHigherPrecision e, string qualifier,
22-
string name
20+
from TokenValidationParametersProperty p, CallableAlwaysReturnsTrue e, string qualifier, string name
2321
where e = p.getAnAssignedValue() and p.hasFullyQualifiedName(qualifier, name)
2422
select e,
2523
"JsonWebTokenHandler security-sensitive property $@ is being delegated to this callable that always returns \"true\".",

0 commit comments

Comments
 (0)