You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2022/09/08 08:06:48 UTC

[GitHub] [sling-org-apache-sling-xss] nonanalou commented on a diff in pull request #28: SLING-7231 Move to owasp sanitizer library

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


##########
src/main/java/org/apache/sling/xss/impl/style/BatikCssCleaner.java:
##########
@@ -0,0 +1,82 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.style;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.apache.batik.css.parser.Parser;
+import org.apache.sling.xss.impl.xml.AntiSamyPolicy.CssPolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.css.sac.CSSException;
+import org.w3c.css.sac.InputSource;
+
+public class BatikCssCleaner {
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final CssPolicy cssPolicy;
+
+    private static final String CDATA_PRE = "<![CDATA[";
+    private static final String CDATA_POST = "]]>";
+
+    public BatikCssCleaner(CssPolicy cssPolicy) {
+        this.cssPolicy = cssPolicy;
+    }
+
+    /**
+     * Parses a CSS stylesheet and returns it in a safe form
+     *
+     * @param untrustedCss a complete CSS stylesheet
+     * @return the cleaned CSS stylesheet text
+     */
+    public String cleanStylesheet(String untrustedCss) {
+        try {
+            if ( untrustedCss.startsWith(CDATA_PRE) && untrustedCss.endsWith(CDATA_POST) )
+                untrustedCss = untrustedCss.substring(CDATA_PRE.length(), untrustedCss.length() - CDATA_POST.length());
+            Parser parser = new Parser();
+            ValidatingDocumentHandler handler = new ValidatingDocumentHandler(cssPolicy, false);
+            parser.setDocumentHandler(handler);
+            parser.parseStyleSheet(new InputSource(new StringReader(untrustedCss)));
+            return handler.getValidCss();
+        } catch (CSSException | IOException e) {
+            logger.debug("Unexpected error while cleaning stylesheet", e);

Review Comment:
   This method is called, by the [CSSValidator newCssAttributePolicy Method](https://github.com/apache/sling-org-apache-sling-xss/pull/28/files#diff-6abfffeec1aa36d3bbef5bb53c2a5ace07f61bab8c38b34a5bf11002f8457d4eR45).
   And this is an Override of the[ AttributePolicy apply Method](https://github.com/OWASP/java-html-sanitizer/blob/main/src/main/java/org/owasp/html/AttributePolicy.java#L59), which I did not write.
   Should i catch it instead in the CSSValidator class?



##########
src/main/java/org/apache/sling/xss/impl/style/BatikCssCleaner.java:
##########
@@ -0,0 +1,82 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.style;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.apache.batik.css.parser.Parser;
+import org.apache.sling.xss.impl.xml.AntiSamyPolicy.CssPolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.css.sac.CSSException;
+import org.w3c.css.sac.InputSource;
+
+public class BatikCssCleaner {
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final CssPolicy cssPolicy;
+
+    private static final String CDATA_PRE = "<![CDATA[";
+    private static final String CDATA_POST = "]]>";
+
+    public BatikCssCleaner(CssPolicy cssPolicy) {
+        this.cssPolicy = cssPolicy;
+    }
+
+    /**
+     * Parses a CSS stylesheet and returns it in a safe form
+     *
+     * @param untrustedCss a complete CSS stylesheet
+     * @return the cleaned CSS stylesheet text
+     */
+    public String cleanStylesheet(String untrustedCss) {
+        try {
+            if ( untrustedCss.startsWith(CDATA_PRE) && untrustedCss.endsWith(CDATA_POST) )
+                untrustedCss = untrustedCss.substring(CDATA_PRE.length(), untrustedCss.length() - CDATA_POST.length());
+            Parser parser = new Parser();
+            ValidatingDocumentHandler handler = new ValidatingDocumentHandler(cssPolicy, false);
+            parser.setDocumentHandler(handler);
+            parser.parseStyleSheet(new InputSource(new StringReader(untrustedCss)));
+            return handler.getValidCss();
+        } catch (CSSException | IOException e) {
+            logger.debug("Unexpected error while cleaning stylesheet", e);
+            return "";
+        }
+    }
+
+    /**
+     * Parses a CSS style declaration (i.e. the text of a <tt>style</tt> attribute) and returns it in a safe form
+     *
+     * @param untrustedCss a css style declaration
+     * @return the cleaned CSS style declaration
+     */
+    public String cleanStyleDeclaration(String untrustedCss) {
+        try {
+            Parser parser = new Parser();
+            ValidatingDocumentHandler handler = new ValidatingDocumentHandler(cssPolicy, true);
+            parser.setDocumentHandler(handler);
+            parser.parseStyleDeclaration(new InputSource(new StringReader(untrustedCss)));
+            return handler.getValidCss();
+        } catch (CSSException | IOException e) {
+            logger.debug("Unexpected error while cleaning style declaration", e);

Review Comment:
   What do you mean with Exception handling? Do you mean logger.error() instead of logger.debug()?



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

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