Skip to content

Commit 4c9ed01

Browse files
KN4CK3Rdelvh
andcommitted
Disallow dangerous url schemes (go-gitea#25960)
Regression: go-gitea#24805 Closes: go-gitea#25945 - Disallow `javascript`, `vbscript` and `data` (data uri images still work) url schemes even if all other schemes are allowed - Fixed older `cbthunderlink` tests --------- Co-authored-by: delvh <dev.lh@web.de>
1 parent b7d054e commit 4c9ed01

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ require (
7676
github.com/mattn/go-sqlite3 v1.14.16
7777
github.com/meilisearch/meilisearch-go v0.24.0
7878
github.com/mholt/archiver/v3 v3.5.1
79-
github.com/microcosm-cc/bluemonday v1.0.24
79+
github.com/microcosm-cc/bluemonday v1.0.25
8080
github.com/minio/minio-go/v7 v7.0.52
8181
github.com/minio/sha256-simd v1.0.0
8282
github.com/msteinert/pam v1.1.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,8 @@ github.com/mholt/acmez v1.1.0 h1:IQ9CGHKOHokorxnffsqDvmmE30mDenO1lptYZ1AYkHY=
877877
github.com/mholt/acmez v1.1.0/go.mod h1:zwo5+fbLLTowAX8o8ETfQzbDtwGEXnPhkmGdKIP+bgs=
878878
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
879879
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
880-
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
881-
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
880+
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
881+
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
882882
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
883883
github.com/miekg/dns v1.1.54 h1:5jon9mWcb0sFJGpnI99tOMhCPyJ+RPVz5b63MQG0VWI=
884884
github.com/miekg/dns v1.1.54/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=

modules/markup/sanitizer.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package markup
66

77
import (
88
"io"
9+
"net/url"
910
"regexp"
1011
"sync"
1112

@@ -79,6 +80,14 @@ func createDefaultPolicy() *bluemonday.Policy {
7980
policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
8081
} else {
8182
policy.AllowURLSchemesMatching(allowAllRegex)
83+
84+
// Even if every scheme is allowed, these three are blocked for security reasons
85+
disallowScheme := func(*url.URL) bool {
86+
return false
87+
}
88+
policy.AllowURLSchemeWithCustomPolicy("javascript", disallowScheme)
89+
policy.AllowURLSchemeWithCustomPolicy("vbscript", disallowScheme)
90+
policy.AllowURLSchemeWithCustomPolicy("data", disallowScheme)
8291
}
8392

8493
// Allow classes for anchors

modules/markup/sanitizer_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ func Test_Sanitizer(t *testing.T) {
5454
`<code style="bad-color: red">Hello World</code>`, `<code>Hello World</code>`,
5555

5656
// URLs
57-
`[my custom URL scheme](cbthunderlink://somebase64string)`, `[my custom URL scheme](cbthunderlink://somebase64string)`,
58-
`[my custom URL scheme](matrix:roomid/psumPMeAfzgAeQpXMG:feneas.org?action=join)`, `[my custom URL scheme](matrix:roomid/psumPMeAfzgAeQpXMG:feneas.org?action=join)`,
57+
`<a href="cbthunderlink://somebase64string)">my custom URL scheme</a>`, `<a href="cbthunderlink://somebase64string)" rel="nofollow">my custom URL scheme</a>`,
58+
`<a href="matrix:roomid/psumPMeAfzgAeQpXMG:feneas.org?action=join">my custom URL scheme</a>`, `<a href="matrix:roomid/psumPMeAfzgAeQpXMG:feneas.org?action=join" rel="nofollow">my custom URL scheme</a>`,
59+
60+
// Disallow dangerous url schemes
61+
`<a href="javascript:alert('xss')">bad</a>`, `bad`,
62+
`<a href="vbscript:no">bad</a>`, `bad`,
63+
`<a href="data:1234">bad</a>`, `bad`,
5964
}
6065

6166
for i := 0; i < len(testCases); i += 2 {

0 commit comments

Comments
 (0)