Skip to content

Commit fc85c87

Browse files
authored
Merge pull request #23239 from aschackmull/guards/midblock-guard
Guards: Support mid-block guards.
2 parents 117209c + 3160b13 commit fc85c87

15 files changed

Lines changed: 130 additions & 15 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The Guards library has been improved to make better inferences related to
5+
conjunctions and disjunctions. This increases precision for a number of queries.
6+
In particular, `cs/constant-comparison` yields more true positives, and
7+
`cs/dereferenced-value-may-be-null` yield fewer false positives.
8+
`cs/user-controlled-bypass` may also yield more results.

‎csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
2222

2323
class Expr = CS::Expr;
2424

25+
predicate nodeIsAfterValue(ControlFlowNode node, Expr e, ConditionalSuccessor s) {
26+
node.isAfterValue(e, s)
27+
}
28+
2529
private newtype TConstantValue =
2630
TStringValue(string value) { any(StringLiteral s).getValue() = value }
2731

‎csharp/ql/test/library-tests/controlflow/guards-large/GuardedExpr.expected‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| GuardsStressTest.cs:8:26:8:27 | access to field ch |
2+
| GuardsStressTest.cs:9:4:9:5 | access to field ch |
23
| GuardsStressTest.cs:9:17:9:18 | access to field ch |
4+
| GuardsStressTest.cs:10:4:10:5 | access to field ch |
35
| GuardsStressTest.cs:11:4:11:5 | access to field ch |
46
| GuardsStressTest.cs:11:17:11:18 | access to field ch |
57
| GuardsStressTest.cs:12:4:12:5 | access to field ch |
@@ -791,4 +793,5 @@
791793
| GuardsStressTest.cs:438:19:438:20 | access to field ch |
792794
| GuardsStressTest.cs:439:4:439:5 | access to field ch |
793795
| GuardsStressTest.cs:439:19:439:20 | access to field ch |
796+
| GuardsStressTest.cs:439:41:439:42 | access to field ch |
794797
| GuardsStressTest.cs:440:23:440:24 | access to field ch |

‎go/ql/lib/semmle/go/controlflow/Guards.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ private module GuardsInput implements
2626

2727
class Expr = G::Expr;
2828

29+
predicate nodeIsAfterValue(CfgImpl::Cfg::ControlFlowNode node, Expr e, ConditionalSuccessor s) {
30+
node.isAfterValue(e, s)
31+
}
32+
2933
private newtype TConstantValue = TStringValue(string s) { s = any(G::Expr e).getStringValue() }
3034

