From 32e273507da0e964b58c50fd8a4c94c9d9363af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Passaro?= Date: Sat, 9 Apr 2022 18:47:07 -0300 Subject: [PATCH 1/2] Fix child node removal on style tag processing --- .../owasp/validator/html/scan/AntiSamyDOMScanner.java | 10 ++++------ .../org/owasp/validator/html/test/AntiSamyTest.java | 4 ++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java b/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java index dc991c46..53747cb2 100644 --- a/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java +++ b/src/main/java/org/owasp/validator/html/scan/AntiSamyDOMScanner.java @@ -407,7 +407,8 @@ private boolean processStyleTag(Element ele, Node parentNode) { CssScanner styleScanner = new CssScanner(policy, messages, policy.isEmbedStyleSheets()); try { - if (ele.getChildNodes().getLength() > 0) { + int childNodesCount = ele.getChildNodes().getLength(); + if (childNodesCount > 0) { StringBuffer toScan = new StringBuffer(); for (int i = 0; i < ele.getChildNodes().getLength(); i++) { @@ -428,7 +429,6 @@ private boolean processStyleTag(Element ele, Node parentNode) { * would normally be left with an empty style tag and * break all CSS. To prevent that, we have this check. */ - String cleanHTML = cr.getCleanHTML(); cleanHTML = cleanHTML == null || cleanHTML.equals("") ? "/* */" : cleanHTML; @@ -436,21 +436,19 @@ private boolean processStyleTag(Element ele, Node parentNode) { /* * Remove every other node after cleaning CSS, there will * be only one node in the end, as it always should have. + * Starting from the end due to list updating on the fly. */ - for (int i = 1; i < ele.getChildNodes().getLength(); i++) { + for (int i = childNodesCount - 1; i >= 1; i--) { Node childNode = ele.getChildNodes().item(i); ele.removeChild(childNode); } } - } catch (DOMException | ScanException | ParseException | NumberFormatException e) { - /* * ParseException shouldn't be possible anymore, but we'll leave it * here because I (Arshan) am hilariously dumb sometimes. * Batik can throw NumberFormatExceptions (see bug #48). */ - addError(ErrorMessageUtil.ERROR_CSS_TAG_MALFORMED, new Object[]{HTMLEntityEncoder.htmlEntityEncode(ele.getFirstChild().getNodeValue())}); parentNode.removeChild(ele); return true; diff --git a/src/test/java/org/owasp/validator/html/test/AntiSamyTest.java b/src/test/java/org/owasp/validator/html/test/AntiSamyTest.java index e251acb5..d8b9cfdd 100644 --- a/src/test/java/org/owasp/validator/html/test/AntiSamyTest.java +++ b/src/test/java/org/owasp/validator/html/test/AntiSamyTest.java @@ -1713,10 +1713,14 @@ public void testSmuggledTagsInStyleContent() throws ScanException, PolicyExcepti Policy revised = policy.cloneWithDirective(Policy.USE_XHTML,"true"); assertThat(as.scan("test", revised, AntiSamy.DOM).getCleanHTML(), not(containsString("javascript"))); assertThat(as.scan("test", revised, AntiSamy.SAX).getCleanHTML(), not(containsString("javascript"))); + assertThat(as.scan("kinput/onfocus=alert(1)>", revised, AntiSamy.DOM).getCleanHTML(), not(containsString("input"))); + assertThat(as.scan("kinput/onfocus=alert(1)>", revised, AntiSamy.SAX).getCleanHTML(), not(containsString("input"))); Policy revised2 = policy.cloneWithDirective(Policy.USE_XHTML,"false"); assertThat(as.scan("Walert(1)", revised2, AntiSamy.DOM).getCleanHTML(), not(containsString("script"))); assertThat(as.scan("Walert(1)", revised2, AntiSamy.SAX).getCleanHTML(), not(containsString("script"))); + assertThat(as.scan("kinput/onfocus=alert(1)>", revised2, AntiSamy.DOM).getCleanHTML(), not(containsString("input"))); + assertThat(as.scan("kinput/onfocus=alert(1)>", revised2, AntiSamy.SAX).getCleanHTML(), not(containsString("input"))); } @Test(timeout = 3000) From a0ec25d2ec70b794e6a78f6251c21735696ea832 Mon Sep 17 00:00:00 2001 From: Dave Wichers Date: Sun, 10 Apr 2022 16:17:55 -0400 Subject: [PATCH 2/2] Update pom to reflect 1.6.7 release. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1504f23a..882a862e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.owasp.antisamy antisamy jar - 1.6.6.1 + 1.6.7 ossrh @@ -43,7 +43,7 @@ true UTF-8 - 2022-04-07T19:23:45Z + 2022-04-10T18:12:34Z 1.7 1.12.0 2.11.0