diff --git a/plugins/auth-node/src/oauth/createAuthErrorCookie.ts b/plugins/auth-node/src/oauth/createAuthErrorCookie.ts index 61914e7c05..46ef4c4a1c 100644 --- a/plugins/auth-node/src/oauth/createAuthErrorCookie.ts +++ b/plugins/auth-node/src/oauth/createAuthErrorCookie.ts @@ -14,7 +14,6 @@ * limitations under the License. */ import { Response } from 'express'; -import { CookieConfigurer } from '../types'; import { serializeError } from '@backstage/errors'; const ONE_MINUTE_MS = 60 * 1000; @@ -29,7 +28,7 @@ function configureAuthErrorCookie(apiUrl: string, appOrigin: string) { // different domain than the app, we set the SameSite attribute // to 'none' to allow third-party access to the cookie, but // only if it's in a secure context (https). - let sameSite: ReturnType['sameSite'] = 'lax'; + let sameSite: 'lax' | 'none' = 'lax'; if (new URL(appOrigin).hostname !== domain && secure) { sameSite = 'none'; } diff --git a/plugins/auth-node/src/oauth/createOAuthRouteHandlers.test.ts b/plugins/auth-node/src/oauth/createOAuthRouteHandlers.test.ts index 7da5519655..2f3eb360cc 100644 --- a/plugins/auth-node/src/oauth/createOAuthRouteHandlers.test.ts +++ b/plugins/auth-node/src/oauth/createOAuthRouteHandlers.test.ts @@ -742,5 +742,29 @@ describe('createOAuthRouteHandlers', () => { }, }); }); + + it('should set cookie on caught error redirect', async () => { + const app = wrapInApp(createOAuthRouteHandlers(baseConfig)); + const res = await request(app) + .get('/my-provider/handler/frame') + .query({ + state: encodeOAuthState({ + env: 'development', + nonce: '123', + flow: 'redirect', + redirectUrl: 'http://localhost:3000', + }), + }); + + // redirects on error with auth error cookie + expect(res.status).toBe(302); + const setCookieHeader = res.header['set-cookie']; + expect(setCookieHeader).toBeDefined(); + + const authErrorCookie = setCookieHeader.find((cookie: string) => + cookie.startsWith('auth-error='), + ); + expect(authErrorCookie).toBeDefined(); + }); }); });