From 05212aaa62bb50ea5bcddb58ea5c2d11370103c4 Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 17 Dec 2025 10:52:01 -0500
Subject: [PATCH 01/12] init: add PotentiallyUnguardedProtocolHandler query
---
.../PotentiallyUnguardedProtocolHandler.qhelp | 26 ++++++
.../PotentiallyUnguardedProtocolHandler.ql | 91 +++++++++++++++++++
...tentiallyUnguardedProtocolHandler.expected | 26 ++++++
.../PotentiallyUnguardedProtocolHandler.java | 56 ++++++++++++
.../PotentiallyUnguardedProtocolHandler.qlref | 1 +
5 files changed, 200 insertions(+)
create mode 100644 java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
create mode 100644 java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
create mode 100644 java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
create mode 100644 java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
create mode 100644 java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
new file mode 100644
index 0000000..1e63ba4
--- /dev/null
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -0,0 +1,26 @@
+
+
+
+
+ URL protocol handlers are used to handle any URL scheme supported by the native desktop.
+ If URL inputs to these handlers are untrusted and not properly sanitized, they can be
+ used to perform unintended actions by another application registered to handle the same
+ protocol.
+
+
+
+
+ Review protocol handler sinks and ensure that any URL inputs are properly sanitized if
+ they come from untrusted sources.
+
+
+
+ Positive Security: Allow
+ arbitrary URLs, expect arbitrary code execution
+
+ CVE-2022-43550: Jitsi Desktop Client
+ RCE By Interacting with Malicious URL Schemes on Windows
+
+
+
\ No newline at end of file
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
new file mode 100644
index 0000000..709a991
--- /dev/null
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -0,0 +1,91 @@
+/**
+ * @name Potentially unguarded protocol handler invocation
+ * @id tob/java/unguarded-protocol-handler
+ * @description Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols
+ * @kind path-problem
+ * @tags security
+ * external/cwe/cwe-939
+ * @precision medium
+ * @problem.severity warning
+ * @security-severity 6.5
+ * @group security
+ */
+
+import java
+import semmle.code.java.dataflow.TaintTracking
+import semmle.code.java.dataflow.FlowSources
+import semmle.code.java.controlflow.Guards
+
+/**
+ * A call to Desktop.browse() method for handling
+ * TODO(alan): also support legacy/generic cases like invoking "rundll32 url.dll,FileProtocolHandler"
+ */
+class DesktopBrowseCall extends MethodCall {
+ DesktopBrowseCall() {
+ this.getMethod().hasName("browse") and
+ this.getMethod().getDeclaringType().hasQualifiedName("java.awt", "Desktop")
+ }
+
+ Expr getUrlArgument() { result = this.getArgument(0) }
+
+ string getDescription() { result = "Desktop.browse()" }
+}
+
+/**
+ * A guard that checks the URL scheme/protocol signifying sanitization before launch
+ */
+predicate isUrlSchemeCheck(Guard g, Expr e, boolean branch) {
+ exists(MethodCall mc |
+ mc = g and
+ e = mc.getQualifier*() and
+ branch = true and
+ (
+ // getScheme() or getProtocol() check
+ mc.getMethod().hasName(["equals", "equalsIgnoreCase", "startsWith", "matches"]) and
+ exists(MethodCall getScheme |
+ getScheme.getParent*() = mc and
+ getScheme.getMethod().hasName(["getScheme", "getProtocol"])
+ )
+ or
+ // URL string contains check like: url.contains("http://") or url.startsWith("https://")
+ mc.getMethod().hasName(["contains", "startsWith", "matches", "indexOf"]) and
+ exists(StringLiteral sl | sl = mc.getAnArgument() | sl.getValue().regexpMatch(".*://.*"))
+ or
+ // Pattern matching on the string representation
+ mc.getMethod().hasName(["matches", "find"]) and
+ mc.getQualifier().getType().(RefType).hasQualifiedName("java.util.regex", "Matcher")
+ )
+ )
+}
+
+/**
+ * Configuration for tracking untrusted data to protocol handler invocations
+ */
+module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
+ predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
+
+ predicate isSink(DataFlow::Node sink) {
+ exists(DesktopBrowseCall call | sink.asExpr() = call.getUrlArgument())
+ }
+
+ predicate isBarrier(DataFlow::Node node) {
+ // Consider sanitized if there's a guard checking the scheme
+ node = DataFlow::BarrierGuard::getABarrierNode()
+ }
+}
+
+module PotentiallyUnguardedProtocolHandlerFlow =
+ TaintTracking::Global;
+
+import PotentiallyUnguardedProtocolHandlerFlow::PathGraph
+
+from
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode source,
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, DesktopBrowseCall call
+where
+ PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
+ sink.getNode().asExpr() = call.getUrlArgument()
+select call, source, sink,
+ call.getDescription() +
+ " is called with untrusted input from $@ without proper URL scheme validation.",
+ source.getNode(), "this source"
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
new file mode 100644
index 0000000..8a66a01
--- /dev/null
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -0,0 +1,26 @@
+edges
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46190 |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44428 Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46190 |
+| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44428 |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46190 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | provenance | Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
+nodes
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | semmle.label | url : String |
+| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
+| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | semmle.label | url : String |
+| PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | semmle.label | uri |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
+| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | semmle.label | url : String |
+| PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | semmle.label | uri |
+subpaths
+#select
+| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:23:13:23:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:39:13:39:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
new file mode 100644
index 0000000..9e8ac93
--- /dev/null
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -0,0 +1,56 @@
+import java.awt.Desktop;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import javax.servlet.http.HttpServletRequest;
+
+public class PotentiallyUnguardedProtocolHandler {
+
+ public void bad1(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ Desktop.getDesktop().browse(new URI(url));
+ }
+
+ public void bad2(String userInput) throws IOException, URISyntaxException {
+ URI uri = new URI(userInput);
+ Desktop.getDesktop().browse(uri);
+ }
+
+ public void safe1(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ URI uri = new URI(url);
+ if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
+ Desktop.getDesktop().browse(uri);
+ }
+ }
+
+ public void safe2(String userInput) throws IOException, URISyntaxException {
+ if (userInput.startsWith("https://") || userInput.startsWith("http://")) {
+ Desktop.getDesktop().browse(new URI(userInput));
+ }
+ }
+
+ public void bad3(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ URI uri = new URI(url);
+
+ // Weak check - only checks if scheme exists, not what it is
+ if (uri.getScheme() != null) {
+ Desktop.getDesktop().browse(uri);
+ }
+ }
+
+ public void safe3(String userInput) throws IOException, URISyntaxException {
+ URI uri = new URI(userInput);
+ String scheme = uri.getScheme();
+
+ if (scheme != null && (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
+ Desktop.getDesktop().browse(uri);
+ }
+ }
+
+ public void safe4() throws IOException, URISyntaxException {
+ // hardcoded, no untrusted input
+ Desktop.getDesktop().browse(new URI("https://example.com"));
+ }
+}
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
new file mode 100644
index 0000000..bc3c1da
--- /dev/null
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
@@ -0,0 +1 @@
+security/PotentiallyUnguardedProtocolhandler/PotentiallyUnguardedProtocolhandler.ql
From 4664d38136eab92756be584113b393e2e0989b0c Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 17 Dec 2025 13:34:36 -0500
Subject: [PATCH 02/12] add legacy protocol handler support
---
.../PotentiallyUnguardedProtocolHandler.ql | 65 ++++++++++++++++---
...tentiallyUnguardedProtocolHandler.expected | 8 +++
.../PotentiallyUnguardedProtocolHandler.java | 40 +++++++++++-
3 files changed, 104 insertions(+), 9 deletions(-)
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index 709a991..b234905 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -15,10 +15,11 @@ import java
import semmle.code.java.dataflow.TaintTracking
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.controlflow.Guards
+import semmle.code.java.security.ExternalProcess
/**
- * A call to Desktop.browse() method for handling
- * TODO(alan): also support legacy/generic cases like invoking "rundll32 url.dll,FileProtocolHandler"
+ * A call to Desktop.browse() method. This is the platform-agnostic standard now.
+ * NOTE(alan): the URLRedirect query will likely also trigger due to a little imprecision there
*/
class DesktopBrowseCall extends MethodCall {
DesktopBrowseCall() {
@@ -27,8 +28,44 @@ class DesktopBrowseCall extends MethodCall {
}
Expr getUrlArgument() { result = this.getArgument(0) }
+}
+
+/**
+ * Legacy Windows-specific command execution for protocol handling, based on https://imagej.net/ij/source/ij/plugin/BrowserLauncher.java and
+ * seen in CVE-2022-43550. We match on the Windows shell command, since it more distinct and pervasive.
+ */
+class LegacyWindowsProtocolHandlerCall extends ArgumentToExec {
+ LegacyWindowsProtocolHandlerCall() {
+ // Single string: "rundll32 url.dll,FileProtocolHandler " + url
+ this instanceof StringArgumentToExec and
+ exists(StringLiteral sl |
+ sl.getParent*() = this and
+ sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*")
+ )
+ or
+ // Array: {"rundll32", "url.dll,FileProtocolHandler", url}
+ this.getType().(Array).getElementType() instanceof TypeString and
+ exists(ArrayInit init, StringLiteral rundll, StringLiteral urldll |
+ init = this.(ArrayCreationExpr).getInit() and
+ rundll = init.getAnInit() and
+ urldll = init.getAnInit() and
+ rundll.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
+ urldll.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler")
+ )
+ }
- string getDescription() { result = "Desktop.browse()" }
+ Expr getUrlArgument() {
+ // For single string exec, the URL is tainted into the concatenated string
+ result = this and this instanceof StringArgumentToExec
+ or
+ // For arrays, find elements after "url.dll,FileProtocolHandler"
+ exists(ArrayInit init, int urlIdx, int dllIdx |
+ init = this.(ArrayCreationExpr).getInit() and
+ result = init.getInit(urlIdx) and
+ init.getInit(dllIdx).(StringLiteral).getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
+ urlIdx > dllIdx
+ )
+ }
}
/**
@@ -66,10 +103,11 @@ module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig
predicate isSink(DataFlow::Node sink) {
exists(DesktopBrowseCall call | sink.asExpr() = call.getUrlArgument())
+ or
+ exists(LegacyWindowsProtocolHandlerCall call | sink.asExpr() = call.getUrlArgument())
}
predicate isBarrier(DataFlow::Node node) {
- // Consider sanitized if there's a guard checking the scheme
node = DataFlow::BarrierGuard::getABarrierNode()
}
}
@@ -81,11 +119,22 @@ import PotentiallyUnguardedProtocolHandlerFlow::PathGraph
from
PotentiallyUnguardedProtocolHandlerFlow::PathNode source,
- PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, DesktopBrowseCall call
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, Expr call, string callType
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
- sink.getNode().asExpr() = call.getUrlArgument()
+ (
+ exists(DesktopBrowseCall dbc |
+ call = dbc and
+ sink.getNode().asExpr() = dbc.getUrlArgument() and
+ callType = "Desktop.browse()"
+ )
+ or
+ exists(LegacyWindowsProtocolHandlerCall whc |
+ call = whc and
+ sink.getNode().asExpr() = whc.getUrlArgument() and
+ callType = "rundll32 url.dll,FileProtocolHandler"
+ )
+ )
select call, source, sink,
- call.getDescription() +
- " is called with untrusted input from $@ without proper URL scheme validation.",
+ callType + " is called with untrusted input from $@ without proper URL scheme validation.",
source.getNode(), "this source"
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index 8a66a01..d8a7dac 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -7,6 +7,8 @@ edges
| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46190 |
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | provenance | Sink:MaD:43969 |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
+| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
@@ -19,8 +21,14 @@ nodes
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | semmle.label | url : String |
| PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | semmle.label | uri |
+| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | semmle.label | ... + ... |
subpaths
#select
| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:23:13:23:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:39:13:39:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
index 9e8ac93..50adb08 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -50,7 +50,45 @@ public void safe3(String userInput) throws IOException, URISyntaxException {
}
public void safe4() throws IOException, URISyntaxException {
- // hardcoded, no untrusted input
Desktop.getDesktop().browse(new URI("https://example.com"));
}
+
+ // rundll32 test cases
+ public void bad4_rundll32(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ // Single string command with concatenation
+ Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
+ }
+
+ public void bad5_rundll32(String userInput) throws IOException {
+ // Array-based command
+ String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", userInput };
+ Runtime.getRuntime().exec(cmd);
+ }
+
+ public void bad6_rundll32(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ // ProcessBuilder with list
+ ProcessBuilder pb = new ProcessBuilder("rundll32", "url.dll,FileProtocolHandler", url);
+ pb.start();
+ }
+
+ public void safe5_rundll32(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ URI uri = new URI(url);
+ if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
+ Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
+ }
+ }
+
+ public void safe6_rundll32(String userInput) throws IOException {
+ if (userInput.startsWith("https://") || userInput.startsWith("http://")) {
+ String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", userInput };
+ Runtime.getRuntime().exec(cmd);
+ }
+ }
+
+ public void safe7_rundll32() throws IOException {
+ Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler https://example.com");
+ }
}
From b2d7c373a104f76443eb16f0592f9114cf075444 Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 17 Dec 2025 14:32:41 -0500
Subject: [PATCH 03/12] add support for C++/Qt, and genericize shell execution
models for all platforms
---
.../PotentiallyUnguardedProtocolHandler.ql | 149 ++++++++++++++++++
...tentiallyUnguardedProtocolHandler.expected | 4 +
.../PotentiallyUnguardedProtocolHandler.qlref | 1 +
.../test.cpp | 60 +++++++
.../PotentiallyUnguardedProtocolHandler.ql | 142 ++++++++++-------
...tentiallyUnguardedProtocolHandler.expected | 34 +++-
.../PotentiallyUnguardedProtocolHandler.java | 45 ++++++
7 files changed, 369 insertions(+), 66 deletions(-)
create mode 100644 cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
create mode 100644 cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
create mode 100644 cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
create mode 100644 cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
new file mode 100644
index 0000000..4a7113f
--- /dev/null
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -0,0 +1,149 @@
+/**
+ * @name Potentially unguarded protocol handler invocation
+ * @id tob/cpp/unguarded-protocol-handler
+ * @description Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols
+ * @kind path-problem
+ * @tags security
+ * external/cwe/cwe-939
+ * @precision medium
+ * @problem.severity warning
+ * @security-severity 6.5
+ * @group security
+ */
+
+import cpp
+private import semmle.code.cpp.ir.dataflow.TaintTracking
+private import semmle.code.cpp.security.FlowSources
+private import semmle.code.cpp.security.CommandExecution
+
+/**
+ * Generic case: invoke protocol handling through OS's protocol handling utilities. This aligns with CVE-2022-43550.
+ */
+class ShellProtocolHandler extends SystemFunction {
+ ShellProtocolHandler() {
+ // Check if any calls to this function contain protocol handler invocations
+ exists(FunctionCall call |
+ call.getTarget() = this and
+ exists(Expr arg |
+ arg = call.getArgument(0).getAChild*() and
+ exists(StringLiteral sl | sl = arg or sl.getParent*() = arg |
+ sl.getValue()
+ .regexpMatch("(?i).*(rundll32.*url\\.dll.*FileProtocolHandler|xdg-open|\\bopen\\b).*")
+ )
+ )
+ )
+ }
+
+ string getHandlerType() {
+ exists(FunctionCall call, StringLiteral sl |
+ call.getTarget() = this and
+ sl.getParent*() = call.getArgument(0) and
+ (
+ sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
+ result = "rundll32 url.dll,FileProtocolHandler"
+ or
+ sl.getValue().regexpMatch("(?i).*xdg-open.*") and
+ result = "xdg-open"
+ or
+ sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ result = "open"
+ )
+ )
+ }
+}
+
+/**
+ * Qt's QDesktopServices::openUrl method
+ */
+class QtProtocolHandler extends FunctionCall {
+ QtProtocolHandler() { this.getTarget().hasQualifiedName("QDesktopServices", "openUrl") }
+
+ Expr getUrlArgument() { result = this.getArgument(0) }
+}
+
+/**
+ * A sanitizer node that represents URL scheme validation
+ */
+class UrlSchemeValidationSanitizer extends DataFlow::Node {
+ UrlSchemeValidationSanitizer() {
+ exists(FunctionCall fc |
+ fc = this.asExpr() and
+ (
+ // String comparison on the untrusted URL
+ fc.getTarget().getName() =
+ [
+ "strcmp", "strncmp", "strcasecmp", "strncasecmp", "strstr", "strcasestr", "_stricmp",
+ "_strnicmp"
+ ]
+ or
+ // Qt QUrl::scheme() comparison - QUrl::scheme() returns QString
+ // Pattern: url.scheme() == "http" or url.scheme() == "https"
+ exists(FunctionCall schemeCall |
+ schemeCall.getTarget().hasQualifiedName("QUrl", "scheme") and
+ (
+ // Direct comparison
+ fc.getTarget().hasName(["operator==", "operator!="]) and
+ fc.getAnArgument() = schemeCall
+ or
+ // QString comparison methods
+ fc = schemeCall and
+ exists(FunctionCall qstringCmp |
+ qstringCmp.getQualifier() = schemeCall and
+ qstringCmp.getTarget().hasQualifiedName("QString", ["compare", "operator=="])
+ )
+ )
+ )
+ or
+ // Qt QString startsWith check for direct URL strings
+ fc.getTarget().hasQualifiedName("QString", "startsWith")
+ )
+ )
+ }
+}
+
+/**
+ * Configuration for tracking untrusted data to protocol handler invocations
+ */
+module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
+ predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
+
+ predicate isSink(DataFlow::Node sink) {
+ // QDesktopServices::openUrl()
+ exists(QtProtocolHandler call | sink.asExpr() = call.getUrlArgument())
+ or
+ // Shell protocol handlers (rundll32, xdg-open, open) via system()/popen()/exec*()
+ exists(FunctionCall call |
+ call.getTarget() instanceof ShellProtocolHandler and
+ sink.asExpr() = call.getArgument(0)
+ )
+ }
+
+ predicate isBarrier(DataFlow::Node node) { node instanceof UrlSchemeValidationSanitizer }
+}
+
+module PotentiallyUnguardedProtocolHandlerFlow =
+ TaintTracking::Global;
+
+import PotentiallyUnguardedProtocolHandlerFlow::PathGraph
+
+from
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode source,
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, FunctionCall call, string callType
+where
+ PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
+ (
+ exists(QtProtocolHandler qtCall |
+ call = qtCall and
+ sink.getNode().asExpr() = qtCall.getUrlArgument() and
+ callType = "QDesktopServices::openUrl()"
+ )
+ or
+ exists(ShellProtocolHandler shellFunc |
+ call.getTarget() = shellFunc and
+ sink.getNode().asExpr() = call.getArgument(0) and
+ callType = shellFunc.getHandlerType()
+ )
+ )
+select call, source, sink,
+ callType + " is called with untrusted input from $@ without proper URL scheme validation.",
+ source.getNode(), "this source"
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
new file mode 100644
index 0000000..e217064
--- /dev/null
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -0,0 +1,4 @@
+edges
+nodes
+subpaths
+#select
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
new file mode 100644
index 0000000..de5b795
--- /dev/null
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
@@ -0,0 +1 @@
+security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
new file mode 100644
index 0000000..4579758
--- /dev/null
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
@@ -0,0 +1,60 @@
+// Forward declarations - minimal to avoid system header dependencies
+extern "C" {
+int system(const char *);
+int sprintf(char *, const char *, ...);
+int strncmp(const char *, const char *, unsigned long);
+}
+
+// Mock QString class
+struct QString {
+ const char *data;
+ QString(const char *s) : data(s) {}
+ bool operator==(const char *other) const;
+};
+
+// Mock Qt-like class for QDesktopServices::openUrl
+struct QUrl {
+ const char *url;
+ QUrl(const char *s) : url(s) {}
+ QString scheme() const;
+ bool startsWith(const char *prefix) const;
+};
+
+struct QDesktopServices {
+ static bool openUrl(const QUrl &url);
+};
+
+// Untrusted input sources
+extern "C" char *getUserInput();
+extern "C" const char *getUrlParam();
+
+// BAD: QDesktopServices::openUrl with untrusted input
+void bad1_qt(const char *userUrl) {
+ QUrl url(userUrl);
+ QDesktopServices::openUrl(url); // BAD
+}
+
+void bad2_qt() {
+ const char *input = getUrlParam();
+ QUrl url(input);
+ QDesktopServices::openUrl(url); // BAD
+}
+
+void safe1_qt() {
+ QUrl url("https://example.com");
+ QDesktopServices::openUrl(url); // GOOD - no taint
+}
+
+void safe2_qt(const char *userUrl) {
+ if (strncmp(userUrl, "https://", 8) == 0 ||
+ strncmp(userUrl, "http://", 7) == 0) {
+ QUrl url(userUrl);
+ QDesktopServices::openUrl(url); // GOOD
+ }
+}
+
+void safe3_qt(QUrl &url) {
+ if (url.scheme() == "https" || url.scheme() == "http") {
+ QDesktopServices::openUrl(url); // GOOD
+ }
+}
\ No newline at end of file
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index b234905..33b3058 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -18,39 +18,24 @@ import semmle.code.java.controlflow.Guards
import semmle.code.java.security.ExternalProcess
/**
- * A call to Desktop.browse() method. This is the platform-agnostic standard now.
- * NOTE(alan): the URLRedirect query will likely also trigger due to a little imprecision there
- */
-class DesktopBrowseCall extends MethodCall {
- DesktopBrowseCall() {
- this.getMethod().hasName("browse") and
- this.getMethod().getDeclaringType().hasQualifiedName("java.awt", "Desktop")
- }
-
- Expr getUrlArgument() { result = this.getArgument(0) }
-}
-
-/**
- * Legacy Windows-specific command execution for protocol handling, based on https://imagej.net/ij/source/ij/plugin/BrowserLauncher.java and
- * seen in CVE-2022-43550. We match on the Windows shell command, since it more distinct and pervasive.
+ * Generic case: invoke protocol handling through OS's protocol handling utilities. This aligns with CVE-2022-43550.
*/
-class LegacyWindowsProtocolHandlerCall extends ArgumentToExec {
- LegacyWindowsProtocolHandlerCall() {
- // Single string: "rundll32 url.dll,FileProtocolHandler " + url
+class ShellProtocolHandler extends ArgumentToExec {
+ ShellProtocolHandler() {
+ // Single string: "rundll32 url.dll,FileProtocolHandler " + url or "xdg-open " + url
this instanceof StringArgumentToExec and
exists(StringLiteral sl |
sl.getParent*() = this and
- sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*")
+ sl.getValue()
+ .regexpMatch("(?i).*(rundll32.*url\\.dll.*FileProtocolHandler|xdg-open|\\bopen\\b).*")
)
or
- // Array: {"rundll32", "url.dll,FileProtocolHandler", url}
+ // Array: {"rundll32", "url.dll,FileProtocolHandler", url} or {"xdg-open", url}
this.getType().(Array).getElementType() instanceof TypeString and
- exists(ArrayInit init, StringLiteral rundll, StringLiteral urldll |
+ exists(ArrayInit init, StringLiteral handler |
init = this.(ArrayCreationExpr).getInit() and
- rundll = init.getAnInit() and
- urldll = init.getAnInit() and
- rundll.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
- urldll.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler")
+ handler = init.getAnInit() and
+ handler.getValue().regexpMatch("(?i)(rundll32(\\.exe)?|xdg-open|open|/usr/bin/open)")
)
}
@@ -58,41 +43,84 @@ class LegacyWindowsProtocolHandlerCall extends ArgumentToExec {
// For single string exec, the URL is tainted into the concatenated string
result = this and this instanceof StringArgumentToExec
or
- // For arrays, find elements after "url.dll,FileProtocolHandler"
+ // For arrays with rundll32, find elements after "url.dll,FileProtocolHandler"
exists(ArrayInit init, int urlIdx, int dllIdx |
init = this.(ArrayCreationExpr).getInit() and
result = init.getInit(urlIdx) and
- init.getInit(dllIdx).(StringLiteral).getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
+ init.getInit(dllIdx)
+ .(StringLiteral)
+ .getValue()
+ .regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
urlIdx > dllIdx
)
+ or
+ // For arrays with xdg-open/open, find elements after the handler
+ exists(ArrayInit init, int urlIdx, int handlerIdx |
+ init = this.(ArrayCreationExpr).getInit() and
+ result = init.getInit(urlIdx) and
+ init.getInit(handlerIdx)
+ .(StringLiteral)
+ .getValue()
+ .regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
+ urlIdx > handlerIdx
+ )
+ }
+
+ string getHandlerType() {
+ exists(StringLiteral sl |
+ sl.getParent*() = this and
+ (
+ sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
+ result = "rundll32 url.dll,FileProtocolHandler"
+ or
+ sl.getValue().regexpMatch("(?i).*xdg-open.*") and
+ result = "xdg-open"
+ or
+ sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ result = "open"
+ )
+ )
}
}
/**
- * A guard that checks the URL scheme/protocol signifying sanitization before launch
+ * A call to Desktop.browse() method. This is the platform-agnostic standard now.
+ * NOTE(alan): the URLRedirect query will likely also trigger due to a little imprecision there
*/
-predicate isUrlSchemeCheck(Guard g, Expr e, boolean branch) {
- exists(MethodCall mc |
- mc = g and
- e = mc.getQualifier*() and
- branch = true and
- (
- // getScheme() or getProtocol() check
- mc.getMethod().hasName(["equals", "equalsIgnoreCase", "startsWith", "matches"]) and
- exists(MethodCall getScheme |
- getScheme.getParent*() = mc and
- getScheme.getMethod().hasName(["getScheme", "getProtocol"])
+class DesktopBrowseProtocolHandler extends MethodCall {
+ DesktopBrowseProtocolHandler() {
+ this.getMethod().hasName("browse") and
+ this.getMethod().getDeclaringType().hasQualifiedName("java.awt", "Desktop")
+ }
+
+ Expr getUrlArgument() { result = this.getArgument(0) }
+}
+
+/**
+ * A sanitizer node that represents URL scheme validation
+ */
+class UrlSchemeValidationSanitizer extends DataFlow::Node {
+ UrlSchemeValidationSanitizer() {
+ exists(MethodCall mc |
+ mc = this.asExpr() and
+ (
+ // String comparison on the untrusted URL
+ mc.getMethod().hasName(["equals", "equalsIgnoreCase", "startsWith", "matches"]) and
+ exists(MethodCall getScheme |
+ getScheme.getParent*() = mc and
+ getScheme.getMethod().hasName(["getScheme", "getProtocol"])
+ )
+ or
+ // URL string contains check like: url.contains("://")
+ mc.getMethod().hasName(["contains", "startsWith", "matches", "indexOf"]) and
+ exists(StringLiteral sl | sl = mc.getAnArgument() | sl.getValue().regexpMatch(".*://.*"))
+ or
+ // Pattern matching on the string representation
+ mc.getMethod().hasName(["matches", "find"]) and
+ mc.getQualifier().getType().(RefType).hasQualifiedName("java.util.regex", "Matcher")
)
- or
- // URL string contains check like: url.contains("http://") or url.startsWith("https://")
- mc.getMethod().hasName(["contains", "startsWith", "matches", "indexOf"]) and
- exists(StringLiteral sl | sl = mc.getAnArgument() | sl.getValue().regexpMatch(".*://.*"))
- or
- // Pattern matching on the string representation
- mc.getMethod().hasName(["matches", "find"]) and
- mc.getQualifier().getType().(RefType).hasQualifiedName("java.util.regex", "Matcher")
)
- )
+ }
}
/**
@@ -102,14 +130,12 @@ module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
predicate isSink(DataFlow::Node sink) {
- exists(DesktopBrowseCall call | sink.asExpr() = call.getUrlArgument())
+ exists(DesktopBrowseProtocolHandler call | sink.asExpr() = call.getUrlArgument())
or
- exists(LegacyWindowsProtocolHandlerCall call | sink.asExpr() = call.getUrlArgument())
+ exists(ShellProtocolHandler call | sink.asExpr() = call.getUrlArgument())
}
- predicate isBarrier(DataFlow::Node node) {
- node = DataFlow::BarrierGuard::getABarrierNode()
- }
+ predicate isBarrier(DataFlow::Node node) { node instanceof UrlSchemeValidationSanitizer }
}
module PotentiallyUnguardedProtocolHandlerFlow =
@@ -123,16 +149,16 @@ from
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
(
- exists(DesktopBrowseCall dbc |
+ exists(DesktopBrowseProtocolHandler dbc |
call = dbc and
sink.getNode().asExpr() = dbc.getUrlArgument() and
callType = "Desktop.browse()"
)
or
- exists(LegacyWindowsProtocolHandlerCall whc |
- call = whc and
- sink.getNode().asExpr() = whc.getUrlArgument() and
- callType = "rundll32 url.dll,FileProtocolHandler"
+ exists(ShellProtocolHandler shc |
+ call = shc and
+ sink.getNode().asExpr() = shc.getUrlArgument() and
+ callType = shc.getHandlerType()
)
)
select call, source, sink,
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index d8a7dac..d621e3a 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -7,8 +7,12 @@ edges
| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46190 |
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | provenance | Sink:MaD:43969 |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
-| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
+| PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
@@ -21,14 +25,28 @@ nodes
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | semmle.label | url : String |
| PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | semmle.label | uri |
-| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | semmle.label | ... + ... |
subpaths
#select
| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:23:13:23:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:39:13:39:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:61:35:61:79 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:83:39:83:83 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | xdg-open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | xdg-open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
index 50adb08..4642b5f 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -91,4 +91,49 @@ public void safe6_rundll32(String userInput) throws IOException {
public void safe7_rundll32() throws IOException {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler https://example.com");
}
+
+ // xdg-open test cases (Linux)
+ public void bad7_xdgopen(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ Runtime.getRuntime().exec("xdg-open " + url);
+ }
+
+ public void bad8_xdgopen(String userInput) throws IOException {
+ String[] cmd = { "xdg-open", userInput };
+ Runtime.getRuntime().exec(cmd);
+ }
+
+ public void safe8_xdgopen(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ URI uri = new URI(url);
+ if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
+ Runtime.getRuntime().exec("xdg-open " + url);
+ }
+ }
+
+ public void safe9_xdgopen() throws IOException {
+ Runtime.getRuntime().exec("xdg-open https://example.com");
+ }
+
+ // open test cases (macOS)
+ public void bad9_open(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ Runtime.getRuntime().exec("open " + url);
+ }
+
+ public void bad10_open(String userInput) throws IOException {
+ ProcessBuilder pb = new ProcessBuilder("open", userInput);
+ pb.start();
+ }
+
+ public void safe10_open(HttpServletRequest request) throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ if (url.startsWith("https://") || url.startsWith("http://")) {
+ Runtime.getRuntime().exec("/usr/bin/open " + url);
+ }
+ }
+
+ public void safe11_open() throws IOException {
+ Runtime.getRuntime().exec("open https://example.com");
+ }
}
From 7efd379b71c15ff50a38996ad77d49e080a522e0 Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Fri, 2 Jan 2026 12:49:08 -0500
Subject: [PATCH 04/12] fix java tests
---
...tentiallyUnguardedProtocolHandler.expected | 32 ++---------
.../PotentiallyUnguardedProtocolHandler.java | 53 +++----------------
2 files changed, 9 insertions(+), 76 deletions(-)
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index d621e3a..14f3d34 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -5,14 +5,8 @@ edges
| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43969 |
| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44428 |
| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46190 |
-| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | provenance | Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43969 |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
-| PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
-| PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | provenance | Src:MaD:46190 Sink:MaD:44131 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
@@ -24,29 +18,9 @@ nodes
| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | semmle.label | url : String |
-| PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | semmle.label | uri |
-| PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | semmle.label | uri |
subpaths
#select
| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:23:13:23:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:39:13:39:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:39:41:39:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:35:60:79 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:58:22:58:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:80:39:80:83 | ... + ... | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:77:22:77:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:98:35:98:51 | ... + ... | xdg-open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:97:22:97:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:110:39:110:55 | ... + ... | xdg-open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:107:22:107:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:121:35:121:47 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:120:22:120:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:132:39:132:60 | ... + ... | open is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:130:22:130:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:38:13:38:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
index 4642b5f..37436d3 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -33,7 +33,6 @@ public void safe2(String userInput) throws IOException, URISyntaxException {
public void bad3(HttpServletRequest request) throws IOException, URISyntaxException {
String url = request.getParameter("url");
URI uri = new URI(url);
-
// Weak check - only checks if scheme exists, not what it is
if (uri.getScheme() != null) {
Desktop.getDesktop().browse(uri);
@@ -43,7 +42,6 @@ public void bad3(HttpServletRequest request) throws IOException, URISyntaxExcept
public void safe3(String userInput) throws IOException, URISyntaxException {
URI uri = new URI(userInput);
String scheme = uri.getScheme();
-
if (scheme != null && (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
Desktop.getDesktop().browse(uri);
}
@@ -53,22 +51,13 @@ public void safe4() throws IOException, URISyntaxException {
Desktop.getDesktop().browse(new URI("https://example.com"));
}
- // rundll32 test cases
- public void bad4_rundll32(HttpServletRequest request) throws IOException {
- String url = request.getParameter("url");
- // Single string command with concatenation
- Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
- }
-
- public void bad5_rundll32(String userInput) throws IOException {
- // Array-based command
+ public void bad4_rundll32(String userInput) throws IOException {
String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", userInput };
Runtime.getRuntime().exec(cmd);
}
public void bad6_rundll32(HttpServletRequest request) throws IOException {
String url = request.getParameter("url");
- // ProcessBuilder with list
ProcessBuilder pb = new ProcessBuilder("rundll32", "url.dll,FileProtocolHandler", url);
pb.start();
}
@@ -77,7 +66,8 @@ public void safe5_rundll32(HttpServletRequest request) throws IOException, URISy
String url = request.getParameter("url");
URI uri = new URI(url);
if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
- Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
+ String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", url };
+ Runtime.getRuntime().exec(cmd);
}
}
@@ -92,13 +82,7 @@ public void safe7_rundll32() throws IOException {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler https://example.com");
}
- // xdg-open test cases (Linux)
- public void bad7_xdgopen(HttpServletRequest request) throws IOException {
- String url = request.getParameter("url");
- Runtime.getRuntime().exec("xdg-open " + url);
- }
-
- public void bad8_xdgopen(String userInput) throws IOException {
+ public void bad7_xdgopen(String userInput) throws IOException {
String[] cmd = { "xdg-open", userInput };
Runtime.getRuntime().exec(cmd);
}
@@ -107,33 +91,8 @@ public void safe8_xdgopen(HttpServletRequest request) throws IOException, URISyn
String url = request.getParameter("url");
URI uri = new URI(url);
if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
- Runtime.getRuntime().exec("xdg-open " + url);
- }
- }
-
- public void safe9_xdgopen() throws IOException {
- Runtime.getRuntime().exec("xdg-open https://example.com");
- }
-
- // open test cases (macOS)
- public void bad9_open(HttpServletRequest request) throws IOException {
- String url = request.getParameter("url");
- Runtime.getRuntime().exec("open " + url);
- }
-
- public void bad10_open(String userInput) throws IOException {
- ProcessBuilder pb = new ProcessBuilder("open", userInput);
- pb.start();
- }
-
- public void safe10_open(HttpServletRequest request) throws IOException, URISyntaxException {
- String url = request.getParameter("url");
- if (url.startsWith("https://") || url.startsWith("http://")) {
- Runtime.getRuntime().exec("/usr/bin/open " + url);
+ String[] cmd = { "xdg-open", url };
+ Runtime.getRuntime().exec(cmd);
}
}
-
- public void safe11_open() throws IOException {
- Runtime.getRuntime().exec("open https://example.com");
- }
}
From 4d28876987c6283a924eafb0980db51cbc7ac45b Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 14 Jan 2026 16:28:42 -0500
Subject: [PATCH 05/12] Add docs and fix CI/CD issue
---
README.md | 5 +-
.../PotentiallyUnguardedProtocolHandler.md | 87 ++++++++++++++++
.../PotentiallyUnguardedProtocolHandler.md | 99 +++++++++++++++++++
.../PotentiallyUnguardedProtocolHandler.qlref | 2 +-
4 files changed, 191 insertions(+), 2 deletions(-)
create mode 100644 cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
create mode 100644 java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
diff --git a/README.md b/README.md
index 8488b85..402f6ba 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,8 @@ codeql database analyze database.db --format=sarif-latest --output=./tob.sarif -
|[Invalid string size passed to string manipulation function](./cpp/src/docs/security/CStrnFinder/CStrnFinder.md)|Finds calls to functions that take as input a string and its size as separate arguments (e.g., `strncmp`, `strncat`, ...) and the size argument is wrong|error|low|
|[Iterator invalidation](./cpp/src/docs/security/IteratorInvalidation/IteratorInvalidation.md)|Modifying a container while iterating over it can invalidate iterators, leading to undefined behavior.|warning|medium|
|[Missing null terminator](./cpp/src/docs/security/NoNullTerminator/NoNullTerminator.md)|This query finds incorrectly initialized strings that are passed to functions expecting null-byte-terminated strings|error|high|
+|[Potentially unguarded protocol handler invocation](./cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
+|[Unsafe implicit integer conversion](./cpp/src/docs/security/UnsafeImplicitConversions/UnsafeImplicitConversions.md)|Finds implicit integer casts that may overflow or be truncated, with false positive reduction via Value Range Analysis|warning|low|
### Go
@@ -77,7 +79,8 @@ codeql database analyze database.db --format=sarif-latest --output=./tob.sarif -
| Name | Description | Severity | Precision |
| --- | ----------- | :----: | :--------: |
-|[Recursive functions](./java-kotlin/src/docs/security/Recursion/Recursion.md)|Detects possibly unbounded recursive calls|warning|low|
+|[Potentially unguarded protocol handler invocation](./java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
+|[Recursive functions](./java-kotlin/src/docs/security/Recursion/Recursion.md)|Detects recursive calls|warning|low|
## Query suites
diff --git a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
new file mode 100644
index 0000000..5e56eef
--- /dev/null
+++ b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -0,0 +1,87 @@
+# Potentially unguarded protocol handler invocation
+
+This query detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols. This vulnerability is related to CWE-939 (Improper Authorization in Handler for Custom URL Scheme) and aligns with CVE-2022-43550.
+
+When applications invoke protocol handlers (like `rundll32 url.dll,FileProtocolHandler` on Windows, `xdg-open` on Linux, `open` on macOS, or Qt's `QDesktopServices::openUrl()`), untrusted URLs can potentially trigger dangerous protocols such as `file://`, `smb://`, or other custom handlers that may lead to unauthorized file access, command execution, or other security issues.
+
+## Detected patterns
+
+The query identifies several common protocol handler invocation patterns:
+
+- **Windows**: `rundll32 url.dll,FileProtocolHandler` via system calls
+- **Linux**: `xdg-open` via system calls
+- **macOS**: `open` command via system calls
+- **Qt applications**: `QDesktopServices::openUrl()`
+
+## Recommendation
+
+Always validate URL schemes before passing them to protocol handlers. Only allow safe protocols like `http://` and `https://`. Reject or sanitize URLs containing potentially dangerous protocols.
+
+## Example
+
+The following vulnerable code passes untrusted input directly to a protocol handler:
+
+```cpp
+#include
+#include
+
+void openUserUrl(const QString& userInput) {
+ // VULNERABLE: No validation of the URL scheme
+ QDesktopServices::openUrl(QUrl(userInput));
+}
+```
+
+An attacker could provide a URL like `file:///etc/passwd` or `smb://attacker-server/share` to access unauthorized resources.
+
+The corrected version validates the URL scheme before opening:
+
+```cpp
+#include
+#include
+
+void openUserUrl(const QString& userInput) {
+ QUrl url(userInput);
+ QString scheme = url.scheme().toLower();
+
+ // Only allow safe protocols
+ if (scheme == "http" || scheme == "https") {
+ QDesktopServices::openUrl(url);
+ } else {
+ // Log error or show warning to user
+ qWarning() << "Rejected unsafe URL scheme:" << scheme;
+ }
+}
+```
+
+For system command invocations:
+
+```cpp
+void openUserUrlViaShell(const char* userInput) {
+ // VULNERABLE: Untrusted input passed to xdg-open
+ char cmd[512];
+ snprintf(cmd, sizeof(cmd), "xdg-open '%s'", userInput);
+ system(cmd);
+}
+```
+
+Should be corrected to validate the scheme:
+
+```cpp
+bool isValidScheme(const char* url) {
+ return (strncasecmp(url, "http://", 7) == 0 ||
+ strncasecmp(url, "https://", 8) == 0);
+}
+
+void openUserUrlViaShell(const char* userInput) {
+ if (isValidScheme(userInput)) {
+ char cmd[512];
+ snprintf(cmd, sizeof(cmd), "xdg-open '%s'", userInput);
+ system(cmd);
+ }
+}
+```
+
+## References
+
+- [CWE-939: Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
+- [CVE-2022-43550: USB Creator has insufficiently protected credentials](https://ubuntu.com/security/CVE-2022-43550)
diff --git a/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
new file mode 100644
index 0000000..445201d
--- /dev/null
+++ b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -0,0 +1,99 @@
+# Potentially unguarded protocol handler invocation
+
+This query detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols. This vulnerability is related to CWE-939 (Improper Authorization in Handler for Custom URL Scheme) and aligns with CVE-2022-43550.
+
+When Java applications invoke protocol handlers (like `Desktop.browse()`, `Runtime.exec()` with `xdg-open`/`open`, or `rundll32 url.dll,FileProtocolHandler` on Windows), untrusted URLs can potentially trigger dangerous protocols such as `file://`, `smb://`, or other custom handlers that may lead to unauthorized file access, command execution, or other security issues.
+
+## Detected patterns
+
+The query identifies several common protocol handler invocation patterns:
+
+- **Java AWT**: `Desktop.browse(URI)` - the platform-agnostic standard
+- **Windows**: `Runtime.exec()` with `rundll32 url.dll,FileProtocolHandler`
+- **Linux**: `Runtime.exec()` with `xdg-open`
+- **macOS**: `Runtime.exec()` with `open` command
+
+## Recommendation
+
+Always validate URL schemes before passing them to protocol handlers. Only allow safe protocols like `http://` and `https://`. Reject or sanitize URLs containing potentially dangerous protocols.
+
+## Example
+
+The following vulnerable code passes untrusted input directly to a protocol handler:
+
+```java
+import java.awt.Desktop;
+import java.net.URI;
+
+public class UrlOpener {
+ public void openUserUrl(String userInput) throws Exception {
+ // VULNERABLE: No validation of the URL scheme
+ Desktop.getDesktop().browse(new URI(userInput));
+ }
+}
+```
+
+An attacker could provide a URL like `file:///etc/passwd` or `smb://attacker-server/share` to access unauthorized resources.
+
+The corrected version validates the URL scheme before opening:
+
+```java
+import java.awt.Desktop;
+import java.net.URI;
+
+public class UrlOpener {
+ public void openUserUrl(String userInput) throws Exception {
+ URI uri = new URI(userInput);
+ String scheme = uri.getScheme();
+
+ // Only allow safe protocols
+ if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
+ Desktop.getDesktop().browse(uri);
+ } else {
+ throw new SecurityException("Rejected unsafe URL scheme: " + scheme);
+ }
+ }
+}
+```
+
+For system command invocations:
+
+```java
+public class UrlOpener {
+ public void openUserUrlViaShell(String userInput) throws Exception {
+ // VULNERABLE: Untrusted input passed to xdg-open
+ Runtime.getRuntime().exec(new String[]{"xdg-open", userInput});
+ }
+}
+```
+
+Should be corrected to validate the scheme:
+
+```java
+import java.net.URI;
+
+public class UrlOpener {
+ private boolean isValidScheme(String url) {
+ try {
+ URI uri = new URI(url);
+ String scheme = uri.getScheme();
+ return "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme);
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ public void openUserUrlViaShell(String userInput) throws Exception {
+ if (isValidScheme(userInput)) {
+ Runtime.getRuntime().exec(new String[]{"xdg-open", userInput});
+ } else {
+ throw new SecurityException("Invalid or unsafe URL scheme");
+ }
+ }
+}
+```
+
+## References
+
+- [CWE-939: Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
+- [CVE-2022-43550: USB Creator has insufficiently protected credentials](https://ubuntu.com/security/CVE-2022-43550)
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
index bc3c1da..de5b795 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref
@@ -1 +1 @@
-security/PotentiallyUnguardedProtocolhandler/PotentiallyUnguardedProtocolhandler.ql
+security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
From e0d42d1e1918800abcd4c775911181286064510e Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Mon, 6 Apr 2026 14:00:12 -0400
Subject: [PATCH 06/12] Fix up tests, and enhance query with better isBarrier
---
.../PotentiallyUnguardedProtocolHandler.ql | 153 ++++++++----------
cpp/test/include/libc/stdint.h | 2 +
cpp/test/include/libc/stdlib.h | 2 +
cpp/test/include/libc/string_stubs.h | 2 +
cpp/test/include/libc/unistd.h | 2 +
...tentiallyUnguardedProtocolHandler.expected | 8 +
.../test.cpp | 60 -------
.../PotentiallyUnguardedProtocolHandler.qhelp | 9 ++
8 files changed, 89 insertions(+), 149 deletions(-)
delete mode 100644 cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index 4a7113f..60fd383 100644
--- a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -15,110 +15,92 @@ import cpp
private import semmle.code.cpp.ir.dataflow.TaintTracking
private import semmle.code.cpp.security.FlowSources
private import semmle.code.cpp.security.CommandExecution
+private import semmle.code.cpp.controlflow.IRGuards
/**
- * Generic case: invoke protocol handling through OS's protocol handling utilities. This aligns with CVE-2022-43550.
+ * Holds when `call` invokes a system function (system, popen, exec*) with a command string
+ * that contains a URL protocol handler, `sink` is the argument carrying the URL, and
+ * `handlerType` describes which handler is invoked.
*/
-class ShellProtocolHandler extends SystemFunction {
- ShellProtocolHandler() {
- // Check if any calls to this function contain protocol handler invocations
- exists(FunctionCall call |
- call.getTarget() = this and
- exists(Expr arg |
- arg = call.getArgument(0).getAChild*() and
- exists(StringLiteral sl | sl = arg or sl.getParent*() = arg |
- sl.getValue()
- .regexpMatch("(?i).*(rundll32.*url\\.dll.*FileProtocolHandler|xdg-open|\\bopen\\b).*")
- )
- )
+predicate isShellProtocolHandlerSink(FunctionCall call, Expr sink, string handlerType) {
+ call.getTarget() instanceof SystemFunction and
+ exists(StringLiteral sl |
+ sl.getParent*() = call.getArgument(0) and
+ (
+ sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
+ handlerType = "rundll32 url.dll,FileProtocolHandler"
+ or
+ sl.getValue().regexpMatch("(?i).*xdg-open.*") and
+ handlerType = "xdg-open"
+ or
+ sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ handlerType = "open"
)
- }
-
- string getHandlerType() {
- exists(FunctionCall call, StringLiteral sl |
- call.getTarget() = this and
- sl.getParent*() = call.getArgument(0) and
- (
- sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
- result = "rundll32 url.dll,FileProtocolHandler"
- or
- sl.getValue().regexpMatch("(?i).*xdg-open.*") and
- result = "xdg-open"
- or
- sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
- result = "open"
- )
- )
- }
+ ) and
+ sink = call.getArgument(0)
}
/**
- * Qt's QDesktopServices::openUrl method
+ * Holds when `call` invokes QDesktopServices::openUrl and `sink` is the URL argument.
*/
-class QtProtocolHandler extends FunctionCall {
- QtProtocolHandler() { this.getTarget().hasQualifiedName("QDesktopServices", "openUrl") }
+predicate isQtProtocolHandlerSink(FunctionCall call, Expr sink) {
+ call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
+ sink = call.getArgument(0)
+}
- Expr getUrlArgument() { result = this.getArgument(0) }
+/**
+ * Gets the sink expression for a given DataFlow::Node matching either Qt or shell handler sinks.
+ */
+Expr getSinkExpr(DataFlow::Node sink) {
+ result = sink.asExpr() or
+ result = sink.asIndirectExpr()
}
/**
- * A sanitizer node that represents URL scheme validation
+ * Holds when `g` is a guard condition that validates the URL scheme of expression `e`.
+ * Flow is considered safe on the `branch` edge.
*/
-class UrlSchemeValidationSanitizer extends DataFlow::Node {
- UrlSchemeValidationSanitizer() {
- exists(FunctionCall fc |
- fc = this.asExpr() and
- (
- // String comparison on the untrusted URL
- fc.getTarget().getName() =
- [
- "strcmp", "strncmp", "strcasecmp", "strncasecmp", "strstr", "strcasestr", "_stricmp",
- "_strnicmp"
- ]
- or
- // Qt QUrl::scheme() comparison - QUrl::scheme() returns QString
- // Pattern: url.scheme() == "http" or url.scheme() == "https"
- exists(FunctionCall schemeCall |
- schemeCall.getTarget().hasQualifiedName("QUrl", "scheme") and
- (
- // Direct comparison
- fc.getTarget().hasName(["operator==", "operator!="]) and
- fc.getAnArgument() = schemeCall
- or
- // QString comparison methods
- fc = schemeCall and
- exists(FunctionCall qstringCmp |
- qstringCmp.getQualifier() = schemeCall and
- qstringCmp.getTarget().hasQualifiedName("QString", ["compare", "operator=="])
- )
- )
- )
- or
- // Qt QString startsWith check for direct URL strings
- fc.getTarget().hasQualifiedName("QString", "startsWith")
- )
+predicate urlSchemeGuardChecks(IRGuardCondition g, Expr e, boolean branch) {
+ branch = [true, false] and
+ exists(FunctionCall fc | g.getUnconvertedResultExpression().getAChild*() = fc |
+ // C string comparison on the URL (strcmp, strncmp, etc.)
+ fc.getTarget().getName() =
+ [
+ "strcmp", "strncmp", "strcasecmp", "strncasecmp", "strstr", "strcasestr", "_stricmp",
+ "_strnicmp"
+ ] and
+ e = fc.getAnArgument()
+ or
+ // Qt QString::startsWith check
+ fc.getTarget().hasQualifiedName("", "QString", "startsWith") and
+ e = fc.getQualifier()
+ or
+ // Qt QUrl::scheme() comparison
+ exists(FunctionCall schemeCall |
+ schemeCall.getTarget().hasQualifiedName("", "QUrl", "scheme") and
+ fc.getAnArgument() = schemeCall and
+ e = schemeCall.getQualifier()
)
- }
+ )
}
/**
- * Configuration for tracking untrusted data to protocol handler invocations
+ * Configuration for tracking untrusted data to protocol handler invocations.
*/
module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
predicate isSink(DataFlow::Node sink) {
- // QDesktopServices::openUrl()
- exists(QtProtocolHandler call | sink.asExpr() = call.getUrlArgument())
+ isQtProtocolHandlerSink(_, getSinkExpr(sink))
or
- // Shell protocol handlers (rundll32, xdg-open, open) via system()/popen()/exec*()
- exists(FunctionCall call |
- call.getTarget() instanceof ShellProtocolHandler and
- sink.asExpr() = call.getArgument(0)
- )
+ isShellProtocolHandlerSink(_, getSinkExpr(sink), _)
}
- predicate isBarrier(DataFlow::Node node) { node instanceof UrlSchemeValidationSanitizer }
+ predicate isBarrier(DataFlow::Node node) {
+ node = DataFlow::BarrierGuard::getABarrierNode()
+ or
+ node = DataFlow::BarrierGuard::getAnIndirectBarrierNode()
+ }
}
module PotentiallyUnguardedProtocolHandlerFlow =
@@ -132,17 +114,10 @@ from
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
(
- exists(QtProtocolHandler qtCall |
- call = qtCall and
- sink.getNode().asExpr() = qtCall.getUrlArgument() and
- callType = "QDesktopServices::openUrl()"
- )
+ isQtProtocolHandlerSink(call, getSinkExpr(sink.getNode())) and
+ callType = "QDesktopServices::openUrl()"
or
- exists(ShellProtocolHandler shellFunc |
- call.getTarget() = shellFunc and
- sink.getNode().asExpr() = call.getArgument(0) and
- callType = shellFunc.getHandlerType()
- )
+ isShellProtocolHandlerSink(call, getSinkExpr(sink.getNode()), callType)
)
select call, source, sink,
callType + " is called with untrusted input from $@ without proper URL scheme validation.",
diff --git a/cpp/test/include/libc/stdint.h b/cpp/test/include/libc/stdint.h
index d02afcd..12c4a23 100644
--- a/cpp/test/include/libc/stdint.h
+++ b/cpp/test/include/libc/stdint.h
@@ -6,6 +6,8 @@
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
+typedef unsigned long size_t;
+typedef long ssize_t;
#endif
#else // --- else USE_HEADERS
diff --git a/cpp/test/include/libc/stdlib.h b/cpp/test/include/libc/stdlib.h
index 7f28000..e35eb79 100644
--- a/cpp/test/include/libc/stdlib.h
+++ b/cpp/test/include/libc/stdlib.h
@@ -7,6 +7,8 @@
extern "C" {
#endif
+extern int system(const char *);
+
extern void *reallocf(void *, unsigned long);
int rand(void) {
diff --git a/cpp/test/include/libc/string_stubs.h b/cpp/test/include/libc/string_stubs.h
index d589139..c9ff6b1 100644
--- a/cpp/test/include/libc/string_stubs.h
+++ b/cpp/test/include/libc/string_stubs.h
@@ -28,6 +28,8 @@ extern int wprintf(const wchar_t * format, ...);
extern wchar_t* wcscpy(wchar_t * s1, const wchar_t * s2);
extern void perror(const char *s);
extern int puts(const char *s);
+extern int strcmp(const char *, const char *);
+extern int strncmp(const char *, const char *, size_t);
extern void openlog(const char*, int, int);
extern void syslog(int, const char*, ...);
diff --git a/cpp/test/include/libc/unistd.h b/cpp/test/include/libc/unistd.h
index 7163a23..16f2352 100644
--- a/cpp/test/include/libc/unistd.h
+++ b/cpp/test/include/libc/unistd.h
@@ -7,6 +7,8 @@
extern "C" {
#endif
+ssize_t read(int, void *, size_t);
+
void _exit(int);
#ifdef __cplusplus
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index e217064..672f879 100644
--- a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,4 +1,12 @@
edges
+| PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | provenance | |
+| PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | provenance | |
nodes
+| PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | semmle.label | *buf |
+| PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | semmle.label | *buf |
subpaths
#select
+| PotentiallyUnguardedProtocolHandler.cpp:28:3:28:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:42:5:42:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | this source |
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
deleted file mode 100644
index 4579758..0000000
--- a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/test.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-// Forward declarations - minimal to avoid system header dependencies
-extern "C" {
-int system(const char *);
-int sprintf(char *, const char *, ...);
-int strncmp(const char *, const char *, unsigned long);
-}
-
-// Mock QString class
-struct QString {
- const char *data;
- QString(const char *s) : data(s) {}
- bool operator==(const char *other) const;
-};
-
-// Mock Qt-like class for QDesktopServices::openUrl
-struct QUrl {
- const char *url;
- QUrl(const char *s) : url(s) {}
- QString scheme() const;
- bool startsWith(const char *prefix) const;
-};
-
-struct QDesktopServices {
- static bool openUrl(const QUrl &url);
-};
-
-// Untrusted input sources
-extern "C" char *getUserInput();
-extern "C" const char *getUrlParam();
-
-// BAD: QDesktopServices::openUrl with untrusted input
-void bad1_qt(const char *userUrl) {
- QUrl url(userUrl);
- QDesktopServices::openUrl(url); // BAD
-}
-
-void bad2_qt() {
- const char *input = getUrlParam();
- QUrl url(input);
- QDesktopServices::openUrl(url); // BAD
-}
-
-void safe1_qt() {
- QUrl url("https://example.com");
- QDesktopServices::openUrl(url); // GOOD - no taint
-}
-
-void safe2_qt(const char *userUrl) {
- if (strncmp(userUrl, "https://", 8) == 0 ||
- strncmp(userUrl, "http://", 7) == 0) {
- QUrl url(userUrl);
- QDesktopServices::openUrl(url); // GOOD
- }
-}
-
-void safe3_qt(QUrl &url) {
- if (url.scheme() == "https" || url.scheme() == "http") {
- QDesktopServices::openUrl(url); // GOOD
- }
-}
\ No newline at end of file
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
index 1e63ba4..b5495da 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -14,6 +14,15 @@
they come from untrusted sources.
+
+
+
+ In the bad example, an untrusted URL is passed directly to Desktop.browse()
+ without any scheme validation.
+ In the good example, the URL scheme is checked with getScheme() before
+ invoking the handler.
+
+
Positive Security: Allow
From 013958180845acc96105e3eb9a4fac6f9a397b2f Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 15 Apr 2026 13:31:32 -0400
Subject: [PATCH 07/12] Fix up tests
---
.../PotentiallyUnguardedProtocolHandler.cpp | 55 +++++++++++++++++++
...tentiallyUnguardedProtocolHandler.expected | 28 +++++++---
...tentiallyUnguardedProtocolHandler.expected | 12 ++--
3 files changed, 81 insertions(+), 14 deletions(-)
create mode 100644 cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
new file mode 100644
index 0000000..9eee1a6
--- /dev/null
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
@@ -0,0 +1,55 @@
+// Forward declarations - minimal stubs
+extern "C" {
+int read(int fd, void *buf, unsigned long count);
+int system(const char *);
+int sprintf(char *, const char *, ...);
+int strncmp(const char *, const char *, unsigned long);
+}
+
+struct QString {
+ const char *data;
+ QString(const char *s) : data(s) {}
+ bool operator==(const char *other) const;
+ bool startsWith(const char *prefix) const;
+};
+
+struct QUrl {
+ const char *url;
+ QUrl(const char *s) : url(s) {}
+ QString scheme() const;
+};
+
+struct QDesktopServices {
+ static bool openUrl(const QUrl &url);
+};
+
+// BAD: read() into buffer, then pass to openUrl without scheme check
+void bad1_qt_read() {
+ char buf[1024];
+ read(0, buf, sizeof(buf));
+ QDesktopServices::openUrl(QUrl(buf)); // BAD
+}
+
+// BAD: read() into buffer, intermediate usage, then openUrl without guard
+void bad2_qt_read_indirect() {
+ char buf[1024];
+ char url[2048];
+ read(0, buf, sizeof(buf));
+ sprintf(url, "file://%s", buf);
+ // no scheme check before opening
+ QDesktopServices::openUrl(QUrl(url)); // BAD
+}
+
+// GOOD: hardcoded URL
+void safe1_qt_hardcoded() {
+ QDesktopServices::openUrl(QUrl("https://example.com")); // GOOD
+}
+
+// GOOD: scheme check before openUrl
+void safe2_qt_scheme_check() {
+ char buf[1024];
+ read(0, buf, sizeof(buf));
+ if (strncmp(buf, "https://", 8) == 0) {
+ QDesktopServices::openUrl(QUrl(buf)); // GOOD
+ }
+}
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index 672f879..18f2338 100644
--- a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,12 +1,24 @@
edges
-| PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | provenance | |
-| PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | provenance | |
+| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | |
+| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | |
nodes
-| PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | semmle.label | read output argument |
-| PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | semmle.label | *buf |
-| PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | semmle.label | read output argument |
-| PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | semmle.label | *buf |
+| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
subpaths
#select
-| PotentiallyUnguardedProtocolHandler.cpp:28:3:28:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:28:29:28:31 | *buf | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:27:11:27:13 | read output argument | this source |
-| PotentiallyUnguardedProtocolHandler.cpp:42:5:42:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:42:31:42:33 | *buf | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:39:11:39:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:30:3:30:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:30:3:30:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:40:3:40:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:40:3:40:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index 14f3d34..a86b07e 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,11 +1,11 @@
edges
-| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46190 |
-| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44428 Sink:MaD:43969 |
-| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46190 |
-| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46220 |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44428 Sink:MaD:43959 |
+| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46220 |
+| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43959 |
| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44428 |
-| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46190 |
-| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43969 |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46220 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43959 |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
From 5fb469347934dc69b214d307106df7e47a2066d9 Mon Sep 17 00:00:00 2001
From: alan <17283637+ex0dus-0x@users.noreply.github.com>
Date: Wed, 15 Apr 2026 15:39:18 -0400
Subject: [PATCH 08/12] fix stubs to pass test
---
cpp/test/include/libc/stdint.h | 7 +++++++
cpp/test/include/libc/string_stubs.h | 5 +++++
cpp/test/include/libc/unistd.h | 10 ++++++++++
3 files changed, 22 insertions(+)
diff --git a/cpp/test/include/libc/stdint.h b/cpp/test/include/libc/stdint.h
index 12c4a23..3e80098 100644
--- a/cpp/test/include/libc/stdint.h
+++ b/cpp/test/include/libc/stdint.h
@@ -6,8 +6,15 @@
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
+#ifndef _SIZE_T_DEFINED
+#define _SIZE_T_DEFINED
typedef unsigned long size_t;
+#endif
+
+#ifndef _SSIZE_T_DEFINED
+#define _SSIZE_T_DEFINED
typedef long ssize_t;
+#endif
#endif
#else // --- else USE_HEADERS
diff --git a/cpp/test/include/libc/string_stubs.h b/cpp/test/include/libc/string_stubs.h
index c9ff6b1..58ab8eb 100644
--- a/cpp/test/include/libc/string_stubs.h
+++ b/cpp/test/include/libc/string_stubs.h
@@ -15,6 +15,11 @@ extern "C" {
typedef int wchar_t;
#endif
+#ifndef _SIZE_T_DEFINED
+#define _SIZE_T_DEFINED
+typedef unsigned long size_t;
+#endif
+
extern void *memcpy(void *dst, const void *src, unsigned long n);
extern char* strcpy_s(char* dst, int max_amount, char* src);
extern int _mbsncat(char* dst, char* src, int count);
diff --git a/cpp/test/include/libc/unistd.h b/cpp/test/include/libc/unistd.h
index 16f2352..13893fc 100644
--- a/cpp/test/include/libc/unistd.h
+++ b/cpp/test/include/libc/unistd.h
@@ -3,6 +3,16 @@
#ifndef HEADER_UNISTD_STUB_H
#define HEADER_UNISTD_STUB_H
+#ifndef _SIZE_T_DEFINED
+#define _SIZE_T_DEFINED
+typedef unsigned long size_t;
+#endif
+
+#ifndef _SSIZE_T_DEFINED
+#define _SSIZE_T_DEFINED
+typedef long ssize_t;
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
From 4f7ae915347a49d116313a3f07285ba1e9d68ac6 Mon Sep 17 00:00:00 2001
From: AkshayK
Date: Thu, 18 Jun 2026 08:05:39 -0400
Subject: [PATCH 09/12] [fix] update MaD indices due to pinned version
---
.../PotentiallyUnguardedProtocolHandler.expected | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index a86b07e..ad2c5b3 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,12 +1,12 @@
edges
-| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46220 |
-| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44428 Sink:MaD:43959 |
-| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46220 |
-| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43959 |
-| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44428 |
-| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46220 |
-| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43959 |
-| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44428 |
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44464 Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44464 |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44464 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
From 6d3eef8f97f9928e4fa696ad82b21a22edf92eaa Mon Sep 17 00:00:00 2001
From: AkshayK
Date: Thu, 18 Jun 2026 08:21:14 -0400
Subject: [PATCH 10/12] [fix]regenerate doc/QUERIES.md for new protocol handler
queries
---
doc/QUERIES.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/doc/QUERIES.md b/doc/QUERIES.md
index 7971538..501e3b7 100644
--- a/doc/QUERIES.md
+++ b/doc/QUERIES.md
@@ -31,6 +31,7 @@
|[Invalid string size passed to string manipulation function](../cpp/src/docs/security/CStrnFinder/CStrnFinder.md)|Finds calls to functions that take as input a string and its size as separate arguments (e.g., `strncmp`, `strncat`, ...) and the size argument is wrong|error|low|
|[Iterator invalidation](../cpp/src/docs/security/IteratorInvalidation/IteratorInvalidation.md)|Modifying a container while iterating over it can invalidate iterators, leading to undefined behavior.|warning|medium|
|[Missing null terminator](../cpp/src/docs/security/NoNullTerminator/NoNullTerminator.md)|This query finds incorrectly initialized strings that are passed to functions expecting null-byte-terminated strings|error|high|
+|[Potentially unguarded protocol handler invocation](../cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
### Go
@@ -54,5 +55,6 @@
| Name | Description | Severity | Precision |
| --- | ----------- | :----: | :--------: |
+|[Potentially unguarded protocol handler invocation](../java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
|[Recursive functions](../java/src/docs/security/Recursion/Recursion.md)|Detects possibly unbounded recursive calls|warning|low|
From 97279d1abf5d349808f25f62be2a688143cb1b78 Mon Sep 17 00:00:00 2001
From: GrosQuildu
Date: Tue, 10 Nov 2026 16:02:32 +0100
Subject: [PATCH 11/12] Fix protocol handler queries and tests
Restrict scheme guards to successful HTTP and HTTPS validation. Model composed C++ commands and Java Runtime and ProcessBuilder calls. Add focused positive and negative tests plus query help.
---
.../PotentiallyUnguardedProtocolHandler.md | 80 +-----
.../PotentiallyUnguardedProtocolHandler.cpp | 13 +
.../PotentiallyUnguardedProtocolHandler.qhelp | 33 +++
.../PotentiallyUnguardedProtocolHandler.ql | 157 +++++++-----
cpp/test/include/libc/stdint.h | 11 +-
cpp/test/include/libc/stdlib.h | 1 -
cpp/test/include/libc/string_stubs.h | 9 +-
cpp/test/include/libc/unistd.h | 14 +-
.../PotentiallyUnguardedProtocolHandler.cpp | 123 ++++++---
...tentiallyUnguardedProtocolHandler.expected | 48 ++--
doc/QUERIES.md | 4 +-
.../PotentiallyUnguardedProtocolHandler.md | 96 ++-----
.../PotentiallyUnguardedProtocolHandler.java | 21 ++
.../PotentiallyUnguardedProtocolHandler.qhelp | 30 ++-
.../PotentiallyUnguardedProtocolHandler.ql | 234 ++++++++++--------
...tentiallyUnguardedProtocolHandler.expected | 58 +++--
.../PotentiallyUnguardedProtocolHandler.java | 112 +++++----
17 files changed, 560 insertions(+), 484 deletions(-)
create mode 100644 cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
create mode 100644 cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
create mode 100644 java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
diff --git a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
index 5e56eef..41e2d8b 100644
--- a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
+++ b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -1,87 +1,33 @@
# Potentially unguarded protocol handler invocation
+Desktop URL handlers accept every scheme registered with the operating system. Passing an untrusted URL to one of these handlers can invoke an unintended application or expose local resources.
-This query detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols. This vulnerability is related to CWE-939 (Improper Authorization in Handler for Custom URL Scheme) and aligns with CVE-2022-43550.
-
-When applications invoke protocol handlers (like `rundll32 url.dll,FileProtocolHandler` on Windows, `xdg-open` on Linux, `open` on macOS, or Qt's `QDesktopServices::openUrl()`), untrusted URLs can potentially trigger dangerous protocols such as `file://`, `smb://`, or other custom handlers that may lead to unauthorized file access, command execution, or other security issues.
-
-## Detected patterns
-
-The query identifies several common protocol handler invocation patterns:
-
-- **Windows**: `rundll32 url.dll,FileProtocolHandler` via system calls
-- **Linux**: `xdg-open` via system calls
-- **macOS**: `open` command via system calls
-- **Qt applications**: `QDesktopServices::openUrl()`
## Recommendation
+Parse the URL and allow only the schemes required by the application before invoking the protocol handler. For web links, allow only HTTP and HTTPS.
-Always validate URL schemes before passing them to protocol handlers. Only allow safe protocols like `http://` and `https://`. Reject or sanitize URLs containing potentially dangerous protocols.
## Example
-The following vulnerable code passes untrusted input directly to a protocol handler:
-
```cpp
#include
#include
-void openUserUrl(const QString& userInput) {
- // VULNERABLE: No validation of the URL scheme
- QDesktopServices::openUrl(QUrl(userInput));
+void openUrl(const char *input) {
+ QDesktopServices::openUrl(QUrl(input)); // BAD
}
-```
-
-An attacker could provide a URL like `file:///etc/passwd` or `smb://attacker-server/share` to access unauthorized resources.
-
-The corrected version validates the URL scheme before opening:
-
-```cpp
-#include
-#include
-void openUserUrl(const QString& userInput) {
- QUrl url(userInput);
- QString scheme = url.scheme().toLower();
-
- // Only allow safe protocols
- if (scheme == "http" || scheme == "https") {
- QDesktopServices::openUrl(url);
- } else {
- // Log error or show warning to user
- qWarning() << "Rejected unsafe URL scheme:" << scheme;
- }
+void openWebUrl(const char *input) {
+ QUrl url(input);
+ if (url.scheme() == "https") {
+ QDesktopServices::openUrl(url); // GOOD
+ }
}
-```
-
-For system command invocations:
-```cpp
-void openUserUrlViaShell(const char* userInput) {
- // VULNERABLE: Untrusted input passed to xdg-open
- char cmd[512];
- snprintf(cmd, sizeof(cmd), "xdg-open '%s'", userInput);
- system(cmd);
-}
```
+The bad example opens an untrusted URL directly. The good example first checks that the URL uses HTTPS.
-Should be corrected to validate the scheme:
-
-```cpp
-bool isValidScheme(const char* url) {
- return (strncasecmp(url, "http://", 7) == 0 ||
- strncasecmp(url, "https://", 8) == 0);
-}
-
-void openUserUrlViaShell(const char* userInput) {
- if (isValidScheme(userInput)) {
- char cmd[512];
- snprintf(cmd, sizeof(cmd), "xdg-open '%s'", userInput);
- system(cmd);
- }
-}
-```
## References
-
-- [CWE-939: Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
-- [CVE-2022-43550: USB Creator has insufficiently protected credentials](https://ubuntu.com/security/CVE-2022-43550)
+* CWE-939: [Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
+* CVE-2022-43550: [Jitsi Desktop Client RCE by interacting with malicious URL schemes](https://hackerone.com/reports/1692603)
+* Common Weakness Enumeration: [CWE-939](https://cwe.mitre.org/data/definitions/939.html).
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
new file mode 100644
index 0000000..252e962
--- /dev/null
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
@@ -0,0 +1,13 @@
+#include
+#include
+
+void openUrl(const char *input) {
+ QDesktopServices::openUrl(QUrl(input)); // BAD
+}
+
+void openWebUrl(const char *input) {
+ QUrl url(input);
+ if (url.scheme() == "https") {
+ QDesktopServices::openUrl(url); // GOOD
+ }
+}
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
new file mode 100644
index 0000000..aa85290
--- /dev/null
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -0,0 +1,33 @@
+
+
+
+
+ Desktop URL handlers accept every scheme registered with the operating system.
+ Passing an untrusted URL to one of these handlers can invoke an unintended
+ application or expose local resources.
+
+
+
+
+ Parse the URL and allow only the schemes required by the application before
+ invoking the protocol handler. For web links, allow only HTTP and HTTPS.
+
+
+
+
+
+ The bad example opens an untrusted URL directly. The good example first
+ checks that the URL uses HTTPS.
+
+
+
+
+ CWE-939: Improper
+ Authorization in Handler for Custom URL Scheme
+
+
+ CVE-2022-43550: Jitsi Desktop
+ Client RCE by interacting with malicious URL schemes
+
+
+
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index 60fd383..25616ab 100644
--- a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -1,7 +1,7 @@
/**
* @name Potentially unguarded protocol handler invocation
* @id tob/cpp/unguarded-protocol-handler
- * @description Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols
+ * @description Finds URL protocol handlers called with URLs from untrusted sources
* @kind path-problem
* @tags security
* external/cwe/cwe-939
@@ -12,88 +12,109 @@
*/
import cpp
+private import semmle.code.cpp.controlflow.IRGuards
private import semmle.code.cpp.ir.dataflow.TaintTracking
-private import semmle.code.cpp.security.FlowSources
private import semmle.code.cpp.security.CommandExecution
-private import semmle.code.cpp.controlflow.IRGuards
+private import semmle.code.cpp.security.FlowSources
-/**
- * Holds when `call` invokes a system function (system, popen, exec*) with a command string
- * that contains a URL protocol handler, `sink` is the argument carrying the URL, and
- * `handlerType` describes which handler is invoked.
- */
-predicate isShellProtocolHandlerSink(FunctionCall call, Expr sink, string handlerType) {
+predicate isProtocolHandlerLiteral(StringLiteral literal, string handlerType) {
+ literal.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
+ handlerType = "rundll32 url.dll,FileProtocolHandler"
+ or
+ literal.getValue().regexpMatch("(?i).*xdg-open.*") and
+ handlerType = "xdg-open"
+ or
+ literal.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ not literal.getValue().regexpMatch("(?i).*xdg-open.*") and
+ handlerType = "open"
+}
+
+predicate localTaint(Expr source, Expr sink) {
+ exists(DataFlow::Node sourceNode, DataFlow::Node sinkNode |
+ source = [sourceNode.asExpr(), sourceNode.asIndirectExpr()] and
+ sink = [sinkNode.asExpr(), sinkNode.asIndirectExpr()] and
+ TaintTracking::localTaint(sourceNode, sinkNode)
+ )
+}
+
+predicate isShellProtocolHandlerCall(FunctionCall call, string handlerType) {
call.getTarget() instanceof SystemFunction and
- exists(StringLiteral sl |
- sl.getParent*() = call.getArgument(0) and
+ exists(StringLiteral literal |
+ isProtocolHandlerLiteral(literal, handlerType) and
+ localTaint(literal, call.getArgument(0))
+ )
+}
+
+predicate isAllowedScheme(StringLiteral literal, Expr length) {
+ literal.getValue() = "http://" and length.getValue() = "7"
+ or
+ literal.getValue() = "https://" and length.getValue() = "8"
+}
+
+predicate comparisonChecksSuccess(IRGuardCondition guard, FunctionCall call, boolean branch) {
+ exists(EqualityOperation comparison |
+ comparison = guard.getUnconvertedResultExpression() and
+ comparison.getAnOperand() = call and
+ comparison.getAnOperand().getValue() = "0" and
(
- sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
- handlerType = "rundll32 url.dll,FileProtocolHandler"
- or
- sl.getValue().regexpMatch("(?i).*xdg-open.*") and
- handlerType = "xdg-open"
+ comparison instanceof EQExpr and branch = true
or
- sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
- handlerType = "open"
+ comparison instanceof NEExpr and branch = false
)
- ) and
- sink = call.getArgument(0)
-}
-
-/**
- * Holds when `call` invokes QDesktopServices::openUrl and `sink` is the URL argument.
- */
-predicate isQtProtocolHandlerSink(FunctionCall call, Expr sink) {
- call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
- sink = call.getArgument(0)
+ )
}
-/**
- * Gets the sink expression for a given DataFlow::Node matching either Qt or shell handler sinks.
- */
-Expr getSinkExpr(DataFlow::Node sink) {
- result = sink.asExpr() or
- result = sink.asIndirectExpr()
+predicate stringPrefixGuard(IRGuardCondition guard, Expr checked, boolean branch) {
+ exists(FunctionCall call, StringLiteral allowed, Expr length |
+ call.getTarget().getName() = ["strncmp", "strncasecmp", "_strnicmp"] and
+ isAllowedScheme(allowed, length) and
+ call.getArgument(2) = length and
+ (
+ call.getArgument(0) = checked and call.getArgument(1) = allowed
+ or
+ call.getArgument(1) = checked and call.getArgument(0) = allowed
+ ) and
+ comparisonChecksSuccess(guard, call, branch)
+ )
+ or
+ exists(FunctionCall call, StringLiteral allowed |
+ call = guard.getUnconvertedResultExpression() and
+ call.getTarget().hasQualifiedName("", "QString", "startsWith") and
+ call.getArgument(0) = allowed and
+ allowed.getValue() = ["http://", "https://"] and
+ checked = call.getQualifier() and
+ branch = true
+ )
}
-/**
- * Holds when `g` is a guard condition that validates the URL scheme of expression `e`.
- * Flow is considered safe on the `branch` edge.
- */
-predicate urlSchemeGuardChecks(IRGuardCondition g, Expr e, boolean branch) {
- branch = [true, false] and
- exists(FunctionCall fc | g.getUnconvertedResultExpression().getAChild*() = fc |
- // C string comparison on the URL (strcmp, strncmp, etc.)
- fc.getTarget().getName() =
- [
- "strcmp", "strncmp", "strcasecmp", "strncasecmp", "strstr", "strcasestr", "_stricmp",
- "_strnicmp"
- ] and
- e = fc.getAnArgument()
- or
- // Qt QString::startsWith check
- fc.getTarget().hasQualifiedName("", "QString", "startsWith") and
- e = fc.getQualifier()
- or
- // Qt QUrl::scheme() comparison
- exists(FunctionCall schemeCall |
- schemeCall.getTarget().hasQualifiedName("", "QUrl", "scheme") and
- fc.getAnArgument() = schemeCall and
- e = schemeCall.getQualifier()
- )
+predicate urlSchemeGuardChecks(IRGuardCondition guard, Expr checked, boolean branch) {
+ stringPrefixGuard(guard, checked, branch)
+ or
+ exists(FunctionCall comparison, FunctionCall scheme, StringLiteral allowed |
+ comparison = guard.getUnconvertedResultExpression() and
+ comparison.getTarget().getName() = "operator==" and
+ scheme.getTarget().hasQualifiedName("", "QUrl", "scheme") and
+ scheme = comparison.getQualifier() and
+ comparison.getArgument(0) = allowed and
+ allowed.getValue() = ["http", "https"] and
+ checked = scheme.getQualifier() and
+ branch = true
)
}
-/**
- * Configuration for tracking untrusted data to protocol handler invocations.
- */
module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
predicate isSink(DataFlow::Node sink) {
- isQtProtocolHandlerSink(_, getSinkExpr(sink))
+ exists(FunctionCall call |
+ call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
+ sink.asConvertedExpr() = call.getArgument(0)
+ )
or
- isShellProtocolHandlerSink(_, getSinkExpr(sink), _)
+ exists(FunctionCall call |
+ isShellProtocolHandlerCall(call, _) and
+ sink.asIndirectExpr() = call.getArgument(0)
+ )
}
predicate isBarrier(DataFlow::Node node) {
@@ -114,11 +135,13 @@ from
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
(
- isQtProtocolHandlerSink(call, getSinkExpr(sink.getNode())) and
+ call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and
+ sink.getNode().asConvertedExpr() = call.getArgument(0) and
callType = "QDesktopServices::openUrl()"
or
- isShellProtocolHandlerSink(call, getSinkExpr(sink.getNode()), callType)
+ isShellProtocolHandlerCall(call, callType) and
+ sink.getNode().asIndirectExpr() = call.getArgument(0)
)
select call, source, sink,
- callType + " is called with untrusted input from $@ without proper URL scheme validation.",
+ callType + " is called with untrusted input from $@ without URL scheme validation.",
source.getNode(), "this source"
diff --git a/cpp/test/include/libc/stdint.h b/cpp/test/include/libc/stdint.h
index 3e80098..4965097 100644
--- a/cpp/test/include/libc/stdint.h
+++ b/cpp/test/include/libc/stdint.h
@@ -6,19 +6,10 @@
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
-#ifndef _SIZE_T_DEFINED
-#define _SIZE_T_DEFINED
-typedef unsigned long size_t;
-#endif
-
-#ifndef _SSIZE_T_DEFINED
-#define _SSIZE_T_DEFINED
-typedef long ssize_t;
-#endif
#endif
#else // --- else USE_HEADERS
#include
-#endif // --- end USE_HEADERS
\ No newline at end of file
+#endif // --- end USE_HEADERS
diff --git a/cpp/test/include/libc/stdlib.h b/cpp/test/include/libc/stdlib.h
index e35eb79..226da84 100644
--- a/cpp/test/include/libc/stdlib.h
+++ b/cpp/test/include/libc/stdlib.h
@@ -8,7 +8,6 @@ extern "C" {
#endif
extern int system(const char *);
-
extern void *reallocf(void *, unsigned long);
int rand(void) {
diff --git a/cpp/test/include/libc/string_stubs.h b/cpp/test/include/libc/string_stubs.h
index 58ab8eb..74804cb 100644
--- a/cpp/test/include/libc/string_stubs.h
+++ b/cpp/test/include/libc/string_stubs.h
@@ -15,11 +15,6 @@ extern "C" {
typedef int wchar_t;
#endif
-#ifndef _SIZE_T_DEFINED
-#define _SIZE_T_DEFINED
-typedef unsigned long size_t;
-#endif
-
extern void *memcpy(void *dst, const void *src, unsigned long n);
extern char* strcpy_s(char* dst, int max_amount, char* src);
extern int _mbsncat(char* dst, char* src, int count);
@@ -33,8 +28,8 @@ extern int wprintf(const wchar_t * format, ...);
extern wchar_t* wcscpy(wchar_t * s1, const wchar_t * s2);
extern void perror(const char *s);
extern int puts(const char *s);
-extern int strcmp(const char *, const char *);
-extern int strncmp(const char *, const char *, size_t);
+extern int sprintf(char *, const char *, ...);
+extern int strncmp(const char *, const char *, unsigned long);
extern void openlog(const char*, int, int);
extern void syslog(int, const char*, ...);
diff --git a/cpp/test/include/libc/unistd.h b/cpp/test/include/libc/unistd.h
index 13893fc..6ed9ed4 100644
--- a/cpp/test/include/libc/unistd.h
+++ b/cpp/test/include/libc/unistd.h
@@ -3,21 +3,11 @@
#ifndef HEADER_UNISTD_STUB_H
#define HEADER_UNISTD_STUB_H
-#ifndef _SIZE_T_DEFINED
-#define _SIZE_T_DEFINED
-typedef unsigned long size_t;
-#endif
-
-#ifndef _SSIZE_T_DEFINED
-#define _SSIZE_T_DEFINED
-typedef long ssize_t;
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
-ssize_t read(int, void *, size_t);
+long read(int, void *, unsigned long);
void _exit(int);
@@ -31,4 +21,4 @@ void _exit(int);
#include
-#endif // --- end USE_HEADERS
\ No newline at end of file
+#endif // --- end USE_HEADERS
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
index 9eee1a6..0316be7 100644
--- a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp
@@ -1,21 +1,15 @@
-// Forward declarations - minimal stubs
-extern "C" {
-int read(int fd, void *buf, unsigned long count);
-int system(const char *);
-int sprintf(char *, const char *, ...);
-int strncmp(const char *, const char *, unsigned long);
-}
+#include "../../../include/libc/stdlib.h"
+#include "../../../include/libc/string_stubs.h"
+#include "../../../include/libc/unistd.h"
struct QString {
- const char *data;
- QString(const char *s) : data(s) {}
+ QString(const char *value);
bool operator==(const char *other) const;
bool startsWith(const char *prefix) const;
};
struct QUrl {
- const char *url;
- QUrl(const char *s) : url(s) {}
+ QUrl(const char *value);
QString scheme() const;
};
@@ -23,33 +17,96 @@ struct QDesktopServices {
static bool openUrl(const QUrl &url);
};
-// BAD: read() into buffer, then pass to openUrl without scheme check
-void bad1_qt_read() {
- char buf[1024];
- read(0, buf, sizeof(buf));
- QDesktopServices::openUrl(QUrl(buf)); // BAD
-}
-
-// BAD: read() into buffer, intermediate usage, then openUrl without guard
-void bad2_qt_read_indirect() {
- char buf[1024];
- char url[2048];
- read(0, buf, sizeof(buf));
- sprintf(url, "file://%s", buf);
- // no scheme check before opening
+void bad_qt() {
+ char url[1024];
+ read(0, url, sizeof(url));
QDesktopServices::openUrl(QUrl(url)); // BAD
}
-// GOOD: hardcoded URL
-void safe1_qt_hardcoded() {
+void good_qt_hardcoded() {
QDesktopServices::openUrl(QUrl("https://example.com")); // GOOD
}
-// GOOD: scheme check before openUrl
-void safe2_qt_scheme_check() {
- char buf[1024];
- read(0, buf, sizeof(buf));
- if (strncmp(buf, "https://", 8) == 0) {
- QDesktopServices::openUrl(QUrl(buf)); // GOOD
+void good_qt_prefix_check() {
+ char url[1024];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "https://", 8) == 0) {
+ QDesktopServices::openUrl(QUrl(url)); // GOOD
+ }
+}
+
+void good_qt_reject_other_prefix() {
+ char url[1024];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "https://", 8) != 0) {
+ return;
+ }
+ QDesktopServices::openUrl(QUrl(url)); // GOOD
+}
+
+void good_qt_scheme_check() {
+ char value[1024];
+ read(0, value, sizeof(value));
+ QUrl url(value);
+ if (url.scheme() == "https") {
+ QDesktopServices::openUrl(url); // GOOD
+ }
+}
+
+void bad_qt_file_scheme() {
+ char url[1024];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "file://", 7) == 0) {
+ QDesktopServices::openUrl(QUrl(url)); // BAD
+ }
+}
+
+void bad_qt_partial_prefix() {
+ char url[1024];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "https://", 4) == 0) {
+ QDesktopServices::openUrl(QUrl(url)); // BAD
+ }
+}
+
+void bad_qt_rejected_scheme() {
+ char url[1024];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "https://", 8) != 0) {
+ QDesktopServices::openUrl(QUrl(url)); // BAD
+ }
+}
+
+void bad_shell() {
+ char url[1024];
+ char command[2048];
+ read(0, url, sizeof(url));
+ sprintf(command, "xdg-open '%s'", url);
+ system(command); // BAD
+}
+
+void bad_shell_rundll() {
+ char url[1024];
+ char command[2048];
+ read(0, url, sizeof(url));
+ sprintf(command, "rundll32 url.dll,FileProtocolHandler '%s'", url);
+ system(command); // BAD
+}
+
+void bad_shell_open() {
+ char url[1024];
+ char command[2048];
+ read(0, url, sizeof(url));
+ sprintf(command, "open '%s'", url);
+ system(command); // BAD
+}
+
+void good_shell() {
+ char url[1024];
+ char command[2048];
+ read(0, url, sizeof(url));
+ if (strncmp(url, "https://", 8) == 0) {
+ sprintf(command, "xdg-open '%s'", url);
+ system(command); // GOOD
}
}
diff --git a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index 18f2338..dd1404b 100644
--- a/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,24 +1,32 @@
edges
-| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | provenance | |
-| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | TaintFunction |
-| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | provenance | |
+| PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | provenance | TaintFunction |
+| PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | provenance | TaintFunction |
nodes
-| PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | semmle.label | read output argument |
-| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
-| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
-| PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | semmle.label | call to QUrl |
-| PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | semmle.label | read output argument |
-| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
-| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
-| PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | semmle.label | call to QUrl |
+| PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | semmle.label | *command |
+| PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | semmle.label | *command |
+| PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | semmle.label | read output argument |
+| PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | semmle.label | *command |
subpaths
#select
-| PotentiallyUnguardedProtocolHandler.cpp:30:3:30:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | this source |
-| PotentiallyUnguardedProtocolHandler.cpp:30:3:30:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:30:29:30:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:29:11:29:13 | read output argument | this source |
-| PotentiallyUnguardedProtocolHandler.cpp:40:3:40:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | this source |
-| PotentiallyUnguardedProtocolHandler.cpp:40:3:40:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:40:29:40:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:37:11:37:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:23:3:23:27 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:23:29:23:37 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:22:11:22:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:60:5:60:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:60:31:60:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:58:11:58:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:68:5:68:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:68:31:68:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:66:11:66:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:76:5:76:29 | call to openUrl | PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:76:31:76:39 | call to QUrl | QDesktopServices::openUrl() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:74:11:74:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:85:3:85:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:85:10:85:16 | *command | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:83:11:83:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:93:3:93:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:93:10:93:16 | *command | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:91:11:91:13 | read output argument | this source |
+| PotentiallyUnguardedProtocolHandler.cpp:101:3:101:8 | call to system | PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | PotentiallyUnguardedProtocolHandler.cpp:101:10:101:16 | *command | open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.cpp:99:11:99:13 | read output argument | this source |
diff --git a/doc/QUERIES.md b/doc/QUERIES.md
index 501e3b7..d3e8d5a 100644
--- a/doc/QUERIES.md
+++ b/doc/QUERIES.md
@@ -31,7 +31,7 @@
|[Invalid string size passed to string manipulation function](../cpp/src/docs/security/CStrnFinder/CStrnFinder.md)|Finds calls to functions that take as input a string and its size as separate arguments (e.g., `strncmp`, `strncat`, ...) and the size argument is wrong|error|low|
|[Iterator invalidation](../cpp/src/docs/security/IteratorInvalidation/IteratorInvalidation.md)|Modifying a container while iterating over it can invalidate iterators, leading to undefined behavior.|warning|medium|
|[Missing null terminator](../cpp/src/docs/security/NoNullTerminator/NoNullTerminator.md)|This query finds incorrectly initialized strings that are passed to functions expecting null-byte-terminated strings|error|high|
-|[Potentially unguarded protocol handler invocation](../cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
+|[Potentially unguarded protocol handler invocation](../cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Finds URL protocol handlers called with URLs from untrusted sources|warning|medium|
### Go
@@ -55,6 +55,6 @@
| Name | Description | Severity | Precision |
| --- | ----------- | :----: | :--------: |
-|[Potentially unguarded protocol handler invocation](../java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols|warning|medium|
+|[Potentially unguarded protocol handler invocation](../java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md)|Finds URL protocol handlers called with URLs from untrusted sources|warning|medium|
|[Recursive functions](../java/src/docs/security/Recursion/Recursion.md)|Detects possibly unbounded recursive calls|warning|low|
diff --git a/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
index 445201d..aa2d007 100644
--- a/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
+++ b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -1,99 +1,41 @@
# Potentially unguarded protocol handler invocation
+Desktop URL handlers accept every scheme registered with the operating system. Passing an untrusted URL to one of these handlers can invoke an unintended application or expose local resources.
-This query detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols. This vulnerability is related to CWE-939 (Improper Authorization in Handler for Custom URL Scheme) and aligns with CVE-2022-43550.
-
-When Java applications invoke protocol handlers (like `Desktop.browse()`, `Runtime.exec()` with `xdg-open`/`open`, or `rundll32 url.dll,FileProtocolHandler` on Windows), untrusted URLs can potentially trigger dangerous protocols such as `file://`, `smb://`, or other custom handlers that may lead to unauthorized file access, command execution, or other security issues.
-
-## Detected patterns
-
-The query identifies several common protocol handler invocation patterns:
-
-- **Java AWT**: `Desktop.browse(URI)` - the platform-agnostic standard
-- **Windows**: `Runtime.exec()` with `rundll32 url.dll,FileProtocolHandler`
-- **Linux**: `Runtime.exec()` with `xdg-open`
-- **macOS**: `Runtime.exec()` with `open` command
## Recommendation
+Parse the URL and allow only the schemes required by the application before invoking the protocol handler. For web links, allow only HTTP and HTTPS.
-Always validate URL schemes before passing them to protocol handlers. Only allow safe protocols like `http://` and `https://`. Reject or sanitize URLs containing potentially dangerous protocols.
## Example
-The following vulnerable code passes untrusted input directly to a protocol handler:
-
```java
import java.awt.Desktop;
+import java.io.IOException;
import java.net.URI;
+import java.net.URISyntaxException;
+import javax.servlet.http.HttpServletRequest;
-public class UrlOpener {
- public void openUserUrl(String userInput) throws Exception {
- // VULNERABLE: No validation of the URL scheme
- Desktop.getDesktop().browse(new URI(userInput));
+public class PotentiallyUnguardedProtocolHandler {
+ public void openUrl(HttpServletRequest request) throws IOException, URISyntaxException {
+ String input = request.getParameter("url");
+ Desktop.getDesktop().browse(new URI(input)); // BAD
}
-}
-```
-
-An attacker could provide a URL like `file:///etc/passwd` or `smb://attacker-server/share` to access unauthorized resources.
-The corrected version validates the URL scheme before opening:
-
-```java
-import java.awt.Desktop;
-import java.net.URI;
-
-public class UrlOpener {
- public void openUserUrl(String userInput) throws Exception {
- URI uri = new URI(userInput);
- String scheme = uri.getScheme();
-
- // Only allow safe protocols
- if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
- Desktop.getDesktop().browse(uri);
- } else {
- throw new SecurityException("Rejected unsafe URL scheme: " + scheme);
+ public void openWebUrl(HttpServletRequest request) throws IOException, URISyntaxException {
+ String input = request.getParameter("url");
+ URI url = new URI(input);
+ if ("https".equalsIgnoreCase(url.getScheme())
+ || "http".equalsIgnoreCase(url.getScheme())) {
+ Desktop.getDesktop().browse(url); // GOOD
}
}
}
-```
-
-For system command invocations:
-```java
-public class UrlOpener {
- public void openUserUrlViaShell(String userInput) throws Exception {
- // VULNERABLE: Untrusted input passed to xdg-open
- Runtime.getRuntime().exec(new String[]{"xdg-open", userInput});
- }
-}
```
+The bad example opens an untrusted URL directly. The good example first checks that the URL uses HTTP or HTTPS.
-Should be corrected to validate the scheme:
-
-```java
-import java.net.URI;
-
-public class UrlOpener {
- private boolean isValidScheme(String url) {
- try {
- URI uri = new URI(url);
- String scheme = uri.getScheme();
- return "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme);
- } catch (Exception e) {
- return false;
- }
- }
-
- public void openUserUrlViaShell(String userInput) throws Exception {
- if (isValidScheme(userInput)) {
- Runtime.getRuntime().exec(new String[]{"xdg-open", userInput});
- } else {
- throw new SecurityException("Invalid or unsafe URL scheme");
- }
- }
-}
-```
## References
-
-- [CWE-939: Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
-- [CVE-2022-43550: USB Creator has insufficiently protected credentials](https://ubuntu.com/security/CVE-2022-43550)
+* CWE-939: [Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
+* CVE-2022-43550: [Jitsi Desktop Client RCE by interacting with malicious URL schemes](https://hackerone.com/reports/1692603)
+* Common Weakness Enumeration: [CWE-939](https://cwe.mitre.org/data/definitions/939.html).
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
new file mode 100644
index 0000000..65e8778
--- /dev/null
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -0,0 +1,21 @@
+import java.awt.Desktop;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import javax.servlet.http.HttpServletRequest;
+
+public class PotentiallyUnguardedProtocolHandler {
+ public void openUrl(HttpServletRequest request) throws IOException, URISyntaxException {
+ String input = request.getParameter("url");
+ Desktop.getDesktop().browse(new URI(input)); // BAD
+ }
+
+ public void openWebUrl(HttpServletRequest request) throws IOException, URISyntaxException {
+ String input = request.getParameter("url");
+ URI url = new URI(input);
+ if ("https".equalsIgnoreCase(url.getScheme())
+ || "http".equalsIgnoreCase(url.getScheme())) {
+ Desktop.getDesktop().browse(url); // GOOD
+ }
+ }
+}
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
index b5495da..033d448 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -2,34 +2,32 @@
- URL protocol handlers are used to handle any URL scheme supported by the native desktop.
- If URL inputs to these handlers are untrusted and not properly sanitized, they can be
- used to perform unintended actions by another application registered to handle the same
- protocol.
+ Desktop URL handlers accept every scheme registered with the operating system.
+ Passing an untrusted URL to one of these handlers can invoke an unintended
+ application or expose local resources.
- Review protocol handler sinks and ensure that any URL inputs are properly sanitized if
- they come from untrusted sources.
+ Parse the URL and allow only the schemes required by the application before
+ invoking the protocol handler. For web links, allow only HTTP and HTTPS.
- In the bad example, an untrusted URL is passed directly to Desktop.browse()
- without any scheme validation.
- In the good example, the URL scheme is checked with getScheme() before
- invoking the handler.
+ The bad example opens an untrusted URL directly. The good example first
+ checks that the URL uses HTTP or HTTPS.
- Positive Security: Allow
- arbitrary URLs, expect arbitrary code execution
+
+ CWE-939: Improper
+ Authorization in Handler for Custom URL Scheme
- CVE-2022-43550: Jitsi Desktop Client
- RCE By Interacting with Malicious URL Schemes on Windows
+
+ CVE-2022-43550: Jitsi Desktop
+ Client RCE by interacting with malicious URL schemes
-
\ No newline at end of file
+
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index 33b3058..cf5998a 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -1,7 +1,7 @@
/**
* @name Potentially unguarded protocol handler invocation
* @id tob/java/unguarded-protocol-handler
- * @description Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols
+ * @description Finds URL protocol handlers called with URLs from untrusted sources
* @kind path-problem
* @tags security
* external/cwe/cwe-939
@@ -12,130 +12,168 @@
*/
import java
-import semmle.code.java.dataflow.TaintTracking
-import semmle.code.java.dataflow.FlowSources
import semmle.code.java.controlflow.Guards
-import semmle.code.java.security.ExternalProcess
+import semmle.code.java.dataflow.FlowSources
+import semmle.code.java.dataflow.TaintTracking
-/**
- * Generic case: invoke protocol handling through OS's protocol handling utilities. This aligns with CVE-2022-43550.
- */
-class ShellProtocolHandler extends ArgumentToExec {
- ShellProtocolHandler() {
- // Single string: "rundll32 url.dll,FileProtocolHandler " + url or "xdg-open " + url
- this instanceof StringArgumentToExec and
- exists(StringLiteral sl |
- sl.getParent*() = this and
- sl.getValue()
- .regexpMatch("(?i).*(rundll32.*url\\.dll.*FileProtocolHandler|xdg-open|\\bopen\\b).*")
- )
- or
- // Array: {"rundll32", "url.dll,FileProtocolHandler", url} or {"xdg-open", url}
- this.getType().(Array).getElementType() instanceof TypeString and
- exists(ArrayInit init, StringLiteral handler |
- init = this.(ArrayCreationExpr).getInit() and
- handler = init.getAnInit() and
- handler.getValue().regexpMatch("(?i)(rundll32(\\.exe)?|xdg-open|open|/usr/bin/open)")
- )
+class DesktopBrowseProtocolHandler extends MethodCall {
+ DesktopBrowseProtocolHandler() {
+ this.getMethod().hasQualifiedName("java.awt", "Desktop", "browse")
}
+ Expr getUrlArgument() { result = this.getArgument(0) }
+
+ string getHandlerType() { result = "Desktop.browse()" }
+}
+
+private ArrayInit getCommandArray(Expr command) {
+ result = command.(ArrayInit)
+ or
+ result = command.(ArrayCreationExpr).getInit()
+ or
+ exists(VarAccess access |
+ command = access and
+ result = getCommandArray(access.getVariable().getAnAssignedValue())
+ )
+}
+
+class RuntimeProtocolHandler extends MethodCall {
+ RuntimeProtocolHandler() { this.getMethod().hasQualifiedName("java.lang", "Runtime", "exec") }
+
Expr getUrlArgument() {
- // For single string exec, the URL is tainted into the concatenated string
- result = this and this instanceof StringArgumentToExec
+ result = this.getArgument(0) and result.getType() instanceof TypeString
or
- // For arrays with rundll32, find elements after "url.dll,FileProtocolHandler"
- exists(ArrayInit init, int urlIdx, int dllIdx |
- init = this.(ArrayCreationExpr).getInit() and
- result = init.getInit(urlIdx) and
- init.getInit(dllIdx)
- .(StringLiteral)
- .getValue()
- .regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
- urlIdx > dllIdx
- )
- or
- // For arrays with xdg-open/open, find elements after the handler
- exists(ArrayInit init, int urlIdx, int handlerIdx |
- init = this.(ArrayCreationExpr).getInit() and
- result = init.getInit(urlIdx) and
- init.getInit(handlerIdx)
- .(StringLiteral)
- .getValue()
- .regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
- urlIdx > handlerIdx
+ exists(ArrayInit command, int urlIndex, int markerIndex, StringLiteral marker |
+ command = getCommandArray(this.getArgument(0)) and
+ result = command.getInit(urlIndex) and
+ marker = command.getInit(markerIndex) and
+ marker.getValue().regexpMatch("(?i)(url\\.dll.*FileProtocolHandler|xdg-open|open)") and
+ urlIndex > markerIndex
)
}
string getHandlerType() {
- exists(StringLiteral sl |
- sl.getParent*() = this and
+ exists(StringLiteral literal |
+ literal.getParent*() = this.getArgument(0) and
(
- sl.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
+ literal.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
result = "rundll32 url.dll,FileProtocolHandler"
or
- sl.getValue().regexpMatch("(?i).*xdg-open.*") and
- result = "xdg-open"
+ literal.getValue().regexpMatch("(?i).*xdg-open.*") and result = "xdg-open"
or
- sl.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ literal.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
+ not literal.getValue().regexpMatch("(?i).*xdg-open.*") and
result = "open"
)
)
+ or
+ exists(ArrayInit command, StringLiteral executable, StringLiteral marker |
+ command = getCommandArray(this.getArgument(0)) and
+ executable = command.getAnInit() and
+ marker = command.getAnInit() and
+ (
+ executable.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
+ marker.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
+ result = "rundll32 url.dll,FileProtocolHandler"
+ or
+ executable.getValue().regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
+ result = executable.getValue()
+ )
+ )
}
}
-/**
- * A call to Desktop.browse() method. This is the platform-agnostic standard now.
- * NOTE(alan): the URLRedirect query will likely also trigger due to a little imprecision there
- */
-class DesktopBrowseProtocolHandler extends MethodCall {
- DesktopBrowseProtocolHandler() {
- this.getMethod().hasName("browse") and
- this.getMethod().getDeclaringType().hasQualifiedName("java.awt", "Desktop")
+class ProcessBuilderProtocolHandler extends ClassInstanceExpr {
+ ProcessBuilderProtocolHandler() {
+ this.getConstructor().getDeclaringType().hasQualifiedName("java.lang", "ProcessBuilder") and
+ exists(MethodCall start |
+ start.getMethod().hasQualifiedName("java.lang", "ProcessBuilder", "start") and
+ DataFlow::localFlow(DataFlow::exprNode(this), DataFlow::exprNode(start.getQualifier()))
+ )
}
- Expr getUrlArgument() { result = this.getArgument(0) }
-}
+ Expr getUrlArgument() {
+ exists(int urlIndex, int markerIndex, StringLiteral marker |
+ result = this.getArgument(urlIndex) and
+ marker = this.getArgument(markerIndex) and
+ marker.getValue().regexpMatch("(?i)(url\\.dll.*FileProtocolHandler|xdg-open|open)") and
+ urlIndex > markerIndex
+ )
+ }
-/**
- * A sanitizer node that represents URL scheme validation
- */
-class UrlSchemeValidationSanitizer extends DataFlow::Node {
- UrlSchemeValidationSanitizer() {
- exists(MethodCall mc |
- mc = this.asExpr() and
+ string getHandlerType() {
+ exists(StringLiteral executable, StringLiteral marker |
+ executable = this.getAnArgument() and
+ marker = this.getAnArgument() and
(
- // String comparison on the untrusted URL
- mc.getMethod().hasName(["equals", "equalsIgnoreCase", "startsWith", "matches"]) and
- exists(MethodCall getScheme |
- getScheme.getParent*() = mc and
- getScheme.getMethod().hasName(["getScheme", "getProtocol"])
- )
- or
- // URL string contains check like: url.contains("://")
- mc.getMethod().hasName(["contains", "startsWith", "matches", "indexOf"]) and
- exists(StringLiteral sl | sl = mc.getAnArgument() | sl.getValue().regexpMatch(".*://.*"))
+ executable.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
+ marker.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
+ result = "rundll32 url.dll,FileProtocolHandler"
or
- // Pattern matching on the string representation
- mc.getMethod().hasName(["matches", "find"]) and
- mc.getQualifier().getType().(RefType).hasQualifiedName("java.util.regex", "Matcher")
+ executable.getValue().regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
+ result = executable.getValue()
)
)
}
}
-/**
- * Configuration for tracking untrusted data to protocol handler invocations
- */
+predicate protocolHandlerCall(Expr call, Expr url, string handlerType) {
+ exists(DesktopBrowseProtocolHandler handler |
+ call = handler and
+ url = handler.getUrlArgument() and
+ handlerType = handler.getHandlerType()
+ )
+ or
+ exists(RuntimeProtocolHandler handler |
+ call = handler and
+ url = handler.getUrlArgument() and
+ handlerType = handler.getHandlerType()
+ )
+ or
+ exists(ProcessBuilderProtocolHandler handler |
+ call = handler and
+ url = handler.getUrlArgument() and
+ handlerType = handler.getHandlerType()
+ )
+}
+
+predicate urlSchemeGuardChecks(Guard guard, Expr checked, boolean branch) {
+ exists(MethodCall comparison, MethodCall scheme, CompileTimeConstantExpr allowed |
+ guard = comparison and
+ comparison.getMethod().getDeclaringType() instanceof TypeString and
+ comparison.getMethod().hasName(["equals", "equalsIgnoreCase"]) and
+ (
+ scheme = comparison.getQualifier() and allowed = comparison.getArgument(0)
+ or
+ scheme = comparison.getArgument(0) and allowed = comparison.getQualifier()
+ ) and
+ allowed.getStringValue() = ["http", "https"] and
+ scheme.getMethod().hasName(["getScheme", "getProtocol"]) and
+ checked = scheme.getQualifier() and
+ branch = true
+ )
+ or
+ exists(MethodCall comparison, CompileTimeConstantExpr allowed |
+ guard = comparison and
+ comparison.getMethod().getDeclaringType() instanceof TypeString and
+ comparison.getMethod().hasName("startsWith") and
+ comparison.getArgument(0) = allowed and
+ allowed.getStringValue() = ["http://", "https://"] and
+ checked = comparison.getQualifier() and
+ branch = true
+ )
+}
+
module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
predicate isSink(DataFlow::Node sink) {
- exists(DesktopBrowseProtocolHandler call | sink.asExpr() = call.getUrlArgument())
- or
- exists(ShellProtocolHandler call | sink.asExpr() = call.getUrlArgument())
+ exists(Expr call | protocolHandlerCall(call, sink.asExpr(), _))
}
- predicate isBarrier(DataFlow::Node node) { node instanceof UrlSchemeValidationSanitizer }
+ predicate isBarrier(DataFlow::Node node) {
+ node = DataFlow::BarrierGuard::getABarrierNode()
+ }
}
module PotentiallyUnguardedProtocolHandlerFlow =
@@ -145,22 +183,10 @@ import PotentiallyUnguardedProtocolHandlerFlow::PathGraph
from
PotentiallyUnguardedProtocolHandlerFlow::PathNode source,
- PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, Expr call, string callType
+ PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, Expr call, string handlerType
where
PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and
- (
- exists(DesktopBrowseProtocolHandler dbc |
- call = dbc and
- sink.getNode().asExpr() = dbc.getUrlArgument() and
- callType = "Desktop.browse()"
- )
- or
- exists(ShellProtocolHandler shc |
- call = shc and
- sink.getNode().asExpr() = shc.getUrlArgument() and
- callType = shc.getHandlerType()
- )
- )
+ protocolHandlerCall(call, sink.getNode().asExpr(), handlerType)
select call, source, sink,
- callType + " is called with untrusted input from $@ without proper URL scheme validation.",
+ handlerType + " is called with untrusted input from $@ without URL scheme validation.",
source.getNode(), "this source"
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index ad2c5b3..66acc7d 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,26 +1,52 @@
edges
-| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46265 |
-| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44464 Sink:MaD:43993 |
-| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | provenance | Src:MaD:46265 |
-| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | provenance | Sink:MaD:43993 |
-| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | provenance | MaD:44464 |
-| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46265 |
-| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | provenance | Sink:MaD:43993 |
-| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44464 |
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:46899 Sink:MaD:46430 |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:37:41:37:43 | uri | provenance | Sink:MaD:46430 |
+| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:46899 |
+| PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | PotentiallyUnguardedProtocolHandler.java:45:41:45:52 | new URI(...) | provenance | MaD:46899 Sink:MaD:46430 |
+| PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:54:41:54:43 | uri | provenance | Sink:MaD:46430 |
+| PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | provenance | MaD:46899 |
+| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:41:60:43 | url | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | provenance | Src:MaD:48700 Sink:MaD:46593 |
+| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | provenance | Src:MaD:48700 Sink:MaD:46593 |
+| PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | provenance | Src:MaD:48700 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | semmle.label | url : String |
-| PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:21:19:21:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
-| PotentiallyUnguardedProtocolHandler.java:21:27:21:29 | url : String | semmle.label | url : String |
-| PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | semmle.label | uri |
| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | semmle.label | url : String |
-| PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | semmle.label | uri |
+| PotentiallyUnguardedProtocolHandler.java:37:41:37:43 | uri | semmle.label | uri |
+| PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:45:41:45:52 | new URI(...) | semmle.label | new URI(...) |
+| PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | semmle.label | url : String |
+| PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | semmle.label | new URI(...) : URI |
+| PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | semmle.label | url : String |
+| PotentiallyUnguardedProtocolHandler.java:54:41:54:43 | uri | semmle.label | uri |
+| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:60:41:60:43 | url | semmle.label | url |
+| PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | semmle.label | url |
+| PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | semmle.label | ... + ... |
+| PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | semmle.label | url |
subpaths
#select
-| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:23:13:23:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:23:41:23:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:20:22:20:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:38:13:38:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:38:41:38:43 | uri | Desktop.browse() is called with untrusted input from $@ without proper URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:37:13:37:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:37:41:37:43 | uri | Desktop.browse() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:45:13:45:53 | browse(...) | PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:45:41:45:52 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:54:13:54:44 | browse(...) | PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:54:41:54:43 | uri | Desktop.browse() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:61:9:61:42 | exec(...) | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:41:60:43 | url | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:74:9:74:52 | exec(...) | PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:80:9:80:42 | exec(...) | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:85:9:85:48 | exec(...) | PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:95:34:95:68 | new ProcessBuilder(...) | PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
index 37436d3..753f78b 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -5,94 +5,102 @@
import javax.servlet.http.HttpServletRequest;
public class PotentiallyUnguardedProtocolHandler {
-
- public void bad1(HttpServletRequest request) throws IOException, URISyntaxException {
+ public void badDesktop(HttpServletRequest request)
+ throws IOException, URISyntaxException {
String url = request.getParameter("url");
- Desktop.getDesktop().browse(new URI(url));
- }
-
- public void bad2(String userInput) throws IOException, URISyntaxException {
- URI uri = new URI(userInput);
- Desktop.getDesktop().browse(uri);
+ Desktop.getDesktop().browse(new URI(url)); // BAD
}
- public void safe1(HttpServletRequest request) throws IOException, URISyntaxException {
+ public void goodDesktopScheme(HttpServletRequest request)
+ throws IOException, URISyntaxException {
String url = request.getParameter("url");
URI uri = new URI(url);
- if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
- Desktop.getDesktop().browse(uri);
+ if ("https".equalsIgnoreCase(uri.getScheme())
+ || "http".equalsIgnoreCase(uri.getScheme())) {
+ Desktop.getDesktop().browse(uri); // GOOD
}
}
- public void safe2(String userInput) throws IOException, URISyntaxException {
- if (userInput.startsWith("https://") || userInput.startsWith("http://")) {
- Desktop.getDesktop().browse(new URI(userInput));
+ public void goodDesktopPrefix(HttpServletRequest request)
+ throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ if (url.startsWith("https://") || url.startsWith("http://")) {
+ Desktop.getDesktop().browse(new URI(url)); // GOOD
}
}
- public void bad3(HttpServletRequest request) throws IOException, URISyntaxException {
+ public void badDesktopFileScheme(HttpServletRequest request)
+ throws IOException, URISyntaxException {
String url = request.getParameter("url");
URI uri = new URI(url);
- // Weak check - only checks if scheme exists, not what it is
- if (uri.getScheme() != null) {
- Desktop.getDesktop().browse(uri);
+ if ("file".equalsIgnoreCase(uri.getScheme())) {
+ Desktop.getDesktop().browse(uri); // BAD
}
}
- public void safe3(String userInput) throws IOException, URISyntaxException {
- URI uri = new URI(userInput);
- String scheme = uri.getScheme();
- if (scheme != null && (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
- Desktop.getDesktop().browse(uri);
+ public void badDesktopPartialPrefix(HttpServletRequest request)
+ throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ if (url.startsWith("http")) {
+ Desktop.getDesktop().browse(new URI(url)); // BAD
}
}
- public void safe4() throws IOException, URISyntaxException {
- Desktop.getDesktop().browse(new URI("https://example.com"));
+ public void badDesktopRejectedScheme(HttpServletRequest request)
+ throws IOException, URISyntaxException {
+ String url = request.getParameter("url");
+ URI uri = new URI(url);
+ if (!"https".equalsIgnoreCase(uri.getScheme())) {
+ Desktop.getDesktop().browse(uri); // BAD
+ }
}
- public void bad4_rundll32(String userInput) throws IOException {
- String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", userInput };
- Runtime.getRuntime().exec(cmd);
+ public void badRuntimeArray(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ String[] command = {"xdg-open", url};
+ Runtime.getRuntime().exec(command); // BAD
}
- public void bad6_rundll32(HttpServletRequest request) throws IOException {
+ public void goodRuntimeArray(HttpServletRequest request) throws IOException {
String url = request.getParameter("url");
- ProcessBuilder pb = new ProcessBuilder("rundll32", "url.dll,FileProtocolHandler", url);
- pb.start();
+ if (url.startsWith("https://")) {
+ String[] command = {"xdg-open", url};
+ Runtime.getRuntime().exec(command); // GOOD
+ }
}
- public void safe5_rundll32(HttpServletRequest request) throws IOException, URISyntaxException {
+ public void badRuntimeString(HttpServletRequest request) throws IOException {
String url = request.getParameter("url");
- URI uri = new URI(url);
- if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
- String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", url };
- Runtime.getRuntime().exec(cmd);
- }
+ Runtime.getRuntime().exec("xdg-open " + url); // BAD
}
- public void safe6_rundll32(String userInput) throws IOException {
- if (userInput.startsWith("https://") || userInput.startsWith("http://")) {
- String[] cmd = { "rundll32", "url.dll,FileProtocolHandler", userInput };
- Runtime.getRuntime().exec(cmd);
- }
+ public void badRuntimeRundll(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ String[] command = {"rundll32", "url.dll,FileProtocolHandler", url};
+ Runtime.getRuntime().exec(command); // BAD
}
- public void safe7_rundll32() throws IOException {
- Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler https://example.com");
+ public void badRuntimeOpen(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ Runtime.getRuntime().exec("open " + url); // BAD
}
- public void bad7_xdgopen(String userInput) throws IOException {
- String[] cmd = { "xdg-open", userInput };
- Runtime.getRuntime().exec(cmd);
+ public void unrelatedRuntime(HttpServletRequest request) throws IOException {
+ String argument = request.getParameter("argument");
+ Runtime.getRuntime().exec("echo " + argument); // GOOD
}
- public void safe8_xdgopen(HttpServletRequest request) throws IOException, URISyntaxException {
+ public void badProcessBuilder(HttpServletRequest request) throws IOException {
String url = request.getParameter("url");
- URI uri = new URI(url);
- if (uri.getScheme().equals("https") || uri.getScheme().equals("http")) {
- String[] cmd = { "xdg-open", url };
- Runtime.getRuntime().exec(cmd);
+ ProcessBuilder builder = new ProcessBuilder("xdg-open", url); // BAD
+ builder.start();
+ }
+
+ public void goodProcessBuilder(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ if (url.startsWith("https://")) {
+ ProcessBuilder builder = new ProcessBuilder("xdg-open", url); // GOOD
+ builder.start();
}
}
}
From 551accbaf2109ddb16d237a721ac829160864ab3 Mon Sep 17 00:00:00 2001
From: GrosQuildu
Date: Tue, 10 Nov 2026 16:27:20 +0100
Subject: [PATCH 12/12] Tighten Java protocol handler detection
Require recognized handlers to be the executable rather than a later argument. Add decoy-command regression tests, align Java expectations with CI, and remove duplicate documentation and newline churn.
---
.../PotentiallyUnguardedProtocolHandler.md | 1 -
.../PotentiallyUnguardedProtocolHandler.qhelp | 4 -
cpp/test/include/libc/stdint.h | 2 +-
cpp/test/include/libc/unistd.h | 2 +-
.../PotentiallyUnguardedProtocolHandler.md | 1 -
.../PotentiallyUnguardedProtocolHandler.qhelp | 4 -
.../PotentiallyUnguardedProtocolHandler.ql | 117 ++++++++++--------
...tentiallyUnguardedProtocolHandler.expected | 36 +++---
.../PotentiallyUnguardedProtocolHandler.java | 13 ++
9 files changed, 99 insertions(+), 81 deletions(-)
diff --git a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
index 41e2d8b..cb6da2d 100644
--- a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
+++ b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -28,6 +28,5 @@ The bad example opens an untrusted URL directly. The good example first checks t
## References
-* CWE-939: [Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
* CVE-2022-43550: [Jitsi Desktop Client RCE by interacting with malicious URL schemes](https://hackerone.com/reports/1692603)
* Common Weakness Enumeration: [CWE-939](https://cwe.mitre.org/data/definitions/939.html).
diff --git a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
index aa85290..d17624f 100644
--- a/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
+++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -21,10 +21,6 @@
-
- CWE-939: Improper
- Authorization in Handler for Custom URL Scheme
-
CVE-2022-43550: Jitsi Desktop
Client RCE by interacting with malicious URL schemes
diff --git a/cpp/test/include/libc/stdint.h b/cpp/test/include/libc/stdint.h
index 4965097..d02afcd 100644
--- a/cpp/test/include/libc/stdint.h
+++ b/cpp/test/include/libc/stdint.h
@@ -12,4 +12,4 @@ typedef unsigned int uint32_t;
#include
-#endif // --- end USE_HEADERS
+#endif // --- end USE_HEADERS
\ No newline at end of file
diff --git a/cpp/test/include/libc/unistd.h b/cpp/test/include/libc/unistd.h
index 6ed9ed4..0d7387a 100644
--- a/cpp/test/include/libc/unistd.h
+++ b/cpp/test/include/libc/unistd.h
@@ -21,4 +21,4 @@ void _exit(int);
#include
-#endif // --- end USE_HEADERS
+#endif // --- end USE_HEADERS
\ No newline at end of file
diff --git a/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
index aa2d007..a8ae030 100644
--- a/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
+++ b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md
@@ -36,6 +36,5 @@ The bad example opens an untrusted URL directly. The good example first checks t
## References
-* CWE-939: [Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html)
* CVE-2022-43550: [Jitsi Desktop Client RCE by interacting with malicious URL schemes](https://hackerone.com/reports/1692603)
* Common Weakness Enumeration: [CWE-939](https://cwe.mitre.org/data/definitions/939.html).
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
index 033d448..3bbd8c6 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp
@@ -21,10 +21,6 @@
-
- CWE-939: Improper
- Authorization in Handler for Custom URL Scheme
-
CVE-2022-43550: Jitsi Desktop
Client RCE by interacting with malicious URL schemes
diff --git a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
index cf5998a..3fdb62b 100644
--- a/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
+++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql
@@ -37,52 +37,87 @@ private ArrayInit getCommandArray(Expr command) {
)
}
+private string getExecutableHandlerType(StringLiteral executable) {
+ executable.getValue().regexpMatch("(?i)xdg-open") and result = "xdg-open"
+ or
+ executable.getValue().regexpMatch("(?i)(open|/usr/bin/open)") and result = "open"
+}
+
+private predicate isRundllHandler(Expr executable, Expr marker) {
+ executable.(StringLiteral).getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
+ marker.(StringLiteral).getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler")
+}
+
+private int getFirstUrlIndex(StringLiteral executable, Expr secondArgument, string handlerType) {
+ handlerType = getExecutableHandlerType(executable) and result = 1
+ or
+ isRundllHandler(executable, secondArgument) and
+ handlerType = "rundll32 url.dll,FileProtocolHandler" and
+ result = 2
+}
+
+private predicate arrayProtocolHandler(ArrayInit command, Expr url, string handlerType) {
+ exists(int urlIndex, StringLiteral executable |
+ executable = command.getInit(0) and
+ url = command.getInit(urlIndex) and
+ urlIndex >= getFirstUrlIndex(executable, command.getInit(1), handlerType)
+ )
+}
+
+private StringLiteral getCommandPrefix(Expr command) {
+ result = command.(StringLiteral)
+ or
+ result = getCommandPrefix(command.(AddExpr).getLeftOperand())
+}
+
+private string getStringCommandHandlerType(Expr command) {
+ exists(StringLiteral prefix |
+ prefix = getCommandPrefix(command) and
+ (
+ prefix.getValue().regexpMatch("(?i)\\s*xdg-open\\s+.*") and result = "xdg-open"
+ or
+ prefix.getValue().regexpMatch("(?i)\\s*(open|/usr/bin/open)\\s+.*") and result = "open"
+ or
+ prefix
+ .getValue()
+ .regexpMatch("(?i)\\s*rundll32(\\.exe)?\\s+url\\.dll.*FileProtocolHandler\\s+.*") and
+ result = "rundll32 url.dll,FileProtocolHandler"
+ )
+ )
+}
+
class RuntimeProtocolHandler extends MethodCall {
RuntimeProtocolHandler() { this.getMethod().hasQualifiedName("java.lang", "Runtime", "exec") }
Expr getUrlArgument() {
result = this.getArgument(0) and result.getType() instanceof TypeString
or
- exists(ArrayInit command, int urlIndex, int markerIndex, StringLiteral marker |
+ exists(ArrayInit command |
command = getCommandArray(this.getArgument(0)) and
- result = command.getInit(urlIndex) and
- marker = command.getInit(markerIndex) and
- marker.getValue().regexpMatch("(?i)(url\\.dll.*FileProtocolHandler|xdg-open|open)") and
- urlIndex > markerIndex
+ arrayProtocolHandler(command, result, _)
)
}
string getHandlerType() {
- exists(StringLiteral literal |
- literal.getParent*() = this.getArgument(0) and
- (
- literal.getValue().regexpMatch("(?i).*rundll32.*url\\.dll.*FileProtocolHandler.*") and
- result = "rundll32 url.dll,FileProtocolHandler"
- or
- literal.getValue().regexpMatch("(?i).*xdg-open.*") and result = "xdg-open"
- or
- literal.getValue().regexpMatch("(?i).*\\bopen\\b.*") and
- not literal.getValue().regexpMatch("(?i).*xdg-open.*") and
- result = "open"
- )
- )
+ result = getStringCommandHandlerType(this.getArgument(0))
or
- exists(ArrayInit command, StringLiteral executable, StringLiteral marker |
+ exists(ArrayInit command, Expr url |
command = getCommandArray(this.getArgument(0)) and
- executable = command.getAnInit() and
- marker = command.getAnInit() and
- (
- executable.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
- marker.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
- result = "rundll32 url.dll,FileProtocolHandler"
- or
- executable.getValue().regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
- result = executable.getValue()
- )
+ arrayProtocolHandler(command, url, result)
)
}
}
+private predicate processBuilderProtocolHandler(
+ ClassInstanceExpr command, Expr url, string handlerType
+) {
+ exists(int urlIndex, StringLiteral executable |
+ executable = command.getArgument(0) and
+ url = command.getArgument(urlIndex) and
+ urlIndex >= getFirstUrlIndex(executable, command.getArgument(1), handlerType)
+ )
+}
+
class ProcessBuilderProtocolHandler extends ClassInstanceExpr {
ProcessBuilderProtocolHandler() {
this.getConstructor().getDeclaringType().hasQualifiedName("java.lang", "ProcessBuilder") and
@@ -92,29 +127,9 @@ class ProcessBuilderProtocolHandler extends ClassInstanceExpr {
)
}
- Expr getUrlArgument() {
- exists(int urlIndex, int markerIndex, StringLiteral marker |
- result = this.getArgument(urlIndex) and
- marker = this.getArgument(markerIndex) and
- marker.getValue().regexpMatch("(?i)(url\\.dll.*FileProtocolHandler|xdg-open|open)") and
- urlIndex > markerIndex
- )
- }
+ Expr getUrlArgument() { processBuilderProtocolHandler(this, result, _) }
- string getHandlerType() {
- exists(StringLiteral executable, StringLiteral marker |
- executable = this.getAnArgument() and
- marker = this.getAnArgument() and
- (
- executable.getValue().regexpMatch("(?i)rundll32(\\.exe)?") and
- marker.getValue().regexpMatch("(?i)url\\.dll.*FileProtocolHandler") and
- result = "rundll32 url.dll,FileProtocolHandler"
- or
- executable.getValue().regexpMatch("(?i)(xdg-open|open|/usr/bin/open)") and
- result = executable.getValue()
- )
- )
- }
+ string getHandlerType() { processBuilderProtocolHandler(this, _, result) }
}
predicate protocolHandlerCall(Expr call, Expr url, string handlerType) {
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
index 66acc7d..0d6a02e 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected
@@ -1,19 +1,19 @@
edges
-| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:46899 Sink:MaD:46430 |
-| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:37:41:37:43 | uri | provenance | Sink:MaD:46430 |
-| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:46899 |
-| PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | PotentiallyUnguardedProtocolHandler.java:45:41:45:52 | new URI(...) | provenance | MaD:46899 Sink:MaD:46430 |
-| PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:54:41:54:43 | uri | provenance | Sink:MaD:46430 |
-| PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | provenance | MaD:46899 |
-| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:41:60:43 | url | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | provenance | Src:MaD:48700 Sink:MaD:46593 |
-| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | provenance | Src:MaD:48700 |
-| PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | provenance | Src:MaD:48700 Sink:MaD:46593 |
-| PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | provenance | Src:MaD:48700 |
+| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | provenance | MaD:44464 Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:34:22:34:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:37:41:37:43 | uri | provenance | Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:35:27:35:29 | url : String | PotentiallyUnguardedProtocolHandler.java:35:19:35:30 | new URI(...) : URI | provenance | MaD:44464 |
+| PotentiallyUnguardedProtocolHandler.java:43:22:43:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:45:49:45:51 | url : String | PotentiallyUnguardedProtocolHandler.java:45:41:45:52 | new URI(...) | provenance | MaD:44464 Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:51:22:51:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | PotentiallyUnguardedProtocolHandler.java:54:41:54:43 | uri | provenance | Sink:MaD:43993 |
+| PotentiallyUnguardedProtocolHandler.java:52:27:52:29 | url : String | PotentiallyUnguardedProtocolHandler.java:52:19:52:30 | new URI(...) : URI | provenance | MaD:44464 |
+| PotentiallyUnguardedProtocolHandler.java:59:22:59:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:60:41:60:43 | url | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | provenance | Src:MaD:46265 Sink:MaD:44157 |
+| PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | provenance | Src:MaD:46265 |
+| PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | provenance | Src:MaD:46265 Sink:MaD:44157 |
+| PotentiallyUnguardedProtocolHandler.java:101:22:101:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:102:65:102:67 | url | provenance | Src:MaD:46265 |
nodes
| PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | semmle.label | new URI(...) |
@@ -37,8 +37,8 @@ nodes
| PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | semmle.label | url |
| PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
| PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | semmle.label | ... + ... |
-| PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
-| PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | semmle.label | url |
+| PotentiallyUnguardedProtocolHandler.java:101:22:101:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
+| PotentiallyUnguardedProtocolHandler.java:102:65:102:67 | url | semmle.label | url |
subpaths
#select
| PotentiallyUnguardedProtocolHandler.java:11:9:11:49 | browse(...) | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:11:37:11:48 | new URI(...) | Desktop.browse() is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:10:22:10:48 | getParameter(...) | this source |
@@ -49,4 +49,4 @@ subpaths
| PotentiallyUnguardedProtocolHandler.java:74:9:74:52 | exec(...) | PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:74:35:74:51 | ... + ... | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:73:22:73:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:80:9:80:42 | exec(...) | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:79:72:79:74 | url | rundll32 url.dll,FileProtocolHandler is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:78:22:78:48 | getParameter(...) | this source |
| PotentiallyUnguardedProtocolHandler.java:85:9:85:48 | exec(...) | PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:85:35:85:47 | ... + ... | open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:84:22:84:48 | getParameter(...) | this source |
-| PotentiallyUnguardedProtocolHandler.java:95:34:95:68 | new ProcessBuilder(...) | PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:95:65:95:67 | url | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:94:22:94:48 | getParameter(...) | this source |
+| PotentiallyUnguardedProtocolHandler.java:102:34:102:68 | new ProcessBuilder(...) | PotentiallyUnguardedProtocolHandler.java:101:22:101:48 | getParameter(...) : String | PotentiallyUnguardedProtocolHandler.java:102:65:102:67 | url | xdg-open is called with untrusted input from $@ without URL scheme validation. | PotentiallyUnguardedProtocolHandler.java:101:22:101:48 | getParameter(...) | this source |
diff --git a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
index 753f78b..29f3d80 100644
--- a/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
+++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java
@@ -90,6 +90,13 @@ public void unrelatedRuntime(HttpServletRequest request) throws IOException {
Runtime.getRuntime().exec("echo " + argument); // GOOD
}
+ public void unrelatedRuntimeWithHandlerArgument(HttpServletRequest request)
+ throws IOException {
+ String url = request.getParameter("url");
+ Runtime.getRuntime().exec(new String[]{"echo", "xdg-open", url}); // GOOD
+ Runtime.getRuntime().exec("echo xdg-open " + url); // GOOD
+ }
+
public void badProcessBuilder(HttpServletRequest request) throws IOException {
String url = request.getParameter("url");
ProcessBuilder builder = new ProcessBuilder("xdg-open", url); // BAD
@@ -103,4 +110,10 @@ public void goodProcessBuilder(HttpServletRequest request) throws IOException {
builder.start();
}
}
+
+ public void unrelatedProcessBuilder(HttpServletRequest request) throws IOException {
+ String url = request.getParameter("url");
+ ProcessBuilder builder = new ProcessBuilder("echo", "xdg-open", url); // GOOD
+ builder.start();
+ }
}