From 3b8f9e0e1389ffa4ea1b71a293beb82857a07ab5 Mon Sep 17 00:00:00 2001 From: Matus Kasak Date: Tue, 27 Oct 2026 13:17:38 +0100 Subject: [PATCH 1/2] Harden dsAuthInfo auth cookie with Secure and SameSite attributes Set secure and sameSite='lax' on the auth JWT cookie written in storeToken(), as defense-in-depth. The Secure flag is derived from the actual page protocol (location.protocol === 'https:') so it is applied on any https page, including behind a TLS-terminating reverse proxy, while local http dev still works. 'lax' is used rather than 'strict' so the cookie is still sent on the top-level navigation that returns from external authentication. --- src/app/core/auth/auth.service.spec.ts | 26 +++++++++++++++++++++++++- src/app/core/auth/auth.service.ts | 10 +++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/app/core/auth/auth.service.spec.ts b/src/app/core/auth/auth.service.spec.ts index 32ceee35d65..27aab85ca7a 100644 --- a/src/app/core/auth/auth.service.spec.ts +++ b/src/app/core/auth/auth.service.spec.ts @@ -13,7 +13,7 @@ import { CookieService } from '../services/cookie.service'; import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service.stub'; import { AuthRequestService } from './auth-request.service'; import { AuthStatus } from './models/auth-status.model'; -import { AuthTokenInfo } from './models/auth-token-info.model'; +import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model'; import { EPerson } from '../eperson/models/eperson.model'; import { EPersonMock } from '../../shared/testing/eperson.mock'; import { AppState } from '../../app.reducer'; @@ -369,6 +369,30 @@ describe('AuthService test', () => { expect(storage.set).toHaveBeenCalled(); }); + it('should store the token with a Secure, SameSite cookie when served over https', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'https:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: true, + sameSite: 'lax', + })); + }); + + it('should store the token without the Secure flag when served over http', () => { + Object.defineProperty(window, 'nativeWindow', { + get: () => ({ location: { protocol: 'http:' } }), + configurable: true, + }); + authService.storeToken(token); + expect(storage.set).toHaveBeenCalledWith(TOKENITEM, token, jasmine.objectContaining({ + secure: false, + sameSite: 'lax', + })); + }); + it('should remove token from storage', () => { authService.removeToken(); expect(storage.remove).toHaveBeenCalled(); diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 6d4ee2de4f6..e0efcef7ede 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -456,7 +456,15 @@ export class AuthService { // Set the cookie expire date const expires = new Date(expireDate); - const options: CookieAttributes = {expires: expires}; + // Harden the auth cookie: mark it Secure when the page is actually served over HTTPS so it + // is only sent over encrypted connections, and constrain it to same-site requests. 'lax' + // still allows the cookie on top-level navigations, which the external-auth redirect flow + // relies on. + const options: CookieAttributes = { + expires: expires, + secure: this._window.nativeWindow.location?.protocol === 'https:', + sameSite: 'lax', + }; // Save cookie with the token return this.storage.set(TOKENITEM, token, options); From f1f40bb2fff7f0b7baf26412e9c46305b66b99a6 Mon Sep 17 00:00:00 2001 From: Matus Kasak Date: Tue, 27 Oct 2026 14:32:55 +0100 Subject: [PATCH 2/2] docs: shorten auth cookie security comment --- src/app/core/auth/auth.service.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index e0efcef7ede..936efac6d3d 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -456,10 +456,7 @@ export class AuthService { // Set the cookie expire date const expires = new Date(expireDate); - // Harden the auth cookie: mark it Secure when the page is actually served over HTTPS so it - // is only sent over encrypted connections, and constrain it to same-site requests. 'lax' - // still allows the cookie on top-level navigations, which the external-auth redirect flow - // relies on. + // Use Secure on HTTPS; Lax allows external-auth redirects via top-level GET navigation. const options: CookieAttributes = { expires: expires, secure: this._window.nativeWindow.location?.protocol === 'https:',