Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : agentilab-web8 * File : tests/unit/security.test.ts9 * Module : Tests10 * Repository : https://git.spboucher.ai/agentilab-web11 * Project Website : Not specified12 * Created : 2026-12-0513 * Last Modified : 2026-12-0514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { describe, expect, it } from 'vitest';44import { basicAuthOk, contentSecurityPolicy, hstsHeader, safeEqual, staticSecurityHeaders } from '@/lib/security/headers';4546describe('en-têtes de sécurité', () => {47 const csp = contentSecurityPolicy(false);4849 it('ferme la CSP à la seule origine du site', () => {50 expect(csp).toContain("default-src 'self'");51 expect(csp).toContain("object-src 'none'");52 expect(csp).toContain("frame-ancestors 'none'");53 expect(csp).toContain("base-uri 'self'");54 expect(csp).toContain("form-action 'self'");55 expect(csp).not.toMatch(/https?:|\*/);56 });5758 it('n’autorise unsafe-eval qu’en développement', () => {59 expect(csp).not.toContain('unsafe-eval');60 expect(contentSecurityPolicy(true)).toContain('unsafe-eval');61 });6263 it('pose les en-têtes attendus', () => {64 const keys = staticSecurityHeaders().map((h) => h.key);65 expect(keys).toEqual(expect.arrayContaining(['Content-Security-Policy', 'X-Content-Type-Options', 'X-Frame-Options', 'Referrer-Policy', 'Permissions-Policy']));66 });6768 it('garde HSTS progressif : ni includeSubDomains ni preload', () => {69 expect(hstsHeader(undefined)).toBeNull();70 expect(hstsHeader('0')).toBeNull();71 expect(hstsHeader('abc')).toBeNull();72 expect(hstsHeader('86400')?.value).toBe('max-age=86400');73 });74});7576describe('authentification du staging', () => {77 const header = (user: string, password: string) => `Basic ${btoa(`${user}:${password}`)}`;78 it('accepte les bons identifiants, refuse le reste', () => {79 expect(basicAuthOk(header('u', 'p:w'), 'u', 'p:w')).toBe(true);80 expect(basicAuthOk(header('u', 'x'), 'u', 'p:w')).toBe(false);81 expect(basicAuthOk(header('x', 'p:w'), 'u', 'p:w')).toBe(false);82 expect(basicAuthOk(null, 'u', 'p')).toBe(false);83 expect(basicAuthOk('Basic %%%', 'u', 'p')).toBe(false);84 expect(basicAuthOk('Bearer abc', 'u', 'p')).toBe(false);85 });86 it('compare sans court-circuit sur la longueur', () => {87 expect(safeEqual('abc', 'abc')).toBe(true);88 expect(safeEqual('abc', 'abcd')).toBe(false);89 expect(safeEqual('', '')).toBe(true);90 });91});92