This is an automated email from the ASF dual-hosted git repository.

mraible pushed a commit to branch html-subset-jsoup
in repository https://gitbox.apache.org/repos/asf/roller.git

commit c7e255b89a197a9feb55d6ca13a3adb212df3c21
Author: Matt Raible <[email protected]>
AuthorDate: Wed Nov 18 10:27:36 2026 -0700

    Restrict HTML comments to the HTML subset with jsoup's Cleaner
    
    The HTML subset was built with regular expressions: the comment was 
HTML-encoded, then 21 patterns turned the encoded tags in the subset back into 
tags and rebuilt links. Utilities.sanitizeToHTMLSubset now uses jsoup's Cleaner 
with a Safelist of the same tags (a, b, blockquote, br, i, li, ol, p, pre, ul) 
and the same link schemes (http, https, mailto) on the raw HTML. The HTMLSubset 
comment plugin and HTML comment notification e-mails call it directly. 
transformToHTMLSubset stays for  [...]
    
    What changes for commenters:
    - A tag outside the subset is removed and its text kept, instead of being 
shown as encoded markup. Script and style contents are removed.
    - A link keeps its href when it has other attributes, such as a title; the 
other attributes are removed.
    - The output is jsoup's: tags are balanced, <br> has no slash, and the link 
scheme is lowercase.
    
    The new tests cover script, style, event handler, javascript: and data: 
links, an entity-split javascript: scheme, SVG, inline styles, HTML comments, 
iframes, and a quote inside an href.
---
 .../business/plugins/comment/HTMLSubsetPlugin.java |   9 +-
 .../org/apache/roller/weblogger/util/MailUtil.java |   2 +-
 .../apache/roller/weblogger/util/Utilities.java    | 146 +++++----------------
 .../wrapper/WeblogEntryCommentWrapperTest.java     |   3 +-
 .../roller/weblogger/util/UtilitiesTest.java       |  44 ++++++-
 5 files changed, 74 insertions(+), 130 deletions(-)

diff --git 
a/app/src/main/java/org/apache/roller/weblogger/business/plugins/comment/HTMLSubsetPlugin.java
 
