Improve error message when no cookie is found
This commit is contained in:
		
							parent
							
								
									6cc7da8993
								
							
						
					
					
						commit
						d8deaa124b
					
				|  | @ -18,6 +18,7 @@ | |||
| 
 | ||||
| ## Changes since v7.1.3 | ||||
| 
 | ||||
| - [#1404](https://github.com/oauth2-proxy/oauth2-proxy/pull/1404) Improve error message when no cookie is found (@JoelSpeed) | ||||
| - [#1315](https://github.com/oauth2-proxy/oauth2-proxy/pull/1315) linkedin: Update provider to v2 (@wuurrd) | ||||
| - [#1348](https://github.com/oauth2-proxy/oauth2-proxy/pull/1348) Using the native httputil proxy code for websockets rather than yhat/wsutil to properly handle HTTP-level failures (@thetrime) | ||||
| - [#1379](https://github.com/oauth2-proxy/oauth2-proxy/pull/1379) Fix the manual sign in with --htpasswd-user-group switch (@janrotter) | ||||
|  |  | |||
|  | @ -853,11 +853,13 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) { | |||
| 	case ErrNeedsLogin: | ||||
| 		// we need to send the user to a login screen
 | ||||
| 		if p.forceJSONErrors || isAjax(req) { | ||||
| 			logger.Printf("No valid authentication in request. Access Denied.") | ||||
| 			// no point redirecting an AJAX request
 | ||||
| 			p.errorJSON(rw, http.StatusUnauthorized) | ||||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		logger.Printf("No valid authentication in request. Initiating login.") | ||||
| 		if p.SkipProviderButton { | ||||
| 			p.OAuthStart(rw, req) | ||||
| 		} else { | ||||
|  |  | |||
|  | @ -71,7 +71,7 @@ func (s *storedSessionLoader) loadSession(next http.Handler) http.Handler { | |||
| 		} | ||||
| 
 | ||||
| 		session, err := s.getValidatedSession(rw, req) | ||||
| 		if err != nil { | ||||
| 		if err != nil && !errors.Is(err, http.ErrNoCookie) { | ||||
| 			// In the case when there was an error loading the session,
 | ||||
| 			// we should clear the session
 | ||||
| 			logger.Errorf("Error loading cookied session: %v, removing session", err) | ||||
|  |  | |||
|  | @ -51,7 +51,7 @@ func (s *SessionStore) Load(req *http.Request) (*sessions.SessionState, error) { | |||
| 	c, err := loadCookie(req, s.Cookie.Name) | ||||
| 	if err != nil { | ||||
| 		// always http.ErrNoCookie
 | ||||
| 		return nil, fmt.Errorf("cookie %q not present", s.Cookie.Name) | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	val, _, ok := encryption.Validate(c, s.Cookie.Secret, s.Cookie.Expire) | ||||
| 	if !ok { | ||||
|  | @ -216,7 +216,7 @@ func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) { | |||
| 		} | ||||
| 	} | ||||
| 	if len(cookies) == 0 { | ||||
| 		return nil, fmt.Errorf("could not find cookie %s", cookieName) | ||||
| 		return nil, http.ErrNoCookie | ||||
| 	} | ||||
| 	return joinCookies(cookies, cookieName) | ||||
| } | ||||
|  |  | |||
|  | @ -452,21 +452,38 @@ func SessionStoreInterfaceTests(in *testInput) { | |||
| 	}) | ||||
| 
 | ||||
| 	Context("when Load is called", func() { | ||||
| 		BeforeEach(func() { | ||||
| 			req := httptest.NewRequest("GET", "http://example.com/", nil) | ||||
| 			resp := httptest.NewRecorder() | ||||
| 			err := in.ss().Save(resp, req, in.session) | ||||
| 			Expect(err).ToNot(HaveOccurred()) | ||||
| 		Context("with a valid session cookie in the request", func() { | ||||
| 			BeforeEach(func() { | ||||
| 				req := httptest.NewRequest("GET", "http://example.com/", nil) | ||||
| 				resp := httptest.NewRecorder() | ||||
| 				err := in.ss().Save(resp, req, in.session) | ||||
| 				Expect(err).ToNot(HaveOccurred()) | ||||
| 				for _, cookie := range resp.Result().Cookies() { | ||||
| 					in.request.AddCookie(cookie) | ||||
| 				} | ||||
| 			}) | ||||
| 
 | ||||
| 			for _, cookie := range resp.Result().Cookies() { | ||||
| 				in.request.AddCookie(cookie) | ||||
| 			} | ||||
| 			Context("before the refresh period", func() { | ||||
| 				LoadSessionTests(in) | ||||
| 			}) | ||||
| 		}) | ||||
| 
 | ||||
| 		Context("before the refresh period", func() { | ||||
| 			LoadSessionTests(in) | ||||
| 		}) | ||||
| 		Context("with no cookies in the request", func() { | ||||
| 			var loadedSession *sessionsapi.SessionState | ||||
| 			var loadErr error | ||||
| 
 | ||||
| 			BeforeEach(func() { | ||||
| 				loadedSession, loadErr = in.ss().Load(in.request) | ||||
| 			}) | ||||
| 
 | ||||
| 			It("returns an empty session", func() { | ||||
| 				Expect(loadedSession).To(BeNil()) | ||||
| 			}) | ||||
| 
 | ||||
| 			It("should return a no cookie error", func() { | ||||
| 				Expect(loadErr).To(MatchError(http.ErrNoCookie)) | ||||
| 			}) | ||||
| 		}) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue