Skip to content

Commit b0aa0ca

Browse files
unhappychoiceclaude
andcommitted
fix(fetcher): escape markdown metacharacters in forge_items::render_markdown
`forge_items::render_markdown` interpolated user-controlled `label`, `title`, and `url` directly into Markdown source. A PR title like `fix ](oops)` would truncate the link target and trail the rest as plain text; `*_wip_*` would render as bold-italic instead of literal punctuation; and a URL carrying `)` would break the link target the same way. Add small `escape_markdown_inline` / `escape_markdown_url` helpers covering the documented set of CommonMark-significant inline glyphs and the two URL characters that can terminate a `(...)` link target. Adds unit coverage for both helpers and a regression test on the combined rendering path. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 49c1d43 commit b0aa0ca

1 file changed

Lines changed: 72 additions & 3 deletions

File tree

‎src/fetcher/forge_items.rs‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,40 @@ fn render_text_block(rows: &[ForgeRow]) -> Body {
9999
fn render_markdown(rows: &[ForgeRow]) -> Body {
100100
let value = rows
101101
.iter()
102-
.map(|r| match &r.url {
103-
Some(url) => format!("- **{}** [{}]({})", r.label, r.title, url),
104-
None => format!("- **{}** {}", r.label, r.title),
102+
.map(|r| {
103+
let label = escape_markdown_inline(&r.label);
104+
let title = escape_markdown_inline(&r.title);
105+
match &r.url {
106+
Some(url) => format!("- **{label}** [{title}]({})", escape_markdown_url(url)),
107+
None => format!("- **{label}** {title}"),
108+
}
105109
})
106110
.collect::<Vec<_>>()
107111
.join("\n");
108112
Body::MarkdownTextBlock(MarkdownTextBlockData { value })
109113
}
110114

115+
/// Escape the small set of Markdown inline metacharacters likely to appear in forge titles
116+
/// and labels: `\`, `` ` ``, `*`, `_`, `[`, `]`. `<` and `&` are not handled because
117+
/// `text_markdown` consumes the value as Markdown source and CommonMark treats them as
118+
/// literal text unless followed by a valid HTML tag — which forge titles never are.
119+
fn escape_markdown_inline(value: &str) -> String {
120+
value
121+
.replace('\\', "\\\\")
122+
.replace('`', "\\`")
123+
.replace('*', "\\*")
124+
.replace('_', "\\_")
125+
.replace('[', "\\[")
126+
.replace(']', "\\]")
127+
}
128+
129+
/// URL-side escape limited to the two characters that can break a Markdown link target:
130+
/// closing paren ends the `(...)`, backslash is the escape glyph itself. Everything else is
131+
/// already URL-encoded by the forge.
132+
fn escape_markdown_url(url: &str) -> String {
133+
url.replace('\\', "%5C").replace(')', "%29")
134+
}
135+
111136
fn render_linked(rows: &[ForgeRow]) -> Body {
112137
Body::LinkedTextBlock(LinkedTextBlockData {
113138
items: rows
@@ -296,6 +321,50 @@ mod tests {
296321
assert!(b.value.contains("- **splashboard#51** feat(fetcher)"));
297322
}
298323

324+
#[test]
325+
fn markdown_escapes_inline_metacharacters_in_label_and_title() {
326+
// A title like `fix ](oops)` used to escape the link target prematurely; `*_wip_*`
327+
// used to render as bold-italic text instead of literal underscores and asterisks.
328+
// Both are user-supplied (forge issue / PR titles) and must round-trip as text, not
329+
// markup.
330+
let rows = vec![ForgeRow {
331+
label: "splashboard#99".into(),
332+
title: "fix ](oops) and *_wip_*".into(),
333+
url: Some("https://example.com/path?x=1)y".into()),
334+
avatar_url: None,
335+
avatar_path: None,
336+
updated_at_unix: 1_700_000_000,
337+
activity_count: 0,
338+
}];
339+
let Body::MarkdownTextBlock(b) = render_forge_rows(&rows, Shape::MarkdownTextBlock) else {
340+
panic!("expected markdown");
341+
};
342+
// `]` is escaped so it can't close the surrounding markdown link prematurely;
343+
// `*` / `_` are escaped so the title stays literal rather than rendering bold /
344+
// italic. Parens are intentionally not escaped because they only carry markup
345+
// significance inside the link target itself, not in the visible link text.
346+
assert!(
347+
b.value.contains("fix \\](oops) and \\*\\_wip\\_\\*"),
348+
"title not escaped: {}",
349+
b.value
350+
);
351+
// URL keeps its closing paren escaped so it can't truncate the markdown link target.
352+
assert!(
353+
b.value.contains("1%29y"),
354+
"url paren not escaped: {}",
355+
b.value
356+
);
357+
}
358+
359+
#[test]
360+
fn escape_markdown_inline_handles_each_documented_glyph() {
361+
assert_eq!(
362+
escape_markdown_inline("a\\b`c*d_e[f]g"),
363+
"a\\\\b\\`c\\*d\\_e\\[f\\]g"
364+
);
365+
assert_eq!(escape_markdown_inline("plain text"), "plain text");
366+
}
367+
299368
#[test]
300369
fn linked_carries_optional_url_through() {
301370
let Body::LinkedTextBlock(b) = render_forge_rows(&rows(), Shape::LinkedTextBlock) else {

0 commit comments

Comments
 (0)