Skip to content

Commit 154b137

Browse files
authored
Looking at github/markup#245 it is clear that GH uses https://github.com/jch/html-pipeline to sanitize. This PR relaxes our sanitization to more closely match this. Fixes #10471 and likely others...
1 parent e0ecddc commit 154b137

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

modules/markup/html_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ func TestRender_ShortLinks(t *testing.T) {
267267
`<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
268268
test(
269269
"[["+favicon+"]]",
270-
`<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
271-
`<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
270+
`<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`,
271+
`<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico" alt="`+favicon+`"/></a></p>`)
272272
test(
273273
"[[Name|Link]]",
274274
`<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
@@ -311,16 +311,16 @@ func TestRender_ShortLinks(t *testing.T) {
311311
`<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
312312
test(
313313
"[[Link #.jpg]]",
314-
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
315-
`<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
314+
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`,
315+
`<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Link #.jpg" alt="Link #.jpg"/></a></p>`)
316316
test(
317317
"[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
318318
`<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
319319
`<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
320320
test(
321321
"[[some/path/Link #.jpg]]",
322-
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
323-
`<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
322+
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`,
323+
`<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`" title="Link #.jpg" alt="some/path/Link #.jpg"/></a></p>`)
324324
test(
325325
"<p><a href=\"https://example.org\">[[foobar]]</a></p>",
326326
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,

modules/markup/sanitizer.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,44 @@ func ReplaceSanitizer() {
5050
// Allow keyword markup
5151
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^` + keywordClass + `$`)).OnElements("span")
5252

53-
// Allow <kbd> tags for keyboard shortcut styling
54-
sanitizer.policy.AllowElements("kbd")
55-
5653
// Allow classes for anchors
5754
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`ref-issue`)).OnElements("a")
5855

56+
// Allow generally safe attributes
57+
generalSafeAttrs := []string{"abbr", "accept", "accept-charset",
58+
"accesskey", "action", "align", "alt",
59+
"aria-describedby", "aria-hidden", "aria-label", "aria-labelledby",
60+
"axis", "border", "cellpadding", "cellspacing", "char",
61+
"charoff", "charset", "checked",
62+
"clear", "cols", "colspan", "color",
63+
"compact", "coords", "datetime", "dir",
64+
"disabled", "enctype", "for", "frame",
65+
"headers", "height", "hreflang",
66+
"hspace", "ismap", "label", "lang",
67+
"maxlength", "media", "method",
68+
"multiple", "name", "nohref", "noshade",
69+
"nowrap", "open", "prompt", "readonly", "rel", "rev",
70+
"rows", "rowspan", "rules", "scope",
71+
"selected", "shape", "size", "span",
72+
"start", "summary", "tabindex", "target",
73+
"title", "type", "usemap", "valign", "value",
74+
"vspace", "width", "itemprop",
75+
}
76+
77+
generalSafeElements := []string{
78+
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
79+
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote",
80+
"dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
81+
"details", "caption", "figure", "figcaption",
82+
"abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "wbr",
83+
}
84+
85+
sanitizer.policy.AllowAttrs(generalSafeAttrs...).OnElements(generalSafeElements...)
86+
87+
sanitizer.policy.AllowAttrs("itemscope", "itemtype").OnElements("div")
88+
89+
// FIXME: Need to handle longdesc in img but there is no easy way to do it
90+
5991
// Custom keyword markup
6092
for _, rule := range setting.ExternalSanitizerRules {
6193
if rule.Regexp != nil {

0 commit comments

Comments
 (0)