b/app/src/main/java/org/apache/roller/weblogger/business/plugins/comment/HTMLSubsetPlugin.java
index fda6ae858..3c34ce598 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/business/plugins/comment/HTMLSubsetPlugin.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/business/plugins/comment/HTMLSubsetPlugin.java
@@ -64,14 +64,7 @@ public class HTMLSubsetPlugin implements 
WeblogEntryCommentPlugin {
         
         // only do this if comment is HTML
         if ("text/html".equals(comment.getContentType())) {
-            LOG.debug("ending value:\n" + output);
-                       
-               // escape html
-               output = Utilities.escapeHTML(output);
-               
-               // just use old utilities method
-               output = Utilities.transformToHTMLSubset(output);
-               LOG.debug("starting value:\n" + text);
+            output = Utilities.sanitizeToHTMLSubset(output);
         }
                 
         return output;
diff --git a/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java 
b/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
index a26d939c2..8ccb74d5b 100644
--- a/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
+++ b/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
@@ -336,7 +336,7 @@ public class MailUtil {
 
         // Don't escape the content if email will be sent as plain text
         msg.append((isPlainText) ? commentObject.getContent()
-        : 
Utilities.transformToHTMLSubset(Utilities.escapeHTML(commentObject.getContent())));
+        : Utilities.sanitizeToHTMLSubset(commentObject.getContent()));
         
         msg.append((isPlainText) ? "\n\n----\n"
                 : "<br /><br /><hr /><span style=\"font-size: 11px\">");
diff --git a/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java 
b/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
index 569d5a32b..95129521a 100644
--- a/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
+++ b/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java
@@ -53,6 +53,7 @@ import org.apache.roller.util.RollerConstants;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
+import org.jsoup.safety.Safelist;
 
 /**
  * General purpose utilities, not for use in templates.
@@ -63,53 +64,12 @@ public class Utilities {
 
     public static final String TAG_SPLIT_CHARS = " ,\n\r\f\t";
 
-    private static final Pattern OPENING_B_TAG_PATTERN = Pattern.compile(
-            "&lt;b&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_B_TAG_PATTERN = Pattern.compile(
-            "&lt;/b&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_I_TAG_PATTERN = Pattern.compile(
-            "&lt;i&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_I_TAG_PATTERN = Pattern.compile(
-            "&lt;/i&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN = Pattern
-            .compile("&lt;blockquote&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN = Pattern
-            .compile("&lt;/blockquote&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern BR_TAG_PATTERN = Pattern.compile(
-            "&lt;br */*&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_P_TAG_PATTERN = Pattern.compile(
-            "&lt;p&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_P_TAG_PATTERN = Pattern.compile(
-            "&lt;/p&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_PRE_TAG_PATTERN = Pattern.compile(
-            "&lt;pre&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_PRE_TAG_PATTERN = Pattern.compile(
-            "&lt;/pre&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_UL_TAG_PATTERN = Pattern.compile(
-            "&lt;ul&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_UL_TAG_PATTERN = Pattern.compile(
-            "&lt;/ul&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_OL_TAG_PATTERN = Pattern.compile(
-            "&lt;ol&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_OL_TAG_PATTERN = Pattern.compile(
-            "&lt;/ol&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_LI_TAG_PATTERN = Pattern.compile(
-            "&lt;li&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_LI_TAG_PATTERN = Pattern.compile(
-            "&lt;/li&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern CLOSING_A_TAG_PATTERN = Pattern.compile(
-            "&lt;/a&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern OPENING_A_TAG_START_PATTERN = Pattern.compile(
-            "&lt;a\\s+href\\s*=", Pattern.CASE_INSENSITIVE);
-    private static final Pattern TAG_END_PATTERN = Pattern.compile(
-            "&gt;", Pattern.CASE_INSENSITIVE);
-    private static final Pattern A_HREF_PATTERN = Pattern.compile(
-            
"&lt;a\\s+href\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s\"'=<>`]+))\\s*&gt;",
-            Pattern.CASE_INSENSITIVE);
-    private static final Pattern SUBSET_LINK_PATTERN = Pattern.compile(
-            "(?:https?://|mailto:)[^\\x00-\\x20\\x7f]+", 
Pattern.CASE_INSENSITIVE);
-    private static final Pattern QUOTE_PATTERN = Pattern.compile("&quot;",
-            Pattern.CASE_INSENSITIVE);
+    // Comment HTML that survives sanitizeToHTMLSubset: a few formatting tags,
+    // and links to http, https and mailto addresses
+    private static final Safelist HTML_SUBSET = new Safelist()
+            .addTags("a", "b", "blockquote", "br", "i", "li", "ol", "p", 
"pre", "ul")
+            .addAttributes("a", "href")
+            .addProtocols("a", "href", "http", "https", "mailto");
 
     // ------------------------------------------------------------------------
     /** Strip jsessionid off of a URL */
@@ -1043,78 +1003,36 @@ public class Utilities {
     }
 
     /**
-     * Transforms the given String into a subset of HTML displayable on a web
-     * page. The subset includes &lt;b&gt;, &lt;i&gt;, &lt;p&gt;, &lt;br&gt;,
-     * &lt;pre&gt; and &lt;a href&gt; (and their corresponding end tags).
-     * 
-     * @param s
-     *            the String to transform
-     * @return the transformed String
+     * Reduces HTML, such as a comment, to the subset of HTML a weblog page
+     * shows: &lt;b&gt;, &lt;i&gt;, &lt;blockquote&gt;, &lt;br&gt;, &lt;p&gt;,
+     * &lt;pre&gt;, lists, and links to http, https and mailto addresses.
+     * jsoup's Cleaner removes every other tag and attribute, keeping the text
+     * inside them, and the text is HTML-encoded.
+     *
+     * @param html the HTML to reduce
+     * @return the HTML subset, or null if html is null
      */
-    public static String transformToHTMLSubset(String s) {
-
-        if (s == null) {
+    public static String sanitizeToHTMLSubset(String html) {
+        if (html == null) {
             return null;
         }
-
-        s = replace(s, OPENING_B_TAG_PATTERN, "<b>");
-        s = replace(s, CLOSING_B_TAG_PATTERN, "</b>");
-        s = replace(s, OPENING_I_TAG_PATTERN, "<i>");
-        s = replace(s, CLOSING_I_TAG_PATTERN, "</i>");
-        s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>");
-        s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>");
-        s = replace(s, BR_TAG_PATTERN, "<br />");
-        s = replace(s, OPENING_P_TAG_PATTERN, "<p>");
-        s = replace(s, CLOSING_P_TAG_PATTERN, "</p>");
-        s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>");
-        s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>");
-        s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>");
-        s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>");
-        s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>");
-        s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>");
-        s = replace(s, OPENING_LI_TAG_PATTERN, "<li>");
-        s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>");
-        s = replace(s, QUOTE_PATTERN, "\"");
-
-        // Normalize supported links while retaining the surrounding text.
-        s = replace(s, CLOSING_A_TAG_PATTERN, "</a>");
-        // Each opening tag runs to the first "&gt;" after its "href=". Once no
-        // "&gt;" is left, no later tag can close either, so stop there; this
-        // keeps the scan linear on text with many unclosed tags.
-        Matcher m = OPENING_A_TAG_START_PATTERN.matcher(s);
-        Matcher tagEnd = TAG_END_PATTERN.matcher(s);
-        StringBuilder result = new StringBuilder(s.length());
-        int end = 0;
-        while (m.find() && tagEnd.find(m.end())) {
-            result.append(restoreSubsetEntities(s.substring(end, m.start())));
-            Matcher hrefMatcher = 
A_HREF_PATTERN.matcher(s.substring(m.start(), tagEnd.end()));
-            String link = "<a>";
-            if (hrefMatcher.matches()) {
-                String href = hrefMatcher.group(1);
-                if (href == null) {
-                    href = hrefMatcher.group(2);
-                }
-                if (href == null) {
-                    href = hrefMatcher.group(3);
-                }
-                href = 
StringEscapeUtils.unescapeHtml4(restoreSubsetEntities(href));
-                if (SUBSET_LINK_PATTERN.matcher(href).matches()) {
-                    link = "<a href=\"" + StringEscapeUtils.escapeHtml4(href) 
+ "\">";
-                }
-            }
-            result.append(link);
-            end = tagEnd.end();
-            m.region(end, s.length());
-        }
-        result.append(restoreSubsetEntities(s.substring(end)));
-        return result.toString();
+        return Jsoup.clean(html, "", HTML_SUBSET,
+                new Document.OutputSettings().prettyPrint(false));
     }
 
-    private static String restoreSubsetEntities(String s) {
-        s = s.replace("&amp;lt;", "&lt;");
-        s = s.replace("&amp;gt;", "&gt;");
-        s = s.replace("&amp;#", "&#");
-        return s;
+    /**
+     * Like sanitizeToHTMLSubset, for text that is already HTML-encoded, such
+     * as a comment passed through escapeHTML: the encoded tags in the subset
+     * become tags again.
+     *
+     * @param s the HTML-encoded text
+     * @return the HTML subset, or null if s is null
+     */
+    public static String transformToHTMLSubset(String s) {
+        if (s == null) {
+            return null;
+        }
+        return sanitizeToHTMLSubset(StringEscapeUtils.unescapeHtml4(s));
     }
 
     private static String replace(String string, Pattern pattern,
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryCommentWrapperTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryCommentWrapperTest.java
index 864507d6a..681006633 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryCommentWrapperTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryCommentWrapperTest.java
@@ -124,7 +124,8 @@ class WeblogEntryCommentWrapperTest {
 
     @Test
     void htmlCommentKeepsHtmlSubsetOutput() {
-        assertEquals("<b>Hi</b>&lt;img src=x onerror=alert(1)&gt;",
+        // the image isn't in the subset, so it's removed
+        assertEquals("<b>Hi</b>",
                 content("text/html", "HTMLSubset", List.of(new 
HTMLSubsetPlugin())));
     }
 
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java 
b/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
index af6112288..ba795192e 100644
--- a/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
+++ b/app/src/test/java/org/apache/roller/weblogger/util/UtilitiesTest.java
@@ -36,7 +36,7 @@ public class UtilitiesTest  {
     public void testHtmlSubsetLinkFormats() {
         assertEquals("<a href=\"https://example.com/\";>site</a>",
                 htmlSubset("<a href='https://example.com/'>site</a>"));
-        assertEquals("<a href=\"HTTP://example.com/\">site</a>",
+        assertEquals("<a href=\"http://example.com/\";>site</a>",
                 htmlSubset("<A HREF=HTTP://example.com/>site</A>"));
         assertEquals("<a href=\"mailto:[email protected]\";>mail</a>",
                 htmlSubset("<a href=\"mailto:[email protected]\";>mail</a>"));
@@ -52,7 +52,8 @@ public class UtilitiesTest  {
     public void testHtmlSubsetLinkSubset() {
         assertEquals("<a>file</a>", htmlSubset("<a 
href=\"ftp://example.com/file\";>file</a>"));
         assertEquals("<a>local</a>", htmlSubset("<a 
href=\"../page\">local</a>"));
-        assertEquals("<a>site</a>",
+        // other attributes are removed, and the link stays
+        assertEquals("<a href=\"https://example.com/\";>site</a>",
                 htmlSubset("<a href=\"https://example.com/\"; 
title=\"site\">site</a>"));
         assertEquals("<a>empty</a>", htmlSubset("<a href=\"\">empty</a>"));
     }
@@ -62,8 +63,10 @@ public class UtilitiesTest  {
         assertNull(Utilities.transformToHTMLSubset(null));
         assertEquals("", htmlSubset(""));
         String text = "<p><b>Bold</b> and <i>italic</i><br>line</p>";
-        assertEquals("<p><b>Bold</b> and <i>italic</i><br />line</p>", 
htmlSubset(text));
-        assertEquals("&#65; &lt;example&gt;", htmlSubset("&#65; 
&lt;example&gt;"));
+        assertEquals("<p><b>Bold</b> and <i>italic</i><br>line</p>", 
htmlSubset(text));
+        assertEquals("A &lt;example&gt;", htmlSubset("&#65; &lt;example&gt;"));
+        
assertEquals("<blockquote><pre>code</pre></blockquote><ul><li>one</li></ul><ol><li>two</li></ol>",
+                
htmlSubset("<blockquote><pre>code</pre></blockquote><ul><li>one</li></ul><ol><li>two</li></ol>"));
         assertEquals("&lt;a href=\"https://example.com/\"&gt;site&lt;/a&gt;";,
                 htmlSubset("&lt;a 
href=\"https://example.com/\"&gt;site&lt;/a&gt;";));
         assertEquals("<a href=\"https://example.com/$1\";>one</a> <a>two</a>",
@@ -81,6 +84,34 @@ public class UtilitiesTest  {
         assertEquals(text, plugin.render(comment, text));
     }
 
+    @Test
+    public void testHtmlSubsetRemovesActiveContent() {
+        // tags outside the subset are removed; their text stays, encoded
+        assertEquals("<b>Hi</b>", htmlSubset("<b>Hi</b><img src=x 
onerror=alert(1)>"));
+        assertEquals("", htmlSubset("<script>alert(1)</script>"));
+        assertEquals("", htmlSubset("<style>body{display:none}</style>"));
+        assertEquals("<b>x</b>", htmlSubset("<b 
onmouseover=\"alert(1)\">x</b>"));
+        assertEquals("<a>x</a>", htmlSubset("<a 
href=\"javascript:alert(1)\">x</a>"));
+        assertEquals("<a>x</a>", htmlSubset("<a 
href=\"jav&#x09;ascript:alert(1)\">x</a>"));
+        assertEquals("<a>x</a>", htmlSubset("<a 
href=\"data:text/html,<script>alert(1)</script>\">x</a>"));
+        assertEquals("<a href=\"https://example.com/\";>x</a>",
+                htmlSubset("<svg><a href=\"https://example.com/\"; 
onload=alert(1)>x</a></svg>"));
+        assertEquals("<p>x</p>", htmlSubset("<p 
style=\"background:url(javascript:alert(1))\">x</p>"));
+        assertEquals("&lt;!-- --&gt; text", htmlSubset("&lt;!-- --&gt; text"));
+        assertEquals("text", htmlSubset("<!-- <script>alert(1)</script> 
-->text"));
+        assertEquals("x", htmlSubset("<iframe 
src=\"https://example.com/\";></iframe>x"));
+        // a quote can't end the href value and add an attribute
+        assertEquals("<a href=\"https://example.com/&quot; 
onclick=&quot;alert(1)\">x</a>",
+                htmlSubset("<a href='https://example.com/\"; 
onclick=\"alert(1)'>x</a>"));
+    }
+
+    @Test
+    public void testSanitizeToHtmlSubsetTakesRawHtml() {
+        assertNull(Utilities.sanitizeToHTMLSubset(null));
+        assertEquals("<b>Hi</b> &amp; bye", 
Utilities.sanitizeToHTMLSubset("<b>Hi</b> &amp; bye"));
+        assertEquals("<b>Hi</b>", 
Utilities.sanitizeToHTMLSubset("<b>Hi</b><img src=x onerror=alert(1)>"));
+    }
+
     private String htmlSubset(String text) {
         return Utilities.transformToHTMLSubset(Utilities.escapeHTML(text));
     }
@@ -219,8 +250,9 @@ public class UtilitiesTest  {
         String raw = "<a href=".repeat(50000);
 
         long start = System.nanoTime();
-        assertEquals(escaped, Utilities.transformToHTMLSubset(escaped));
-        assertEquals(escaped, 
Utilities.transformToHTMLSubset(Utilities.escapeHTML(raw)));
+        // a tag that never ends is dropped
+        assertEquals("", Utilities.transformToHTMLSubset(escaped));
+        assertEquals("", 
Utilities.transformToHTMLSubset(Utilities.escapeHTML(raw)));
         assertEquals("", Utilities.addNofollow(raw));
         long millis = (System.nanoTime() - start) / 1_000_000;
         // a quadratic scan takes far longer than this; leave room for slow

Reply via email to