gitea源码

sanitizer_default_test.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Copyright 2017 The Gogs Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package markup
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSanitizer(t *testing.T) {
  10. testCases := []string{
  11. // Regular
  12. `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`,
  13. "<scrİpt>&lt;script&gt;alert(document.domain)&lt;/script&gt;</scrİpt>", "&lt;script&gt;alert(document.domain)&lt;/script&gt;",
  14. // Code highlighting class
  15. `<code class="random string"></code>`, `<code></code>`,
  16. `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`,
  17. `<span class="k"></span><span class="nb"></span>`, `<span class="k"></span><span class="nb"></span>`,
  18. // Input checkbox
  19. `<input type="hidden">`, ``,
  20. `<input type="checkbox">`, `<input type="checkbox">`,
  21. `<input checked disabled autofocus>`, `<input checked="" disabled="">`,
  22. // Code highlight injection
  23. `<code class="language-random&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center"></code>`, `<code></code>`,
  24. `<code class="language-lol&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center">
  25. <code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment&#32;center">&nbsp;</code>
  26. <img src="https://try.gogs.io/img/favicon.png" width="200" height="200">
  27. <code class="language-lol&#32;ui&#32;container&#32;input&#32;massive&#32;basic&#32;segment">Hello there! Something has gone wrong, we are working on it.</code>
  28. <code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment">In the meantime, play a game with us at&nbsp;<a href="http://example.com/">example.com</a>.</code>
  29. </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>",
  30. // <kbd> tags
  31. `<kbd>Ctrl + C</kbd>`, `<kbd>Ctrl + C</kbd>`,
  32. `<i class="dropdown icon">NAUGHTY</i>`, `<i>NAUGHTY</i>`,
  33. `<input type="checkbox" disabled=""/>unchecked`, `<input type="checkbox" disabled=""/>unchecked`,
  34. `<span class="emoji dropdown">NAUGHTY</span>`, `<span>NAUGHTY</span>`,
  35. // Color property
  36. `<span style="color: red">Hello World</span>`, `<span style="color: red">Hello World</span>`,
  37. `<p style="color: red">Hello World</p>`, `<p style="color: red">Hello World</p>`,
  38. `<code style="color: red">Hello World</code>`, `<code>Hello World</code>`,
  39. `<span style="bad-color: red">Hello World</span>`, `<span>Hello World</span>`,
  40. `<p style="bad-color: red">Hello World</p>`, `<p>Hello World</p>`,
  41. `<code style="bad-color: red">Hello World</code>`, `<code>Hello World</code>`,
  42. // Org mode status of list items.
  43. `<li class="checked"></li>`, `<li class="checked"></li>`,
  44. `<li class="unchecked"></li>`, `<li class="unchecked"></li>`,
  45. `<li class="indeterminate"></li>`, `<li class="indeterminate"></li>`,
  46. // URLs
  47. `<a href="cbthunderlink://somebase64string)">my custom URL scheme</a>`, `<a href="cbthunderlink://somebase64string)" rel="nofollow">my custom URL scheme</a>`,
  48. `<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>`,
  49. // Disallow dangerous url schemes
  50. `<a href="javascript:alert('xss')">bad</a>`, `bad`,
  51. `<a href="vbscript:no">bad</a>`, `bad`,
  52. `<a href="data:1234">bad</a>`, `bad`,
  53. // Some classes and attributes are used by the frontend framework and will execute JS code, so make sure they are removed
  54. `<div class="link-action" data-attr-class="foo" data-url="xxx">txt</div>`, `<div data-attr-class="foo">txt</div>`,
  55. `<div class="form-fetch-action" data-markdown-generated-content="bar" data-global-init="a" data-global-click="b">txt</div>`, `<div data-markdown-generated-content="bar">txt</div>`,
  56. }
  57. for i := 0; i < len(testCases); i += 2 {
  58. assert.Equal(t, testCases[i+1], string(Sanitize(testCases[i])))
  59. }
  60. }