From f02361a93cfc0075cb7b283d267b3ce3bf87f549 Mon Sep 17 00:00:00 2001 From: Kasinhou <129340513+Kasinhou@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:55:45 +0100 Subject: [PATCH] LINDAT/fix(security): harden dsAuthInfo auth cookie with Secure and SameSite * 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. * docs: shorten auth cookie security comment --------- Co-authored-by: Matus Kasak (cherry picked from commit 9bcd483826745abc5662e50b4c5b93b9edaa7482) Co-Authored-By: Claude Opus 5.5 --- src/app/core/auth/auth.service.spec.ts | 26 +++++++++++++++++++++++++- src/app/core/auth/auth.service.ts | 7 ++++++- 2 files changed, 31 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..936efac6d3d 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -456,7 +456,12 @@ export class AuthService { // Set the cookie expire date const expires = new Date(expireDate); - const options: CookieAttributes = {expires: expires}; + // 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:', + sameSite: 'lax', + }; // Save cookie with the token return this.storage.set(TOKENITEM, token, options);