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

snoopdave pushed a commit to branch roller-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git


The following commit(s) were added to refs/heads/roller-6.1.x by this push:
     new 3dc3cc90a Filter HTML-significant characters out of tag names (#208)
3dc3cc90a is described below

commit 3dc3cc90ad6a651b62d59d8a44109e3969bcff08
Author: David M. Johnson <[email protected]>
AuthorDate: Fri Oct 9 16:46:19 2026 -0400

    Filter HTML-significant characters out of tag names (#208)
    
    * Normalize AtomPub tags like editor tags; escape tag names in themes
    
    AtomPub categories with no scheme become entry tags. Apply the same
    normalization the weblog editor uses, so both paths store the same tags.
    
    Bundled themes and the showEntryTags macro now HTML-escape tag names
    where they print them as link text.
    
    * Filter HTML-significant characters out of tag names
    
    Tag names now exclude " & ' < > ` in Utilities.stripInvalidTagCharacters,
    which every tag path uses. The entry and tag wrappers and the tag
    statistics apply the same filter when they read stored names, so names
    stored earlier are covered too. Templates can print tag names as they are.
    
    This replaces the earlier AtomPub-only normalization and the theme edits.
---
 CHANGES.md                                         |  3 +
 .../business/jpa/JPAWeblogEntryManagerImpl.java    |  7 +-
 .../pojos/wrapper/WeblogEntryTagWrapper.java       |  8 ++-
 .../pojos/wrapper/WeblogEntryWrapper.java          |  4 +-
 .../apache/roller/weblogger/util/Utilities.java    | 22 ++++--
 .../pojos/wrapper/WeblogEntryTagWrapperTest.java   | 80 ++++++++++++++++++++++
 .../roller/weblogger/util/TagCharactersTest.java   | 60 ++++++++++++++++
 7 files changed, 174 insertions(+), 10 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 3cba7887e..68a5944c8 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -36,6 +36,9 @@
   templates. A custom theme that uses them will print the reference text
   as-is, without an error. Use the wrapper's own properties instead, for
   example `$weblog.handle` or `$entry.title`.
+- **Tag names can no longer contain `"`, `&`, `'`, `<`, `>` or `` ` ``.**
+  These characters are removed when a tag is saved, and from existing tag
+  names when they are shown.
 - **Planet is off by default.** `planet.aggregator.enabled` now defaults to
   `false`. While it is off, the Planet admin pages, `/planetrss` and the
   Planet background tasks (`RefreshRollerPlanetTask`, `SyncWebsitesTask`) do
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
 
b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
index 7a84bb5d3..def32afee 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAWeblogEntryManagerImpl.java
@@ -32,6 +32,7 @@ import org.apache.commons.logging.LogFactory;
 import org.apache.roller.util.RollerConstants;
 import org.apache.roller.weblogger.WebloggerException;
 import org.apache.roller.weblogger.business.Weblogger;
+import org.apache.roller.weblogger.util.Utilities;
 import org.apache.roller.weblogger.pojos.CommentSearchCriteria;
 import org.apache.roller.weblogger.pojos.WeblogEntryComment;
 import org.apache.roller.weblogger.pojos.WeblogEntryComment.ApprovalStatus;
@@ -1046,7 +1047,8 @@ public class JPAWeblogEntryManagerImpl implements 
WeblogEntryManager {
             for (Object obj : queryResults) {
                 Object[] row = (Object[]) obj;
                 TagStat t = new TagStat();
-                t.setName((String) row[0]);
+                // filter names stored before the current tag character rules
+                t.setName(Utilities.stripInvalidTagCharacters((String) 
row[0]));
                 t.setCount(((Number) row[1]).intValue());
 
                 min = Math.min(min, t.getCount());
@@ -1116,7 +1118,8 @@ public class JPAWeblogEntryManagerImpl implements 
WeblogEntryManager {
             for (Object obj : queryResults) {
                 Object[] row = (Object[]) obj;
                 TagStat ce = new TagStat();
-                ce.setName((String) row[0]);
+                // filter names stored before the current tag character rules
+                ce.setName(Utilities.stripInvalidTagCharacters((String) 
row[0]));
                 // The JPA query retrieves SUM(w.total) always as long
                 ce.setCount(((Long) row[1]).intValue());
                 results.add(ce);
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapper.java
 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapper.java
index ced8a0569..7f24de452 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapper.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapper.java
@@ -20,6 +20,7 @@ package org.apache.roller.weblogger.pojos.wrapper;
 
 import java.sql.Timestamp;
 import org.apache.roller.weblogger.pojos.WeblogEntryTag;
+import org.apache.roller.weblogger.util.Utilities;
 
 
 /**
@@ -51,8 +52,13 @@ public final class WeblogEntryTagWrapper {
     }
     
     
+    /**
+     * The tag name, filtered again in case it was stored before the current
+     * tag character rules.
+     */
     public String getName() {
-        return this.pojo.getName();
+        String name = this.pojo.getName();
+        return name == null ? null : Utilities.stripInvalidTagCharacters(name);
     }
     
     
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java
 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java
index 4ed80b184..026001a64 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java
@@ -187,7 +187,9 @@ public final class WeblogEntryWrapper {
     
     
     public String getTagsAsString() {
-        return this.pojo.getTagsAsString();
+        return getTags().stream()
+                .map(WeblogEntryTagWrapper::getName)
+                .collect(Collectors.joining(" "));
     }
     
     
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 7caf9a682..f8a74602c 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
@@ -900,8 +900,13 @@ public class Utilities {
     }
 
     /**
-     * @param tag
-     * @return
+     * Removes characters that are not allowed in a tag name: whitespace,
+     * control characters, comma, and the HTML-significant characters
+     * <code>" &amp; ' &lt; &gt; `</code>. Templates can then print tag names
+     * as they are.
+     *
+     * @param tag the tag name
+     * @return the tag name without invalid characters
      */
     public static String stripInvalidTagCharacters(String tag) {
         if (tag == null) {
@@ -913,12 +918,17 @@ public class Utilities {
         for (int i = 0; i < charArray.length; i++) {
             char c = charArray[i];
 
-            // fast-path exclusions quotes and commas are obvious
-            // 34 = double-quote, 44 = comma
             switch (c) {
-            case 34:
-            case 44:
+            case '"':
+            case '&':
+            case '\'':
+            case ',':
+            case '<':
+            case '>':
+            case '`':
                 continue;
+            default:
+                break;
             }
 
             if ((33 <= c && c <= 126) || Character.isUnicodeIdentifierPart(c)
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapperTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapperTest.java
new file mode 100644
index 000000000..ef0bd49d8
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryTagWrapperTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.pojos.wrapper;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.roller.weblogger.pojos.WeblogEntry;
+import org.apache.roller.weblogger.pojos.WeblogEntryTag;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tag names reach templates through the entry and tag wrappers. They must not
+ * carry HTML-significant characters, whichever way the tag was stored.
+ */
+public class WeblogEntryTagWrapperTest {
+
+    private static WeblogEntryTag storedTag(String name) {
+        WeblogEntryTag tag = new WeblogEntryTag();
+        tag.setName(name);
+        return tag;
+    }
+
+    private static boolean hasHtmlCharacters(String s) {
+        return s.matches(".*[<>&\"'`].*");
+    }
+
+    @Test
+    public void tagsSetOnAnEntryAreFiltered() throws Exception {
+        WeblogEntry entry = new WeblogEntry();
+        entry.setTagsAsString("java <script>alert(1)</script> a\"b");
+        for (WeblogEntryTag tag : entry.getTags()) {
+            assertFalse(hasHtmlCharacters(tag.getName()), tag.getName());
+        }
+        assertEquals(3, entry.getTags().size());
+    }
+
+    @Test
+    public void wrapperFiltersTagsStoredBeforeTheRules() {
+        WeblogEntryTagWrapper wrapper = WeblogEntryTagWrapper.wrap(
+                storedTag("<script>alert(1)</script>"));
+        assertEquals("scriptalert(1)/script", wrapper.getName());
+    }
+
+    @Test
+    public void wrapperKeepsOrdinaryTags() {
+        assertEquals("c++", 
WeblogEntryTagWrapper.wrap(storedTag("c++")).getName());
+    }
+
+    @Test
+    public void entryTagsAsStringIsFiltered() throws Exception {
+        WeblogEntry entry = new WeblogEntry();
+        Set<WeblogEntryTag> tags = new HashSet<>();
+        tags.add(storedTag("java"));
+        tags.add(storedTag("<b>x</b>"));
+        entry.setTags(tags);
+        String tagsAsString = WeblogEntryWrapper.wrap(entry, 
null).getTagsAsString();
+        assertFalse(hasHtmlCharacters(tagsAsString), tagsAsString);
+        assertTrue(tagsAsString.contains("java"), tagsAsString);
+    }
+}
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/util/TagCharactersTest.java 
b/app/src/test/java/org/apache/roller/weblogger/util/TagCharactersTest.java
new file mode 100644
index 000000000..eb01daacb
--- /dev/null
+++ b/app/src/test/java/org/apache/roller/weblogger/util/TagCharactersTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.util;
+
+import java.util.Locale;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tag names may not contain characters that are significant in HTML, so
+ * templates can print them as they are.
+ */
+public class TagCharactersTest {
+
+    @Test
+    public void htmlCharactersAreRemoved() {
+        assertEquals("scriptalert(1)/script",
+                
Utilities.stripInvalidTagCharacters("<script>alert(1)</script>"));
+        assertEquals("aonmouseover=x",
+                Utilities.stripInvalidTagCharacters("a\"'onmouseover=x"));
+        assertEquals("ab", Utilities.stripInvalidTagCharacters("a&b"));
+        assertEquals("ab", Utilities.stripInvalidTagCharacters("a`b"));
+    }
+
+    @Test
+    public void commaAndWhitespaceAreRemoved() {
+        assertEquals("ab", Utilities.stripInvalidTagCharacters("a,b"));
+        assertEquals("ab", Utilities.stripInvalidTagCharacters("a b\t"));
+    }
+
+    @Test
+    public void ordinaryTagsAreKept() {
+        for (String tag : new String[] {"java", "c++", ".net", "c#", "foo-bar",
+                "foo_bar", "v1.2", "café", "日本"}) {
+            assertEquals(tag, Utilities.stripInvalidTagCharacters(tag));
+        }
+    }
+
+    @Test
+    public void normalizeTagFiltersAndLowercases() {
+        assertEquals("bfoo/b", Utilities.normalizeTag("<B>Foo</B>", 
Locale.ENGLISH));
+    }
+}

Reply via email to