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 931161efb Keep blogroll links to web and relative URLs (#221)
931161efb is described below

commit 931161efb103ba6345043b0ef4815b8e7a876bb4
Author: David M. Johnson <[email protected]>
AuthorDate: Thu Oct 29 22:40:08 2026 -0400

    Keep blogroll links to web and relative URLs (#221)
    
    * Keep blogroll links to web and relative URLs
    * Log skipped OPML outlines and note the import change in CHANGES.md
    * Note in CHANGES.md that OPML links must parse as URIs
    * Read blogroll link schemes one way and escape bookmark names
---
 CHANGES.md                                         |  6 ++
 .../business/jpa/JPABookmarkManagerImpl.java       | 33 ++++++++++
 .../pojos/wrapper/WeblogBookmarkWrapper.java       | 27 +++++++-
 .../apache/roller/weblogger/util/LinkScheme.java   | 73 ++++++++++++++++++++++
 app/src/main/webapp/WEB-INF/velocity/weblog.vm     | 16 ++---
 .../business/BookmarkImportParsingTest.java        | 32 ++++++++++
 .../pojos/wrapper/WeblogBookmarkWrapperTest.java   | 56 +++++++++++++++++
 .../roller/weblogger/util/LinkSchemeTest.java      | 51 +++++++++++++++
 8 files changed, 280 insertions(+), 14 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index c0070846e..7dcfa9c18 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -64,6 +64,12 @@
   a `<`, as in `a < b`, `x<y` or `C<C++ fans`, is kept, and no longer gets a
   space added before the `<`. The Your Weblogs page shows a weblog's
   description as text.
+- **OPML import keeps only web links.** An imported bookmark is kept only when
+  its link is an absolute `http` or `https` URL; others are skipped and named
+  in the log. A feed URL that is not one is dropped. Blogroll links shown on
+  weblog pages must be relative or use `http`, `https`, `ftp` or `mailto`;
+  other links, and links with `&` before the path, are shown without a
+  target. The blogroll escapes bookmark names and descriptions.
 
 ### Bug fixes
 
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPABookmarkManagerImpl.java
 
b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPABookmarkManagerImpl.java
index f1fa7478e..77c919fc1 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPABookmarkManagerImpl.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/business/jpa/JPABookmarkManagerImpl.java
@@ -32,6 +32,7 @@ import org.apache.roller.weblogger.business.Weblogger;
 import org.apache.roller.weblogger.pojos.WeblogBookmark;
 import org.apache.roller.weblogger.pojos.WeblogBookmarkFolder;
 import org.apache.roller.weblogger.pojos.Weblog;
+import org.apache.roller.weblogger.util.LinkScheme;
 import org.jdom2.Document;
 import org.jdom2.Element;
 import org.jdom2.input.JDOMParseException;
@@ -228,6 +229,19 @@ public class JPABookmarkManagerImpl implements 
BookmarkManager {
         desc =    null!=desc ? desc : title;
         xmlUrl =  null!=xmlUrl ? xmlUrl : url;
         url =     null!=htmlUrl ? htmlUrl : url;
+
+        // keep only web links; an outline with anything else gets no url
+        String importedUrl = url;
+        String importedXmlUrl = xmlUrl;
+        url = webLinkOrNull(url);
+        xmlUrl = webLinkOrNull(xmlUrl);
+        if (importedUrl != null && url == null) {
+            log.warn("OPML import skipped outline '" + title
+                    + "': its link is not an absolute http or https URL: " + 
importedUrl);
+        } else if (importedXmlUrl != null && xmlUrl == null) {
+            log.warn("OPML import dropped the feed URL of outline '" + title
+                    + "': it is not an absolute http or https URL: " + 
importedXmlUrl);
+        }
         
         // better to truncate imported OPML fields than to fail import or drop 
whole bookmark
         int maxLength = RollerConstants.TEXTWIDTH_255;
@@ -270,6 +284,25 @@ public class JPABookmarkManagerImpl implements 
BookmarkManager {
         }
     }
 
+    /**
+     * Returns the value, trimmed, if it is an absolute http or https URL with
+     * a host, else null. The scheme is read as the blogroll reads it (see
+     * {@link LinkScheme#of(String)}), so links that browsers accept are kept.
+     */
+    static String webLinkOrNull(String value) {
+        if (value == null) {
+            return null;
+        }
+        String scheme = LinkScheme.of(value);
+        if ("http".equals(scheme) || "https".equals(scheme)) {
+            String rest = LinkScheme.compact(value).substring(scheme.length() 
+ 1);
+            if (rest.startsWith("//") && rest.length() > 2 && rest.charAt(2) 
!= '/') {
+                return value.trim();
+            }
+        }
+        return null;
+    }
+
     /**
      * @see org.apache.roller.weblogger.business.BookmarkManager#getBookmarks(
      *      org.apache.roller.weblogger.pojos.WeblogBookmarkFolder)
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapper.java
 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapper.java
index 66a6cccda..281e404ac 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapper.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapper.java
@@ -18,13 +18,18 @@
 
 package org.apache.roller.weblogger.pojos.wrapper;
 
+import java.util.Set;
+
 import org.apache.roller.weblogger.pojos.WeblogBookmark;
+import org.apache.roller.weblogger.util.LinkScheme;
 
 
 /**
  * Pojo safety wrapper for WeblogBookmark object.
  */
 public final class WeblogBookmarkWrapper {
+
+    private static final Set<String> ALLOWED_SCHEMES = Set.of("http", "https", 
"ftp", "mailto");
     
     // keep a reference to the wrapped pojo
     private final WeblogBookmark pojo;
@@ -59,7 +64,7 @@ public final class WeblogBookmarkWrapper {
     
     
     public String getUrl() {
-        return this.pojo.getUrl();
+        return linkOrEmpty(this.pojo.getUrl());
     }
 
     public Integer getPriority() {
@@ -67,12 +72,28 @@ public final class WeblogBookmarkWrapper {
     }
 
     public String getImage() {
-        return this.pojo.getImage();
+        return linkOrEmpty(this.pojo.getImage());
     }
     
     
     public String getFeedUrl() {
-        return this.pojo.getFeedUrl();
+        return linkOrEmpty(this.pojo.getFeedUrl());
+    }
+
+    /**
+     * Returns the link if it is relative or uses a scheme a blogroll link
+     * needs (http, https, ftp, mailto), else an empty string. See
+     * {@link LinkScheme#of(String)} for how the scheme is read.
+     */
+    static String linkOrEmpty(String link) {
+        if (link == null) {
+            return null;
+        }
+        String scheme = LinkScheme.of(link);
+        if (scheme == null || (!scheme.isEmpty() && 
!ALLOWED_SCHEMES.contains(scheme))) {
+            return "";
+        }
+        return link;
     }
     
     
diff --git a/app/src/main/java/org/apache/roller/weblogger/util/LinkScheme.java 
b/app/src/main/java/org/apache/roller/weblogger/util/LinkScheme.java
new file mode 100644
index 000000000..761fd2074
--- /dev/null
+++ b/app/src/main/java/org/apache/roller/weblogger/util/LinkScheme.java
@@ -0,0 +1,73 @@
+/*
+ * 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.roller.weblogger.util;
+
+import java.util.Locale;
+import java.util.regex.Pattern;
+
+/**
+ * Reads the scheme of a link the way a browser would.
+ */
+public final class LinkScheme {
+
+    private static final Pattern SCHEME = 
Pattern.compile("[a-zA-Z][a-zA-Z0-9+.-]*");
+
+    private LinkScheme() {
+    }
+
+    /**
+     * Removes what browsers ignore in a link: tabs and line breaks anywhere,
+     * and spaces and control characters at either end.
+     */
+    public static String compact(String link) {
+        return link.replaceAll("[\\t\\n\\r]", "").trim();
+    }
+
+    /**
+     * Returns the lower-case scheme of the link, an empty string if the link
+     * is relative, or null if the part before the path cannot be read. That
+     * part is everything before the first '/', '?' or '#'; it is refused if
+     * it holds a character reference ('&') or a scheme that is not valid.
+     */
+    public static String of(String link) {
+        if (link == null) {
+            return null;
+        }
+        String compact = compact(link);
+        int end = compact.length();
+        for (char c : new char[] {'/', '?', '#'}) {
+            int i = compact.indexOf(c);
+            if (i >= 0 && i < end) {
+                end = i;
+            }
+        }
+        String head = compact.substring(0, end);
+        if (head.indexOf('&') >= 0) {
+            return null;
+        }
+        int colon = head.indexOf(':');
+        if (colon < 0) {
+            return "";
+        }
+        String scheme = head.substring(0, colon);
+        if (!SCHEME.matcher(scheme).matches()) {
+            return null;
+        }
+        return scheme.toLowerCase(Locale.ROOT);
+    }
+}
diff --git a/app/src/main/webapp/WEB-INF/velocity/weblog.vm 
b/app/src/main/webapp/WEB-INF/velocity/weblog.vm
index b80804dac..2d9886c2e 100644
--- a/app/src/main/webapp/WEB-INF/velocity/weblog.vm
+++ b/app/src/main/webapp/WEB-INF/velocity/weblog.vm
@@ -478,22 +478,16 @@ Show bookmarks as a HTML ul list.
     #foreach($bookmark in $bookmarks)
         <li class="rFolderItem">
         #if($utils.isNotEmpty($bookmark.Image))
-          #if( $bookmark.Image.startsWith("http://";) )
-          <a href="$bookmark.url">
-             <img alt="$bookmark.description" border="0"
-                src="$bookmark.image" class="rBookmark" /></a>
-          #else
-          <a href="$bookmark.url">
-             <img alt="$bookmark.description" border="0"
-                src="$bookmark.image" class="rBookmark" /></a>
-          #end
+          <a href="$utils.escapeHTML($bookmark.url)">
+             <img alt="$utils.escapeHTML($bookmark.description)" border="0"
+                src="$utils.escapeHTML($bookmark.image)" class="rBookmark" 
/></a>
         #else
             #if($utils.isNotEmpty( $bookmark.FeedUrl))
-            <a href="$bookmark.feedUrl"><img class="smrssbadge"
+            <a href="$utils.escapeHTML($bookmark.feedUrl)"><img 
class="smrssbadge"
                src="$url.absoluteSite/images/smrssbadge.gif"
                alt="$text.get("macro.bookmark.urlFeed")"/></a>
             #end
-            <a href="$bookmark.url" 
title="$bookmark.description">$bookmark.name</a>
+            <a href="$utils.escapeHTML($bookmark.url)" 
title="$utils.escapeHTML($bookmark.description)">$utils.escapeHTML($bookmark.name)</a>
         #end
         </li>
     #end
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/business/BookmarkImportParsingTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/business/BookmarkImportParsingTest.java
index ce8e8e29a..1d2530d5b 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/business/BookmarkImportParsingTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/business/BookmarkImportParsingTest.java
@@ -32,12 +32,15 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 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.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Covers the OPML bookmark import's handling of document type declarations:
  * documents that carry one are refused, while ordinary OPML still imports.
+ * Also checks that imported links are kept only when they are web links.
  */
 public class BookmarkImportParsingTest {
 
@@ -133,4 +136,33 @@ public class BookmarkImportParsingTest {
         assertTrue(importedBookmarks().isEmpty(),
                 "a document carrying a DOCTYPE was still imported");
     }
+
+    /** Only absolute http and https links are imported. */
+    @Test
+    public void importKeepsOnlyWebLinks() throws Exception {
+        String opml = "<?xml version=\"1.0\"?><opml 
version=\"1.1\"><head><title>t</title></head><body>"
+                + "<outline text=\"web\" htmlUrl=\"https://example.com/a\"; 
xmlUrl=\"http://example.com/feed\"/>"
+                + "<outline text=\"ftp\" htmlUrl=\"ftp://example.com/a\"/>"
+                + "<outline text=\"mail\" 
htmlUrl=\"mailto:[email protected]\"/>"
+                + "<outline text=\"relative\" htmlUrl=\"/a/b\"/>"
+                + "<outline text=\"spaces\" htmlUrl=\"http://example.com/a 
b\"/>"
+                + "<outline text=\"underscore\" htmlUrl=\" 
https://my_blog.example.com/ \"/>"
+                + "<outline text=\"nohost\" htmlUrl=\"http:///a\"/>"
+                + "<outline text=\"feed\" htmlUrl=\"https://example.com/b\"; 
xmlUrl=\"file:///etc/feed\"/>"
+                + "</body></opml>";
+        
bookmarkManager().importBookmarks(TestUtils.getManagedWebsite(testWeblog), 
folderName, opml);
+        TestUtils.endSession(true);
+
+        java.util.Map<String, WeblogBookmark> byName = new 
java.util.HashMap<>();
+        for (WeblogBookmark b : importedBookmarks()) {
+            byName.put(b.getName(), b);
+        }
+        // links that browsers accept are kept even when java.net.URI refuses 
them
+        assertEquals(java.util.Set.of("web", "feed", "spaces", "underscore"), 
byName.keySet());
+        assertEquals("https://example.com/a";, byName.get("web").getUrl());
+        assertEquals("http://example.com/feed";, 
byName.get("web").getFeedUrl());
+        assertEquals("https://example.com/b";, byName.get("feed").getUrl());
+        assertNull(byName.get("feed").getFeedUrl());
+        assertEquals("https://my_blog.example.com/";, 
byName.get("underscore").getUrl());
+    }
 }
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapperTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapperTest.java
new file mode 100644
index 000000000..a1b41206b
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogBookmarkWrapperTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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 org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Blogroll links reach templates only when they are relative or use a scheme
+ * a blogroll link needs.
+ */
+class WeblogBookmarkWrapperTest {
+
+    @Test
+    void keepsWebAndRelativeLinks() {
+        assertEquals("https://example.com/a";, 
WeblogBookmarkWrapper.linkOrEmpty("https://example.com/a";));
+        assertEquals("HTTP://example.com/", 
WeblogBookmarkWrapper.linkOrEmpty("HTTP://example.com/"));
+        assertEquals("ftp://example.com/f";, 
WeblogBookmarkWrapper.linkOrEmpty("ftp://example.com/f";));
+        assertEquals("mailto:[email protected]";, 
WeblogBookmarkWrapper.linkOrEmpty("mailto:[email protected]";));
+        assertEquals("/blog/page", 
WeblogBookmarkWrapper.linkOrEmpty("/blog/page"));
+        assertEquals("page?a=b:c", 
WeblogBookmarkWrapper.linkOrEmpty("page?a=b:c"));
+        assertNull(WeblogBookmarkWrapper.linkOrEmpty(null));
+    }
+
+    @Test
+    void dropsOtherSchemes() {
+        assertEquals("", 
WeblogBookmarkWrapper.linkOrEmpty("data:text/plain,x"));
+        assertEquals("", WeblogBookmarkWrapper.linkOrEmpty("FILE:///etc/x"));
+        assertEquals("", WeblogBookmarkWrapper.linkOrEmpty(" 
fi\tle:///etc/x"));
+    }
+
+    @Test
+    void dropsCharacterReferencesBeforeThePath() {
+        assertEquals("", WeblogBookmarkWrapper.linkOrEmpty("x&#58;y"));
+        assertEquals("", WeblogBookmarkWrapper.linkOrEmpty("x&colon;y"));
+        assertEquals("", WeblogBookmarkWrapper.linkOrEmpty("&#120;:y"));
+        assertEquals("/a?b=1&c=2", 
WeblogBookmarkWrapper.linkOrEmpty("/a?b=1&c=2"));
+    }
+}
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/util/LinkSchemeTest.java 
b/app/src/test/java/org/apache/roller/weblogger/util/LinkSchemeTest.java
new file mode 100644
index 000000000..71631d378
--- /dev/null
+++ b/app/src/test/java/org/apache/roller/weblogger/util/LinkSchemeTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.roller.weblogger.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class LinkSchemeTest {
+
+    @Test
+    void readsSchemes() {
+        assertEquals("https", LinkScheme.of("HTTPS://example.com/"));
+        assertEquals("mailto", LinkScheme.of("mailto:[email protected]";));
+        assertEquals("file", LinkScheme.of(" fi\tle:///etc/x"));
+    }
+
+    @Test
+    void relativeLinksHaveNoScheme() {
+        assertEquals("", LinkScheme.of("/a/b"));
+        assertEquals("", LinkScheme.of("page?a=b:c"));
+        assertEquals("", LinkScheme.of("#top"));
+        assertEquals("", LinkScheme.of("/a?b=1&c=2"));
+    }
+
+    @Test
+    void refusesWhatCannotBeRead() {
+        assertNull(LinkScheme.of(null));
+        assertNull(LinkScheme.of("x&#58;y"));
+        assertNull(LinkScheme.of("x&colon;y"));
+        assertNull(LinkScheme.of("&#120;:y"));
+        assertNull(LinkScheme.of(":y"));
+        assertNull(LinkScheme.of("1x:y"));
+    }
+}

Reply via email to