|
| 1 | +From 3898b6266943f5e1c4723ebab278674c2d70468d Mon Sep 17 00:00:00 2001 |
| 2 | +From: yuin <yuin@inforno.net> |
| 3 | +Date: Thu, 19 Mar 2026 15:21:23 +0900 |
| 4 | +Subject: [PATCH] fix: prevent XSS by escaping dangerous URLs in links and |
| 5 | + images |
| 6 | + |
| 7 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 8 | +Upstream-reference: https://github.com/yuin/goldmark/commit/cb46bbc4eca29d55aa9721e04ad207c23ccc44f9.patch |
| 9 | +--- |
| 10 | + .../yuin/goldmark/renderer/html/html.go | 16 ++++++++++------ |
| 11 | + 1 file changed, 10 insertions(+), 6 deletions(-) |
| 12 | + |
| 13 | +diff --git a/vendor/github.com/yuin/goldmark/renderer/html/html.go b/vendor/github.com/yuin/goldmark/renderer/html/html.go |
| 14 | +index 8738c2a1..cfcd02ab 100644 |
| 15 | +--- a/vendor/github.com/yuin/goldmark/renderer/html/html.go |
| 16 | ++++ b/vendor/github.com/yuin/goldmark/renderer/html/html.go |
| 17 | +@@ -558,12 +558,14 @@ func (r *Renderer) renderAutoLink( |
| 18 | + return ast.WalkContinue, nil |
| 19 | + } |
| 20 | + _, _ = w.WriteString(`<a href="`) |
| 21 | +- url := n.URL(source) |
| 22 | ++ url := util.URLEscape(n.URL(source), false) |
| 23 | + label := n.Label(source) |
| 24 | + if n.AutoLinkType == ast.AutoLinkEmail && !bytes.HasPrefix(bytes.ToLower(url), []byte("mailto:")) { |
| 25 | + _, _ = w.WriteString("mailto:") |
| 26 | + } |
| 27 | +- _, _ = w.Write(util.EscapeHTML(util.URLEscape(url, false))) |
| 28 | ++ if r.Unsafe || !IsDangerousURL(url) { |
| 29 | ++ _, _ = w.Write(util.EscapeHTML(url)) |
| 30 | ++ } |
| 31 | + if n.Attributes() != nil { |
| 32 | + _ = w.WriteByte('"') |
| 33 | + RenderAttributes(w, n, LinkAttributeFilter) |
| 34 | +@@ -633,8 +635,9 @@ func (r *Renderer) renderLink(w util.BufWriter, source []byte, node ast.Node, en |
| 35 | + n := node.(*ast.Link) |
| 36 | + if entering { |
| 37 | + _, _ = w.WriteString("<a href=\"") |
| 38 | +- if r.Unsafe || !IsDangerousURL(n.Destination) { |
| 39 | +- _, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true))) |
| 40 | ++ dest := util.URLEscape(n.Destination, true) |
| 41 | ++ if r.Unsafe || !IsDangerousURL(dest) { |
| 42 | ++ _, _ = w.Write(util.EscapeHTML(dest)) |
| 43 | + } |
| 44 | + _ = w.WriteByte('"') |
| 45 | + if n.Title != nil { |
| 46 | +@@ -676,8 +679,9 @@ func (r *Renderer) renderImage(w util.BufWriter, source []byte, node ast.Node, e |
| 47 | + } |
| 48 | + n := node.(*ast.Image) |
| 49 | + _, _ = w.WriteString("<img src=\"") |
| 50 | +- if r.Unsafe || !IsDangerousURL(n.Destination) { |
| 51 | +- _, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true))) |
| 52 | ++ dest := util.URLEscape(n.Destination, true) |
| 53 | ++ if r.Unsafe || !IsDangerousURL(dest) { |
| 54 | ++ _, _ = w.Write(util.EscapeHTML(dest)) |
| 55 | + } |
| 56 | + _, _ = w.WriteString(`" alt="`) |
| 57 | + _, _ = w.Write(nodeToHTMLText(n, source)) |
| 58 | +-- |
| 59 | +2.45.4 |
| 60 | + |
0 commit comments