|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + buildLoginCspHeaders, |
| 5 | + DEFAULT_CSP_ENFORCED, |
| 6 | + DEFAULT_CSP_REPORT_ONLY, |
| 7 | + getRealmFromLoginUrl, |
| 8 | + isHtmlResponse, |
| 9 | +} from './csp.utilities'; |
| 10 | + |
| 11 | +describe('Login CSP utilities', () => { |
| 12 | + it('uses root as the default realm', () => { |
| 13 | + const realm = getRealmFromLoginUrl(new URL('https://example.com/login')); |
| 14 | + |
| 15 | + expect(realm).toBe('root'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('uses customer CSP values only for alpha and bravo realm requests', () => { |
| 19 | + const alphaHeaders = buildLoginCspHeaders(new URL('https://example.com/login?realm=/alpha'), { |
| 20 | + enforced: "frame-ancestors 'self' https://alpha.example.com", |
| 21 | + reportOnly: "default-src 'self'; report-uri https://reports.example.com", |
| 22 | + }); |
| 23 | + const rootHeaders = buildLoginCspHeaders(new URL('https://example.com/login?realm=/'), { |
| 24 | + enforced: "frame-ancestors 'self' https://alpha.example.com", |
| 25 | + reportOnly: "default-src 'self'; report-uri https://reports.example.com", |
| 26 | + }); |
| 27 | + |
| 28 | + expect(alphaHeaders.get('Content-Security-Policy')).toBe( |
| 29 | + "frame-ancestors 'self' https://alpha.example.com", |
| 30 | + ); |
| 31 | + expect(alphaHeaders.get('Content-Security-Policy-Report-Only')).toBe( |
| 32 | + "default-src 'self'; report-uri https://reports.example.com", |
| 33 | + ); |
| 34 | + expect(rootHeaders.get('Content-Security-Policy')).toBe(DEFAULT_CSP_ENFORCED); |
| 35 | + expect(rootHeaders.get('Content-Security-Policy-Report-Only')).toBe(DEFAULT_CSP_REPORT_ONLY); |
| 36 | + }); |
| 37 | + |
| 38 | + it('uses customer CSP values for custom hosts', () => { |
| 39 | + const headers = buildLoginCspHeaders( |
| 40 | + new URL('https://login.example.com/login?realm=/'), |
| 41 | + { |
| 42 | + enforced: "frame-ancestors 'self' https://custom.example.com", |
| 43 | + reportOnly: "default-src 'self'; report-uri https://reports.example.com", |
| 44 | + }, |
| 45 | + { |
| 46 | + amUrl: 'https://openam.example.com/am', |
| 47 | + currentHost: 'login.example.com', |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + expect(headers.get('Content-Security-Policy')).toBe( |
| 52 | + "frame-ancestors 'self' https://custom.example.com", |
| 53 | + ); |
| 54 | + expect(headers.get('Content-Security-Policy-Report-Only')).toBe( |
| 55 | + "default-src 'self'; report-uri https://reports.example.com", |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('uses default CSP values for the AM host root realm', () => { |
| 60 | + const headers = buildLoginCspHeaders( |
| 61 | + new URL('https://openam.example.com/login?realm=/'), |
| 62 | + { |
| 63 | + enforced: "frame-ancestors 'self' https://custom.example.com", |
| 64 | + reportOnly: "default-src 'self'; report-uri https://reports.example.com", |
| 65 | + }, |
| 66 | + { |
| 67 | + amUrl: 'https://openam.example.com/am', |
| 68 | + currentHost: 'openam.example.com:443', |
| 69 | + }, |
| 70 | + ); |
| 71 | + |
| 72 | + expect(headers.get('Content-Security-Policy')).toBe(DEFAULT_CSP_ENFORCED); |
| 73 | + expect(headers.get('Content-Security-Policy-Report-Only')).toBe(DEFAULT_CSP_REPORT_ONLY); |
| 74 | + }); |
| 75 | + |
| 76 | + it('detects HTML responses', () => { |
| 77 | + const response = new Response('<main></main>', { |
| 78 | + headers: { |
| 79 | + 'content-type': 'text/html; charset=utf-8', |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(isHtmlResponse(response)).toBe(true); |
| 84 | + }); |
| 85 | +}); |
0 commit comments