nonanalou commented on code in PR #28:
URL: 
https://github.com/apache/sling-org-apache-sling-xss/pull/28#discussion_r944294489


##########
src/main/java/org/apache/sling/xss/impl/xml/MapBuilder.java:
##########
@@ -0,0 +1,247 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.xss.impl.xml;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.sling.xss.impl.PolicyException;
+
+class MapBuilder {
+
+    PolicyProvider policy;
+    // Antisamy hardcodes the allowed-empty-tags default:
+    // 
https://github.com/nahsra/antisamy/blob/main/src/main/java/org/owasp/validator/html/scan/Constants.java#L37
+    public static final List<String> ALLOWED_EMPTY_TAGS = Arrays.asList(
+            "br",
+            "hr",
+            "a",
+            "img",
+            "link",
+            "iframe",
+            "script",
+            "object",
+            "applet",
+            "frame",
+            "base",
+            "param",
+            "meta",
+            "input",
+            "textarea",
+            "embed",
+            "basefont",
+            "col");
+
+    public void createRulesMap(PolicyProvider policy, AntiSamyRules 
topLevelElement) throws PolicyException {
+        this.policy = policy;
+
+        parseCommonRegExps(topLevelElement.getRegexpList());
+        parseDirectives(topLevelElement.getDirectiveList());
+        parseAllowedEmptyTags(topLevelElement.getAllowedEmptyTags());
+        parseCommonAttributes(topLevelElement.getCommonAttributeList());
+        
parseGlobalAttributes(topLevelElement.getGlobalTagAttributes().getGlobalTagAttributeList());
+        
parseDynamicAttributes(topLevelElement.getDynamicTagAttribute().getDynamicTagAttributeList());
+        parseTagRules(topLevelElement.getTagRulesList());
+
+        parseCSSRules(topLevelElement.getPropertyList());
+    }
+
+    /**
+     * Go through the <common-regexps> section of the policy file.
+     *
+     * @param root                      Top level of <common-regexps>
+     */
+    private void parseCommonRegExps(List<Regexp> root) {
+        for (Regexp regex : root) {
+            String name = regex.getName();
+            Pattern regexp = Pattern.compile(regex.getValue(),
+                    Pattern.DOTALL);
+            policy.commonRegularExpressions.put(name, regexp);
+        }
+    }
+
+    /**
+     * Go through <directives> section of the policy file.
+     *
+     * @param root       Top level of <directives>
+     */
+    private void parseDirectives(List<Directive> root) {
+        for (Directive directive : root) {
+            String name = directive.getName();
+            String value = directive.getValue();
+            policy.directives.put(name, value);
+        }
+    }
+
+    private void parseCommonAttributes(List<Attribute> root) {
+        for (Attribute attribute : root) {
+            List<Regexp> allowedRegexps = 
getAllowedRegexps(attribute.getRegexpList());
+            Attribute newAttribute = new Attribute(attribute.getName(), 
allowedRegexps, attribute.getLiteralList(),
+                    attribute.getOnInvalid(), attribute.getDescription());
+            policy.commonAttributes.put(attribute.getName().toLowerCase(), 
newAttribute);
+        }
+    }
+
+    // /**

Review Comment:
   Resolved: 99bb91ab6e2529fd6ef25858f874a26cb159e87e



##########
src/main/java/org/apache/sling/xss/impl/xml/MapBuilder.java:
##########
@@ -0,0 +1,247 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.xss.impl.xml;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import org.apache.sling.xss.impl.PolicyException;
+
+class MapBuilder {
+
+    PolicyProvider policy;
+    // Antisamy hardcodes the allowed-empty-tags default:
+    // 
https://github.com/nahsra/antisamy/blob/main/src/main/java/org/owasp/validator/html/scan/Constants.java#L37
+    public static final List<String> ALLOWED_EMPTY_TAGS = Arrays.asList(
+            "br",
+            "hr",
+            "a",
+            "img",
+            "link",
+            "iframe",
+            "script",
+            "object",
+            "applet",
+            "frame",
+            "base",
+            "param",
+            "meta",
+            "input",
+            "textarea",
+            "embed",
+            "basefont",
+            "col");
+
+    public void createRulesMap(PolicyProvider policy, AntiSamyRules 
topLevelElement) throws PolicyException {
+        this.policy = policy;
+
+        parseCommonRegExps(topLevelElement.getRegexpList());
+        parseDirectives(topLevelElement.getDirectiveList());
+        parseAllowedEmptyTags(topLevelElement.getAllowedEmptyTags());
+        parseCommonAttributes(topLevelElement.getCommonAttributeList());
+        
parseGlobalAttributes(topLevelElement.getGlobalTagAttributes().getGlobalTagAttributeList());
+        
parseDynamicAttributes(topLevelElement.getDynamicTagAttribute().getDynamicTagAttributeList());
+        parseTagRules(topLevelElement.getTagRulesList());
+
+        parseCSSRules(topLevelElement.getPropertyList());
+    }
+
+    /**
+     * Go through the <common-regexps> section of the policy file.
+     *
+     * @param root                      Top level of <common-regexps>
+     */
+    private void parseCommonRegExps(List<Regexp> root) {
+        for (Regexp regex : root) {
+            String name = regex.getName();
+            Pattern regexp = Pattern.compile(regex.getValue(),
+                    Pattern.DOTALL);
+            policy.commonRegularExpressions.put(name, regexp);
+        }
+    }
+
+    /**
+     * Go through <directives> section of the policy file.
+     *
+     * @param root       Top level of <directives>
+     */
+    private void parseDirectives(List<Directive> root) {
+        for (Directive directive : root) {
+            String name = directive.getName();
+            String value = directive.getValue();
+            policy.directives.put(name, value);
+        }
+    }
+
+    private void parseCommonAttributes(List<Attribute> root) {
+        for (Attribute attribute : root) {
+            List<Regexp> allowedRegexps = 
getAllowedRegexps(attribute.getRegexpList());
+            Attribute newAttribute = new Attribute(attribute.getName(), 
allowedRegexps, attribute.getLiteralList(),
+                    attribute.getOnInvalid(), attribute.getDescription());
+            policy.commonAttributes.put(attribute.getName().toLowerCase(), 
newAttribute);
+        }
+    }
+
+    // /**
+    // * Go through <allowed-empty-tags> section of the policy file.
+    // *
+    // * @param allowedEmptyTagsListNode Top level of <allowed-empty-tags>
+    // * @param allowedEmptyTags The tags that can be empty
+    // */
+    private void parseAllowedEmptyTags(AllowedEmptyTags allowedEmptyTagsList) 
throws PolicyException {
+        if (allowedEmptyTagsList != null) {
+            policy.allowedEmptyTags = allowedEmptyTagsList.getLiterals();
+        } else
+            policy.allowedEmptyTags.addAll(ALLOWED_EMPTY_TAGS);
+    }
+
+    // /**

Review Comment:
   Resolved: 99bb91ab6e2529fd6ef25858f874a26cb159e87e



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to