Link addresses written without a scheme, over https
Servers write [url=www.teamspeak.com]; it showed as raw BBCode. An address without a scheme now opens over https, as bare www. text in a message does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,9 @@ public final class BBCode {
|
|||||||
/** Web links plus TeamSpeak's own {@code client://} / {@code channel://} / {@code ts3server://} schemes. */
|
/** Web links plus TeamSpeak's own {@code client://} / {@code channel://} / {@code ts3server://} schemes. */
|
||||||
private static final Pattern SAFE_URL =
|
private static final Pattern SAFE_URL =
|
||||||
Pattern.compile("(?i)(https?|ftp|client|channel|ts3server|ts3file|offline)://[^\\s\"'<>]*");
|
Pattern.compile("(?i)(https?|ftp|client|channel|ts3server|ts3file|offline)://[^\\s\"'<>]*");
|
||||||
|
/** A web address without a scheme, such as {@code www.teamspeak.com} or {@code example.org:8080/x}. */
|
||||||
|
private static final Pattern SCHEMELESS_URL =
|
||||||
|
Pattern.compile("(?i)[a-z0-9-]+(?:\\.[a-z0-9-]+)+(?::\\d+)?(?:[/?#][^\\s\"'<>]*)?");
|
||||||
/** TeamSpeak's own protocols, which are rendered without an underline. */
|
/** TeamSpeak's own protocols, which are rendered without an underline. */
|
||||||
private static final Pattern TS_PROTOCOL =
|
private static final Pattern TS_PROTOCOL =
|
||||||
Pattern.compile("(?i)^(client|channel|ts3server|ts3file|offline)://");
|
Pattern.compile("(?i)^(client|channel|ts3server|ts3file|offline)://");
|
||||||
@@ -110,8 +113,8 @@ public final class BBCode {
|
|||||||
int last = 0;
|
int last = 0;
|
||||||
while (m.find()) {
|
while (m.find()) {
|
||||||
out.append(escape(input.substring(last, m.start())));
|
out.append(escape(input.substring(last, m.start())));
|
||||||
String href = m.group(1);
|
String href = href(m.group(1));
|
||||||
if (SAFE_URL.matcher(href).matches()) {
|
if (href != null) {
|
||||||
out.append(link(href)).append(escape(m.group(2))).append(linkClose(href));
|
out.append(link(href)).append(escape(m.group(2))).append(linkClose(href));
|
||||||
} else {
|
} else {
|
||||||
out.append(escape(m.group()));
|
out.append(escape(m.group()));
|
||||||
@@ -211,8 +214,9 @@ public final class BBCode {
|
|||||||
return push(out, open, name, "<span style=\"font-size:" + px + "px\">", "</span>");
|
return push(out, open, name, "<span style=\"font-size:" + px + "px\">", "</span>");
|
||||||
}
|
}
|
||||||
case "url": {
|
case "url": {
|
||||||
if (!SAFE_URL.matcher(value).matches()) return false;
|
String href = href(value);
|
||||||
return push(out, open, name, link(value), linkClose(value));
|
if (href == null) return false;
|
||||||
|
return push(out, open, name, link(href), linkClose(href));
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -232,6 +236,17 @@ public final class BBCode {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where a link may lead: a URL in a scheme we open as it is, an address without one
|
||||||
|
* (servers write {@code [url=www.…]}) over https, or {@code null} for anything else.
|
||||||
|
*/
|
||||||
|
private static String href(String value) {
|
||||||
|
String url = value.trim();
|
||||||
|
if (SAFE_URL.matcher(url).matches()) return url;
|
||||||
|
if (SCHEMELESS_URL.matcher(url).matches()) return "https://" + url;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private static String link(String href) {
|
private static String link(String href) {
|
||||||
return "<a class=\"" + linkClass(href) + "\" href=\"" + escape(href) + "\">";
|
return "<a class=\"" + linkClass(href) + "\" href=\"" + escape(href) + "\">";
|
||||||
}
|
}
|
||||||
@@ -284,7 +299,7 @@ public final class BBCode {
|
|||||||
while (m.find()) {
|
while (m.find()) {
|
||||||
out.append(escape(raw.substring(last, m.start())).replace("\n", "<br>"));
|
out.append(escape(raw.substring(last, m.start())).replace("\n", "<br>"));
|
||||||
String url = m.group();
|
String url = m.group();
|
||||||
String href = url.regionMatches(true, 0, "www.", 0, 4) ? "http://" + url : url;
|
String href = url.regionMatches(true, 0, "www.", 0, 4) ? "https://" + url : url;
|
||||||
out.append(link(href)).append(escape(url)).append(linkClose(href));
|
out.append(link(href)).append(escape(url)).append(linkClose(href));
|
||||||
last = m.end();
|
last = m.end();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package eu.lixko.ts3j.client.text;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
/** Where a link may lead: known schemes as they are, addresses without one over https. */
|
||||||
|
class BBCodeAddressesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void linksAddressesWithoutAScheme() {
|
||||||
|
// TeamSpeak's default welcome message.
|
||||||
|
String html = BBCode.toHtml("check [url=www.teamspeak.com]www.teamspeak.com[/url] for", false);
|
||||||
|
assertTrue(html.contains("href=\"https://www.teamspeak.com\">www.teamspeak.com</a>"), html);
|
||||||
|
assertFalse(html.contains("[url"), html);
|
||||||
|
assertTrue(BBCode.toHtml("[url]example.org:8080/a?b=c[/url]", false)
|
||||||
|
.contains("href=\"https://example.org:8080/a?b=c\""));
|
||||||
|
assertTrue(BBCode.toHtml("see www.example.org", false).contains("href=\"https://www.example.org\""));
|
||||||
|
assertTrue(BBCode.linksToHtml("[url=www.example.org]x[/url]").contains("href=\"https://www.example.org\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void keepsTheSchemeAnAddressHas() {
|
||||||
|
assertTrue(BBCode.toHtml("[url=http://example.org]x[/url]", false).contains("href=\"http://example.org\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void refusesWhatIsNeitherALinkNorAnAddress() {
|
||||||
|
assertFalse(BBCode.toHtml("[url=javascript:alert(1)]x[/url]", false).contains("href"));
|
||||||
|
assertFalse(BBCode.toHtml("[url=just words]x[/url]", false).contains("href"));
|
||||||
|
assertFalse(BBCode.toHtml("[url=localhost]x[/url]", false).contains("href"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user