3135
class ConstantValue extends TConstantValue {

‎go/ql/test/experimental/CWE-942/CorsMisconfiguration.expected‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
| CorsMisconfiguration.go:53:4:53:44 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
88
| CorsMisconfiguration.go:60:4:60:56 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
99
| CorsMisconfiguration.go:67:5:67:57 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
10-
| CorsMisconfiguration.go:130:7:130:63 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
11-
| CorsMisconfiguration.go:143:5:143:57 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
12-
| CorsMisconfiguration.go:158:5:158:57 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
13-
| CorsMisconfiguration.go:173:6:173:58 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
1410
| CorsMisconfiguration.go:224:5:224:57 | call to Set | access-control-allow-origin header is set to a user-defined value, and access-control-allow-credentials is set to `true` |
1511
| RsCors.go:11:21:11:59 | slice literal | access-control-allow-origin header is set to `null`, and access-control-allow-credentials is set to `true` |
1612
| RsCors.go:31:23:31:61 | slice literal | access-control-allow-origin header is set to `null`, and access-control-allow-credentials is set to `true` |

‎go/ql/test/experimental/CWE-942/CorsMisconfiguration.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func main() {
127127
if origin != "" && origin != "null" {
128128
if len(AccessControlAllowOrigins) == 0 ||
129129
AccessControlAllowOrigins[origin] {
130-
responseHeader.Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert
130+
responseHeader.Set("Access-Control-Allow-Origin", origin)
131131
responseHeader.Set("Access-Control-Allow-Credentials", "true")
132132
}
133133
} else {
@@ -140,7 +140,7 @@ func main() {
140140
// OK-ish: the input origin header is validated against a suffix.
141141
origin := req.Header.Get("Origin")
142142
if origin != "" && (originSuffix == "" || strings.HasSuffix(origin, originSuffix)) {
143-
w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert
143+
w.Header().Set("Access-Control-Allow-Origin", origin)
144144
w.Header().Set("Access-Control-Allow-Credentials", "true")
145145
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, x-requested-by, *")
146146
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
@@ -155,7 +155,7 @@ func main() {
155155
// OK-ish: the input origin header is validated against a whitelist.
156156
origin := req.Header.Get("Origin")
157157
if origin != "" && (originSuffix == "" || AccessControlAllowOrigins[origin]) {
158-
w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert
158+
w.Header().Set("Access-Control-Allow-Origin", origin)
159159
w.Header().Set("Access-Control-Allow-Credentials", "true")
160160
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, x-requested-by, *")
161161
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
@@ -170,7 +170,7 @@ func main() {
170170
origin := req.Header.Get("origin")
171171
if origin != "" && origin != "null" {
172172
if len(AccessControlAllowOrigins) == 0 || AccessControlAllowOrigins[origin] {
173-
w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert
173+
w.Header().Set("Access-Control-Allow-Origin", origin)
174174
w.Header().Set("Access-Control-Allow-Credentials", "true")
175175
}
176176
} else {

‎go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ controlsResult
1515
| guards.go:42:2:43:33 | case clause | tagged false comparison | true |
1616
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
1717
| guards.go:48:5:48:5 | a | compound true | true |
18+
| guards.go:48:5:48:17 | ...&&... | compound false | false |
19+
| guards.go:48:5:48:17 | ...&&... | compound true | true |
20+
| guards.go:48:11:48:16 | ...\|\|... | compound true | true |
1821
| guards.go:56:5:56:20 | ...==... | upcast | true |
1922
| guards.go:59:5:59:19 | ...==... | downcast | true |
2023
| guards.go:62:5:62:22 | ...==... | upcast narrow | true |
@@ -83,6 +86,9 @@ valueControlsResult
8386
| guards.go:42:7:42:12 | number | tagged false comparison | Lower bound 10 |
8487
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
8588
| guards.go:48:5:48:5 | a | compound true | true |
89+
| guards.go:48:5:48:17 | ...&&... | compound false | false |
90+
| guards.go:48:5:48:17 | ...&&... | compound true | true |
91+
| guards.go:48:11:48:16 | ...\|\|... | compound true | true |
8692
| guards.go:56:5:56:15 | type conversion | upcast | 0 |
8793
| guards.go:56:5:56:20 | ...==... | upcast | true |
8894
| guards.go:56:11:56:14 | wide | upcast | 0 |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The Guards library has been improved to make better inferences related to
5+
conjunctions and disjunctions. This increases precision for a number of queries.
6+
In particular, `java/constant-comparison` and `java/useless-null-check` yield
7+
more true positives, and `java/dereferenced-value-may-be-null` yields fewer false positives.

‎java/ql/lib/semmle/code/java/controlflow/Guards.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
8787

8888
class Expr = J::Expr;
8989

90+
predicate nodeIsAfterValue(ControlFlowNode node, Expr e, ConditionalSuccessor s) {
91+
node.isAfterValue(e, s)
92+
}
93+
9094
private newtype TConstantValue =
9195
TCharValue(string c) { any(CharacterLiteral lit).getValue() = c } or
9296
TStringValue(string value) { any(CompileTimeConstantExpr c).getStringValue() = value } or

‎java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fun fn(x:Any?, y: Any?) {
55

66
if (x != null) {
77
println("x not null")
8-
} else if (y != null) {
8+
} else if (y != null) { // $ Alert
99
println("y not null")
1010
}
1111
}

0 commit comments

Comments
 (0)