server_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package server
  4. import (
  5. "bytes"
  6. "log/slog"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. "time"
  14. "miniflux.app/v2/internal/config"
  15. )
  16. func TestDetermineListenTargets(t *testing.T) {
  17. tests := []struct {
  18. name string
  19. addresses []string
  20. certDomain string
  21. certFile string
  22. keyFile string
  23. expected []listenTarget
  24. }{
  25. {
  26. name: "single HTTP listener",
  27. addresses: []string{":8080"},
  28. expected: []listenTarget{
  29. {address: ":8080", mode: modeHTTP},
  30. },
  31. },
  32. {
  33. name: "multiple HTTP listeners",
  34. addresses: []string{":8080", ":9090"},
  35. expected: []listenTarget{
  36. {address: ":8080", mode: modeHTTP},
  37. {address: ":9090", mode: modeHTTP},
  38. },
  39. },
  40. {
  41. name: "TLS with cert files",
  42. addresses: []string{":443"},
  43. certFile: "/path/to/cert.pem",
  44. keyFile: "/path/to/key.pem",
  45. expected: []listenTarget{
  46. {address: ":443", mode: modeTLS},
  47. },
  48. },
  49. {
  50. name: "cert file without key file falls back to HTTP",
  51. addresses: []string{":8080"},
  52. certFile: "/path/to/cert.pem",
  53. expected: []listenTarget{
  54. {address: ":8080", mode: modeHTTP},
  55. },
  56. },
  57. {
  58. name: "key file without cert file falls back to HTTP",
  59. addresses: []string{":8080"},
  60. keyFile: "/path/to/key.pem",
  61. expected: []listenTarget{
  62. {address: ":8080", mode: modeHTTP},
  63. },
  64. },
  65. {
  66. name: "autocert with :https address",
  67. addresses: []string{":https"},
  68. certDomain: "example.com",
  69. expected: []listenTarget{
  70. {address: ":https", mode: modeAutocertTLS},
  71. },
  72. },
  73. {
  74. name: "autocert with first address containing colon",
  75. addresses: []string{":443"},
  76. certDomain: "example.com",
  77. expected: []listenTarget{
  78. {address: ":443", mode: modeAutocertTLS},
  79. },
  80. },
  81. {
  82. name: "autocert does not apply to second non-https address",
  83. addresses: []string{":https", ":8080"},
  84. certDomain: "example.com",
  85. expected: []listenTarget{
  86. {address: ":https", mode: modeAutocertTLS},
  87. {address: ":8080", mode: modeHTTP},
  88. },
  89. },
  90. {
  91. name: "unix socket",
  92. addresses: []string{"/var/run/miniflux.sock"},
  93. expected: []listenTarget{
  94. {address: "/var/run/miniflux.sock", mode: modeUnixSocket},
  95. },
  96. },
  97. {
  98. name: "unix socket with TLS",
  99. addresses: []string{"/var/run/miniflux.sock"},
  100. certFile: "/path/to/cert.pem",
  101. keyFile: "/path/to/key.pem",
  102. expected: []listenTarget{
  103. {address: "/var/run/miniflux.sock", mode: modeUnixSocketTLS},
  104. },
  105. },
  106. {
  107. name: "mixed unix socket and TCP",
  108. addresses: []string{"/var/run/miniflux.sock", ":8080"},
  109. certFile: "/path/to/cert.pem",
  110. keyFile: "/path/to/key.pem",
  111. expected: []listenTarget{
  112. {address: "/var/run/miniflux.sock", mode: modeUnixSocketTLS},
  113. {address: ":8080", mode: modeTLS},
  114. },
  115. },
  116. {
  117. name: "empty address list",
  118. addresses: []string{},
  119. expected: nil,
  120. },
  121. }
  122. for _, tc := range tests {
  123. t.Run(tc.name, func(t *testing.T) {
  124. got := determineListenTargets(tc.addresses, tc.certDomain, tc.certFile, tc.keyFile)
  125. if len(got) != len(tc.expected) {
  126. t.Fatalf("got %d targets, want %d", len(got), len(tc.expected))
  127. }
  128. for i := range got {
  129. if got[i] != tc.expected[i] {
  130. t.Errorf("target[%d] = %+v, want %+v", i, got[i], tc.expected[i])
  131. }
  132. }
  133. })
  134. }
  135. }
  136. func TestAnyTLS(t *testing.T) {
  137. tests := []struct {
  138. name string
  139. targets []listenTarget
  140. expected bool
  141. }{
  142. {
  143. name: "empty list",
  144. targets: nil,
  145. expected: false,
  146. },
  147. {
  148. name: "HTTP only",
  149. targets: []listenTarget{{mode: modeHTTP}},
  150. expected: false,
  151. },
  152. {
  153. name: "systemd only",
  154. targets: []listenTarget{{mode: modeSystemd}},
  155. expected: false,
  156. },
  157. {
  158. name: "unix socket without TLS",
  159. targets: []listenTarget{{mode: modeUnixSocket}},
  160. expected: false,
  161. },
  162. {
  163. name: "TLS mode",
  164. targets: []listenTarget{{mode: modeTLS}},
  165. expected: true,
  166. },
  167. {
  168. name: "autocert TLS mode",
  169. targets: []listenTarget{{mode: modeAutocertTLS}},
  170. expected: true,
  171. },
  172. {
  173. name: "unix socket TLS mode",
  174. targets: []listenTarget{{mode: modeUnixSocketTLS}},
  175. expected: true,
  176. },
  177. {
  178. name: "mixed with one TLS",
  179. targets: []listenTarget{{mode: modeHTTP}, {mode: modeTLS}, {mode: modeUnixSocket}},
  180. expected: true,
  181. },
  182. }
  183. for _, tc := range tests {
  184. t.Run(tc.name, func(t *testing.T) {
  185. if got := anyTLS(tc.targets); got != tc.expected {
  186. t.Errorf("anyTLS() = %v, want %v", got, tc.expected)
  187. }
  188. })
  189. }
  190. }
  191. func TestNewHTTPServerConfiguresTimeouts(t *testing.T) {
  192. previousOptions := config.Opts
  193. config.Opts = config.NewConfigOptions()
  194. t.Cleanup(func() { config.Opts = previousOptions })
  195. handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
  196. server := newHTTPServer(":8080", handler)
  197. timeout := config.Opts.HTTPServerTimeout()
  198. if server.Addr != ":8080" {
  199. t.Errorf("server address = %q, want %q", server.Addr, ":8080")
  200. }
  201. if server.Handler == nil {
  202. t.Error("server handler must not be nil")
  203. }
  204. for name, actual := range map[string]time.Duration{
  205. "ReadTimeout": server.ReadTimeout,
  206. "WriteTimeout": server.WriteTimeout,
  207. "IdleTimeout": server.IdleTimeout,
  208. "ReadHeaderTimeout": server.ReadHeaderTimeout,
  209. } {
  210. if actual != timeout {
  211. t.Errorf("%s = %v, want %v", name, actual, timeout)
  212. }
  213. }
  214. }
  215. func TestCreateUnixSocketListenerPermissions(t *testing.T) {
  216. if runtime.GOOS == "windows" {
  217. t.Skip("Unix sockets are not supported on Windows")
  218. }
  219. tempFile, err := os.CreateTemp("/tmp", "miniflux-*.sock")
  220. if err != nil {
  221. t.Fatalf("Unable to allocate Unix socket path: %v", err)
  222. }
  223. socketFile := tempFile.Name()
  224. if err := tempFile.Close(); err != nil {
  225. t.Fatalf("Unable to close temporary file: %v", err)
  226. }
  227. if err := os.Remove(socketFile); err != nil {
  228. t.Fatalf("Unable to prepare Unix socket path: %v", err)
  229. }
  230. t.Cleanup(func() { os.Remove(socketFile) })
  231. listener := createUnixSocketListener(socketFile)
  232. t.Cleanup(func() { listener.Close() })
  233. fileInfo, err := os.Stat(socketFile)
  234. if err != nil {
  235. t.Fatalf("Unable to stat Unix socket: %v", err)
  236. }
  237. if got, want := fileInfo.Mode().Perm(), os.FileMode(0660); got != want {
  238. t.Errorf("Unix socket permissions = %04o, want %04o", got, want)
  239. }
  240. }
  241. func TestMiddlewareDoesNotLogQueryString(t *testing.T) {
  242. const secret = "Abcdef123456"
  243. previousOptions := config.Opts
  244. config.Opts = config.NewConfigOptions()
  245. t.Cleanup(func() { config.Opts = previousOptions })
  246. var logs bytes.Buffer
  247. previousLogger := slog.Default()
  248. slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
  249. t.Cleanup(func() { slog.SetDefault(previousLogger) })
  250. handler := middleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
  251. handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/fever/?api&api_key="+secret, nil))
  252. if !strings.Contains(logs.String(), "Incoming request") || !strings.Contains(logs.String(), "/fever/") {
  253. t.Fatalf("Expected the request path to be logged, got:\n%s", logs.String())
  254. }
  255. if strings.Contains(logs.String(), secret) {
  256. t.Errorf("Logs contain the query string:\n%s", logs.String())
  257. }
  258. }