usercontent_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package utils
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. )
  10. func TestIsActiveUserContent(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. want bool
  14. }{
  15. {"evil.html", true},
  16. {"EVIL.HTM", true},
  17. {"page.xhtml", true},
  18. {"drawing.svg", true},
  19. {"feed.xml", true},
  20. {"style.xsl", true},
  21. {"no_extension", true},
  22. {"data.unknownext123", true},
  23. {"photo.png", false},
  24. {"photo.JPG", false},
  25. {"clip.mp4", false},
  26. {"song.mp3", false},
  27. {"report.pdf", false},
  28. {"notes.txt", false},
  29. {"style.css", false},
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. if got := IsActiveUserContent(tt.name); got != tt.want {
  34. t.Errorf("IsActiveUserContent(%q) = %v, want %v", tt.name, got, tt.want)
  35. }
  36. })
  37. }
  38. }
  39. // What a browser gets for a user file served the way /media serves it
  40. func serveUserFile(t *testing.T, name string, content string) *http.Response {
  41. t.Helper()
  42. dir := t.TempDir()
  43. path := filepath.Join(dir, name)
  44. if err := os.WriteFile(path, []byte(content), 0644); err != nil {
  45. t.Fatalf("write: %v", err)
  46. }
  47. rec := httptest.NewRecorder()
  48. req := httptest.NewRequest(http.MethodGet, "/media?file=user:/"+name, nil)
  49. SetUserContentHeaders(rec, name)
  50. http.ServeFile(rec, req, path)
  51. return rec.Result()
  52. }
  53. func TestSetUserContentHeaders(t *testing.T) {
  54. script := "<!DOCTYPE html><html><script>fetch('/system/users/list')</script></html>"
  55. tests := []struct {
  56. name string
  57. file string
  58. content string
  59. wantSandbox bool
  60. wantType string // prefix of the Content-Type, "" to skip
  61. }{
  62. {"html page", "evil.html", script, true, "text/html"},
  63. {"svg with script", "evil.svg", `<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"/>`, true, "image/svg+xml"},
  64. {"html without extension", "evil", script, true, ""},
  65. {"html named as an unknown type", "evil.aopart", script, true, ""},
  66. {"html named as a png", "evil.png", script, false, "image/png"},
  67. {"html named as text", "evil.txt", script, false, "text/plain"},
  68. {"real picture", "photo.jpg", "\xff\xd8\xff\xe0", false, "image/jpeg"},
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. res := serveUserFile(t, tt.file, tt.content)
  73. csp := res.Header.Get("Content-Security-Policy")
  74. if tt.wantSandbox {
  75. if !strings.HasPrefix(csp, "sandbox") || strings.Contains(csp, "allow-same-origin") {
  76. t.Errorf("Content-Security-Policy = %q, want a sandbox without allow-same-origin", csp)
  77. }
  78. if res.Header.Get("X-Content-Type-Options") != "nosniff" {
  79. t.Error("nosniff is missing")
  80. }
  81. } else if csp != "" {
  82. t.Errorf("Content-Security-Policy = %q, want none", csp)
  83. }
  84. if tt.wantType != "" && !strings.HasPrefix(res.Header.Get("Content-Type"), tt.wantType) {
  85. t.Errorf("Content-Type = %q, want %s", res.Header.Get("Content-Type"), tt.wantType)
  86. }
  87. if strings.HasPrefix(res.Header.Get("Content-Type"), "text/html") && !tt.wantSandbox {
  88. t.Error("an HTML page went out without the sandbox")
  89. }
  90. })
  91. }
  92. }
  93. func TestSetUserContentHeadersKeepsAGivenType(t *testing.T) {
  94. rec := httptest.NewRecorder()
  95. rec.Header().Set("Content-Type", "image/jpeg")
  96. SetUserContentHeaders(rec, "photo.arw")
  97. if got := rec.Header().Get("Content-Type"); got != "image/jpeg" {
  98. t.Errorf("Content-Type = %q, want the one already set", got)
  99. }
  100. }