Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(elasticsearch): prevent query parameter encoding in URLs (#17759)

Vincent Huang6ab9405858a0
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.go
index c302a08a5d..3ace73cc47 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch.go
@@ -68,9 +68,23 @@ func (client *BasicAuthClient) Do(method string, route []byte, queryString []byt
return nil, errors.Wrapf(err, "failed to parse base URL")
}
- fullURL := baseURL.JoinPath(string(route)).String()
+ // Split route into path and query parameters
+ // The route may contain query parameters (e.g., "/_mapping?pretty")
+ // We need to separate them to avoid URL-encoding the '?' character
+ routeStr := string(route)
+ pathPart, queryPart, hasQuery := strings.Cut(routeStr, "?")
+
+ // Join only the path part (without query parameters)
+ // This ensures JoinPath only processes the actual path
+ fullURL := baseURL.JoinPath(pathPart)
+
+ // Add query parameters if present in the route
+ // Assign directly to RawQuery to preserve the query string as-is
+ if hasQuery {
+ fullURL.RawQuery = queryPart
+ }
- req, err := http.NewRequest(method, fullURL, bytes.NewReader(queryString))
+ req, err := http.NewRequest(method, fullURL.String(), bytes.NewReader(queryString))
if err != nil {
return nil, errors.Wrapf(err, "failed to init a HTTP request")
}
diff --git a/backend/plugin/db/elasticsearch/elasticsearch_test.go b/backend/plugin/db/elasticsearch/elasticsearch_test.go
index ff9a01b205..b4d3a9c94b 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch_test.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch_test.go
@@ -2,8 +2,11 @@ package elasticsearch
import (
"context"
+ "net/url"
+ "strings"
"testing"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
@@ -56,3 +59,67 @@ func TestOpenWithAWSAuth(t *testing.T) {
})
}
}
+
+func TestURLConstructionWithQueryParams(t *testing.T) {
+ testCases := []struct {
+ name string
+ baseAddress string
+ route string
+ wantURL string
+ wantRawQuery string
+ }{
+ {
+ name: "route with single query param",
+ baseAddress: "https://localhost:9200",
+ route: "/_mapping?pretty",
+ wantURL: "https://localhost:9200/_mapping?pretty",
+ wantRawQuery: "pretty",
+ },
+ {
+ name: "route with multiple query params",
+ baseAddress: "https://localhost:9200",
+ route: "/_cat/indices?format=json&v",
+ wantURL: "https://localhost:9200/_cat/indices?format=json&v",
+ wantRawQuery: "format=json&v",
+ },
+ {
+ name: "route without query params",
+ baseAddress: "https://localhost:9200",
+ route: "/_cat/indices",
+ wantURL: "https://localhost:9200/_cat/indices",
+ wantRawQuery: "",
+ },
+ {
+ name: "route with index name containing special chars",
+ baseAddress: "https://localhost:9200",
+ route: "/test-index-2024/_mapping?pretty",
+ wantURL: "https://localhost:9200/test-index-2024/_mapping?pretty",
+ wantRawQuery: "pretty",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Parse base URL
+ baseURL, err := url.Parse(tc.baseAddress)
+ require.NoError(t, err)
+
+ // Apply the fix logic
+ routeStr := tc.route
+ pathPart, queryPart, hasQuery := strings.Cut(routeStr, "?")
+
+ fullURL := baseURL.JoinPath(pathPart)
+
+ if hasQuery {
+ fullURL.RawQuery = queryPart
+ }
+
+ // Verify results
+ assert.Equal(t, tc.wantURL, fullURL.String(), "Full URL should match")
+ assert.Equal(t, tc.wantRawQuery, fullURL.RawQuery, "Query should match")
+
+ // Critical: Verify no URL encoding of the '?' character
+ assert.NotContains(t, fullURL.String(), "%3F", "URL should not contain encoded question mark")
+ })
+ }
+}