|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Copyright 2017 The Gogs Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a MIT-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package markdown |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_Sanitizer(t *testing.T) { |
| 15 | + NewSanitizer() |
| 16 | + testCases := []string{ |
| 17 | + // Regular |
| 18 | + `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`, |
| 19 | + |
| 20 | + // Code highlighting class |
| 21 | + `<code class="random string"></code>`, `<code></code>`, |
| 22 | + `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`, |
| 23 | + `<code class="language-go"></code>`, `<code class="language-go"></code>`, |
| 24 | + |
| 25 | + // Input checkbox |
| 26 | + `<input type="hidden">`, ``, |
| 27 | + `<input type="checkbox">`, `<input type="checkbox">`, |
| 28 | + `<input checked disabled autofocus>`, `<input checked="" disabled="">`, |
| 29 | + |
| 30 | + // Code highlight injection |
| 31 | + `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`, |
| 32 | + `<code class="language-lol ui tab active menu attached animating sidebar following bar center"> |
| 33 | +<code class="language-lol ui container input huge basic segment center"> </code> |
| 34 | +<img src="https://try.gogs.io/img/favicon.png" width="200" height="200"> |
| 35 | +<code class="language-lol ui container input massive basic segment">Hello there! Something has gone wrong, we are working on it.</code> |
| 36 | +<code class="language-lol ui container input huge basic segment">In the meantime, play a game with us at <a href="http://example.com/">example.com</a>.</code> |
| 37 | +</code>`, "<code>\n<code>\u00a0</code>\n<img src=\"https://try.gogs.io/img/favicon.png\" width=\"200\" height=\"200\">\n<code>Hello there! Something has gone wrong, we are working on it.</code>\n<code>In the meantime, play a game with us at\u00a0<a href=\"http://example.com/\" rel=\"nofollow\">example.com</a>.</code>\n</code>", |
| 38 | + } |
| 39 | + |
| 40 | + for i := 0; i < len(testCases); i += 2 { |
| 41 | + assert.Equal(t, testCases[i+1], Sanitize(testCases[i])) |
| 42 | + assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i])))) |
| 43 | + } |
| 44 | +} |
0 commit comments