|
| 1 | +import { SELF } from "#src/utils.ts"; |
| 2 | +import { expect, it } from "vitest"; |
| 3 | +import { ContentSecurityPolicy } from "./content-security-policy"; |
| 4 | + |
| 5 | +it("creates a CSP policy with a single directive", () => { |
| 6 | + let csp = new ContentSecurityPolicy(); |
| 7 | + csp.set("default-src", [SELF]); |
| 8 | + expect(csp.toString()).toBe("default-src 'self'"); |
| 9 | +}); |
| 10 | + |
| 11 | +it("creates a CSP policy with no directives", () => { |
| 12 | + let csp = new ContentSecurityPolicy(); |
| 13 | + expect(csp.toString()).toBe(""); |
| 14 | +}); |
| 15 | + |
| 16 | +it("creates a CSP policy with multiple directives", () => { |
| 17 | + let csp = new ContentSecurityPolicy(); |
| 18 | + csp.set("default-src", [SELF]); |
| 19 | + csp.set("script-src", ["https://example.com"]); |
| 20 | + expect(csp.toString()).toBe( |
| 21 | + "default-src 'self'; script-src https://example.com", |
| 22 | + ); |
| 23 | +}); |
| 24 | + |
| 25 | +it("creates a CSP policy with multiple values in a directive", () => { |
| 26 | + let csp = new ContentSecurityPolicy(); |
| 27 | + csp.set("script-src", [SELF, "https://example.com"]); |
| 28 | + expect(csp.toString()).toBe("script-src 'self' https://example.com"); |
| 29 | +}); |
| 30 | + |
| 31 | +it("can parse a CSP string", () => { |
| 32 | + let csp = new ContentSecurityPolicy(); |
| 33 | + csp.parse("default-src 'self'; script-src https://example.com"); |
| 34 | + expect(csp.get("default-src")).toEqual(["'self'"]); |
| 35 | + expect(csp.get("script-src")).toEqual(["https://example.com"]); |
| 36 | +}); |
| 37 | + |
| 38 | +it("handles `upgrade-insecure-requests` directive", () => { |
| 39 | + let csp = new ContentSecurityPolicy(); |
| 40 | + csp.set("upgrade-insecure-requests", []); |
| 41 | + expect(csp.toString()).toBe("upgrade-insecure-requests"); |
| 42 | +}); |
| 43 | + |
| 44 | +it("handles upgradeInsecureRequests method", () => { |
| 45 | + let csp = new ContentSecurityPolicy(); |
| 46 | + csp.upgradeInsecureRequests(); |
| 47 | + expect(csp.toString()).toBe("upgrade-insecure-requests"); |
| 48 | +}); |
| 49 | + |
| 50 | +it("can call `append` multiple times for the same key", () => { |
| 51 | + let csp = new ContentSecurityPolicy(); |
| 52 | + csp.append("default-src", [SELF]); |
| 53 | + csp.append("default-src", ["https://example.com"]); |
| 54 | + expect(csp.toString()).toBe("default-src 'self' https://example.com"); |
| 55 | +}); |
| 56 | + |
| 57 | +it("can create csp with predefined directives", () => { |
| 58 | + let csp = new ContentSecurityPolicy({ |
| 59 | + "default-src": [SELF], |
| 60 | + "script-src": ["https://example.com"], |
| 61 | + }); |
| 62 | + expect(csp.toString()).toBe( |
| 63 | + "default-src 'self'; script-src https://example.com", |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +it("can create csp with predefined policy string", () => { |
| 68 | + let csp = new ContentSecurityPolicy( |
| 69 | + "default-src 'self'; script-src https://example.com", |
| 70 | + ); |
| 71 | + expect(csp.toString()).toBe( |
| 72 | + "default-src 'self'; script-src https://example.com", |
| 73 | + ); |
| 74 | +}); |
| 75 | + |
| 76 | +it("allows and filters out `undefined` values", () => { |
| 77 | + let csp = new ContentSecurityPolicy({ |
| 78 | + "connect-src": [undefined, "'self'", undefined], |
| 79 | + }); |
| 80 | + |
| 81 | + expect(csp.toString()).toMatchInlineSnapshot(`"connect-src 'self'"`); |
| 82 | +}); |
| 83 | + |
| 84 | +it("allows there to be no define values for a csp key", () => { |
| 85 | + let csp = new ContentSecurityPolicy({ |
| 86 | + "base-uri": [undefined], |
| 87 | + "default-src": ["'none'"], |
| 88 | + }); |
| 89 | + expect(csp.toString()).toBe("default-src 'none'"); |
| 90 | +}); |
| 91 | + |
| 92 | +it("throws an error if the value is reserved, but not properly quoted", () => { |
| 93 | + expect( |
| 94 | + () => |
| 95 | + new ContentSecurityPolicy({ |
| 96 | + "default-src": ["self", "https://example.com"], |
| 97 | + }), |
| 98 | + ).toThrowErrorMatchingInlineSnapshot( |
| 99 | + `[ContentSecurityPolicyError: reserved keyword self must be quoted.]`, |
| 100 | + ); |
| 101 | +}); |
0 commit comments