Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sanitizer_test.go 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 markup
  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. // <kbd> tags
  32. `<kbd>Ctrl + C</kbd>`, `<kbd>Ctrl + C</kbd>`,
  33. `<i class="dropdown icon">NAUGHTY</i>`, `<i>NAUGHTY</i>`,
  34. `<i class="icon dropdown"></i>`, `<i class="icon dropdown"></i>`,
  35. `<span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>unchecked</label></span>`, `<span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>unchecked</label></span>`,
  36. `<span class="emoji dropdown">NAUGHTY</span>`, `<span>NAUGHTY</span>`,
  37. `<span class="emoji">contents</span>`, `<span class="emoji">contents</span>`,
  38. }
  39. for i := 0; i < len(testCases); i += 2 {
  40. assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
  41. assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i]))))
  42. }
  43. }