middleware_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. )
  11. func TestAuthMiddlewareRejectsTokenWithEmptyUsername(t *testing.T) {
  12. token := getAuthToken("", "")
  13. getRequest := httptest.NewRequest(http.MethodGet, "/reader/api/0/user-info", nil)
  14. getRequest.Header.Set("Authorization", "GoogleLogin auth="+token)
  15. postRequest := httptest.NewRequest(http.MethodPost, "/reader/api/0/edit-tag", strings.NewReader(url.Values{"T": {token}}.Encode()))
  16. postRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  17. // The store is nil: the middleware must reject the token before querying it.
  18. middleware := newAuthMiddleware(nil)
  19. next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. t.Fatal("next handler should not be called")
  21. })
  22. for _, request := range []*http.Request{getRequest, postRequest} {
  23. recorder := httptest.NewRecorder()
  24. middleware.validateApiKey(next).ServeHTTP(recorder, request)
  25. if recorder.Code != http.StatusUnauthorized {
  26. t.Errorf("%s: expected status %d, got %d", request.Method, http.StatusUnauthorized, recorder.Code)
  27. }
  28. }
  29. }
  30. func TestAuthMiddlewareRejectsMalformedCredentials(t *testing.T) {
  31. newGetRequest := func(target, authorization string) *http.Request {
  32. r := httptest.NewRequest(http.MethodGet, target, nil)
  33. if authorization != "" {
  34. r.Header.Set("Authorization", authorization)
  35. }
  36. return r
  37. }
  38. newPostRequest := func(body, authorization string) *http.Request {
  39. r := httptest.NewRequest(http.MethodPost, "/reader/api/0/edit-tag", strings.NewReader(body))
  40. r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  41. if authorization != "" {
  42. r.Header.Set("Authorization", authorization)
  43. }
  44. return r
  45. }
  46. scenarios := []struct {
  47. name string
  48. request *http.Request
  49. }{
  50. {"GET without Authorization header", newGetRequest("/reader/api/0/user-info", "")},
  51. {"GET with the token in the query string", newGetRequest("/reader/api/0/user-info?T=reader/abc", "")},
  52. {"GET with a scheme only", newGetRequest("/reader/api/0/user-info", "GoogleLogin")},
  53. {"GET with another scheme", newGetRequest("/reader/api/0/user-info", "Bearer auth=reader/abc")},
  54. {"GET with a capitalized auth field", newGetRequest("/reader/api/0/user-info", "GoogleLogin Auth=reader/abc")},
  55. {"GET without an auth value", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth")},
  56. {"GET with several equal signs", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth=reader/abc=")},
  57. {"GET with an extra field", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth=reader/abc extra")},
  58. {"GET with a token without a slash", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth=readerabc")},
  59. {"GET with a token with several slashes", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth=reader/abc/def")},
  60. {"GET with a token without a username", newGetRequest("/reader/api/0/user-info", "GoogleLogin auth=/abc")},
  61. {"POST without a T field", newPostRequest(url.Values{"i": {"1"}}.Encode(), "")},
  62. {"POST with the token in the Authorization header", newPostRequest("", "GoogleLogin auth=reader/abc")},
  63. {"POST with an empty T field", newPostRequest("T=", "")},
  64. {"POST with a token without a slash", newPostRequest("T=readerabc", "")},
  65. {"POST with a token without a username", newPostRequest("T=%2Fabc", "")},
  66. {"POST with an invalid form body", newPostRequest("T=%zz", "")},
  67. }
  68. // The store is nil: every request must be rejected before the store is queried.
  69. middleware := newAuthMiddleware(nil)
  70. next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. t.Fatal("next handler should not be called")
  72. })
  73. for _, scenario := range scenarios {
  74. t.Run(scenario.name, func(t *testing.T) {
  75. recorder := httptest.NewRecorder()
  76. middleware.validateApiKey(next).ServeHTTP(recorder, scenario.request)
  77. checkUnauthorizedResponse(t, recorder)
  78. })
  79. }
  80. }