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

mraible pushed a commit to branch feed-authors-and-nofollow-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git

commit 530f38ed842e812f1d066271f8b12be5286b36b1
Author: Matt Raible <[email protected]>
AuthorDate: Tue Nov 17 20:05:15 2026 -0700

    Show comment and media file authors once-escaped in feeds and keep existing 
rel values in addNofollow
    
    The comment feed macros and the media file feed now turn author names back 
into text with unescapeHTML before XML-escaping them, as the entry author 
macros do, so a name like "A & B" no longer shows as "A &amp; B" in feed 
readers. FeedAuthorEscapingTest covers the Atom and RSS comment feeds and the 
media file feed.
    
    Utilities.addNofollow adds nofollow to a link's existing rel value instead 
of adding a second rel attribute, and a ">" inside a quoted attribute value no 
longer ends the link early. The linear-time test now allows 2 seconds so it 
doesn't fail on slow build machines.
---
 .../apache/roller/weblogger/util/Utilities.java    |  66 +++++++++--
 app/src/main/webapp/WEB-INF/velocity/feeds.vm      |   6 +-
 .../rendering/velocity/FeedAuthorEscapingTest.java | 124 ++++++++++++++++++++-
 .../roller/weblogger/util/UtilitiesTest.java       |  20 +++-
 4 files changed, 199 insertions(+), 17 deletions(-)

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 401edd55e..4d4188d74 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
@@ -43,6 +43,9 @@ public class Utilities {
 
     private static final Pattern mLinkPattern = Pattern.compile("<a href=",
             Pattern.CASE_INSENSITIVE);
+    private static final Pattern REL_ATTRIBUTE_PATTERN = Pattern.compile(
+            "\\srel\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s\"'=<>`]+))",
+            Pattern.CASE_INSENSITIVE);
     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(
@@ -267,29 +270,26 @@ public class Utilities {
 
     /**
      * Code (stolen from Pebble) to add rel="nofollow" string to all links in
-     * HTML that don't already have it.
+     * HTML that don't already have it. A link with another rel value gets
+     * nofollow added to that value.
      */
     public static String addNofollow(String html) {
         if (html == null || html.length() == 0) {
             return html;
         }
-        // Each link runs to the first ">" after its "href=". Once no ">" is
-        // left, no later link can close either, so stop there.
+        // Each link runs to the first ">" after its "href=" that isn't inside
+        // a quoted attribute value. Once no ">" is left, no later link can
+        // close either, so stop there.
         Matcher m = mLinkPattern.matcher(html);
         StringBuilder buf = new StringBuilder(html.length());
         int end = 0;
         while (m.find()) {
-            int close = html.indexOf('>', m.end());
+            int close = findTagEnd(html, m.end());
             if (close < 0) {
                 break;
             }
-            String link = html.substring(m.start(), close + 1);
             buf.append(html, end, m.start());
-            if (link.contains("rel=\"nofollow\"")) {
-                buf.append(link);
-            } else {
-                buf.append(link, 0, link.length() - 1).append(" 
rel=\"nofollow\">");
-            }
+            buf.append(addNofollowToLink(html.substring(m.start(), close + 
1)));
             end = close + 1;
             m.region(end, html.length());
         }
@@ -297,6 +297,52 @@ public class Utilities {
         return buf.toString();
     }
 
+    /**
+     * Index of the ">" that ends the tag, skipping any inside quoted attribute
+     * values, or -1 if the tag doesn't end.
+     */
+    private static int findTagEnd(String html, int from) {
+        char quote = 0;
+        char previous = '=';
+        for (int i = from; i < html.length(); i++) {
+            char c = html.charAt(i);
+            if (quote != 0) {
+                if (c == quote) {
+                    quote = 0;
+                }
+            } else if ((c == '"' || c == '\'') && previous == '=') {
+                quote = c;
+            } else if (c == '>') {
+                return i;
+            }
+            if (!Character.isWhitespace(c)) {
+                previous = c;
+            }
+        }
+        return -1;
+    }
+
+    private static String addNofollowToLink(String link) {
+        Matcher rel = REL_ATTRIBUTE_PATTERN.matcher(link);
+        if (!rel.find()) {
+            return link.substring(0, link.length() - 1) + " rel=\"nofollow\">";
+        }
+        int group = rel.group(1) != null ? 1 : rel.group(2) != null ? 2 : 3;
+        String value = rel.group(group);
+        for (String token : StringUtils.split(value)) {
+            if ("nofollow".equalsIgnoreCase(token)) {
+                return link;
+            }
+        }
+        String values = value.isBlank() ? "nofollow" : value + " nofollow";
+        if (group == 3) {
+            // unquoted value: quote it so the added token stays part of it
+            return link.substring(0, rel.start(group)) + '"' + values + '"'
+                    + link.substring(rel.end(group));
+        }
+        return link.substring(0, rel.start(group)) + values + 
link.substring(rel.end(group));
+    }
+
     // ------------------------------------------------------------------------
     /**
      * Replaces occurences of non-alphanumeric characters with an underscore.
diff --git a/app/src/main/webapp/WEB-INF/velocity/feeds.vm 
b/app/src/main/webapp/WEB-INF/velocity/feeds.vm
index 62416ad93..51f0a4beb 100644
--- a/app/src/main/webapp/WEB-INF/velocity/feeds.vm
+++ b/app/src/main/webapp/WEB-INF/velocity/feeds.vm
@@ -92,7 +92,7 @@
     <entry>
         <id>${comment.weblogEntry.permalink}#comment-${comment.timestamp}</id> 
         <title type="html">Re: 
$utils.escapeXML($comment.weblogEntry.title)</title>
-        
<author><name>$utils.escapeXML($utils.removeHTML($comment.name))</name></author>
+        
<author><name>$utils.escapeXML($utils.unescapeHTML($comment.name))</name></author>
         <link rel="alternate" type="text/html" 
href="${comment.weblogEntry.permalink}#comment-${comment.timestamp}"/>
         <published>$utils.formatIso8601Date($comment.postTime)</published>
         <updated>$utils.formatIso8601Date($comment.postTime)</updated> 
@@ -106,7 +106,7 @@
     <item>
         <guid 
isPermaLink="true">${comment.weblogEntry.permalink}#comment-${comment.timestamp}</guid>
         <title>Re: $utils.escapeXML($comment.weblogEntry.title)</title>
-        
<dc:creator>$utils.escapeXML($utils.removeHTML($comment.name))</dc:creator>
+        
<dc:creator>$utils.escapeXML($utils.unescapeHTML($comment.name))</dc:creator>
         <pubDate>$utils.formatRfc822Date($comment.postTime)</pubDate>
         <description>$utils.escapeXML($comment.content)</description>
     </item>
@@ -118,7 +118,7 @@
     <entry>
         <id>$file.permalink</id>
         <title type="html">$utils.escapeXML($file.name)</title>
-        
<author><name>$utils.escapeXML($utils.removeHTML($file.creator.screenName))</name></author>
+        
<author><name>$utils.escapeXML($utils.unescapeHTML($file.creator.screenName))</name></author>
         <link rel="alternate" type="text/html" href="$file.permalink"/>
         <published>$utils.formatIso8601Date($file.dateUploaded)</published>
         <updated>$utils.formatIso8601Date($file.lastUpdated)</updated> 
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/ui/rendering/velocity/FeedAuthorEscapingTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/ui/rendering/velocity/FeedAuthorEscapingTest.java
index 9ab5f5c49..f3afaf5b5 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/ui/rendering/velocity/FeedAuthorEscapingTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/ui/rendering/velocity/FeedAuthorEscapingTest.java
@@ -29,7 +29,9 @@ import java.util.Properties;
 import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.roller.weblogger.pojos.User;
+import org.apache.roller.weblogger.pojos.WeblogEntryComment;
 import org.apache.roller.weblogger.pojos.wrapper.UserWrapper;
+import org.apache.roller.weblogger.pojos.wrapper.WeblogEntryCommentWrapper;
 import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -42,9 +44,10 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 /**
- * Entry authors in the built-in feeds are plain text, so the feed macros must
- * turn the HTML-escaped screen name from {@link UserWrapper} back into text
- * and XML-escape it once.
+ * Entry, comment and media file authors in the built-in feeds are plain text,
+ * so the feed macros must turn the HTML-escaped names from {@link UserWrapper}
+ * and {@link WeblogEntryCommentWrapper}, and screen names stored already
+ * encoded, back into text and XML-escape them once.
  */
 public class FeedAuthorEscapingTest {
 
@@ -58,6 +61,13 @@ public class FeedAuthorEscapingTest {
         {"Tom &amp; &quot;Jerry&quot;", "Tom & \"Jerry\""},
     };
 
+    /** Comment author name as submitted, and the text the feed must contain. 
*/
+    private static final String[][] COMMENT_NAMES = {
+        {"A & B", "A & B"},
+        {"<b>Bold</b>", "<b>Bold</b>"},
+        {"José \"Pepe\" O'Brien", "José \"Pepe\" O'Brien"},
+    };
+
     /** Minimal stand-ins for the model objects the feed macros read. */
     public static class StubEntry {
         private final UserWrapper creator;
@@ -107,6 +117,78 @@ public class FeedAuthorEscapingTest {
         }
     }
 
+    /** Comment stand-in that takes its name from the real comment wrapper. */
+    public static class StubComment {
+        private final WeblogEntryCommentWrapper wrapper;
+
+        StubComment(String name) {
+            WeblogEntryComment comment = new WeblogEntryComment();
+            comment.setName(name);
+            this.wrapper = WeblogEntryCommentWrapper.wrap(comment, null);
+        }
+
+        public String getName() {
+            return wrapper.getName();
+        }
+
+        public StubEntry getWeblogEntry() {
+            return new StubEntry(null);
+        }
+
+        public String getTimestamp() {
+            return "0";
+        }
+
+        public Date getPostTime() {
+            return new Date(0);
+        }
+
+        public String getContent() {
+            return "Comment";
+        }
+    }
+
+    /** Media file stand-in; media files expose the creator's User directly. */
+    public static class StubFile {
+        private final User creator;
+
+        StubFile(User creator) {
+            this.creator = creator;
+        }
+
+        public String getPermalink() {
+            return "https://example.org/blog/mediaresource/file";;
+        }
+
+        public String getName() {
+            return "file.png";
+        }
+
+        public User getCreator() {
+            return creator;
+        }
+
+        public Date getDateUploaded() {
+            return new Date(0);
+        }
+
+        public Date getLastUpdated() {
+            return new Date(0);
+        }
+
+        public List<Object> getTags() {
+            return Collections.emptyList();
+        }
+
+        public String getDescription() {
+            return null;
+        }
+
+        public String getContentType() {
+            return "image/png";
+        }
+    }
+
     public static class StubCategory {
         public String getName() {
             return "General";
@@ -126,6 +208,8 @@ public class FeedAuthorEscapingTest {
         when(user.getScreenName()).thenReturn(name);
         VelocityContext ctx = new VelocityContext();
         ctx.put("entries", List.of(new StubEntry(UserWrapper.wrap(user))));
+        ctx.put("comments", List.of(new StubComment(name)));
+        ctx.put("files", List.of(new StubFile(user)));
         ctx.put("utils", new UtilitiesModel());
         StringWriter out = new StringWriter();
         engine.evaluate(ctx, out, "test", "#parse('feeds.vm')" + macro);
@@ -160,4 +244,38 @@ public class FeedAuthorEscapingTest {
                     .item(0).getTextContent(), xml);
         }
     }
+
+    @Test
+    public void atomCommentAuthorIsText() throws Exception {
+        for (String[] name : COMMENT_NAMES) {
+            String xml = render("<feed xmlns=\"http://www.w3.org/2005/Atom\";>"
+                    + "#showCommentsAtom10($comments)</feed>", name[0]);
+            Document feed = parse(xml);
+            assertEquals(name[1], 
feed.getElementsByTagNameNS("http://www.w3.org/2005/Atom";, "name")
+                    .item(0).getTextContent(), xml);
+        }
+    }
+
+    @Test
+    public void rssCommentCreatorIsText() throws Exception {
+        for (String[] name : COMMENT_NAMES) {
+            String xml = render("<rss 
xmlns:dc=\"http://purl.org/dc/elements/1.1/\"";
+                    + " xmlns:atom=\"http://www.w3.org/2005/Atom\";><channel>"
+                    + "#showCommentsRSS20($comments)</channel></rss>", 
name[0]);
+            Document feed = parse(xml);
+            assertEquals(name[1], 
feed.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/";, "creator")
+                    .item(0).getTextContent(), xml);
+        }
+    }
+
+    @Test
+    public void atomMediaFileAuthorIsText() throws Exception {
+        for (String[] name : NAMES) {
+            String xml = render("<feed xmlns=\"http://www.w3.org/2005/Atom\";>"
+                    + "#showFilesAtom10($files)</feed>", name[0]);
+            Document feed = parse(xml);
+            assertEquals(name[1], 
feed.getElementsByTagNameNS("http://www.w3.org/2005/Atom";, "name")
+                    .item(0).getTextContent(), xml);
+        }
+    }
 }
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 e725e9cae..f56b198df 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
@@ -170,6 +170,22 @@ public class UtilitiesTest  {
                 + " <a href=\"/two\" rel=\"nofollow\">two</a>";
         assertEquals(expect3, Utilities.addNofollow(test3));
 
+        // an existing rel gets nofollow added to its value
+        assertEquals("<a href=\"/x\" rel=\"external nofollow\">x</a>",
+                Utilities.addNofollow("<a href=\"/x\" 
rel=\"external\">x</a>"));
+        assertEquals("<a href=\"/x\" REL='noopener nofollow'>x</a>",
+                Utilities.addNofollow("<a href=\"/x\" REL='noopener'>x</a>"));
+        assertEquals("<a href=\"/x\" rel=\"external nofollow\">x</a>",
+                Utilities.addNofollow("<a href=\"/x\" rel=external>x</a>"));
+        assertEquals("<a href=\"/x\" rel=\"Nofollow external\">x</a>",
+                Utilities.addNofollow("<a href=\"/x\" rel=\"Nofollow 
external\">x</a>"));
+
+        // a ">" inside a quoted attribute value doesn't end the tag
+        assertEquals("<a href=\"/x\" title=\"a > b\" rel=\"nofollow\">x</a>",
+                Utilities.addNofollow("<a href=\"/x\" title=\"a > b\">x</a>"));
+        assertEquals("<a href='/x?a>b' rel=\"nofollow\">x</a>",
+                Utilities.addNofollow("<a href='/x?a>b'>x</a>"));
+
         assertEquals("no links", Utilities.addNofollow("no links"));
         assertEquals("<a href=\"x\"", Utilities.addNofollow("<a href=\"x\""));
         assertNull(Utilities.addNofollow(null));
@@ -185,7 +201,9 @@ public class UtilitiesTest  {
         assertEquals(escaped, 
Utilities.transformToHTMLSubset(Utilities.escapeHTML(raw)));
         assertEquals(raw, Utilities.addNofollow(raw));
         long millis = (System.nanoTime() - start) / 1_000_000;
-        assertTrue(millis < 200, "took " + millis + " ms");
+        // a quadratic scan takes far longer than this; leave room for slow
+        // build machines
+        assertTrue(millis < 2000, "took " + millis + " ms");
     }
 
     @Test

Reply via email to