Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-5915: fix(slack): neutralize links in member issue titles (#6799)

Jamison-J0f8c300dacfa
diff --git a/server/internal/integrations/channel/member_text.go b/server/internal/integrations/channel/member_text.go
new file mode 100644
index 0000000000..d68b86fa5e
--- /dev/null
+++ b/server/internal/integrations/channel/member_text.go
@@ -0,0 +1,10 @@
+package channel
+
+import "strings"
+
+// BreakMarkdownLinkAdjacency separates the standard inline Markdown "](" link
+// adjacency in untrusted text. Platform-native markup needs its own guard.
+// Text without "](" is returned byte-for-byte unchanged.
+func BreakMarkdownLinkAdjacency(s string) string {
+ return strings.ReplaceAll(s, "](", "] (")
+}
diff --git a/server/internal/integrations/channel/member_text_test.go b/server/internal/integrations/channel/member_text_test.go
new file mode 100644
index 0000000000..98ba3106b3
--- /dev/null
+++ b/server/internal/integrations/channel/member_text_test.go
@@ -0,0 +1,39 @@
+package channel
+
+import "testing"
+
+func TestBreakMarkdownLinkAdjacency(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ in string
+ want string
+ }{
+ {
+ name: "inline link with nested parentheses",
+ in: "[重置密码](https://evil.example/reset_(now))",
+ want: "[重置密码] (https://evil.example/reset_(now))",
+ },
+ {
+ name: "image-like syntax",
+ in: "![重置密码](https://evil.example/image.png)",
+ want: "![重置密码] (https://evil.example/image.png)",
+ },
+ {
+ name: "reference definition unchanged",
+ in: "[重置密码]: https://evil.example\n\n[重置密码]",
+ want: "[重置密码]: https://evil.example\n\n[重置密码]",
+ },
+ {name: "ordinary CJK title", in: "修复登录失败", want: "修复登录失败"},
+ {name: "ordinary English title", in: "Fix login failure", want: "Fix login failure"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := BreakMarkdownLinkAdjacency(tc.in)
+ if got != tc.want {
+ t.Fatalf("BreakMarkdownLinkAdjacency(%q) = %q, want %q", tc.in, got, tc.want)
+ }
+ if twice := BreakMarkdownLinkAdjacency(got); twice != got {
+ t.Fatalf("second pass changed the guarded text:\n once %q\ntwice %q", got, twice)
+ }
+ })
+ }
+}
diff --git a/server/internal/integrations/slack/replier.go b/server/internal/integrations/slack/replier.go
index 12c7567552..60e85bd0dd 100644
--- a/server/internal/integrations/slack/replier.go
+++ b/server/internal/integrations/slack/replier.go
@@ -195,7 +195,7 @@ func (r *OutboundReplier) post(ctx context.Context, inst engine.ResolvedInstalla
func issueCreatedText(res engine.Result) string {
id := issueResultIdentifier(res)
- title := strings.TrimSpace(res.IssueTitle)
+ title := memberIssueTitle(strings.TrimSpace(res.IssueTitle))
if title == "" {
return "✅ Created " + id
}
@@ -204,13 +204,21 @@ func issueCreatedText(res engine.Result) string {
func issueDuplicateText(res engine.Result) string {
id := issueResultIdentifier(res)
- title := strings.TrimSpace(res.IssueTitle)
+ title := memberIssueTitle(strings.TrimSpace(res.IssueTitle))
if title == "" {
return "⚠️ Not created — active issue " + id + " already exists."
}
return "⚠️ Not created — active issue " + id + " already exists: " + title
}
+func memberIssueTitle(title string) string {
+ title = channel.BreakMarkdownLinkAdjacency(title)
+ // formatMrkdwn deliberately preserves existing Slack entities such as
+ // <url|label> and <@user>. Encode their opening delimiter before that pass
+ // so member-authored links and mentions are handled as visible text.
+ return strings.ReplaceAll(title, "<", "&lt;")
+}
+
func issueResultIdentifier(res engine.Result) string {
if res.IssueIdentifier != "" {
return res.IssueIdentifier
diff --git a/server/internal/integrations/slack/replier_test.go b/server/internal/integrations/slack/replier_test.go
index 591c7ca68b..0056a829d8 100644
--- a/server/internal/integrations/slack/replier_test.go
+++ b/server/internal/integrations/slack/replier_test.go
@@ -220,6 +220,88 @@ func TestIssueDuplicateText(t *testing.T) {
}
}
+func TestIssueReplyTitlesCannotCreateSlackLinks(t *testing.T) {
+ title := "安全升级:请点击 [重置密码](https://evil.example/reset_(now)) 完成验证"
+ for _, tc := range []struct {
+ name string
+ reply func(engine.Result) string
+ }{
+ {name: "created", reply: issueCreatedText},
+ {name: "duplicate", reply: issueDuplicateText},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ text := tc.reply(engine.Result{IssueIdentifier: "MUL-7", IssueTitle: title})
+ formatted := formatMrkdwn(text)
+ want := map[string]string{
+ "created": "✅ Created MUL-7 — 安全升级:请点击 [重置密码] (https://evil.example/reset_(now)) 完成验证",
+ "duplicate": "⚠️ Not created — active issue MUL-7 already exists: 安全升级:请点击 [重置密码] (https://evil.example/reset_(now)) 完成验证",
+ }[tc.name]
+ if formatted != want {
+ t.Fatalf("member-authored title was not rendered as inert visible text:\n got %q\nwant %q", formatted, want)
+ }
+ if twice := formatMrkdwn(formatted); twice != formatted {
+ t.Fatalf("second formatter pass changed the guarded reply:\n once %q\ntwice %q", formatted, twice)
+ }
+ })
+ }
+}
+
+func TestIssueReplyTitlesCannotCreateNativeSlackEntities(t *testing.T) {
+ title := "安全升级:<http://evil.example|重置密码> <https://evil.example|验证> <mailto:[email protected]|邮件> <tel:+15555550100|电话> <@U123>"
+ for _, tc := range []struct {
+ name string
+ reply func(engine.Result) string
+ prefix string
+ }{
+ {name: "created", reply: issueCreatedText, prefix: "✅ Created MUL-7 — "},
+ {name: "duplicate", reply: issueDuplicateText, prefix: "⚠️ Not created — active issue MUL-7 already exists: "},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ formatted := formatMrkdwn(tc.reply(engine.Result{IssueIdentifier: "MUL-7", IssueTitle: title}))
+ want := tc.prefix + "安全升级:&lt;http://evil.example|重置密码&gt; &lt;https://evil.example|验证&gt; &lt;mailto:[email protected]|邮件&gt; &lt;tel:+15555550100|电话&gt; &lt;@U123&gt;"
+ if formatted != want {
+ t.Fatalf("member-authored Slack entity stayed active:\n got %q\nwant %q", formatted, want)
+ }
+ if twice := formatMrkdwn(formatted); twice != formatted {
+ t.Fatalf("second formatter pass changed the guarded reply:\n once %q\ntwice %q", formatted, twice)
+ }
+ })
+ }
+}
+
+func TestIssueReplyTitleMarkdownBoundaries(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ title string
+ }{
+ {name: "image-like syntax", title: "![重置密码](https://evil.example/image.png)"},
+ {name: "reference definition", title: "[重置密码]: https://evil.example\n\n[重置密码]"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ formatted := formatMrkdwn(issueCreatedText(engine.Result{
+ IssueIdentifier: "MUL-7",
+ IssueTitle: tc.title,
+ }))
+ if strings.Contains(formatted, "<https://evil.example") {
+ t.Fatalf("unsupported member syntax became a Slack manual link: %q", formatted)
+ }
+ })
+ }
+}
+
+func TestIssueReplyOrdinaryTitlesStayByteIdentical(t *testing.T) {
+ for _, title := range []string{"修复登录失败", "Fix login failure"} {
+ created := issueCreatedText(engine.Result{IssueIdentifier: "MUL-7", IssueTitle: title})
+ if want := "✅ Created MUL-7 — " + title; created != want {
+ t.Fatalf("ordinary created title changed:\n got %q\nwant %q", created, want)
+ }
+ duplicate := issueDuplicateText(engine.Result{IssueIdentifier: "MUL-7", IssueTitle: title})
+ if want := "⚠️ Not created — active issue MUL-7 already exists: " + title; duplicate != want {
+ t.Fatalf("ordinary duplicate title changed:\n got %q\nwant %q", duplicate, want)
+ }
+ }
+}
+
func textOrEmpty(m *channel.OutboundMessage) string {
if m == nil {
return ""
diff --git a/server/internal/integrations/wecom/markdown.go b/server/internal/integrations/wecom/markdown.go
index 138152af16..e361c4f8d2 100644
--- a/server/internal/integrations/wecom/markdown.go
+++ b/server/internal/integrations/wecom/markdown.go
@@ -8,6 +8,8 @@ package wecom
import (
"strings"
"unicode/utf8"
+
+ "github.com/multica-ai/multica/server/internal/integrations/channel"
)
// breakMemberLinks is what every caller splicing member-authored text into a
@@ -64,7 +66,7 @@ func breakMemberLinks(s string) string {
// link without "](" is a CommonMark link reference definition, and this
// renderer resolves those — see breakLinkReferenceDefinitions.
func breakLinkAdjacency(s string) string {
- return strings.ReplaceAll(s, "](", "] (")
+ return channel.BreakMarkdownLinkAdjacency(s)
}
// breakLinkReferenceDefinitions stops member-authored text from *defining* a