2017-04-13 02:52:24 +00:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Copyright 2017 The Gogs Authors. All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2017-04-13 02:52:24 +00:00
2017-09-16 17:17:57 +00:00
package markup
2017-04-13 02:52:24 +00:00
import (
"testing"
"github.com/stretchr/testify/assert"
)
2024-05-31 13:26:01 +00:00
func TestSanitizer ( t * testing . T ) {
2017-04-13 02:52:24 +00:00
testCases := [ ] string {
// Regular
` <a onblur="alert(secret)" href="http://www.google.com">Google</a> ` , ` <a href="http://www.google.com" rel="nofollow">Google</a> ` ,
2024-05-31 13:26:01 +00:00
"<scrİpt><script>alert(document.domain)</script></scrİpt>" , "<script>alert(document.domain)</script>" ,
2017-04-13 02:52:24 +00:00
// Code highlighting class
` <code class="random string"></code> ` , ` <code></code> ` ,
` <code class="language-random ui tab active menu attached animating sidebar following bar center"></code> ` , ` <code></code> ` ,
2024-11-24 08:18:57 +00:00
` <span class="k"></span><span class="nb"></span> ` , ` <span class="k"></span><span class="nb"></span> ` ,
2017-04-13 02:52:24 +00:00
// Input checkbox
` <input type="hidden"> ` , ` ` ,
` <input type="checkbox"> ` , ` <input type="checkbox"> ` ,
` <input checked disabled autofocus> ` , ` <input checked="" disabled=""> ` ,
// Code highlight injection
` <code class="language-random ui tab active menu attached animating sidebar following bar center"></code> ` , ` <code></code> ` ,
` < code class = "language-lol ui tab active menu attached animating sidebar following bar center" >
< code class = "language-lol ui container input huge basic segment center" > & nbsp ; < / code >
< img src = "https://try.gogs.io/img/favicon.png" width = "200" height = "200" >
< code class = "language-lol ui container input massive basic segment" > Hello there ! Something has gone wrong , we are working on it . < / code >
< code class = "language-lol ui container input huge basic segment" > In the meantime , play a game with us at & nbsp ; < a href = "http://example.com/" > example . com < / a > . < / code >
< / 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>" ,
2019-12-03 19:02:41 +00:00
// <kbd> tags
` <kbd>Ctrl + C</kbd> ` , ` <kbd>Ctrl + C</kbd> ` ,
2020-05-03 20:17:24 +00:00
` <i class="dropdown icon">NAUGHTY</i> ` , ` <i>NAUGHTY</i> ` ,
2020-12-13 01:05:50 +00:00
` <input type="checkbox" disabled=""/>unchecked ` , ` <input type="checkbox" disabled=""/>unchecked ` ,
2020-05-03 20:17:24 +00:00
` <span class="emoji dropdown">NAUGHTY</span> ` , ` <span>NAUGHTY</span> ` ,
2022-07-15 06:38:10 +00:00
// Color property
` <span style="color: red">Hello World</span> ` , ` <span style="color: red">Hello World</span> ` ,
` <p style="color: red">Hello World</p> ` , ` <p style="color: red">Hello World</p> ` ,
` <code style="color: red">Hello World</code> ` , ` <code>Hello World</code> ` ,
` <span style="bad-color: red">Hello World</span> ` , ` <span>Hello World</span> ` ,
` <p style="bad-color: red">Hello World</p> ` , ` <p>Hello World</p> ` ,
` <code style="bad-color: red">Hello World</code> ` , ` <code>Hello World</code> ` ,
2023-05-19 15:17:07 +00:00
2023-07-27 14:15:31 +00:00
// Org mode status of list items.
` <li class="checked"></li> ` , ` <li class="checked"></li> ` ,
` <li class="unchecked"></li> ` , ` <li class="unchecked"></li> ` ,
` <li class="indeterminate"></li> ` , ` <li class="indeterminate"></li> ` ,
2023-05-19 15:17:07 +00:00
// URLs
2023-07-18 15:18:37 +00:00
` <a href="cbthunderlink://somebase64string)">my custom URL scheme</a> ` , ` <a href="cbthunderlink://somebase64string)" rel="nofollow">my custom URL scheme</a> ` ,
` <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> ` ,
// Disallow dangerous url schemes
` <a href="javascript:alert('xss')">bad</a> ` , ` bad ` ,
` <a href="vbscript:no">bad</a> ` , ` bad ` ,
` <a href="data:1234">bad</a> ` , ` bad ` ,
2017-04-13 02:52:24 +00:00
}
for i := 0 ; i < len ( testCases ) ; i += 2 {
assert . Equal ( t , testCases [ i + 1 ] , Sanitize ( testCases [ i ] ) )
}
}