|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { generateNonce, injectNonceIntoHTML } from "./security-headers"; |
| 4 | + |
| 5 | +describe("Security headers functionality", () => { |
| 6 | + describe("generateNonce", () => { |
| 7 | + it("should generate a unique nonce each time", () => { |
| 8 | + const nonce1 = generateNonce(); |
| 9 | + const nonce2 = generateNonce(); |
| 10 | + |
| 11 | + expect(nonce1).toBeTruthy(); |
| 12 | + expect(nonce2).toBeTruthy(); |
| 13 | + expect(nonce1).not.toBe(nonce2); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should generate a base64-encoded string", () => { |
| 17 | + const nonce = generateNonce(); |
| 18 | + |
| 19 | + // Base64 string should only contain valid base64 characters |
| 20 | + expect(nonce).toMatch(/^[A-Za-z0-9+/]+=*$/); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("injectNonceIntoHTML", () => { |
| 25 | + const testNonce = "test-nonce-123"; |
| 26 | + |
| 27 | + it("should inject nonce into simple script tag", () => { |
| 28 | + const html = '<script src="/test.js"></script>'; |
| 29 | + const result = injectNonceIntoHTML(html, testNonce); |
| 30 | + |
| 31 | + expect(result).toBe( |
| 32 | + `<script nonce="${testNonce}" src="/test.js"></script>` |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should inject nonce into script tag with type module", () => { |
| 37 | + const html = '<script type="module" src="/test.js"></script>'; |
| 38 | + const result = injectNonceIntoHTML(html, testNonce); |
| 39 | + |
| 40 | + expect(result).toBe( |
| 41 | + `<script nonce="${testNonce}" type="module" src="/test.js"></script>` |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should inject nonce into script tag with multiple attributes", () => { |
| 46 | + const html = |
| 47 | + '<script type="module" crossorigin src="/assets/index-CeBMaJSY.js"></script>'; |
| 48 | + const result = injectNonceIntoHTML(html, testNonce); |
| 49 | + |
| 50 | + expect(result).toBe( |
| 51 | + `<script nonce="${testNonce}" type="module" crossorigin src="/assets/index-CeBMaJSY.js"></script>` |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should inject nonce into inline script tag", () => { |
| 56 | + const html = '<script type="module">console.log("test");</script>'; |
| 57 | + const result = injectNonceIntoHTML(html, testNonce); |
| 58 | + |
| 59 | + expect(result).toBe( |
| 60 | + `<script nonce="${testNonce}" type="module">console.log("test");</script>` |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should inject nonce into complex inline script", () => { |
| 65 | + const html = `<script type="module">import { injectIntoGlobalHook } from "/@react-refresh"; |
| 66 | +injectIntoGlobalHook(window); |
| 67 | +window.$RefreshReg$ = () => {}; |
| 68 | +window.$RefreshSig$ = () => (type) => type;</script>`; |
| 69 | + const result = injectNonceIntoHTML(html, testNonce); |
| 70 | + |
| 71 | + expect(result).toContain(`<script nonce="${testNonce}" type="module">`); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should not modify script tags that already have nonce", () => { |
| 75 | + const html = '<script nonce="existing-nonce" src="/test.js"></script>'; |
| 76 | + const result = injectNonceIntoHTML(html, testNonce); |
| 77 | + |
| 78 | + expect(result).toBe(html); // Should remain unchanged |
| 79 | + }); |
| 80 | + |
| 81 | + it("should handle multiple script tags correctly", () => { |
| 82 | + const html = ` |
| 83 | + <script src="/test1.js"></script> |
| 84 | + <script nonce="existing" src="/test2.js"></script> |
| 85 | + <script type="module" src="/test3.js"></script> |
| 86 | + `; |
| 87 | + const result = injectNonceIntoHTML(html, testNonce); |
| 88 | + |
| 89 | + expect(result).toContain( |
| 90 | + `<script nonce="${testNonce}" src="/test1.js"></script>` |
| 91 | + ); |
| 92 | + expect(result).toContain( |
| 93 | + `<script nonce="existing" src="/test2.js"></script>` |
| 94 | + ); |
| 95 | + expect(result).toContain( |
| 96 | + `<script nonce="${testNonce}" type="module" src="/test3.js"></script>` |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle the actual HTML from development mode", () => { |
| 101 | + const html = `<!doctype html> |
| 102 | +<html lang="en"> |
| 103 | + <head> |
| 104 | + <script type="module">import { injectIntoGlobalHook } from "/@react-refresh"; |
| 105 | +injectIntoGlobalHook(window); |
| 106 | +window.$RefreshReg$ = () => {}; |
| 107 | +window.$RefreshSig$ = () => (type) => type;</script> |
| 108 | +
|
| 109 | + <script type="module" src="/@vite/client"></script> |
| 110 | +
|
| 111 | + <meta charset="UTF-8" /> |
| 112 | + <link rel="icon" type="image/svg+xml" href="/icon.svg" /> |
| 113 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 114 | + <title>Dafthunk</title><meta name="description" content="Workflow execution platform." data-managed-tag="true"/><meta property="og:title" content="Dafthunk" data-managed-tag="true"/><meta property="og:description" content="Workflow execution platform." data-managed-tag="true"/><meta property="og:type" content="website" data-managed-tag="true"/><meta name="twitter:card" content="summary_large_image" data-managed-tag="true"/><meta name="twitter:title" content="Dafthunk" data-managed-tag="true"/><meta name="twitter:description" content="Workflow execution platform." data-managed-tag="true"/> |
| 115 | + </head> |
| 116 | + <body> |
| 117 | + <div id="root"></div> |
| 118 | + <script type="module" src="/src/entry-client.tsx"></script> |
| 119 | + </body> |
| 120 | +</html>`; |
| 121 | + |
| 122 | + const result = injectNonceIntoHTML(html, testNonce); |
| 123 | + |
| 124 | + // Should inject nonce into all three script tags |
| 125 | + const scriptMatches = result.match(/<script[^>]*>/g); |
| 126 | + expect(scriptMatches).toHaveLength(3); |
| 127 | + |
| 128 | + // All script tags should have the nonce |
| 129 | + scriptMatches?.forEach((tag) => { |
| 130 | + expect(tag).toContain(`nonce="${testNonce}"`); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should not affect non-script tags", () => { |
| 135 | + const html = ` |
| 136 | + <div>content</div> |
| 137 | + <script src="/test.js"></script> |
| 138 | + <link rel="stylesheet" href="/style.css"> |
| 139 | + `; |
| 140 | + const result = injectNonceIntoHTML(html, testNonce); |
| 141 | + |
| 142 | + expect(result).toContain("<div>content</div>"); |
| 143 | + expect(result).toContain('<link rel="stylesheet" href="/style.css">'); |
| 144 | + expect(result).toContain( |
| 145 | + `<script nonce="${testNonce}" src="/test.js"></script>` |
| 146 | + ); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments