From 6d1c7957d26db723cdc7eca624f2501a819e7351 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Wed, 15 May 2024 14:17:15 -0500 Subject: [PATCH] fix(request_builder): encode unresolved path string prior to path parameter insertion This commit modifies RequestBuilder.ResolveRequestURL() slightly so that the passed-in path string (unresolved, potentially containing special characters and path parameter references) is URL-encoded prior to replacing path param references with their corresponding encoded path param values. This ensures that the URL string passed to http.NewRequest() by RequestBuilder.Build() will be properly encoded (escaped) such that all special characters appearing in path segments are converted to their %nn equivalents, but yet any escaped characters within the encoded path param values are NOT decoded. This avoids a quirk in the url.Parse() method that is invoked by the http.NewRequest() method. This change is needed in order to properly support a scenario where: 1. a path parameter value contains one or more embedded slashes and is therefore escaped AND 2. the unresolved path string ALSO contains other special characters needing to be escaped Signed-off-by: Phil Adams --- Makefile | 2 +- core/request_builder.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index dcfa0d7..d01bf82 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ test: lint: ${LINT} run --build-tags=all - DIFF=$$(${FORMATTER} -d core); if [[ -n "$$DIFF" ]]; then printf "\n$$DIFF" && exit 1; fi + DIFF=$$(${FORMATTER} -d core); if [ -n "$$DIFF" ]; then printf "\n$$DIFF\n" && exit 1; fi scan-gosec: ${GOSEC} ./... diff --git a/core/request_builder.go b/core/request_builder.go index be82b47..e0c7f28 100644 --- a/core/request_builder.go +++ b/core/request_builder.go @@ -152,6 +152,14 @@ func (requestBuilder *RequestBuilder) ResolveRequestURL(serviceURL string, path // If we have a non-empty "path" input parameter, then process it for possible path param references. if path != "" { + // Encode the unresolved path string. This will convert all special characters to their + // "%" encoding counterparts. Then we need to revert the encodings for '/', '{' and '}' characters + // to retain the original path segments and to make it easy to insert the encoded path param values below. + path = url.PathEscape(path) + path = strings.ReplaceAll(path, "%2F", "/") + path = strings.ReplaceAll(path, "%7B", "{") + path = strings.ReplaceAll(path, "%7D", "}") + // If path parameter values were passed in, then for each one, replace any references to it // within "path" with the path parameter's encoded value. if len(pathParams) > 0 {