You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sanitizer_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "html/template"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func Test_Sanitizer(t *testing.T) {
  12. NewSanitizer()
  13. testCases := []string{
  14. // Regular
  15. `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`,
  16. // Code highlighting class
  17. `<code class="random string"></code>`, `<code></code>`,
  18. `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`,
  19. `<code class="language-go"></code>`, `<code class="language-go"></code>`,
  20. // Input checkbox
  21. `<input type="hidden">`, ``,
  22. `<input type="checkbox">`, `<input type="checkbox">`,
  23. `<input checked disabled autofocus>`, `<input checked="" disabled="">`,
  24. // Code highlight injection
  25. `<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>`,
  26. `<code class="language-lol&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center">
  27. <code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment&#32;center">&nbsp;</code>
  28. <img src="https://try.gogs.io/img/favicon.png" width="200" height="200">
  29. <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>
  30. <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>
  31. </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>",
  32. // <kbd> tags
  33. `<kbd>Ctrl + C</kbd>`, `<kbd>Ctrl + C</kbd>`,
  34. `<i class="dropdown icon">NAUGHTY</i>`, `<i>NAUGHTY</i>`,
  35. `<i class="icon dropdown"></i>`, `<i class="icon dropdown"></i>`,
  36. `<input type="checkbox" disabled=""/>unchecked`, `<input type="checkbox" disabled=""/>unchecked`,
  37. `<span class="emoji dropdown">NAUGHTY</span>`, `<span>NAUGHTY</span>`,
  38. `<span class="emoji">contents</span>`, `<span class="emoji">contents</span>`,
  39. // Color property
  40. `<span style="color: red">Hello World</span>`, `<span style="color: red">Hello World</span>`,
  41. `<p style="color: red">Hello World</p>`, `<p style="color: red">Hello World</p>`,
  42. `<code style="color: red">Hello World</code>`, `<code>Hello World</code>`,
  43. `<span style="bad-color: red">Hello World</span>`, `<span>Hello World</span>`,
  44. `<p style="bad-color: red">Hello World</p>`, `<p>Hello World</p>`,
  45. `<code style="bad-color: red">Hello World</code>`, `<code>Hello World</code>`,
  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. }
  54. for i := 0; i < len(testCases); i += 2 {
  55. assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
  56. }
  57. }
  58. func TestSanitizeNonEscape(t *testing.T) {
  59. descStr := "<scrİpt>&lt;script&gt;alert(document.domain)&lt;/script&gt;</scrİpt>"
  60. output := template.HTML(Sanitize(descStr))
  61. if strings.Contains(string(output), "<script>") {
  62. t.Errorf("un-escaped <script> in output: %q", output)
  63. }
  64. }