You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@knox.apache.org by "smolnar82 (via GitHub)" <gi...@apache.org> on 2024/01/17 18:18:51 UTC

[PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

smolnar82 opened a new pull request, #834:
URL: https://github.com/apache/knox/pull/834

   ## What changes were proposed in this pull request?
   
   As described in [KNOX-3001](https://issues.apache.org/jira/browse/KNOX-3001), if the shared provider, descriptor, service, or application originally contains XML-escaped values, Knox will persist topologies in a way such that those already escaped values become doubly-escaped.
   To avoid this issue the following was implemented:
   - unescape the value first
   - escape the unescaped result
   
   ## How was this patch tested?
   
   I updated existing JUnit tests where a shared provider config contains both a single `&` and an escaped `&amp;` in its .json file. The test then proves the generated XML is parsed properly and contains the desired `&` value in the SAX document.
   
   I also ran integration tests with the following shared-provider and descriptor files:
   ```
   $ cat conf/shared-providers/smolnar.json 
   {
     "providers" : [ {
       "role" : "webappsec",
       "name" : "WebAppSec",
       "enabled" : true,
       "params" : {
         "xframe.options.enabled" : "true"
       }
     }, {
       "role" : "authentication",
       "name" : "ShiroProvider",
       "enabled" : true,
       "params" : {
         "main.ldapContextFactory" : "org.apache.knox.gateway.shirorealm.KnoxLdapContextFactory",
         "main.ldapRealm" : "org.apache.knox.gateway.shirorealm.KnoxLdapRealm",
         "main.ldapRealm.authenticationCachingEnabled" : "false",
         "main.ldapRealm.contextFactory" : "$ldapContextFactory",
         "main.ldapRealm.contextFactory.authenticationMechanism" : "simple",
         "main.ldapRealm.contextFactory.url" : "ldap://localhost:33389",
         "main.ldapRealm.userDnTemplate" : "uid=0ou=people,dc=hadoop,dc=apache,dc=org",
         "main.ldapRealm.userSearchFilter" : "(&(&amp;(objectclass=person)(sAMAccountName={0}))(|(memberOf=CN=SecXX-users,OU=ManagedGroups,OU=Groups,OU=XX,OU=xx,DC=xx,DC=int)(memberOf=CN=SecXX-rls-serviceuser,OU=ManagedGroups,OU=Groups,OU=XX,OU=xx,DC=xx,DC=int)))",
         "redirectToUrl" : "/${GATEWAY_PATH}/knoxsso/knoxauth/login.html",
         "restrictedCookies" : "rememberme,WWW-Authenticate",
         "sessionTimeout" : "30",
         "urls./**" : "authcBasic"
       }
     }, {
       "role" : "identity-assertion",
       "name" : "Default",
       "enabled" : true,
       "params" : { }
     } ],
     "readOnly" : true
   }
   
   $ cat conf/descriptors/smolnar.json 
   {
      "provider-config-ref": "smolnar",
      "services": [
         {
           "name": "KNOXSSO",
           "params": {
               "knoxsso.token.ttl": "86400000",
               "knoxsso.token.sigalg": "",
               "knoxsso.redirect.whitelist.regex": "^https?:\\/\\/(.*smolnar\\.root\\.xyz\\.com)(?::[0-9]+)?(?:\\/.*)?$"
            }
         }
      ],
      "applications": [
        {
          "name": "knoxauth"
        }
      ]
   }
   ```
   
   Then I confirmed that the generated topology is in good shape:
   ```
   $ cat conf/topologies/smolnar.xml 
   <?xml version="1.0" encoding="utf-8"?>
   <!--==============================================-->
   <!-- DO NOT EDIT. This is an auto-generated file. -->
   <!--==============================================-->
   <topology>
       <generated>true</generated>
       <gateway>
           <provider>
               <role>webappsec</role>
               <name>WebAppSec</name>
               <enabled>true</enabled>
               <param>
                   <name>xframe.options.enabled</name>
                   <value>true</value>
               </param>
           </provider>
           <provider>
               <role>authentication</role>
               <name>ShiroProvider</name>
               <enabled>true</enabled>
               <param>
                   <name>main.ldapContextFactory</name>
                   <value>org.apache.knox.gateway.shirorealm.KnoxLdapContextFactory</value>
               </param>
               <param>
                   <name>main.ldapRealm</name>
                   <value>org.apache.knox.gateway.shirorealm.KnoxLdapRealm</value>
               </param>
               <param>
                   <name>main.ldapRealm.authenticationCachingEnabled</name>
                   <value>false</value>
               </param>
               <param>
                   <name>main.ldapRealm.contextFactory</name>
                   <value>$ldapContextFactory</value>
               </param>
               <param>
                   <name>main.ldapRealm.contextFactory.authenticationMechanism</name>
                   <value>simple</value>
               </param>
               <param>
                   <name>main.ldapRealm.contextFactory.url</name>
                   <value>ldap://localhost:33389</value>
               </param>
               <param>
                   <name>main.ldapRealm.userDnTemplate</name>
                   <value>uid=0ou=people,dc=hadoop,dc=apache,dc=org</value>
               </param>
               <param>
                   <name>main.ldapRealm.userSearchFilter</name>
                   <value>(&amp;(&amp;(objectclass=person)(sAMAccountName={0}))(|(memberOf=CN=SecXX-users,OU=ManagedGroups,OU=Groups,OU=XX,OU=xx,DC=xx,DC=int)(memberOf=CN=SecXX-rls-serviceuser,OU=ManagedGroups,OU=Groups,OU=XX,OU=xx,DC=xx,DC=int)))</value>
               </param>
               <param>
                   <name>redirectToUrl</name>
                   <value>/${GATEWAY_PATH}/knoxsso/knoxauth/login.html</value>
               </param>
               <param>
                   <name>restrictedCookies</name>
                   <value>rememberme,WWW-Authenticate</value>
               </param>
               <param>
                   <name>sessionTimeout</name>
                   <value>30</value>
               </param>
               <param>
                   <name>urls./**</name>
                   <value>authcBasic</value>
               </param>
           </provider>
           <provider>
               <role>identity-assertion</role>
               <name>Default</name>
               <enabled>true</enabled>
           </provider>
       </gateway>
   
       <service>
           <role>KNOXSSO</role>
           <param>
               <name>knoxsso.token.ttl</name>
               <value>86400000</value>
           </param>
           <param>
               <name>knoxsso.token.sigalg</name>
               <value></value>
           </param>
           <param>
               <name>knoxsso.redirect.whitelist.regex</name>
               <value>^https?:\/\/(.*smolnar\.root\.xyz\.com)(?::[0-9]+)?(?:\/.*)?$</value>
           </param>
       </service>
       <application>
           <name>knoxauth</name>
       </application>
   </topology>
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "pzampino (via GitHub)" <gi...@apache.org>.
pzampino commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1459490179


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);

Review Comment:
   Perhaps, even just a check for '&' since I don't think that's expected in unescaped forms of this content? I'm not sure if that's entirely true though.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "smolnar82 (via GitHub)" <gi...@apache.org>.
smolnar82 commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1457218446


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);
+      return StringEscapeUtils.escapeXml10(unescapedValue);

Review Comment:
   Yes, there is. According to the JavaDoc of `.escpaXml11`:
   ```
   The returned string can be inserted into a valid XML 1.1 document. Do not use it for XML 1.0 documents.
   ```
   We do generate XML 1.0 documents [here](https://github.com/apache/knox/blob/master/gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java#L453), therefore we need to use the `.escapeXml10` API.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "smolnar82 (via GitHub)" <gi...@apache.org>.
smolnar82 merged PR #834:
URL: https://github.com/apache/knox/pull/834


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "smolnar82 (via GitHub)" <gi...@apache.org>.
smolnar82 commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1456945417


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);

Review Comment:
   I had this idea too, but [`StringEscapeUtils`](https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringEscapeUtils.html) does not have any test method to see if the given value requires unescape.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "pzampino (via GitHub)" <gi...@apache.org>.
pzampino commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1459490179


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);

Review Comment:
   Perhaps, even just a check for '&' since I don't think that's expected in unescaped forms of this content?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "zeroflag (via GitHub)" <gi...@apache.org>.
zeroflag commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1457211424


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);
+      return StringEscapeUtils.escapeXml10(unescapedValue);

Review Comment:
   Is there a difference between `.escapeXml11` which was used before and `.escapeXml10`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "pzampino (via GitHub)" <gi...@apache.org>.
pzampino commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1456373088


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);

Review Comment:
   I wonder if a "cheap" check for escaped values is worthwhile here before unescaping.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KNOX-3001 - Avoid double XML-escaping during topology persistence from descriptors [knox]

Posted by "pzampino (via GitHub)" <gi...@apache.org>.
pzampino commented on code in PR #834:
URL: https://github.com/apache/knox/pull/834#discussion_r1459492673


##########
gateway-topology-simple/src/main/java/org/apache/knox/gateway/topology/simple/SimpleDescriptorHandler.java:
##########
@@ -635,6 +635,15 @@ private static Map<String, File> generateTopology(final SimpleDescriptor desc,
         return result;
     }
 
+    /*
+     * First, undoes any previous manual XML escape.
+     * Second applies XML-escape on the result of the first step.
+     */
+    private static String getXmlEscapedValue(String value) {
+      final String unescapedValue = StringEscapeUtils.unescapeXml(value);

Review Comment:
   Proper parsing for &XXX; is probably more expensive than the unescaping.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@knox.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org