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.

44 lines
2.3 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Copyright 2017 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package markdown
  6. import (
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func Test_Sanitizer(t *testing.T) {
  11. NewSanitizer()
  12. testCases := []string{
  13. // Regular
  14. `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, `<a href="http://www.google.com" rel="nofollow">Google</a>`,
  15. // Code highlighting class
  16. `<code class="random string"></code>`, `<code></code>`,
  17. `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, `<code></code>`,
  18. `<code class="language-go"></code>`, `<code class="language-go"></code>`,
  19. // Input checkbox
  20. `<input type="hidden">`, ``,
  21. `<input type="checkbox">`, `<input type="checkbox">`,
  22. `<input checked disabled autofocus>`, `<input checked="" disabled="">`,
  23. // Code highlight injection
  24. `<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>`,
  25. `<code class="language-lol&#32;ui&#32;tab&#32;active&#32;menu&#32;attached&#32;animating&#32;sidebar&#32;following&#32;bar&#32;center">
  26. <code class="language-lol&#32;ui&#32;container&#32;input&#32;huge&#32;basic&#32;segment&#32;center">&nbsp;</code>
  27. <img src="https://try.gogs.io/img/favicon.png" width="200" height="200">
  28. <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>
  29. <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>
  30. </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>",
  31. }
  32. for i := 0; i < len(testCases); i += 2 {
  33. assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
  34. assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i]))))
  35. }
  36. }