File tree Expand file tree Collapse file tree
main/java/ai/openclaw/app/ui/chat
test/java/ai/openclaw/app/ui/chat Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -75,6 +75,8 @@ import org.commonmark.node.SoftLineBreak
7575import org.commonmark.node.StrongEmphasis
7676import org.commonmark.node.ThematicBreak
7777import org.commonmark.parser.Parser
78+ import java.net.URI
79+ import java.util.Locale
7880import org.commonmark.node.Image as MarkdownImage
7981import 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+
573581internal fun buildChatInlineMarkdown (
574582 text : String ,
575583 linkColor : Color = Color .Blue ,
Original file line number Diff line number Diff 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" )
You can’t perform that action at this time.
0 commit comments