Skip to content

Commit

Permalink
Fix #403, Fix #406
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Jul 23, 2023
1 parent 31ccfc4 commit 254b9f8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
19 changes: 19 additions & 0 deletions _test/extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,22 @@ a <!-- b -->
<pre>
</pre>
//= = = = = = = = = = = = = = = = = = = = = = = =//

59: Raw HTML tag with one new line
//- - - - - - - - -//
<img src=./.assets/logo.svg
/>
//- - - - - - - - -//
<p><img src=./.assets/logo.svg
/></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

60: Raw HTML tag with multiple new lines
//- - - - - - - - -//
<img src=./.assets/logo.svg

/>
//- - - - - - - - -//
<p>&lt;img src=./.assets/logo.svg</p>
<p>/&gt;</p>
//= = = = = = = = = = = = = = = = = = = = = = = =//
16 changes: 16 additions & 0 deletions extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ func TestManyCommentPerformance(t *testing.T) {
t.Error("Parsing processing instructions took too long")
}
}

func TestDangerousURLStringCase(t *testing.T) {
markdown := New()

source := []byte(`[Basic](javascript:alert('Basic'))
[CaseInsensitive](JaVaScRiPt:alert('CaseInsensitive'))
`)
expected := []byte(`<p><a href="">Basic</a>
<a href="">CaseInsensitive</a></p>
`)
var b bytes.Buffer
_ = markdown.Convert(source, &b)
if !bytes.Equal(expected, b.Bytes()) {
t.Error("Dangerous URL should ignore cases:\n" + string(testutil.DiffPretty(expected, b.Bytes())))
}
}
6 changes: 3 additions & 3 deletions parser/raw_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func (s *rawHTMLParser) Parse(parent ast.Node, block text.Reader, pc Context) as
}

var tagnamePattern = `([A-Za-z][A-Za-z0-9-]*)`

var spaceOrOneNewline = `(?:[ \t]|(?:\r\n|\n){0,1})`
var attributePattern = `(?:[\r\n \t]+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:[\r\n \t]*=[\r\n \t]*(?:[^\"'=<>` + "`" + `\x00-\x20]+|'[^']*'|"[^"]*"))?)`
var openTagRegexp = regexp.MustCompile("^<" + tagnamePattern + attributePattern + `*[ \t]*/?>`)
var closeTagRegexp = regexp.MustCompile("^</" + tagnamePattern + `\s*>`)
var openTagRegexp = regexp.MustCompile("^<" + tagnamePattern + attributePattern + `*` + spaceOrOneNewline + `*/?>`)
var closeTagRegexp = regexp.MustCompile("^</" + tagnamePattern + spaceOrOneNewline + `*>`)

var openProcessingInstruction = []byte("<?")
var closeProcessingInstruction = []byte("?>")
Expand Down
16 changes: 10 additions & 6 deletions renderer/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,20 +901,24 @@ var bVb = []byte("vbscript:")
var bFile = []byte("file:")
var bData = []byte("data:")

func hasPrefix(s, prefix []byte) bool {
return len(s) >= len(prefix) && bytes.Equal(bytes.ToLower(s[0:len(prefix)]), bytes.ToLower(prefix))
}

// IsDangerousURL returns true if the given url seems a potentially dangerous url,
// otherwise false.
func IsDangerousURL(url []byte) bool {
if bytes.HasPrefix(url, bDataImage) && len(url) >= 11 {
if hasPrefix(url, bDataImage) && len(url) >= 11 {
v := url[11:]
if bytes.HasPrefix(v, bPng) || bytes.HasPrefix(v, bGif) ||
bytes.HasPrefix(v, bJpeg) || bytes.HasPrefix(v, bWebp) ||
bytes.HasPrefix(v, bSvg) {
if hasPrefix(v, bPng) || hasPrefix(v, bGif) ||
hasPrefix(v, bJpeg) || hasPrefix(v, bWebp) ||
hasPrefix(v, bSvg) {
return false
}
return true
}
return bytes.HasPrefix(url, bJs) || bytes.HasPrefix(url, bVb) ||
bytes.HasPrefix(url, bFile) || bytes.HasPrefix(url, bData)
return hasPrefix(url, bJs) || hasPrefix(url, bVb) ||
hasPrefix(url, bFile) || hasPrefix(url, bData)
}

func nodeToHTMLText(n ast.Node, source []byte) []byte {
Expand Down
4 changes: 4 additions & 0 deletions text/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ func findSubMatchReader(r Reader, reg *regexp.Regexp) [][]byte {
bs := bb.Bytes()
var result [][]byte
for i := 0; i < len(match); i += 2 {
if match[i] < 0 {
result = append(result, []byte{})
continue
}
result = append(result, bs[match[i]:match[i+1]])
}

Expand Down

0 comments on commit 254b9f8

Please sign in to comment.