html_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package response // import "miniflux.app/v2/internal/http/response"
  4. import (
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. func TestHTMLServerErrorResponse(t *testing.T) {
  11. r, err := http.NewRequest("GET", "/", nil)
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. w := httptest.NewRecorder()
  16. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. HTMLServerError(w, r, errors.New("Some error with injected HTML <script>alert('XSS')</script>"))
  18. })
  19. handler.ServeHTTP(w, r)
  20. resp := w.Result()
  21. if resp.StatusCode != http.StatusInternalServerError {
  22. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusInternalServerError)
  23. }
  24. if actualBody := w.Body.String(); actualBody != `Some error with injected HTML &lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;` {
  25. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, `Some error with injected HTML &lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;`)
  26. }
  27. if actualContentType := resp.Header.Get("Content-Type"); actualContentType != "text/plain; charset=utf-8" {
  28. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, "text/plain; charset=utf-8")
  29. }
  30. }
  31. func TestHTMLBadRequestResponse(t *testing.T) {
  32. r, err := http.NewRequest("GET", "/", nil)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. w := httptest.NewRecorder()
  37. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  38. HTMLBadRequest(w, r, errors.New("Some error with injected HTML <script>alert('XSS')</script>"))
  39. })
  40. handler.ServeHTTP(w, r)
  41. resp := w.Result()
  42. if resp.StatusCode != http.StatusBadRequest {
  43. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusBadRequest)
  44. }
  45. if actualBody := w.Body.String(); actualBody != `Some error with injected HTML &lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;` {
  46. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, `Some error with injected HTML &lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;`)
  47. }
  48. if actualContentType := resp.Header.Get("Content-Type"); actualContentType != "text/plain; charset=utf-8" {
  49. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, "text/plain; charset=utf-8")
  50. }
  51. }
  52. func TestHTMLForbiddenResponse(t *testing.T) {
  53. r, err := http.NewRequest("GET", "/", nil)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. w := httptest.NewRecorder()
  58. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  59. HTMLForbidden(w, r)
  60. })
  61. handler.ServeHTTP(w, r)
  62. resp := w.Result()
  63. if resp.StatusCode != http.StatusForbidden {
  64. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusForbidden)
  65. }
  66. if actualBody := w.Body.String(); actualBody != `Access Forbidden` {
  67. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, `Access Forbidden`)
  68. }
  69. if actualContentType := resp.Header.Get("Content-Type"); actualContentType != "text/html; charset=utf-8" {
  70. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, "text/html; charset=utf-8")
  71. }
  72. }
  73. func TestHTMLNotFoundResponse(t *testing.T) {
  74. r, err := http.NewRequest("GET", "/", nil)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. w := httptest.NewRecorder()
  79. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. HTMLNotFound(w, r)
  81. })
  82. handler.ServeHTTP(w, r)
  83. resp := w.Result()
  84. if resp.StatusCode != http.StatusNotFound {
  85. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusNotFound)
  86. }
  87. if actualBody := w.Body.String(); actualBody != `Page Not Found` {
  88. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, `Page Not Found`)
  89. }
  90. if actualContentType := resp.Header.Get("Content-Type"); actualContentType != "text/html; charset=utf-8" {
  91. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, "text/html; charset=utf-8")
  92. }
  93. }
  94. func TestHTMLRedirectResponse(t *testing.T) {
  95. r, err := http.NewRequest("GET", "/", nil)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. w := httptest.NewRecorder()
  100. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  101. HTMLRedirect(w, r, "/path")
  102. })
  103. handler.ServeHTTP(w, r)
  104. resp := w.Result()
  105. defer resp.Body.Close()
  106. if resp.StatusCode != http.StatusFound {
  107. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusFound)
  108. }
  109. if actualResult := resp.Header.Get("Location"); actualResult != "/path" {
  110. t.Fatalf(`Unexpected redirect location, got %q instead of %q`, actualResult, "/path")
  111. }
  112. }
  113. func TestHTMLRedirectAcceptedTargets(t *testing.T) {
  114. scenarios := []string{
  115. "/feeds",
  116. "/category/1/entries",
  117. "https://example.org/article",
  118. "http://example.org/article",
  119. }
  120. for _, target := range scenarios {
  121. t.Run(target, func(t *testing.T) {
  122. r, err := http.NewRequest("GET", "/", nil)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. w := httptest.NewRecorder()
  127. HTMLRedirect(w, r, target)
  128. resp := w.Result()
  129. defer resp.Body.Close()
  130. if resp.StatusCode != http.StatusFound {
  131. t.Fatalf(`Unexpected status code for %q, got %d instead of %d`, target, resp.StatusCode, http.StatusFound)
  132. }
  133. if actualResult := resp.Header.Get("Location"); actualResult != target {
  134. t.Fatalf(`Unexpected redirect location, got %q instead of %q`, actualResult, target)
  135. }
  136. })
  137. }
  138. }
  139. func TestHTMLRedirectRejectsUnsafeTargets(t *testing.T) {
  140. scenarios := []string{
  141. "javascript:alert(1)",
  142. "JAVASCRIPT:alert(1)",
  143. "data:text/html,<script>alert(1)</script>",
  144. "vbscript:msgbox(1)",
  145. "file:///etc/passwd",
  146. "mailto:victim@example.org",
  147. "//evil.example.org/path",
  148. " //evil.example.org/path",
  149. `/\evil.example.org/path`,
  150. ` /\evil.example.org/path`,
  151. `\evil.example.org\path`,
  152. `/foo\bar`,
  153. "ftp://example.org/file",
  154. "",
  155. }
  156. for _, target := range scenarios {
  157. t.Run(target, func(t *testing.T) {
  158. r, err := http.NewRequest("GET", "/", nil)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. w := httptest.NewRecorder()
  163. HTMLRedirect(w, r, target)
  164. resp := w.Result()
  165. defer resp.Body.Close()
  166. if resp.StatusCode != http.StatusBadRequest {
  167. t.Fatalf(`Expected 400 for %q, got %d`, target, resp.StatusCode)
  168. }
  169. if location := resp.Header.Get("Location"); location != "" {
  170. t.Fatalf(`Expected no Location header for %q, got %q`, target, location)
  171. }
  172. })
  173. }
  174. }
  175. func TestHTMLRequestedRangeNotSatisfiable(t *testing.T) {
  176. r, err := http.NewRequest("GET", "/", nil)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. w := httptest.NewRecorder()
  181. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  182. HTMLRequestedRangeNotSatisfiable(w, r, "bytes */12777")
  183. })
  184. handler.ServeHTTP(w, r)
  185. resp := w.Result()
  186. defer resp.Body.Close()
  187. if resp.StatusCode != http.StatusRequestedRangeNotSatisfiable {
  188. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, http.StatusRequestedRangeNotSatisfiable)
  189. }
  190. if actualContentRangeHeader := resp.Header.Get("Content-Range"); actualContentRangeHeader != "bytes */12777" {
  191. t.Fatalf(`Unexpected content range header, got %q instead of %q`, actualContentRangeHeader, "bytes */12777")
  192. }
  193. }