You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/07/30 01:23:29 UTC

(groovy) branch master updated: GROOVY-12213: avoid decoding the request URI in HttpBuilder.appendQuery

This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 00f7489712 GROOVY-12213: avoid decoding the request URI in HttpBuilder.appendQuery
00f7489712 is described below

commit 00f74897126a3959fd8e47ef0ace2df7f1a89234
Author: Kartik Kenchi <ne...@gmail.com>
AuthorDate: Wed Jul 29 16:44:35 2026 +0530

    GROOVY-12213: avoid decoding the request URI in HttpBuilder.appendQuery
---
 .../src/main/groovy/groovy/http/HttpBuilder.groovy | 23 +++++++++--
 .../test/groovy/groovy/http/HttpBuilderTest.groovy | 47 +++++++++++++++++++++-
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/subprojects/groovy-http-builder/src/main/groovy/groovy/http/HttpBuilder.groovy b/subprojects/groovy-http-builder/src/main/groovy/groovy/http/HttpBuilder.groovy
index 5a02a643ff..a47b1f048a 100644
--- a/subprojects/groovy-http-builder/src/main/groovy/groovy/http/HttpBuilder.groovy
+++ b/subprojects/groovy-http-builder/src/main/groovy/groovy/http/HttpBuilder.groovy
@@ -624,8 +624,8 @@ final class HttpBuilder {
         }
 
         List<String> pairs = new ArrayList<>()
-        if (uri.query != null && !uri.query.isEmpty()) {
-            pairs.add(uri.query)
+        if (uri.rawQuery != null && !uri.rawQuery.isEmpty()) {
+            pairs.add(uri.rawQuery)
         }
 
         queryValues.each { String key, Object value ->
@@ -635,7 +635,24 @@ final class HttpBuilder {
         }
 
         String query = pairs.join('&')
-        return new URI(uri.scheme, uri.authority, uri.path, query, uri.fragment)
+        // reassemble from the encoded components: the multi-argument URI constructor
+        // takes decoded ones, which would turn a %2F or %2E in the original path back
+        // into a separator or a dot segment
+        StringBuilder rebuilt = new StringBuilder()
+        if (uri.scheme != null) {
+            rebuilt.append(uri.scheme).append(':')
+        }
+        if (uri.rawAuthority != null) {
+            rebuilt.append('//').append(uri.rawAuthority)
+        }
+        if (uri.rawPath != null) {
+            rebuilt.append(uri.rawPath)
+        }
+        rebuilt.append('?').append(query)
+        if (uri.rawFragment != null) {
+            rebuilt.append('#').append(uri.rawFragment)
+        }
+        return URI.create(rebuilt.toString())
     }
 
     private static String encodeQueryComponent(final String value) {
diff --git a/subprojects/groovy-http-builder/src/test/groovy/groovy/http/HttpBuilderTest.groovy b/subprojects/groovy-http-builder/src/test/groovy/groovy/http/HttpBuilderTest.groovy
index 8f603a5c2e..07fc3e2038 100644
--- a/subprojects/groovy-http-builder/src/test/groovy/groovy/http/HttpBuilderTest.groovy
+++ b/subprojects/groovy-http-builder/src/test/groovy/groovy/http/HttpBuilderTest.groovy
@@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows
 
 import java.nio.charset.StandardCharsets
 import java.time.Duration
+import java.util.concurrent.atomic.AtomicReference
 
 class HttpBuilderTest {
 
@@ -37,7 +38,7 @@ class HttpBuilderTest {
     void setup() {
         server = HttpServer.create(new InetSocketAddress('127.0.0.1', 0), 0)
         server.createContext('/hello') { exchange ->
-            String body = "method=${exchange.requestMethod};query=${exchange.requestURI.query};ua=${exchange.requestHeaders.getFirst('User-Agent')}"
+            String body = "method=${exchange.requestMethod};query=${exchange.requestURI.rawQuery};ua=${exchange.requestHeaders.getFirst('User-Agent')}"
             byte[] bytes = body.getBytes(StandardCharsets.UTF_8)
             exchange.sendResponseHeaders(200, bytes.length)
             exchange.responseBody.withCloseable { it.write(bytes) }
@@ -525,6 +526,50 @@ class HttpBuilderTest {
         assert result.body == 'method=GET'
     }
 
+    @Test
+    void appendedQueryKeepsPercentEncodedPathSegments() {
+        AtomicReference<URI> seen = capture('/encoded')
+        HttpBuilder http = HttpBuilder.http { baseUri rootUri }
+
+        http.get('/encoded/a%2Fb') { query page: 1 }
+
+        assert seen.get().rawPath == '/encoded/a%2Fb'
+    }
+
+    @Test
+    void appendedQueryKeepsExistingEncodedQueryValues() {
+        AtomicReference<URI> seen = capture('/existing')
+        HttpBuilder http = HttpBuilder.http { baseUri rootUri }
+
+        http.get('/existing?filter=a%26admin%3D1') { query page: 1 }
+
+        assert seen.get().rawQuery == 'filter=a%26admin%3D1&page=1'
+    }
+
+    @Test
+    void confinedRequestWithQueryStaysInsideTheBasePath() {
+        AtomicReference<URI> seen = capture('/api')
+        HttpBuilder http = HttpBuilder.http {
+            baseUri "${rootUri}api/v2/"
+            confineToBaseUri true
+        }
+
+        http.get('..%2Fadmin') { query page: 1 }
+
+        assert seen.get().rawPath == '/api/v2/..%2Fadmin'
+    }
+
+    private AtomicReference<URI> capture(String path) {
+        AtomicReference<URI> seen = new AtomicReference<>()
+        server.createContext(path) { exchange ->
+            seen.set(exchange.requestURI)
+            byte[] bytes = 'ok'.getBytes(StandardCharsets.UTF_8)
+            exchange.sendResponseHeaders(200, bytes.length)
+            exchange.responseBody.withCloseable { it.write(bytes) }
+        }
+        seen
+    }
+
     private void redirect(String path, int status, String location) {
         server.createContext(path) { exchange ->
             exchange.responseHeaders.add('Location', location)