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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Org mode status of list items.
  47. `<li class="checked"></li>`, `<li class="checked"></li>`,
  48. `<li class="unchecked"></li>`, `<li class="unchecked"></li>`,
  49. `<li class="indeterminate"></li>`, `<li class="indeterminate"></li>`,
  50. // URLs
  51. `<a href="cbthunderlink://somebase64string)">my custom URL scheme</a>`, `<a href="cbthunderlink://somebase64string)" rel="nofollow">my custom URL scheme</a>`,
  52. `<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>`,
  53. // Disallow dangerous url schemes
  54. `<a href="javascript:alert('xss')">bad</a>`, `bad`,
  55. `<a href="vbscript:no">bad</a>`, `bad`,
  56. `<a href="data:1234">bad</a>`, `bad`,
  57. }
  58. for i := 0; i < len(testCases); i += 2 {
  59. assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
  60. }
  61. }
  62. func TestDescriptionSanitizer(t *testing.T) {
  63. NewSanitizer()
  64. testCases := []string{
  65. `<h1>Title</h1>`, `Title`,
  66. `<img src='img.png' alt='image'>`, ``,
  67. `<span class="emoji" aria-label="thumbs up">THUMBS UP</span>`, `<span class="emoji" aria-label="thumbs up">THUMBS UP</span>`,
  68. `<span style="color: red">Hello World</span>`, `<span>Hello World</span>`,
  69. `<br>`, ``,
  70. `<a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a>`, `<a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a>`,
  71. `<mark>Important!</mark>`, `Important!`,
  72. `<details>Click me! <summary>Nothing to see here.</summary></details>`, `Click me! Nothing to see here.`,
  73. `<input type="hidden">`, ``,
  74. `<b>I</b> have a <i>strong</i> <strong>opinion</strong> about <em>this</em>.`, `<b>I</b> have a <i>strong</i> <strong>opinion</strong> about <em>this</em>.`,
  75. `Provides alternative <code>wg(8)</code> tool`, `Provides alternative <code>wg(8)</code> tool`,
  76. }
  77. for i := 0; i < len(testCases); i += 2 {
  78. assert.Equal(t, testCases[i+1], SanitizeDescription(testCases[i]))
  79. }
  80. }
  81. func TestSanitizeNonEscape(t *testing.T) {
  82. descStr := "<scrİpt>&lt;script&gt;alert(document.domain)&lt;/script&gt;</scrİpt>"
  83. output := template.HTML(Sanitize(descStr))
  84. if strings.Contains(string(output), "<script>") {
  85. t.Errorf("un-escaped <script> in output: %q", output)
  86. }
  87. }