From 2d6678f253adea60bf06c5b8c22d252bc0041fe1 Mon Sep 17 00:00:00 2001 From: rmfcnb Date: Tue, 15 Apr 2025 11:13:33 +0200 Subject: [PATCH] DIME-6543: the built-in java URL only accepts absolute URLs, which prevents us using relative paths. The URI already has this information, so there is no need to convert it to URL, and the escher does not use the host (presented or not), so we can go with relative paths. --- src/main/java/com/emarsys/escher/Helper.java | 12 ++++-------- .../java/com/emarsys/escher/HelperTest.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/emarsys/escher/Helper.java b/src/main/java/com/emarsys/escher/Helper.java index 5072369..7b7201e 100644 --- a/src/main/java/com/emarsys/escher/Helper.java +++ b/src/main/java/com/emarsys/escher/Helper.java @@ -41,13 +41,9 @@ public String canonicalize(EscherRequest request, List signedHeaders) th } - private String canonicalizePath(EscherRequest request) throws EscherException { - try { - String path = request.getURI().toURL().getPath(); - return path.equals("") ? "/" : path; - } catch (MalformedURLException e) { - throw new EscherException(e); - } + private String canonicalizePath(EscherRequest request) { + String path = request.getURI().getRawPath(); + return path.isEmpty() ? "/" : path; } @@ -72,7 +68,7 @@ private String queryParameterToString(NameValuePair entry) { public static String encodeURIComponent(String s) throws UnsupportedEncodingException { - // We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with + // We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with // some of the RFC3986 reserved characters kept as they were (-._~) which is used by URLEncoder. // This will result an encoding method similar to other escher libs. return URLEncoder.encode(s, "UTF-8") diff --git a/src/test/java/com/emarsys/escher/HelperTest.java b/src/test/java/com/emarsys/escher/HelperTest.java index a9ae6b1..377517e 100644 --- a/src/test/java/com/emarsys/escher/HelperTest.java +++ b/src/test/java/com/emarsys/escher/HelperTest.java @@ -140,6 +140,23 @@ public void testCanonicalizeWithUrlParamsContainingSpecialCharacters() throws Ex assertThat(canonicalized, containsString("specialchars=-_.~")); } + @Test + public void testCanonicalizeWithRelativeUri() throws Exception { + TestParam param = parseTestData("get-vanilla"); + TestParam.Request paramRequest = param.getRequest(); + paramRequest.setUrl(paramRequest.getUrl() + "?name=John%2BWick"); + + URI relativeUri = new URI(paramRequest.getUrl()); + + EscherRequestImpl request = new EscherRequestImpl(paramRequest.getMethod(), relativeUri, new ArrayList<>(), paramRequest.getBody()); + + Helper helper = new Helper(createConfig(param)); + + String canonicalized = helper.canonicalize(request, param.getHeadersToSign()); + + assertThat(canonicalized, containsString("name=John%2BWick")); + } + @Test @UseDataProvider("getAddMandatoryHeadersCases")