This commit is contained in:
bo0tzz 2026-08-03 21:56:53 +02:00
parent de796491da
commit 91bd1ebe81
No known key found for this signature in database

View file

@ -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'`] });
});
});