diff --git a/src/main/java/io/getstream/services/framework/StreamRequest.java b/src/main/java/io/getstream/services/framework/StreamRequest.java index 7b04d153..d64deb14 100644 --- a/src/main/java/io/getstream/services/framework/StreamRequest.java +++ b/src/main/java/io/getstream/services/framework/StreamRequest.java @@ -137,19 +137,60 @@ 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); + } + + // Build path segment-by-segment so path parameters are percent-encoded as single segments + // 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 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) { + 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 the processed path - // Remove leading slash if present to avoid double slashes - String processedPath = path.startsWith("/") ? path.substring(1) : path; - - // Start building with the base URL - HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder(processedPath); + if (rawQuery != null) { + urlBuilder.encodedQuery(rawQuery); + } // 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..6c235522 --- /dev/null +++ b/src/test/java/io/getstream/services/framework/StreamRequestPathEncodingTest.java @@ -0,0 +1,78 @@ +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 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 = + 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", + url.toString()); + assertNull(url.query()); + assertNull(url.fragment()); + } + + @Test + void encodesSpacesAsPercent20() throws Exception { + HttpUrl url = + 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() throws Exception { + StreamRequest> helper = newHelper(); + 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)); + } +}