code.gitea.io/gitea@v1.22.3/modules/markup/sanitizer_default_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // Copyright 2017 The Gogs Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 package markup 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestSanitizer(t *testing.T) { 14 testCases := []string{ 15 // Regular 16 `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`, 17 "<scrİpt><script>alert(document.domain)</script></scrİpt>", "<script>alert(document.domain)</script>", 18 19 // Code highlighting class 20 `<code class="random string"></code>`, `<code></code>`, 21 `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`, 22 `<code class="language-go"></code>`, `<code class="language-go"></code>`, 23 24 // Input checkbox 25 `<input type="hidden">`, ``, 26 `<input type="checkbox">`, `<input type="checkbox">`, 27 `<input checked disabled autofocus>`, `<input checked="" disabled="">`, 28 29 // Code highlight injection 30 `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`, 31 `<code class="language-lol ui tab active menu attached animating sidebar following bar center"> 32 <code class="language-lol ui container input huge basic segment center"> </code> 33 <img src="https://try.gogs.io/img/favicon.png" width="200" height="200"> 34 <code class="language-lol ui container input massive basic segment">Hello there! Something has gone wrong, we are working on it.</code> 35 <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> 36 </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>", 37 38 // <kbd> tags 39 `<kbd>Ctrl + C</kbd>`, `<kbd>Ctrl + C</kbd>`, 40 `<i class="dropdown icon">NAUGHTY</i>`, `<i>NAUGHTY</i>`, 41 `<i class="icon dropdown"></i>`, `<i class="icon dropdown"></i>`, 42 `<input type="checkbox" disabled=""/>unchecked`, `<input type="checkbox" disabled=""/>unchecked`, 43 `<span class="emoji dropdown">NAUGHTY</span>`, `<span>NAUGHTY</span>`, 44 `<span class="emoji">contents</span>`, `<span class="emoji">contents</span>`, 45 46 // Color property 47 `<span style="color: red">Hello World</span>`, `<span style="color: red">Hello World</span>`, 48 `<p style="color: red">Hello World</p>`, `<p style="color: red">Hello World</p>`, 49 `<code style="color: red">Hello World</code>`, `<code>Hello World</code>`, 50 `<span style="bad-color: red">Hello World</span>`, `<span>Hello World</span>`, 51 `<p style="bad-color: red">Hello World</p>`, `<p>Hello World</p>`, 52 `<code style="bad-color: red">Hello World</code>`, `<code>Hello World</code>`, 53 54 // Org mode status of list items. 55 `<li class="checked"></li>`, `<li class="checked"></li>`, 56 `<li class="unchecked"></li>`, `<li class="unchecked"></li>`, 57 `<li class="indeterminate"></li>`, `<li class="indeterminate"></li>`, 58 59 // URLs 60 `<a href="cbthunderlink://somebase64string)">my custom URL scheme</a>`, `<a href="cbthunderlink://somebase64string)" rel="nofollow">my custom URL scheme</a>`, 61 `<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>`, 62 63 // Disallow dangerous url schemes 64 `<a href="javascript:alert('xss')">bad</a>`, `bad`, 65 `<a href="vbscript:no">bad</a>`, `bad`, 66 `<a href="data:1234">bad</a>`, `bad`, 67 } 68 69 for i := 0; i < len(testCases); i += 2 { 70 assert.Equal(t, testCases[i+1], Sanitize(testCases[i])) 71 } 72 }