|
| 1 | +# Potentially unguarded protocol handler invocation |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Detected patterns |
| 8 | + |
| 9 | +The query identifies several common protocol handler invocation patterns: |
| 10 | + |
| 11 | +- **Java AWT**: `Desktop.browse(URI)` - the platform-agnostic standard |
| 12 | +- **Windows**: `Runtime.exec()` with `rundll32 url.dll,FileProtocolHandler` |
| 13 | +- **Linux**: `Runtime.exec()` with `xdg-open` |
| 14 | +- **macOS**: `Runtime.exec()` with `open` command |
| 15 | + |
| 16 | +## Recommendation |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +The following vulnerable code passes untrusted input directly to a protocol handler: |
| 23 | + |
| 24 | +```java |
| 25 | +import java.awt.Desktop; |
| 26 | +import java.net.URI; |
| 27 | + |
| 28 | +public class UrlOpener { |
| 29 | + public void openUserUrl(String userInput) throws Exception { |
| 30 | + // VULNERABLE: No validation of the URL scheme |
| 31 | + Desktop.getDesktop().browse(new URI(userInput)); |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +An attacker could provide a URL like `file:///etc/passwd` or `smb://attacker-server/share` to access unauthorized resources. |
| 37 | + |
| 38 | +The corrected version validates the URL scheme before opening: |
| 39 | + |
| 40 | +```java |
| 41 | +import java.awt.Desktop; |
| 42 | +import java.net.URI; |
| 43 | + |
| 44 | +public class UrlOpener { |
| 45 | + public void openUserUrl(String userInput) throws Exception { |
| 46 | + URI uri = new URI(userInput); |
| 47 | + String scheme = uri.getScheme(); |
| 48 | + |
| 49 | + // Only allow safe protocols |
| 50 | + if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) { |
| 51 | + Desktop.getDesktop().browse(uri); |
| 52 | + } else { |
| 53 | + throw new SecurityException("Rejected unsafe URL scheme: " + scheme); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +For system command invocations: |
| 60 | + |
| 61 | +```java |
| 62 | +public class UrlOpener { |
| 63 | + public void openUserUrlViaShell(String userInput) throws Exception { |
| 64 | + // VULNERABLE: Untrusted input passed to xdg-open |
| 65 | + Runtime.getRuntime().exec(new String[]{"xdg-open", userInput}); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Should be corrected to validate the scheme: |
| 71 | + |
| 72 | +```java |
| 73 | +import java.net.URI; |
| 74 | + |
| 75 | +public class UrlOpener { |
| 76 | + private boolean isValidScheme(String url) { |
| 77 | + try { |
| 78 | + URI uri = new URI(url); |
| 79 | + String scheme = uri.getScheme(); |
| 80 | + return "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme); |
| 81 | + } catch (Exception e) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void openUserUrlViaShell(String userInput) throws Exception { |
| 87 | + if (isValidScheme(userInput)) { |
| 88 | + Runtime.getRuntime().exec(new String[]{"xdg-open", userInput}); |
| 89 | + } else { |
| 90 | + throw new SecurityException("Invalid or unsafe URL scheme"); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## References |
| 97 | + |
| 98 | +- [CWE-939: Improper Authorization in Handler for Custom URL Scheme](https://cwe.mitre.org/data/definitions/939.html) |
| 99 | +- [CVE-2022-43550: USB Creator has insufficiently protected credentials](https://ubuntu.com/security/CVE-2022-43550) |
0 commit comments