From dcb2f173c3c76a72e01d0b200f081c7f41cfdd64 Mon Sep 17 00:00:00 2001 From: Tommaso Barbugli Date: Tue, 24 Nov 2026 17:32:31 +0100 Subject: [PATCH 1/3] fix(http): encode path parameter segments individually and reject dot segments --- .../services/framework/StreamRequest.java | 49 +++++++++--- .../StreamRequestPathEncodingTest.java | 74 +++++++++++++++++++ 2 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java diff --git a/src/main/java/io/getstream/services/framework/StreamRequest.java b/src/main/java/io/getstream/services/framework/StreamRequest.java index 7b04d153..e385275d 100644 --- a/src/main/java/io/getstream/services/framework/StreamRequest.java +++ b/src/main/java/io/getstream/services/framework/StreamRequest.java @@ -137,19 +137,48 @@ private static RateLimit getRateLimit(Response response) { public HttpUrl buildUrl( String baseUrl, String path, Map pathParams, Object queryParams) throws JsonProcessingException, NullPointerException, IllegalAccessException { - // Handle path parameters - if (pathParams != null && !pathParams.isEmpty()) { - for (Map.Entry entry : pathParams.entrySet()) { - path = path.replace("{" + entry.getKey() + "}", entry.getValue()); - } + HttpUrl parsedBase = HttpUrl.parse(baseUrl); + if (parsedBase == null) { + throw new IllegalArgumentException("Invalid base URL: " + baseUrl); } - // Add the processed path - // Remove leading slash if present to avoid double slashes + // Build path segment-by-segment so path parameters are percent-encoded as single segments + // and cannot traverse parent paths or inject query/fragment components. + HttpUrl.Builder urlBuilder = parsedBase.newBuilder(); String processedPath = path.startsWith("/") ? path.substring(1) : path; - - // Start building with the base URL - HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder(processedPath); + if (!processedPath.isEmpty()) { + for (String segment : processedPath.split("/", -1)) { + if (segment.startsWith("{") && segment.endsWith("}") && pathParams != null) { + String key = segment.substring(1, segment.length() - 1); + if (pathParams.containsKey(key)) { + String value = pathParams.get(key); + if (value == null || value.isEmpty() || ".".equals(value) || "..".equals(value)) { + throw new IllegalArgumentException( + "Invalid path parameter '" + key + "': must not be empty or a dot-segment"); + } + urlBuilder.addPathSegment(value); + continue; + } + } + String resolved = segment; + if (pathParams != null && !pathParams.isEmpty()) { + for (Map.Entry entry : pathParams.entrySet()) { + String placeholder = "{" + entry.getKey() + "}"; + if (resolved.contains(placeholder)) { + String value = entry.getValue(); + if (value == null || value.isEmpty() || ".".equals(value) || "..".equals(value)) { + throw new IllegalArgumentException( + "Invalid path parameter '" + + entry.getKey() + + "': must not be empty or a dot-segment"); + } + resolved = resolved.replace(placeholder, value); + } + } + } + urlBuilder.addPathSegment(resolved); + } + } // Add query parameters if (queryParams != null) { diff --git a/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java b/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java new file mode 100644 index 00000000..c4133a1b --- /dev/null +++ b/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java @@ -0,0 +1,74 @@ +package io.getstream.services.framework; + +import static org.junit.jupiter.api.Assertions.*; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.HashMap; +import java.util.Map; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import org.junit.jupiter.api.Test; + +class StreamRequestPathEncodingTest { + private final StreamRequest> helper = + new StreamRequest<>( + new OkHttpClient(), + new ObjectMapper(), + "https://chat.stream-io-api.com", + "GET", + "/api/v2/users", + null, + null, + new TypeReference>() {}); + + @Test + void encodesPathTraversalQueryAndFragmentCharacters() throws Exception { + HttpUrl url = + helper.buildUrl( + "https://chat.stream-io-api.com", + "/api/v2/video/call/{type}/{id}", + Map.of("type", "default", "id", "../other/path?hard_delete=true#frag"), + null); + + assertEquals( + "https://chat.stream-io-api.com/api/v2/video/call/default/..%2Fother%2Fpath%3Fhard_delete=true%23frag", + url.toString()); + assertNull(url.query()); + assertNull(url.fragment()); + } + + @Test + void encodesSpacesAsPercent20() throws Exception { + HttpUrl url = + helper.buildUrl( + "https://chat.stream-io-api.com", + "/api/v2/roles/{name}", + Map.of("name", "custom role"), + null); + + assertEquals("https://chat.stream-io-api.com/api/v2/roles/custom%20role", url.toString()); + } + + @Test + void rejectsEmptyAndDotSegmentPathParams() { + for (String bad : new String[] {"", ".", ".."}) { + assertThrows( + IllegalArgumentException.class, + () -> + helper.buildUrl( + "https://chat.stream-io-api.com", + "/api/v2/video/call/{type}/{id}", + Map.of("type", "default", "id", bad), + null)); + } + + Map withNull = new HashMap<>(); + withNull.put("id", null); + assertThrows( + IllegalArgumentException.class, + () -> + helper.buildUrl( + "https://chat.stream-io-api.com", "/api/v2/tasks/{id}", withNull, null)); + } +} From afc2644a9126f60e80848b86dc1e368bc8ef11d2 Mon Sep 17 00:00:00 2001 From: Tommaso Barbugli Date: Thu, 26 Nov 2026 13:41:57 +0100 Subject: [PATCH 2/3] test(http): construct StreamRequest helper inside throwing method --- .../StreamRequestPathEncodingTest.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java b/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java index c4133a1b..6c235522 100644 --- a/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java +++ b/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java @@ -11,25 +11,27 @@ import org.junit.jupiter.api.Test; class StreamRequestPathEncodingTest { - private final StreamRequest> helper = - new StreamRequest<>( - new OkHttpClient(), - new ObjectMapper(), - "https://chat.stream-io-api.com", - "GET", - "/api/v2/users", - null, - null, - new TypeReference>() {}); + private static StreamRequest> newHelper() throws Exception { + return new StreamRequest<>( + new OkHttpClient(), + new ObjectMapper(), + "https://chat.stream-io-api.com", + "GET", + "/api/v2/users", + null, + null, + new TypeReference>() {}); + } @Test void encodesPathTraversalQueryAndFragmentCharacters() throws Exception { HttpUrl url = - helper.buildUrl( - "https://chat.stream-io-api.com", - "/api/v2/video/call/{type}/{id}", - Map.of("type", "default", "id", "../other/path?hard_delete=true#frag"), - null); + newHelper() + .buildUrl( + "https://chat.stream-io-api.com", + "/api/v2/video/call/{type}/{id}", + Map.of("type", "default", "id", "../other/path?hard_delete=true#frag"), + null); assertEquals( "https://chat.stream-io-api.com/api/v2/video/call/default/..%2Fother%2Fpath%3Fhard_delete=true%23frag", @@ -41,17 +43,19 @@ void encodesPathTraversalQueryAndFragmentCharacters() throws Exception { @Test void encodesSpacesAsPercent20() throws Exception { HttpUrl url = - helper.buildUrl( - "https://chat.stream-io-api.com", - "/api/v2/roles/{name}", - Map.of("name", "custom role"), - null); + newHelper() + .buildUrl( + "https://chat.stream-io-api.com", + "/api/v2/roles/{name}", + Map.of("name", "custom role"), + null); assertEquals("https://chat.stream-io-api.com/api/v2/roles/custom%20role", url.toString()); } @Test - void rejectsEmptyAndDotSegmentPathParams() { + void rejectsEmptyAndDotSegmentPathParams() throws Exception { + StreamRequest> helper = newHelper(); for (String bad : new String[] {"", ".", ".."}) { assertThrows( IllegalArgumentException.class, From 9a98722e17127f5193404d49085cb976c62b8cd5 Mon Sep 17 00:00:00 2001 From: Tommaso Barbugli Date: Thu, 26 Nov 2026 14:32:04 +0100 Subject: [PATCH 3/3] fix(http): preserve literal query strings on request path templates --- .../services/framework/StreamRequest.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/getstream/services/framework/StreamRequest.java b/src/main/java/io/getstream/services/framework/StreamRequest.java index e385275d..d64deb14 100644 --- a/src/main/java/io/getstream/services/framework/StreamRequest.java +++ b/src/main/java/io/getstream/services/framework/StreamRequest.java @@ -143,9 +143,17 @@ public HttpUrl buildUrl( } // Build path segment-by-segment so path parameters are percent-encoded as single segments - // and cannot traverse parent paths or inject query/fragment components. + // and cannot traverse parent paths or inject query/fragment components. Preserve any literal + // query string already present on the path template itself before substituting pathParams. HttpUrl.Builder urlBuilder = parsedBase.newBuilder(); - String processedPath = path.startsWith("/") ? path.substring(1) : path; + String pathPart = path; + String rawQuery = null; + int queryIdx = path.indexOf('?'); + if (queryIdx >= 0) { + pathPart = path.substring(0, queryIdx); + rawQuery = path.substring(queryIdx + 1); + } + String processedPath = pathPart.startsWith("/") ? pathPart.substring(1) : pathPart; if (!processedPath.isEmpty()) { for (String segment : processedPath.split("/", -1)) { if (segment.startsWith("{") && segment.endsWith("}") && pathParams != null) { @@ -180,6 +188,10 @@ public HttpUrl buildUrl( } } + if (rawQuery != null) { + urlBuilder.encodedQuery(rawQuery); + } + // Add query parameters if (queryParams != null) { Map queryMap = QueryConverter.getQueryParameters(queryParams, objectMapper);