You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by am...@apache.org on 2023/07/12 08:53:13 UTC

[knox] branch master updated: KNOX-2931 - Some special characters in the rewrite rule cannot be escaped (#769)

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

amagyar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 8bb882d74 KNOX-2931 - Some special characters in the rewrite rule cannot be escaped (#769)
8bb882d74 is described below

commit 8bb882d7432709af2844b8acdbe939ac9203225d
Author: Attila Magyar <m....@gmail.com>
AuthorDate: Wed Jul 12 10:53:07 2023 +0200

    KNOX-2931 - Some special characters in the rewrite rule cannot be escaped (#769)
---
 .../rewrite/api/UrlRewriteProcessorTest.java       | 24 ++++++++++++++++++++++
 .../api/UrlRewriteProcessorTest/rewrite_escape.xml | 21 +++++++++++++++++++
 .../knox/gateway/util/urltemplate/Expander.java    | 14 ++++++++++++-
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
index 898a2e5fd..4408d0a69 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
@@ -410,6 +410,30 @@ public class UrlRewriteProcessorTest {
     processor.destroy();
   }
 
+  @Test
+  public void testRuleWithDoubleBrackets() throws IOException, URISyntaxException {
+    UrlRewriteEnvironment environment = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+    HttpServletRequest request = EasyMock.createNiceMock( HttpServletRequest.class );
+    HttpServletResponse response = EasyMock.createNiceMock( HttpServletResponse.class );
+    EasyMock.replay(environment, request, response);
+
+    UrlRewriteProcessor processor = new UrlRewriteProcessor();
+    UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load(
+            "xml", getTestResourceReader( "rewrite_escape.xml"));
+    processor.initialize(environment, config);
+
+    Template outputUrl = processor.rewrite( null,
+            Parser.parseLiteral("{{prot}}://{{hostname}}:{{portno}}"),
+            UrlRewriter.Direction.OUT,
+            "test_rule");
+
+    assertThat(
+            outputUrl.toString(), is( "GATEWAY?host={{prot}}://{{hostname}}:{{portno}}"));
+
+    processor.destroy();
+  }
+
+
   /**
    * Turn a string containing URL parameters, e.g.
    *
diff --git a/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite_escape.xml b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite_escape.xml
new file mode 100644
index 000000000..04b326e39
--- /dev/null
+++ b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite_escape.xml
@@ -0,0 +1,21 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<rules>
+    <rule dir="OUT" name="test_rule">
+        <rewrite template="GATEWAY?host=\{\{prot\}\}://\{\{hostname\}\}:\{\{portno\}\}"/>
+    </rule>
+</rules>
\ No newline at end of file
diff --git a/gateway-util-urltemplate/src/main/java/org/apache/knox/gateway/util/urltemplate/Expander.java b/gateway-util-urltemplate/src/main/java/org/apache/knox/gateway/util/urltemplate/Expander.java
index 32a79a710..77b2ddf6f 100644
--- a/gateway-util-urltemplate/src/main/java/org/apache/knox/gateway/util/urltemplate/Expander.java
+++ b/gateway-util-urltemplate/src/main/java/org/apache/knox/gateway/util/urltemplate/Expander.java
@@ -17,6 +17,9 @@
  */
 package org.apache.knox.gateway.util.urltemplate;
 
+import static org.apache.knox.gateway.util.urltemplate.Parser.TEMPLATE_CLOSE_MARKUP;
+import static org.apache.knox.gateway.util.urltemplate.Parser.TEMPLATE_OPEN_MARKUP;
+
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -184,7 +187,7 @@ public class Expander {
             String pattern = value.getOriginalPattern();
             if (pattern != null) {
               builder.append('=');
-              builder.append(pattern);
+              builder.append(unescape(pattern));
             }
             break;
           case (Segment.DEFAULT):
@@ -201,6 +204,15 @@ public class Expander {
     }
   }
 
+  private static String unescape(String pattern) {
+    if (pattern == null) {
+      return null;
+    }
+    return pattern
+            .replace("\\" + TEMPLATE_OPEN_MARKUP, String.valueOf(TEMPLATE_OPEN_MARKUP))
+            .replace("\\" + TEMPLATE_CLOSE_MARKUP, String.valueOf(TEMPLATE_CLOSE_MARKUP));
+  }
+
   private static void expandExtraQuery( Template template, Set<String> names, Params params, StringBuilder builder, AtomicInteger index ) {
     Query extra = template.getExtra();
     if( extra != null ) {