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


##########
src/main/java/org/apache/sling/xss/impl/CustomPolicy.java:
##########
@@ -0,0 +1,265 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import javax.annotation.Nullable;
+
+import org.apache.sling.xss.impl.style.CssValidator;
+import org.apache.sling.xss.impl.xml.Attribute;
+import org.apache.sling.xss.impl.xml.Policy;
+import org.apache.sling.xss.impl.xml.Tag;
+import org.owasp.html.AttributePolicy;
+import org.owasp.html.HtmlPolicyBuilder;
+import org.owasp.html.PolicyFactory;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSet;
+
+public class CustomPolicy {
+    private PolicyFactory policyFactory;
+    private List<String> onInvalidRemoveTagList = new ArrayList<>();
+    private Map<String, AttributePolicy> dynamicAttributesPolicyMap = new 
HashMap<>();
+    private CssValidator cssValidator;
+
+    public CustomPolicy(Policy policy) {
+        removeAttributeGuards();
+        HtmlPolicyBuilder policyBuilder = new HtmlPolicyBuilder();
+
+        cssValidator = new CssValidator(policy.getCssPolicy());
+
+        // ------------ this is for the global attributes -------------
+        Map<String, Attribute> globalAttributes = policy.getGlobalAttributes();
+        for (Attribute attribute : globalAttributes.values()) {
+
+            if (attribute.getOnInvalid().equals("removeTag")) {
+                onInvalidRemoveTagList.add(attribute.getName());
+            }
+
+            if (CssValidator.STYLE_ATTRIBUTE_NAME.equals(attribute.getName())) 
{
+                // we match style tags separately
+                
policyBuilder.allowAttributes(attribute.getName()).matching(cssValidator.newCssAttributePolicy())
+                        .globally();
+            } else {
+                List<String> allowedValuesFromAttribute = 
attribute.getLiterals();
+                if (allowedValuesFromAttribute != null && 
allowedValuesFromAttribute.size() > 0) {
+                    for (String allowedValue : allowedValuesFromAttribute) {
+                        
policyBuilder.allowAttributes(attribute.getName()).matching(true, 
allowedValue).globally();
+                    }
+
+                }
+                List<Pattern> regexsFromAttribute = attribute.getPatternList();
+                if (regexsFromAttribute != null && regexsFromAttribute.size() 
> 0) {
+                    
policyBuilder.allowAttributes(attribute.getName()).matching(matchesToPatterns(regexsFromAttribute))
+                            .globally();
+                } else {
+                    
policyBuilder.allowAttributes(attribute.getName()).globally();
+                }
+
+            }
+        }
+
+        // ------------ this is for the allowed emty tags -------------
+        List<String> allowedEmptyTags = policy.getAllowedEmptyTags();
+        for (String allowedEmptyTag : allowedEmptyTags) {
+            policyBuilder.allowWithoutAttributes(allowedEmptyTag);
+        }
+
+        // ------------ this is for the tag rules -------------
+        Map<String, Tag> tagMap = policy.getTagRules();
+        for (Map.Entry<String, Tag> tag : tagMap.entrySet()) {
+
+            String tagAction = tag.getValue().getAction();
+            switch (tagAction) {
+            // Tag.action
+            case "truncate":
+                policyBuilder.allowElements(tag.getValue().getName());
+
+                break;
+            // filter: remove tags, but keep content,
+            case "filter":
+                break;
+            // remove: remove tag and contents
+            case "remove":
+                policyBuilder.disallowElements(tag.getValue().getName());
+                break;
+
+            // validate is also the default
+            // validate: keep content as long as it passes rules,
+            default:
+                policyBuilder.allowElements(tag.getValue().getName());
+                boolean styleSeen = false;
+                // get the allowed Attributes for the tag
+                Map<String, Attribute> allowedAttributes = 
tag.getValue().getAttributeMap();
+                if (allowedAttributes != null && allowedAttributes.size() > 0) 
{
+
+                    // if there are allowed Attributes, map over them
+                    for (Attribute attribute : allowedAttributes.values()) {
+                        if (attribute.getOnInvalid().equals("removeTag")) {
+                            onInvalidRemoveTagList.add(attribute.getName());
+                        }
+                        if 
(CssValidator.STYLE_ATTRIBUTE_NAME.equals(attribute.getName()))
+                            styleSeen = true;
+                        List<String> allowedValuesFromAttribute = 
attribute.getLiterals();
+                        if (allowedValuesFromAttribute != null && 
allowedValuesFromAttribute.size() > 0) {
+                            for (String allowedValue : 
allowedValuesFromAttribute) {
+                                
policyBuilder.allowAttributes(attribute.getName()).matching(true, allowedValue)
+                                        .onElements(tag.getValue().getName());
+                            }
+
+                        }
+                        List<Pattern> regexsFromAttribute = 
attribute.getPatternList();
+                        if (regexsFromAttribute != null && 
regexsFromAttribute.size() > 0) {
+                            policyBuilder.allowAttributes(attribute.getName())
+                                    
.matching(matchesToPatterns(regexsFromAttribute))
+                                    .onElements(tag.getValue().getName());
+                        } else {
+                            
policyBuilder.allowAttributes(attribute.getName()).onElements(tag.getValue().getName());
+                        }
+                    }
+
+                    if (!styleSeen) {
+                        
policyBuilder.allowAttributes(CssValidator.STYLE_ATTRIBUTE_NAME)
+                                
.matching(cssValidator.newCssAttributePolicy()).onElements(tag.getValue().getName());
+                    }
+                }
+                break;
+            }
+        }
+
+        // disallow style tag on specific elements
+        policyBuilder.disallowAttributes(CssValidator.STYLE_ATTRIBUTE_NAME)
+                .onElements(cssValidator.getDisallowedTagNames().toArray(new 
String[0]));
+
+        // ---------- dynamic attributes ------------
+        Map<String, Attribute> dynamicAttributes = new HashMap<>();
+        // checks if the dynamic attributes are allowed
+        if 
(policy.getDirectives().get("allowDynamicAttributes").equals("true")) {
+            dynamicAttributes.putAll(policy.getDynamicAttributes());
+            for (Attribute attribute : dynamicAttributes.values()) {
+                if (attribute.getOnInvalid().equals("removeTag")) {

Review Comment:
   Resolved: 81cc4a4f07ce77e8ac383fe3263a0787a2ffa447



##########
src/main/java/org/apache/sling/xss/impl/CustomPolicy.java:
##########
@@ -0,0 +1,265 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import javax.annotation.Nullable;
+
+import org.apache.sling.xss.impl.style.CssValidator;
+import org.apache.sling.xss.impl.xml.Attribute;
+import org.apache.sling.xss.impl.xml.Policy;
+import org.apache.sling.xss.impl.xml.Tag;
+import org.owasp.html.AttributePolicy;
+import org.owasp.html.HtmlPolicyBuilder;
+import org.owasp.html.PolicyFactory;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSet;
+
+public class CustomPolicy {
+    private PolicyFactory policyFactory;
+    private List<String> onInvalidRemoveTagList = new ArrayList<>();
+    private Map<String, AttributePolicy> dynamicAttributesPolicyMap = new 
HashMap<>();
+    private CssValidator cssValidator;
+
+    public CustomPolicy(Policy policy) {
+        removeAttributeGuards();
+        HtmlPolicyBuilder policyBuilder = new HtmlPolicyBuilder();
+
+        cssValidator = new CssValidator(policy.getCssPolicy());
+
+        // ------------ this is for the global attributes -------------
+        Map<String, Attribute> globalAttributes = policy.getGlobalAttributes();
+        for (Attribute attribute : globalAttributes.values()) {
+
+            if (attribute.getOnInvalid().equals("removeTag")) {
+                onInvalidRemoveTagList.add(attribute.getName());
+            }
+
+            if (CssValidator.STYLE_ATTRIBUTE_NAME.equals(attribute.getName())) 
{
+                // we match style tags separately
+                
policyBuilder.allowAttributes(attribute.getName()).matching(cssValidator.newCssAttributePolicy())
+                        .globally();
+            } else {
+                List<String> allowedValuesFromAttribute = 
attribute.getLiterals();
+                if (allowedValuesFromAttribute != null && 
allowedValuesFromAttribute.size() > 0) {
+                    for (String allowedValue : allowedValuesFromAttribute) {
+                        
policyBuilder.allowAttributes(attribute.getName()).matching(true, 
allowedValue).globally();
+                    }
+
+                }
+                List<Pattern> regexsFromAttribute = attribute.getPatternList();
+                if (regexsFromAttribute != null && regexsFromAttribute.size() 
> 0) {
+                    
policyBuilder.allowAttributes(attribute.getName()).matching(matchesToPatterns(regexsFromAttribute))
+                            .globally();
+                } else {
+                    
policyBuilder.allowAttributes(attribute.getName()).globally();
+                }
+
+            }
+        }
+
+        // ------------ this is for the allowed emty tags -------------
+        List<String> allowedEmptyTags = policy.getAllowedEmptyTags();
+        for (String allowedEmptyTag : allowedEmptyTags) {
+            policyBuilder.allowWithoutAttributes(allowedEmptyTag);
+        }
+
+        // ------------ this is for the tag rules -------------
+        Map<String, Tag> tagMap = policy.getTagRules();
+        for (Map.Entry<String, Tag> tag : tagMap.entrySet()) {
+
+            String tagAction = tag.getValue().getAction();
+            switch (tagAction) {
+            // Tag.action
+            case "truncate":
+                policyBuilder.allowElements(tag.getValue().getName());
+
+                break;
+            // filter: remove tags, but keep content,
+            case "filter":
+                break;
+            // remove: remove tag and contents
+            case "remove":
+                policyBuilder.disallowElements(tag.getValue().getName());
+                break;
+
+            // validate is also the default
+            // validate: keep content as long as it passes rules,
+            default:
+                policyBuilder.allowElements(tag.getValue().getName());
+                boolean styleSeen = false;
+                // get the allowed Attributes for the tag
+                Map<String, Attribute> allowedAttributes = 
tag.getValue().getAttributeMap();
+                if (allowedAttributes != null && allowedAttributes.size() > 0) 
{
+
+                    // if there are allowed Attributes, map over them
+                    for (Attribute attribute : allowedAttributes.values()) {
+                        if (attribute.getOnInvalid().equals("removeTag")) {
+                            onInvalidRemoveTagList.add(attribute.getName());
+                        }
+                        if 
(CssValidator.STYLE_ATTRIBUTE_NAME.equals(attribute.getName()))
+                            styleSeen = true;
+                        List<String> allowedValuesFromAttribute = 
attribute.getLiterals();
+                        if (allowedValuesFromAttribute != null && 
allowedValuesFromAttribute.size() > 0) {
+                            for (String allowedValue : 
allowedValuesFromAttribute) {
+                                
policyBuilder.allowAttributes(attribute.getName()).matching(true, allowedValue)
+                                        .onElements(tag.getValue().getName());
+                            }
+
+                        }
+                        List<Pattern> regexsFromAttribute = 
attribute.getPatternList();
+                        if (regexsFromAttribute != null && 
regexsFromAttribute.size() > 0) {
+                            policyBuilder.allowAttributes(attribute.getName())
+                                    
.matching(matchesToPatterns(regexsFromAttribute))
+                                    .onElements(tag.getValue().getName());
+                        } else {
+                            
policyBuilder.allowAttributes(attribute.getName()).onElements(tag.getValue().getName());
+                        }
+                    }
+
+                    if (!styleSeen) {
+                        
policyBuilder.allowAttributes(CssValidator.STYLE_ATTRIBUTE_NAME)
+                                
.matching(cssValidator.newCssAttributePolicy()).onElements(tag.getValue().getName());
+                    }
+                }
+                break;
+            }
+        }
+
+        // disallow style tag on specific elements
+        policyBuilder.disallowAttributes(CssValidator.STYLE_ATTRIBUTE_NAME)
+                .onElements(cssValidator.getDisallowedTagNames().toArray(new 
String[0]));
+
+        // ---------- dynamic attributes ------------
+        Map<String, Attribute> dynamicAttributes = new HashMap<>();
+        // checks if the dynamic attributes are allowed
+        if 
(policy.getDirectives().get("allowDynamicAttributes").equals("true")) {

Review Comment:
   Resolved: 81cc4a4f07ce77e8ac383fe3263a0787a2ffa447



-- 
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-unsubscr...@sling.apache.org

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

Reply via email to