From d221726c3f0b9c4946e4524b8d23f6a44eb755ce Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Wed, 25 Nov 2026 13:03:07 +0100 Subject: [PATCH 1/5] Guards: Support mid-block guards. --- .../controlflow/codeql/controlflow/Guards.qll | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/shared/controlflow/codeql/controlflow/Guards.qll b/shared/controlflow/codeql/controlflow/Guards.qll index 7550363f0ea2..70095115d845 100644 --- a/shared/controlflow/codeql/controlflow/Guards.qll +++ b/shared/controlflow/codeql/controlflow/Guards.qll @@ -78,6 +78,11 @@ signature module InputSig; @@ -1057,6 +1087,13 @@ module Make< ) } + private predicate guardControlsBlock(Guard guard, BasicBlock bb, GuardValue v) { + exists(Guard g0, GuardValue v0 | + midBlockGuard(g0, bb, v0) and + BranchImplies::guardControls(guard, v, g0, v0) + ) + } + /** * Holds if `def` evaluating to `v` controls the control-flow branch * edge from `bb1` to `bb2`. That is, following the edge from `bb1` to @@ -1080,6 +1117,12 @@ module Make< dominatingEdge(guard, succ) and succ.dominates(bb) ) + or + exists(Guard g0, BasicBlock guardbb, GuardValue v0 | + midBlockGuard(g0, guardbb, v0) and + BranchImplies::ssaControls(def, v, g0, v0) and + guardbb.dominates(bb) + ) } private import DisjunctiveGuard @@ -1532,6 +1575,11 @@ module Make< dominatingEdge(guard, succ) and succ.dominates(bb) ) + or + exists(BasicBlock guard | + guardControlsBlock(this, guard, v) and + guard.dominates(bb) + ) } /** From 93e9834f00bacfc773d98483344ca7f90010982c Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Wed, 25 Nov 2026 13:08:32 +0100 Subject: [PATCH 2/5] C#/Go/Java: Instantiate mid-block guards. --- csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll | 4 ++++ go/ql/lib/semmle/go/controlflow/Guards.qll | 4 ++++ java/ql/lib/semmle/code/java/controlflow/Guards.qll | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll b/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll index 031a48906869..2c214b11e904 100644 --- a/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll +++ b/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll @@ -22,6 +22,10 @@ private module GuardsInput implements SharedGuards::InputSig Date: Wed, 25 Nov 2026 14:29:24 +0100 Subject: [PATCH 3/5] C#/Go/Java: Update expected output. --- .../controlflow/guards-large/GuardedExpr.expected | 3 +++ .../experimental/CWE-942/CorsMisconfiguration.expected | 4 ---- go/ql/test/experimental/CWE-942/CorsMisconfiguration.go | 8 ++++---- .../semmle/go/controlflow/Guards/Guards.expected | 6 ++++++ java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt | 2 +- .../UselessNullCheck/UselessNullCheck.expected | 1 + java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt | 2 +- .../UselessNullCheck/UselessNullCheck.expected | 1 + java/ql/test/library-tests/guards/guardslogic.expected | 4 ++++ 9 files changed, 21 insertions(+), 10 deletions(-) diff --git a/csharp/ql/test/library-tests/controlflow/guards-large/GuardedExpr.expected b/csharp/ql/test/library-tests/controlflow/guards-large/GuardedExpr.expected index b3fb032a26c6..b390b5be6394 100644 --- a/csharp/ql/test/library-tests/controlflow/guards-large/GuardedExpr.expected +++ b/csharp/ql/test/library-tests/controlflow/guards-large/GuardedExpr.expected @@ -1,5 +1,7 @@ | GuardsStressTest.cs:8:26:8:27 | access to field ch | +| GuardsStressTest.cs:9:4:9:5 | access to field ch | | GuardsStressTest.cs:9:17:9:18 | access to field ch | +| GuardsStressTest.cs:10:4:10:5 | access to field ch | | GuardsStressTest.cs:11:4:11:5 | access to field ch | | GuardsStressTest.cs:11:17:11:18 | access to field ch | | GuardsStressTest.cs:12:4:12:5 | access to field ch | @@ -791,4 +793,5 @@ | GuardsStressTest.cs:438:19:438:20 | access to field ch | | GuardsStressTest.cs:439:4:439:5 | access to field ch | | GuardsStressTest.cs:439:19:439:20 | access to field ch | +| GuardsStressTest.cs:439:41:439:42 | access to field ch | | GuardsStressTest.cs:440:23:440:24 | access to field ch | diff --git a/go/ql/test/experimental/CWE-942/CorsMisconfiguration.expected b/go/ql/test/experimental/CWE-942/CorsMisconfiguration.expected index 6c3cfb735227..2e53a2b710cd 100644 --- a/go/ql/test/experimental/CWE-942/CorsMisconfiguration.expected +++ b/go/ql/test/experimental/CWE-942/CorsMisconfiguration.expected @@ -7,10 +7,6 @@ | 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` | | 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` | | 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` | -| 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` | -| 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` | -| 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` | -| 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` | | 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` | | 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` | | 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` | diff --git a/go/ql/test/experimental/CWE-942/CorsMisconfiguration.go b/go/ql/test/experimental/CWE-942/CorsMisconfiguration.go index 7e36752cd4b0..5f8c80083cc1 100644 --- a/go/ql/test/experimental/CWE-942/CorsMisconfiguration.go +++ b/go/ql/test/experimental/CWE-942/CorsMisconfiguration.go @@ -127,7 +127,7 @@ func main() { if origin != "" && origin != "null" { if len(AccessControlAllowOrigins) == 0 || AccessControlAllowOrigins[origin] { - responseHeader.Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert + responseHeader.Set("Access-Control-Allow-Origin", origin) responseHeader.Set("Access-Control-Allow-Credentials", "true") } } else { @@ -140,7 +140,7 @@ func main() { // OK-ish: the input origin header is validated against a suffix. origin := req.Header.Get("Origin") if origin != "" && (originSuffix == "" || strings.HasSuffix(origin, originSuffix)) { - w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert + w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Headers", "Content-Type, x-requested-by, *") w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS") @@ -155,7 +155,7 @@ func main() { // OK-ish: the input origin header is validated against a whitelist. origin := req.Header.Get("Origin") if origin != "" && (originSuffix == "" || AccessControlAllowOrigins[origin]) { - w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert + w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Headers", "Content-Type, x-requested-by, *") w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS") @@ -170,7 +170,7 @@ func main() { origin := req.Header.Get("origin") if origin != "" && origin != "null" { if len(AccessControlAllowOrigins) == 0 || AccessControlAllowOrigins[origin] { - w.Header().Set("Access-Control-Allow-Origin", origin) // $ SPURIOUS: Alert + w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Credentials", "true") } } else { diff --git a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected index 044d84d8dab3..24f26c65326d 100644 --- a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected +++ b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected @@ -15,6 +15,9 @@ controlsResult | guards.go:42:2:43:33 | case clause | tagged false comparison | true | | guards.go:42:7:42:17 | ...<... | tagged false comparison | false | | guards.go:48:5:48:5 | a | compound true | true | +| guards.go:48:5:48:17 | ...&&... | compound false | false | +| guards.go:48:5:48:17 | ...&&... | compound true | true | +| guards.go:48:11:48:16 | ...\|\|... | compound true | true | | guards.go:56:5:56:20 | ...==... | upcast | true | | guards.go:59:5:59:19 | ...==... | downcast | true | | guards.go:62:5:62:22 | ...==... | upcast narrow | true | @@ -83,6 +86,9 @@ valueControlsResult | guards.go:42:7:42:12 | number | tagged false comparison | Lower bound 10 | | guards.go:42:7:42:17 | ...<... | tagged false comparison | false | | guards.go:48:5:48:5 | a | compound true | true | +| guards.go:48:5:48:17 | ...&&... | compound false | false | +| guards.go:48:5:48:17 | ...&&... | compound true | true | +| guards.go:48:11:48:16 | ...\|\|... | compound true | true | | guards.go:56:5:56:15 | type conversion | upcast | 0 | | guards.go:56:5:56:20 | ...==... | upcast | true | | guards.go:56:11:56:14 | wide | upcast | 0 | diff --git a/java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt b/java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt index cca4c6fb51dd..2c6209592325 100644 --- a/java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt +++ b/java/ql/test-kotlin1/query-tests/UselessNullCheck/Test.kt @@ -5,7 +5,7 @@ fun fn(x:Any?, y: Any?) { if (x != null) { println("x not null") - } else if (y != null) { + } else if (y != null) { // $ Alert println("y not null") } } diff --git a/java/ql/test-kotlin1/query-tests/UselessNullCheck/UselessNullCheck.expected b/java/ql/test-kotlin1/query-tests/UselessNullCheck/UselessNullCheck.expected index 5cb1138d9927..5e1728bedb7e 100644 --- a/java/ql/test-kotlin1/query-tests/UselessNullCheck/UselessNullCheck.expected +++ b/java/ql/test-kotlin1/query-tests/UselessNullCheck/UselessNullCheck.expected @@ -1 +1,2 @@ +| Test.kt:8:16:8:24 | ... (value not-equals) ... | This check is useless. $@ cannot be null at this check, since it is guarded by $@. | Test.kt:8:16:8:16 | y | y | Test.kt:2:22:2:30 | ... (value equals) ... | ... (value equals) ... | | Test.kt:15:12:15:21 | ... (value equals) ... | This check is useless. $@ cannot be null at this check, since it is guarded by $@. | Test.kt:15:9:15:9 | tmp0_safe_receiver | tmp0_safe_receiver | Test.kt:14:9:14:17 | ... (value not-equals) ... | ... (value not-equals) ... | diff --git a/java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt b/java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt index cca4c6fb51dd..2c6209592325 100644 --- a/java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt +++ b/java/ql/test-kotlin2/query-tests/UselessNullCheck/Test.kt @@ -5,7 +5,7 @@ fun fn(x:Any?, y: Any?) { if (x != null) { println("x not null") - } else if (y != null) { + } else if (y != null) { // $ Alert println("y not null") } } diff --git a/java/ql/test-kotlin2/query-tests/UselessNullCheck/UselessNullCheck.expected b/java/ql/test-kotlin2/query-tests/UselessNullCheck/UselessNullCheck.expected index 0afaa0ab5519..2d9cde1c7754 100644 --- a/java/ql/test-kotlin2/query-tests/UselessNullCheck/UselessNullCheck.expected +++ b/java/ql/test-kotlin2/query-tests/UselessNullCheck/UselessNullCheck.expected @@ -1 +1,2 @@ +| Test.kt:8:16:8:24 | ... (value not-equals) ... | This check is useless. $@ cannot be null at this check, since it is guarded by $@. | Test.kt:8:16:8:16 | y | y | Test.kt:2:22:2:30 | ... (value equals) ... | ... (value equals) ... | | Test.kt:15:12:15:21 | ... (value equals) ... | This check is useless. $@ cannot be null at this check, since it is guarded by $@. | Test.kt:15:12:15:21 | tmp0_safe_receiver | tmp0_safe_receiver | Test.kt:14:9:14:17 | ... (value not-equals) ... | ... (value not-equals) ... | diff --git a/java/ql/test/library-tests/guards/guardslogic.expected b/java/ql/test/library-tests/guards/guardslogic.expected index 3a4aeeeef7be..cbc4cd6f8be7 100644 --- a/java/ql/test/library-tests/guards/guardslogic.expected +++ b/java/ql/test/library-tests/guards/guardslogic.expected @@ -47,6 +47,8 @@ | Logic.java:36:16:36:21 | g(...) | false | Logic.java:37:17:39:5 | { ... } | | Logic.java:36:16:36:21 | g(...) | false | Logic.java:40:5:40:18 | var ...; | | Logic.java:37:9:37:14 | ... > ... | true | Logic.java:37:17:39:5 | { ... } | +| Logic.java:44:9:44:10 | !... | false | Logic.java:43:23:43:31 | Normal Exit | +| Logic.java:44:9:44:10 | !... | true | Logic.java:44:33:44:35 | msg | | Logic.java:44:10:44:10 | b | false | Logic.java:44:33:44:35 | msg | | Logic.java:44:10:44:10 | b | true | Logic.java:43:23:43:31 | Normal Exit | | Logic.java:48:5:48:22 | checkTrue(...) | exception | Logic.java:47:23:47:32 | Exceptional Exit | @@ -68,6 +70,8 @@ | Logic.java:53:21:53:26 | g(...) | false | Logic.java:54:17:56:5 | { ... } | | Logic.java:53:21:53:26 | g(...) | false | Logic.java:57:5:57:18 | var ...; | | Logic.java:54:9:54:14 | ... > ... | true | Logic.java:54:17:56:5 | { ... } | +| Logic.java:61:9:61:10 | !... | false | Logic.java:60:23:60:31 | Normal Exit | +| Logic.java:61:9:61:10 | !... | true | Logic.java:61:33:61:35 | msg | | Logic.java:61:10:61:10 | b | false | Logic.java:61:33:61:35 | msg | | Logic.java:61:10:61:10 | b | true | Logic.java:60:23:60:31 | Normal Exit | | Logic.java:65:5:65:22 | checkTrue(...) | exception | Logic.java:64:23:64:32 | Exceptional Exit | From f22c067aca62414504022048bce386cd3de21479 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Fri, 27 Nov 2026 12:06:32 +0100 Subject: [PATCH 4/5] Guards: Improve poor join. --- .../controlflow/codeql/controlflow/Guards.qll | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/shared/controlflow/codeql/controlflow/Guards.qll b/shared/controlflow/codeql/controlflow/Guards.qll index 70095115d845..45f1f1175beb 100644 --- a/shared/controlflow/codeql/controlflow/Guards.qll +++ b/shared/controlflow/codeql/controlflow/Guards.qll @@ -1202,6 +1202,37 @@ module Make< tgtGuard.getBasicBlock() = bb } + pragma[nomagic] + private predicate relevantValue1(SsaDefinition def, GuardValue v1) { + DisjunctImplies::ssaControls(def, v1, _, _) + } + + pragma[nomagic] + private predicate relevantValue2(SsaDefinition def, GuardValue v2) { + ssaControlsGuard(def, v2, _, _, _) + } + + pragma[nomagic] + private predicate relevantDisjointValues(SsaDefinition def, GuardValue v1, GuardValue v2) { + relevantValue1(def, v1) and + relevantValue2(def, v2) and + disjointValues(v1, v2) + } + + /** + * Holds if `tgtGuard` evaluating to `tgtVal` implies that `def` + * evaluates to a value disjoint from `v`. The basic block of `tgtGuard` is `bb`. + */ + pragma[nomagic] + private predicate ssaControlsGuard2( + SsaDefinition def, GuardValue v, Guard tgtGuard, GuardValue tgtVal, BasicBlock bb + ) { + exists(GuardValue v2 | + ssaControlsGuard(def, v2, tgtGuard, tgtVal, bb) and + relevantDisjointValues(def, v, v2) + ) + } + /** * Holds if `tgtGuard` evaluating to `tgtVal` implies that `guard` * evaluates to `v`. @@ -1210,13 +1241,11 @@ module Make< predicate disjunctiveGuardControls( Guard guard, GuardValue v, Guard tgtGuard, GuardValue tgtVal ) { - exists(SsaDefinition def, GuardValue v1, GuardValue v2, BasicBlock bb | + exists(SsaDefinition def, GuardValue v1, BasicBlock bb | // If `def==v1 || guard==v` controls `bb`, ssaDisjunctionControls(def, v1, guard, v, bb) and - // and `tgtGuard==tgtVal` in `bb` implies `def==v2`, - ssaControlsGuard(def, v2, tgtGuard, tgtVal, bb) and - // and `v1` and `v2` are disjoint, - disjointValues(v1, v2) + // and `tgtGuard==tgtVal` in `bb` implies `def` having some value `v2` disjoint from `v1`, + ssaControlsGuard2(def, v1, tgtGuard, tgtVal, bb) // then assuming `tgtGuard==tgtVal` it follows that `def` cannot be `v1` // and therefore we must have `guard==v`. ) From 3160b13a0aa30b8da5d4034f88c0eda4771c7ae2 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Fri, 27 Nov 2026 13:19:37 +0100 Subject: [PATCH 5/5] Java/C#: Add change notes. --- csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md | 8 ++++++++ java/ql/lib/change-notes/2026-11-27-midblock-guards.md | 7 +++++++ 2 files changed, 15 insertions(+) create mode 100644 csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md create mode 100644 java/ql/lib/change-notes/2026-11-27-midblock-guards.md diff --git a/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md b/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md new file mode 100644 index 000000000000..283f951f0f29 --- /dev/null +++ b/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md @@ -0,0 +1,8 @@ +--- +category: minorAnalysis +--- +* The Guards library has been improved to make better inferences related to + conjunctions and disjunctions. This increases precision for a number of queries. + In particular, `cs/constant-comparison` yields more true positives, and + `cs/dereferenced-value-may-be-null` yield fewer false positives. + `cs/user-controlled-bypass` may also yield more results. diff --git a/java/ql/lib/change-notes/2026-11-27-midblock-guards.md b/java/ql/lib/change-notes/2026-11-27-midblock-guards.md new file mode 100644 index 000000000000..1e3eaefcdfd4 --- /dev/null +++ b/java/ql/lib/change-notes/2026-11-27-midblock-guards.md @@ -0,0 +1,7 @@ +--- +category: minorAnalysis +--- +* The Guards library has been improved to make better inferences related to + conjunctions and disjunctions. This increases precision for a number of queries. + In particular, `java/constant-comparison` and `java/useless-null-check` yield + more true positives, and `java/dereferenced-value-may-be-null` yields fewer false positives.