Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-java): refuse a request path that does not begin with /

Yudhi Armyndharis26b72b9a5ce7
diff --git a/sdk/java/README.md b/sdk/java/README.md
index f2f6cc919..a63583db6 100644
--- a/sdk/java/README.md
+++ b/sdk/java/README.md
@@ -34,8 +34,9 @@ filter of `ListSessionsQuery`, the `after` and `inlineMedia` filters of
fields of `ChatSummary`, the `order` and `product` fields of
`ChatHistoryMessage`, `WebhookSignature.verify`, the `WebhookDelivery` types, the
`code()`, `retryAfterSeconds()` and `headers()` error accessors, the `UNKNOWN`
-enum fallback and the refusal of an empty, `.` or `..` id or path segment; they
-ship with the next SDK release. See [the SDK overview](../README.md#coverage).
+enum fallback, the refusal of an empty, `.` or `..` id or path segment and the
+refusal of a raw request path that does not begin with `/`; they ship with the
+next SDK release. See [the SDK overview](../README.md#coverage).
## Quickstart
@@ -178,8 +179,9 @@ WebhookDelivery delivery =
- **Empty and dot ids are refused.** An empty, `.` or `..` id throws
`IllegalArgumentException` and nothing is sent, so a proxy that resolves dot
segments cannot turn the call into one on the parent resource. The raw
- `request*` methods refuse a `.` or `..` segment the same way but send an
- empty one (a trailing slash) as written.
+ `request*` methods refuse a `.` or `..` segment the same way, and a path
+ that does not begin with `/`, but send an empty one (a trailing slash) as
+ written.
## Development
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java b/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
index 23c720302..1ef1deb51 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
@@ -32,10 +32,15 @@ public final class Http {
/**
* Build an absolute URL, preserving a base path prefix and omitting null query values.
*
- * @throws IllegalArgumentException if the path has a {@code .} or {@code ..} segment (also
- * written {@code %2e}); empty segments, such as a trailing slash, are kept as written.
+ * @throws IllegalArgumentException if the path does not begin with {@code /} (appended to the
+ * base, any other path can change the host the request and its API key go to), or has a
+ * {@code .} or {@code ..} segment (also written {@code %2e}); empty segments, such as a
+ * trailing slash, are kept as written.
*/
public static String buildUrl(String baseUrl, String path, Object query, Gson gson) {
+ if (!path.startsWith("/")) {
+ throw new IllegalArgumentException("OpenWA: path must begin with \"/\": \"" + path + "\"");
+ }
String pathOnly = path.split("[?#]", 2)[0];
for (String segment : pathOnly.split("/", -1)) {
String dots = segment.replaceAll("(?i)%2e", ".");
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
index eb0860c6d..0ee0f6018 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
@@ -42,6 +42,14 @@ class HttpTest {
assertEquals("http://h/api/labels/a.b?x=/..", Http.buildUrl("http://h", "/api/labels/a.b?x=/..", null, gson));
}
+ @Test
+ void buildUrlRefusesPathWithoutLeadingSlash() {
+ // Appended to the base, these would change the host the request and its API key go to.
+ for (String path : new String[] {".evil.example/x", "@evil.example/x", "api/sessions", ""}) {
+ assertThrows(IllegalArgumentException.class, () -> Http.buildUrl("https://api.example.com", path, null, gson), path);
+ }
+ }
+
@Test
void buildUrlStripsTrailingSlashAndPreservesPrefix() {
assertEquals("http://h:2785/api/sessions", Http.buildUrl("http://h:2785/", "/api/sessions", null, gson));