From 91bd1ebe81df0f752fcf221e5370abaa29573a03 Mon Sep 17 00:00:00 2001 From: bo0tzz Date: Mon, 3 Aug 2026 21:56:53 +0200 Subject: [PATCH] tests --- .../repositories/config.repository.spec.ts | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/server/src/repositories/config.repository.spec.ts b/server/src/repositories/config.repository.spec.ts index 3c579a1a94..0b92708edf 100644 --- a/server/src/repositories/config.repository.spec.ts +++ b/server/src/repositories/config.repository.spec.ts @@ -1,5 +1,5 @@ import { ImmichTelemetry } from 'src/enum'; -import { clearEnvCache, ConfigRepository } from 'src/repositories/config.repository'; +import { applyCspHashes, clearEnvCache, ConfigRepository } from 'src/repositories/config.repository'; const getEnv = () => { clearEnvCache(); @@ -325,3 +325,35 @@ describe('getEnv', () => { }); }); }); + +describe('applyCspHashes', () => { + const manifest = { 'script-src': [`'sha256-script'`], 'style-src': [`'sha256-style'`] }; + + it('should append the hashes of the inline content', () => { + const directives = applyCspHashes({ 'script-src': [`'self'`], 'style-src': [`'self'`] }, manifest); + + expect(directives).toEqual({ + 'script-src': [`'self'`, `'sha256-script'`], + 'style-src': [`'self'`, `'sha256-style'`], + }); + }); + + it('should skip a directive that allows unsafe-inline, which a hash would disable', () => { + const directives = applyCspHashes({ 'script-src': [`'self'`, `'unsafe-inline'`] }, manifest); + + expect(directives['script-src']).toEqual([`'self'`, `'unsafe-inline'`]); + }); + + it('should skip a directive the policy does not declare', () => { + const directives = applyCspHashes({ 'script-src': [`'self'`] }, manifest); + + expect(directives).not.toHaveProperty('style-src'); + }); + + it('should not mutate the directives it is given', () => { + const directives = { 'script-src': [`'self'`] }; + applyCspHashes(directives, manifest); + + expect(directives).toEqual({ 'script-src': [`'self'`] }); + }); +});