|
| 1 | +// At the top of XSS.test.ts, before importing getAllowedIframeOrigins |
| 2 | +jest.mock('./domainValidation', () => ({ |
| 3 | + extractChatflowId: jest.fn(), |
| 4 | + isPublicChatflowRequest: jest.fn(), |
| 5 | + isTTSGenerateRequest: jest.fn(), |
| 6 | + validateChatflowDomain: jest.fn() |
| 7 | +})) |
| 8 | + |
| 9 | +import { getAllowedIframeOrigins } from './XSS' |
| 10 | + |
| 11 | +describe('getAllowedIframeOrigins', () => { |
| 12 | + const originalEnv = process.env.IFRAME_ORIGINS |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + // Restore original environment variable after each test |
| 16 | + if (originalEnv !== undefined) { |
| 17 | + process.env.IFRAME_ORIGINS = originalEnv |
| 18 | + } else { |
| 19 | + delete process.env.IFRAME_ORIGINS |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + describe('default behavior', () => { |
| 24 | + it("should return 'self' when IFRAME_ORIGINS is not set", () => { |
| 25 | + delete process.env.IFRAME_ORIGINS |
| 26 | + expect(getAllowedIframeOrigins()).toBe("'self'") |
| 27 | + }) |
| 28 | + |
| 29 | + it("should return 'self' when IFRAME_ORIGINS is empty string", () => { |
| 30 | + process.env.IFRAME_ORIGINS = '' |
| 31 | + expect(getAllowedIframeOrigins()).toBe("'self'") |
| 32 | + }) |
| 33 | + |
| 34 | + it("should return 'self' when IFRAME_ORIGINS is only whitespace", () => { |
| 35 | + process.env.IFRAME_ORIGINS = ' ' |
| 36 | + expect(getAllowedIframeOrigins()).toBe("'self'") |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('CSP special values', () => { |
| 41 | + it("should handle 'self' value", () => { |
| 42 | + process.env.IFRAME_ORIGINS = "'self'" |
| 43 | + expect(getAllowedIframeOrigins()).toBe("'self'") |
| 44 | + }) |
| 45 | + |
| 46 | + it("should handle 'none' value", () => { |
| 47 | + process.env.IFRAME_ORIGINS = "'none'" |
| 48 | + expect(getAllowedIframeOrigins()).toBe("'none'") |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle wildcard * value', () => { |
| 52 | + process.env.IFRAME_ORIGINS = '*' |
| 53 | + expect(getAllowedIframeOrigins()).toBe('*') |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + describe('single domain', () => { |
| 58 | + it('should handle a single FQDN', () => { |
| 59 | + process.env.IFRAME_ORIGINS = 'https://example.com' |
| 60 | + expect(getAllowedIframeOrigins()).toBe('https://example.com') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should trim whitespace from single domain', () => { |
| 64 | + process.env.IFRAME_ORIGINS = ' https://example.com ' |
| 65 | + expect(getAllowedIframeOrigins()).toBe('https://example.com') |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe('multiple domains', () => { |
| 70 | + it('should convert comma-separated domains to space-separated', () => { |
| 71 | + process.env.IFRAME_ORIGINS = 'https://domain1.com,https://domain2.com' |
| 72 | + expect(getAllowedIframeOrigins()).toBe('https://domain1.com https://domain2.com') |
| 73 | + }) |
| 74 | + |
| 75 | + it('should handle multiple domains with spaces', () => { |
| 76 | + process.env.IFRAME_ORIGINS = 'https://domain1.com, https://domain2.com, https://domain3.com' |
| 77 | + expect(getAllowedIframeOrigins()).toBe('https://domain1.com https://domain2.com https://domain3.com') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should trim individual domains in comma-separated list', () => { |
| 81 | + process.env.IFRAME_ORIGINS = ' https://app.com , https://admin.com ' |
| 82 | + expect(getAllowedIframeOrigins()).toBe('https://app.com https://admin.com') |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + describe('edge cases', () => { |
| 87 | + it('should handle domains with ports', () => { |
| 88 | + process.env.IFRAME_ORIGINS = 'https://localhost:3000,https://localhost:4000' |
| 89 | + expect(getAllowedIframeOrigins()).toBe('https://localhost:3000 https://localhost:4000') |
| 90 | + }) |
| 91 | + |
| 92 | + it('should handle domains with paths (though not typical for CSP)', () => { |
| 93 | + process.env.IFRAME_ORIGINS = 'https://example.com/path1,https://example.com/path2' |
| 94 | + expect(getAllowedIframeOrigins()).toBe('https://example.com/path1 https://example.com/path2') |
| 95 | + }) |
| 96 | + |
| 97 | + it('should handle mixed protocols', () => { |
| 98 | + process.env.IFRAME_ORIGINS = 'http://example.com,https://secure.com' |
| 99 | + expect(getAllowedIframeOrigins()).toBe('http://example.com https://secure.com') |
| 100 | + }) |
| 101 | + |
| 102 | + it('should handle trailing comma', () => { |
| 103 | + process.env.IFRAME_ORIGINS = 'https://example.com,' |
| 104 | + expect(getAllowedIframeOrigins()).toBe('https://example.com ') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should handle leading comma', () => { |
| 108 | + process.env.IFRAME_ORIGINS = ',https://example.com' |
| 109 | + expect(getAllowedIframeOrigins()).toBe(' https://example.com') |
| 110 | + }) |
| 111 | + |
| 112 | + it('should handle multiple consecutive commas', () => { |
| 113 | + process.env.IFRAME_ORIGINS = 'https://domain1.com,,https://domain2.com' |
| 114 | + expect(getAllowedIframeOrigins()).toBe('https://domain1.com https://domain2.com') |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('real-world scenarios', () => { |
| 119 | + it('should handle typical production configuration', () => { |
| 120 | + process.env.IFRAME_ORIGINS = 'https://app.example.com,https://admin.example.com,https://dashboard.example.com' |
| 121 | + expect(getAllowedIframeOrigins()).toBe('https://app.example.com https://admin.example.com https://dashboard.example.com') |
| 122 | + }) |
| 123 | + |
| 124 | + it('should handle development configuration with localhost', () => { |
| 125 | + process.env.IFRAME_ORIGINS = 'http://localhost:3000,http://localhost:3001,http://127.0.0.1:3000' |
| 126 | + expect(getAllowedIframeOrigins()).toBe('http://localhost:3000 http://localhost:3001 http://127.0.0.1:3000') |
| 127 | + }) |
| 128 | + |
| 129 | + it("should handle mix of 'self' and domains", () => { |
| 130 | + process.env.IFRAME_ORIGINS = "'self',https://trusted.com" |
| 131 | + expect(getAllowedIframeOrigins()).toBe("'self' https://trusted.com") |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments