Skip to content

Commit 4712931

Browse files
committed
fix(android): filter unsafe markdown links
1 parent ce039eb commit 4712931

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/ui/chat/ChatMarkdown.kt‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ import org.commonmark.node.SoftLineBreak
7575
import org.commonmark.node.StrongEmphasis
7676
import org.commonmark.node.ThematicBreak
7777
import org.commonmark.parser.Parser
78+
import java.net.URI
79+
import java.util.Locale
7880
import org.commonmark.node.Image as MarkdownImage
7981
import org.commonmark.node.Text as MarkdownTextNode
8082

@@ -548,15 +550,13 @@ private fun AnnotatedString.Builder.appendLinkNode(
548550
color = linkColor,
549551
textDecoration = TextDecoration.Underline,
550552
)
551-
if (destination.isEmpty()) {
552-
withStyle(linkStyle) {
553-
appendInlineNode(
554-
link.firstChild,
555-
inlineCodeBg = inlineCodeBg,
556-
inlineCodeColor = inlineCodeColor,
557-
linkColor = linkColor,
558-
)
559-
}
553+
if (destination.isEmpty() || !isSafeMarkdownLinkDestination(destination)) {
554+
appendInlineNode(
555+
link.firstChild,
556+
inlineCodeBg = inlineCodeBg,
557+
inlineCodeColor = inlineCodeColor,
558+
linkColor = linkColor,
559+
)
560560
return
561561
}
562562

@@ -570,6 +570,14 @@ private fun AnnotatedString.Builder.appendLinkNode(
570570
}
571571
}
572572

573+
private fun isSafeMarkdownLinkDestination(destination: String): Boolean {
574+
val scheme =
575+
runCatching { URI(destination).scheme?.lowercase(Locale.US) }
576+
.getOrNull()
577+
?: return false
578+
return scheme == "http" || scheme == "https"
579+
}
580+
573581
internal fun buildChatInlineMarkdown(
574582
text: String,
575583
linkColor: Color = Color.Blue,

‎apps/android/app/src/test/java/ai/openclaw/app/ui/chat/ChatMarkdownTest.kt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ class ChatMarkdownTest {
3333
assertEquals("https://docs.openclaw.ai/help/testing", (links.single().item as LinkAnnotation.Url).url)
3434
}
3535

36+
@Test
37+
fun markdownLinksDropUnsafeDestinations() {
38+
listOf(
39+
"intent://example/#Intent;scheme=openclaw;end",
40+
"file:///sdcard/Download/x",
41+
"content://downloads/public_downloads/1",
42+
"tel:+15551234567",
43+
"javascript:alert(1)",
44+
).forEach { destination ->
45+
val annotated = buildChatInlineMarkdown("Open [settings]($destination)")
46+
47+
assertEquals("Open settings", annotated.text)
48+
assertTrue(annotated.getLinkAnnotations(0, annotated.length).isEmpty())
49+
}
50+
}
51+
3652
@Test
3753
fun plainTextDoesNotAddLinkAnnotations() {
3854
val annotated = buildChatInlineMarkdown("No link here")

0 commit comments

Comments
 (0)