Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(cli): use parsed link destination spans in attachment link rewriter (#11318)
T Mohan Aditya57187d368a13
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 109a2912c7..10785d237f 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -854,7 +854,7 @@ jobs:# silently as "ok".run: go test ./internal/daemon/execenv -v -run '^TestWindowsOpenclawShim' -count=1 -timeout=5m- - name: Test Windows workdir containment guard in the CLI+ - name: Test Windows CLI workdir guard and attachment rewritingif: ${{ needs.changes.outputs.backend == 'true' }}working-directory: server# The CLI's --content-file / --attachment guard decides whether a path is@@ -873,9 +873,10 @@ jobs:# tests run nowhere else. It matters twice over on a non-admin host,# where the package's cross-platform cases skip for want of the symlink# privilege; the ubuntu backend job covers those shapes instead.+ # The attachment regression covers both bare names and drive-letter file URLs.# -v so a silent skip (or a -run pattern that stops matching) is visible# in the log instead of passing as "ok".- run: go test ./cmd/multica -v -run '^TestFileWithinWorkingDirWindows' -count=1 -timeout=5m+ run: go test ./cmd/multica -v -run '^Test(FileWithinWorkingDirWindows|RewriteAttachmentLinksCaseInsensitiveOnWindows$)' -count=1 -timeout=5m- name: Test Windows link-target classification in internal/utilif: ${{ needs.changes.outputs.backend == 'true' }}diff --git a/server/cmd/multica/attachment_link_rewrite.go b/server/cmd/multica/attachment_link_rewrite.goindex 305de02277..699c27b93c 100644--- a/server/cmd/multica/attachment_link_rewrite.go+++ b/server/cmd/multica/attachment_link_rewrite.go@@ -3,14 +3,16 @@ package mainimport ("bytes""fmt"+ "html""io""net/url""path/filepath"+ "runtime""sort""strings"- "github.com/yuin/goldmark""github.com/yuin/goldmark/ast"+ "github.com/yuin/goldmark/parser""github.com/yuin/goldmark/text""github.com/yuin/goldmark/util"@@ -45,6 +47,13 @@ type attachmentLinkMatcher struct {byName map[string][]int}+func pathKey(s string) string {+ if runtime.GOOS == "windows" {+ return strings.ToLower(s)+ }+ return s+}+func newAttachmentLinkMatcher(paths []string) *attachmentLinkMatcher {m := &attachmentLinkMatcher{byPath: map[string]int{}, byName: map[string][]int{}}for i, p := range paths {@@ -55,15 +64,15 @@ func newAttachmentLinkMatcher(paths []string) *attachmentLinkMatcher {if len(keys) == 0 {continue}- if _, dup := m.byPath[keys[0]]; dup {+ if _, dup := m.byPath[pathKey(keys[0])]; dup {// The same file attached twice; the first upload wins and the// duplicate must not make its bare name ambiguous.continue}for _, key := range keys {- m.byPath[key] = i+ m.byPath[pathKey(key)] = i}- base := filepath.Base(p)+ base := pathKey(filepath.Base(p))m.byName[base] = append(m.byName[base], i)}return m@@ -93,7 +102,7 @@ func (m *attachmentLinkMatcher) match(target string) (int, bool) {return -1, false}candidates := []string{raw}- if unescaped := string(util.UnescapePunctuations([]byte(raw))); unescaped != raw {+ if unescaped := unescapeMarkdownDestination(raw); unescaped != raw {candidates = append(candidates, unescaped)}for _, c := range append([]string(nil), candidates...) {@@ -109,6 +118,28 @@ func (m *attachmentLinkMatcher) match(target string) (int, bool) {return -1, false}+func unescapeMarkdownDestination(s string) string {+ var b strings.Builder+ start := 0+ for i := 0; i < len(s); i++ {+ if s[i] == '\\' && i+1 < len(s) && util.IsPunct(s[i+1]) {+ if i > start {+ b.WriteString(html.UnescapeString(s[start:i]))+ }+ b.WriteByte(s[i+1])+ i+++ start = i + 1+ }+ }+ if start == 0 {+ return html.UnescapeString(s)+ }+ if start < len(s) {+ b.WriteString(html.UnescapeString(s[start:]))+ }+ return b.String()+}+func (m *attachmentLinkMatcher) matchPath(target string) (int, bool) {// A Windows drive path parses as a URL with scheme "c", so test for an// absolute path before sniffing schemes — same order as the lint.@@ -125,19 +156,64 @@ func (m *attachmentLinkMatcher) matchPath(target string) (int, bool) {}local := filepath.FromSlash(target)for _, key := range attachmentPathKeys(local) {- if i, ok := m.byPath[key]; ok {+ if i, ok := m.byPath[pathKey(key)]; ok {return i, true}}cleaned := filepath.Clean(local)if !filepath.IsAbs(cleaned) && !strings.ContainsRune(cleaned, filepath.Separator) {- if idx := m.byName[cleaned]; len(idx) == 1 {+ if idx := m.byName[pathKey(cleaned)]; len(idx) == 1 {return idx[0], true}}return -1, false}+type attachmentLinkParser struct {+ parser.InlineParser+ destSpans map[ast.Node]text.Segment+}++func (p *attachmentLinkParser) Parse(parent ast.Node, reader text.Reader, pc parser.Context) ast.Node {+ line, _ := reader.PeekLine()+ if len(line) == 0 || line[0] != ']' {+ return p.InlineParser.Parse(parent, reader, pc)+ }+ openLine, openSeg := reader.Position()+ n := p.InlineParser.Parse(parent, reader, pc)+ var dest []byte+ var ref *ast.ReferenceLink+ switch node := n.(type) {+ case *ast.Link:+ dest, ref = node.Destination, node.Reference+ case *ast.Image:+ dest, ref = node.Destination, node.Reference+ default:+ return n+ }+ if ref != nil || len(dest) == 0 {+ return n+ }+ endLine, endSeg := reader.Position()+ reader.SetPosition(openLine, openSeg)+ reader.Advance(1)+ if reader.Peek() == '(' {+ reader.Advance(1)+ reader.SkipSpaces()+ if reader.Peek() == '<' {+ reader.Advance(1)+ }+ _, destSeg := reader.Position()+ p.destSpans[n] = text.NewSegment(destSeg.Start, destSeg.Start+len(dest))+ }+ reader.SetPosition(endLine, endSeg)+ return n+}++func (p *attachmentLinkParser) CloseBlock(parent ast.Node, reader text.Reader, pc parser.Context) {+ p.InlineParser.(parser.CloseBlocker).CloseBlock(parent, reader, pc)+}+// rewriteAttachmentLinks replaces every inline link/image destination in body// that names one of paths with the URL at the same index in urls. Bodies with// nothing to rewrite are returned unchanged byte for byte.@@ -151,60 +227,53 @@ func rewriteAttachmentLinks(body string, paths, urls []string) (string, []attach}source := []byte(body)- doc := goldmark.New().Parser().Parse(text.NewReader(source))-- var codeSpans []text.Segment- _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {- if entering && n.Kind() == ast.KindCodeSpan {- for c := n.FirstChild(); c != nil; c = c.NextSibling() {- if t, ok := c.(*ast.Text); ok {- codeSpans = append(codeSpans, t.Segment)- }- }+ links := &attachmentLinkParser{+ InlineParser: parser.NewLinkParser(),+ destSpans: map[ast.Node]text.Segment{},+ }+ inlineParsers := parser.DefaultInlineParsers()+ for i := range inlineParsers {+ if inlineParsers[i].Value == links.InlineParser {+ inlineParsers[i].Value = links}- return ast.WalkContinue, nil- })+ }+ markdown := parser.NewParser(+ parser.WithBlockParsers(parser.DefaultBlockParsers()...),+ parser.WithInlineParsers(inlineParsers...),+ parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...),+ )+ doc := markdown.Parse(text.NewReader(source))type edit struct {start, stop inturl string}var edits []edit- claimed := map[int]bool{}var rewrites []attachmentLinkRewritereported := map[string]bool{}- // Post-order: an image nested in a link label claims its own destination- // before the enclosing link searches past it for the outer one._ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {- if entering {+ if !entering {return ast.WalkContinue, nil}- var link *ast.Link+ var dest []byteswitch node := n.(type) {case *ast.Link:- link = node+ dest = node.Destinationcase *ast.Image:- link = &ast.Link{}- link.Destination, link.Reference = node.Destination, node.Reference+ dest = node.Destinationdefault:return ast.WalkContinue, nil}- // Reference-style links keep their destination in a definition- // elsewhere in the body; they are left for the lint.- if link.Reference != nil || len(link.Destination) == 0 || n.Pos() < 0 {+ span, ok := links.destSpans[n]+ if !ok || span.Start < 0 || span.Stop > len(source) || !bytes.Equal(source[span.Start:span.Stop], dest) {return ast.WalkContinue, nil}- target := string(link.Destination)+ target := string(dest)idx, ok := matcher.match(target)if !ok || idx >= len(urls) || urls[idx] == "" {return ast.WalkContinue, nil}- start := locateLinkDestination(source, n.Pos(), link.Destination, claimed, codeSpans)- if start < 0 {- return ast.WalkContinue, nil- }- claimed[start] = true- edits = append(edits, edit{start: start, stop: start + len(link.Destination), url: urls[idx]})+ edits = append(edits, edit{start: span.Start, stop: span.Stop, url: urls[idx]})if !reported[target] {reported[target] = truerewrites = append(rewrites, attachmentLinkRewrite{Target: target, URL: urls[idx]})@@ -227,59 +296,6 @@ func rewriteAttachmentLinks(body string, paths, urls []string) (string, []attachreturn b.String(), rewrites}-// locateLinkDestination returns the byte offset of a link's destination,-// searching from the link's start for the first unclaimed occurrence that is a-// whole destination directly after the `](` closing a label. The parser does-// not record the destination's own position, and the same text may also-// appear in the label or as a prefix of a nested link's destination.-func locateLinkDestination(source []byte, from int, dest []byte, claimed map[int]bool, codeSpans []text.Segment) int {- for from < len(source) {- i := bytes.Index(source[from:], dest)- if i < 0 {- return -1- }- start := from + i- if !claimed[start] && !insideSegments(start, codeSpans) && isWholeLinkDestination(source, start, start+len(dest)) {- return start- }- from = start + 1- }- return -1-}--// isWholeLinkDestination reports whether source[start:stop] is an entire-// inline link destination: opened by `](` (optionally `<`), and closed by the-// matching `>` or by the whitespace / `)` that ends a bare destination.-func isWholeLinkDestination(source []byte, start, stop int) bool {- j := start - 1- angle := j >= 0 && source[j] == '<'- if angle {- j--- if stop >= len(source) || source[stop] != '>' {- return false- }- } else if stop < len(source) && !isLinkSpace(source[stop]) && source[stop] != ')' {- return false- }- for j >= 0 && isLinkSpace(source[j]) {- j--- }- return j >= 1 && source[j] == '(' && source[j-1] == ']'-}--func isLinkSpace(c byte) bool {- return c == ' ' || c == '\t' || c == '\n' || c == '\r'-}--func insideSegments(pos int, segments []text.Segment) bool {- for _, s := range segments {- if pos >= s.Start && pos < s.Stop {- return true- }- }- return false-}-// previewAttachmentLinks rewrites body against placeholder URLs, before// anything is uploaded, so the local-path lint judges the body that will// actually be published: a link to a file attached in the same call is not adiff --git a/server/cmd/multica/attachment_link_rewrite_test.go b/server/cmd/multica/attachment_link_rewrite_test.goindex 06d580a62b..c70cd0dbdb 100644--- a/server/cmd/multica/attachment_link_rewrite_test.go+++ b/server/cmd/multica/attachment_link_rewrite_test.go@@ -6,6 +6,7 @@ import ("net/http/httptest""os""path/filepath"+ "runtime""strings""testing")@@ -14,7 +15,7 @@ const rewriteURL = "https://cdn.example/a"func TestRewriteAttachmentLinks(t *testing.T) {workdir := withWorkdir(t)- for _, name := range []string{"a.png", "b.png", "my shot.png", "shots/a.png"} {+ for _, name := range []string{"a.png", "b.png", "a&b.png", "my shot.png", "shots/a.png"} {if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {t.Fatal(err)}@@ -149,6 +150,54 @@ func TestRewriteAttachmentLinks(t *testing.T) {body: "![shot][s]\n\n[s]: ./a.png\n",want: "![shot][s]\n\n[s]: ./a.png\n",},+ {+ name: "escaped closing bracket in label does not hijack destination rewrite",+ paths: []string{"a.png"},+ body: ` to view](./a.png)`,+ want: ` to view](` + rewriteURL + `)`,+ },+ {+ name: "inline HTML in label does not hijack destination rewrite",+ paths: []string{"a.png"},+ body: `[<span title="](./a.png)">open</span>](./a.png)`,+ want: `[<span title="](./a.png)">open</span>](` + rewriteURL + `)`,+ },+ {+ name: "HTML comment in label does not hijack destination rewrite",+ paths: []string{"a.png"},+ body: `[<!-- ](./a.png) -->open](./a.png)`,+ want: `[<!-- ](./a.png) -->open](` + rewriteURL + `)`,+ },+ {+ name: "blockquote continuation line before destination",+ paths: []string{"a.png"},+ body: "> [click](\n> ./a.png)",+ want: "> [click](\n> " + rewriteURL + ")",+ },+ {+ name: "blockquote continuation line before angle-bracket destination",+ paths: []string{"my shot.png"},+ body: "> [open](\n> <./my shot.png> \"title\")",+ want: "> [open](\n> <" + rewriteURL + "> \"title\")",+ },+ {+ name: "named HTML character reference in destination",+ paths: []string{"a&b.png"},+ body: "",+ want: "",+ },+ {+ name: "numeric HTML character reference in destination",+ paths: []string{"my shot.png"},+ body: "",+ want: "",+ },+ {+ name: "backslash-escaped ampersand is not decoded as an HTML entity",+ paths: []string{"a&b.png"},+ body: ``,+ want: ``,+ },} {t.Run(tc.name, func(t *testing.T) {urls := make([]string, len(tc.paths))@@ -306,3 +355,26 @@ func TestRunIssueCreateKeepsInlineAttachmentInPlace(t *testing.T) {t.Errorf("description =\n%q\nwant\n%q", posted["description"], want)}}++func TestRewriteAttachmentLinksCaseInsensitiveOnWindows(t *testing.T) {+ if runtime.GOOS != "windows" {+ t.Skip("case-insensitive filesystem paths apply on Windows")+ }+ workdir := withWorkdir(t)+ if err := os.MkdirAll("shots", 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join("shots", "Shot.PNG"), []byte("png"), 0o600); err != nil {+ t.Fatal(err)+ }+ fileURL := "file:///" + strings.ToLower(filepath.ToSlash(filepath.Join(workdir, "shots", "shot.png")))+ got, _ := rewriteAttachmentLinks(+ " and ",+ []string{filepath.Join("shots", "Shot.PNG")},+ []string{rewriteURL},+ )+ want := " and "+ if got != want {+ t.Fatalf("rewrite =\n%q\nwant\n%q", got, want)+ }+}