diff --git a/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md new file mode 100644 index 0000000..cb6da2d --- /dev/null +++ b/cpp/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md @@ -0,0 +1,32 @@ +# 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. + + +## 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. + + +## Example + +```cpp +#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 + } +} + +``` +The bad example opens an untrusted URL directly. The good example first checks that the URL uses HTTPS. + + +## References +* 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..d17624f --- /dev/null +++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp @@ -0,0 +1,29 @@ + + + +

+ 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. +

+
+ +
  • + 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 new file mode 100644 index 0000000..25616ab --- /dev/null +++ b/cpp/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql @@ -0,0 +1,147 @@ +/** + * @name Potentially unguarded protocol handler invocation + * @id tob/cpp/unguarded-protocol-handler + * @description Finds URL protocol handlers called with URLs from untrusted sources + * @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.controlflow.IRGuards +private import semmle.code.cpp.ir.dataflow.TaintTracking +private import semmle.code.cpp.security.CommandExecution +private import semmle.code.cpp.security.FlowSources + +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 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 + ( + comparison instanceof EQExpr and branch = true + or + comparison instanceof NEExpr and branch = false + ) + ) +} + +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 + ) +} + +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 + ) +} + +module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } + + predicate isSink(DataFlow::Node sink) { + exists(FunctionCall call | + call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and + sink.asConvertedExpr() = call.getArgument(0) + ) + or + exists(FunctionCall call | + isShellProtocolHandlerCall(call, _) and + sink.asIndirectExpr() = call.getArgument(0) + ) + } + + predicate isBarrier(DataFlow::Node node) { + node = DataFlow::BarrierGuard::getABarrierNode() + or + node = DataFlow::BarrierGuard::getAnIndirectBarrierNode() + } +} + +module PotentiallyUnguardedProtocolHandlerFlow = + TaintTracking::Global; + +import PotentiallyUnguardedProtocolHandlerFlow::PathGraph + +from + PotentiallyUnguardedProtocolHandlerFlow::PathNode source, + PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, FunctionCall call, string callType +where + PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and + ( + call.getTarget().hasQualifiedName("", "QDesktopServices", "openUrl") and + sink.getNode().asConvertedExpr() = call.getArgument(0) and + callType = "QDesktopServices::openUrl()" + or + isShellProtocolHandlerCall(call, callType) and + sink.getNode().asIndirectExpr() = call.getArgument(0) + ) +select call, source, sink, + callType + " is called with untrusted input from $@ without URL scheme validation.", + source.getNode(), "this source" diff --git a/cpp/test/include/libc/stdlib.h b/cpp/test/include/libc/stdlib.h index 7f28000..226da84 100644 --- a/cpp/test/include/libc/stdlib.h +++ b/cpp/test/include/libc/stdlib.h @@ -7,6 +7,7 @@ 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..74804cb 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 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 7163a23..0d7387a 100644 --- a/cpp/test/include/libc/unistd.h +++ b/cpp/test/include/libc/unistd.h @@ -7,6 +7,8 @@ extern "C" { #endif +long read(int, void *, unsigned long); + void _exit(int); #ifdef __cplusplus 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..0316be7 --- /dev/null +++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.cpp @@ -0,0 +1,112 @@ +#include "../../../include/libc/stdlib.h" +#include "../../../include/libc/string_stubs.h" +#include "../../../include/libc/unistd.h" + +struct QString { + QString(const char *value); + bool operator==(const char *other) const; + bool startsWith(const char *prefix) const; +}; + +struct QUrl { + QUrl(const char *value); + QString scheme() const; +}; + +struct QDesktopServices { + static bool openUrl(const QUrl &url); +}; + +void bad_qt() { + char url[1024]; + read(0, url, sizeof(url)); + QDesktopServices::openUrl(QUrl(url)); // BAD +} + +void good_qt_hardcoded() { + QDesktopServices::openUrl(QUrl("https://example.com")); // 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 new file mode 100644 index 0000000..dd1404b --- /dev/null +++ b/cpp/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected @@ -0,0 +1,32 @@ +edges +| 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: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: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/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/doc/QUERIES.md b/doc/QUERIES.md index 7971538..d3e8d5a 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)|Finds URL protocol handlers called with URLs from untrusted sources|warning|medium| ### Go @@ -54,5 +55,6 @@ | Name | Description | Severity | Precision | | --- | ----------- | :----: | :--------: | +|[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 new file mode 100644 index 0000000..a8ae030 --- /dev/null +++ b/java/src/docs/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.md @@ -0,0 +1,40 @@ +# 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. + + +## 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. + + +## Example + +```java +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 + } + } +} + +``` +The bad example opens an untrusted URL directly. The good example first checks that the URL uses HTTP or HTTPS. + + +## References +* 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 new file mode 100644 index 0000000..3bbd8c6 --- /dev/null +++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qhelp @@ -0,0 +1,29 @@ + + + +

    + 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 HTTP or HTTPS. +

    +
    + +
  • + 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 new file mode 100644 index 0000000..3fdb62b --- /dev/null +++ b/java/src/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql @@ -0,0 +1,207 @@ +/** + * @name Potentially unguarded protocol handler invocation + * @id tob/java/unguarded-protocol-handler + * @description Finds URL protocol handlers called with URLs from untrusted sources + * @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.controlflow.Guards +import semmle.code.java.dataflow.FlowSources +import semmle.code.java.dataflow.TaintTracking + +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()) + ) +} + +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 | + command = getCommandArray(this.getArgument(0)) and + arrayProtocolHandler(command, result, _) + ) + } + + string getHandlerType() { + result = getStringCommandHandlerType(this.getArgument(0)) + or + exists(ArrayInit command, Expr url | + command = getCommandArray(this.getArgument(0)) and + 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 + exists(MethodCall start | + start.getMethod().hasQualifiedName("java.lang", "ProcessBuilder", "start") and + DataFlow::localFlow(DataFlow::exprNode(this), DataFlow::exprNode(start.getQualifier())) + ) + } + + Expr getUrlArgument() { processBuilderProtocolHandler(this, result, _) } + + string getHandlerType() { processBuilderProtocolHandler(this, _, result) } +} + +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(Expr call | protocolHandlerCall(call, sink.asExpr(), _)) + } + + predicate isBarrier(DataFlow::Node node) { + node = DataFlow::BarrierGuard::getABarrierNode() + } +} + +module PotentiallyUnguardedProtocolHandlerFlow = + TaintTracking::Global; + +import PotentiallyUnguardedProtocolHandlerFlow::PathGraph + +from + PotentiallyUnguardedProtocolHandlerFlow::PathNode source, + PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, Expr call, string handlerType +where + PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and + protocolHandlerCall(call, sink.getNode().asExpr(), handlerType) +select call, source, sink, + 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 new file mode 100644 index 0000000..0d6a02e --- /dev/null +++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.expected @@ -0,0 +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: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(...) | +| PotentiallyUnguardedProtocolHandler.java:11:45:11:47 | url : String | semmle.label | url : String | +| 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: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: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 | +| 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: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 new file mode 100644 index 0000000..29f3d80 --- /dev/null +++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.java @@ -0,0 +1,119 @@ +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 badDesktop(HttpServletRequest request) + throws IOException, URISyntaxException { + String url = request.getParameter("url"); + Desktop.getDesktop().browse(new URI(url)); // BAD + } + + public void goodDesktopScheme(HttpServletRequest request) + throws IOException, URISyntaxException { + String url = request.getParameter("url"); + URI uri = new URI(url); + if ("https".equalsIgnoreCase(uri.getScheme()) + || "http".equalsIgnoreCase(uri.getScheme())) { + Desktop.getDesktop().browse(uri); // GOOD + } + } + + 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 badDesktopFileScheme(HttpServletRequest request) + throws IOException, URISyntaxException { + String url = request.getParameter("url"); + URI uri = new URI(url); + if ("file".equalsIgnoreCase(uri.getScheme())) { + Desktop.getDesktop().browse(uri); // BAD + } + } + + 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 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 badRuntimeArray(HttpServletRequest request) throws IOException { + String url = request.getParameter("url"); + String[] command = {"xdg-open", url}; + Runtime.getRuntime().exec(command); // BAD + } + + public void goodRuntimeArray(HttpServletRequest request) throws IOException { + String url = request.getParameter("url"); + if (url.startsWith("https://")) { + String[] command = {"xdg-open", url}; + Runtime.getRuntime().exec(command); // GOOD + } + } + + public void badRuntimeString(HttpServletRequest request) throws IOException { + String url = request.getParameter("url"); + Runtime.getRuntime().exec("xdg-open " + url); // BAD + } + + 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 badRuntimeOpen(HttpServletRequest request) throws IOException { + String url = request.getParameter("url"); + Runtime.getRuntime().exec("open " + url); // BAD + } + + public void unrelatedRuntime(HttpServletRequest request) throws IOException { + String argument = request.getParameter("argument"); + 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 + 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(); + } + } + + public void unrelatedProcessBuilder(HttpServletRequest request) throws IOException { + String url = request.getParameter("url"); + ProcessBuilder builder = new ProcessBuilder("echo", "xdg-open", url); // GOOD + builder.start(); + } +} 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..de5b795 --- /dev/null +++ b/java/test/query-tests/security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.qlref @@ -0,0 +1 @@ +security/PotentiallyUnguardedProtocolHandler/PotentiallyUnguardedProtocolHandler.ql