|
| 1 | +describe('addRemoteOrigin', () => { |
| 2 | + let addRemoteOrigin: (origin: string) => void; |
| 3 | + let getRuntimeRemoteOrigins: () => readonly string[]; |
| 4 | + |
| 5 | + beforeEach(async () => { |
| 6 | + // Reset the module so each test starts with an empty registry. |
| 7 | + vi.resetModules(); |
| 8 | + ({ addRemoteOrigin, getRuntimeRemoteOrigins } = |
| 9 | + await import('./runtime-remote-origins')); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('valid origins', () => { |
| 13 | + it('should accept a basic https origin', () => { |
| 14 | + addRemoteOrigin('https://example.com'); |
| 15 | + expect(getRuntimeRemoteOrigins()).toEqual(['https://example.com']); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should accept an http origin', () => { |
| 19 | + addRemoteOrigin('http://example.com'); |
| 20 | + expect(getRuntimeRemoteOrigins()).toEqual(['http://example.com']); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should accept an origin with a non-default port', () => { |
| 24 | + addRemoteOrigin('http://localhost:5401'); |
| 25 | + expect(getRuntimeRemoteOrigins()).toEqual([ |
| 26 | + 'http://localhost:5401', |
| 27 | + ]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should accept a bracketed IPv6 origin', () => { |
| 31 | + addRemoteOrigin('http://[::1]:8080'); |
| 32 | + expect(getRuntimeRemoteOrigins()).toEqual(['http://[::1]:8080']); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should accept a punycode host', () => { |
| 36 | + addRemoteOrigin('https://xn--exmple-cua.com'); |
| 37 | + expect(getRuntimeRemoteOrigins()).toEqual([ |
| 38 | + 'https://xn--exmple-cua.com', |
| 39 | + ]); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('invalid origins', () => { |
| 44 | + it('should reject an empty string', () => { |
| 45 | + expect(() => addRemoteOrigin('')).toThrow("Invalid origin: ''"); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should reject an unparseable string', () => { |
| 49 | + expect(() => addRemoteOrigin('not a url')).toThrow( |
| 50 | + "Invalid origin: 'not a url'" |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should reject an origin with a path', () => { |
| 55 | + expect(() => addRemoteOrigin('https://example.com/path')).toThrow( |
| 56 | + "Invalid origin: 'https://example.com/path'" |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should reject an origin with a trailing slash', () => { |
| 61 | + expect(() => addRemoteOrigin('https://example.com/')).toThrow( |
| 62 | + "Invalid origin: 'https://example.com/'" |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should reject an origin with a query string', () => { |
| 67 | + expect(() => addRemoteOrigin('https://example.com?x=1')).toThrow( |
| 68 | + "Invalid origin: 'https://example.com?x=1'" |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should reject an origin with a fragment', () => { |
| 73 | + expect(() => addRemoteOrigin('https://example.com#frag')).toThrow( |
| 74 | + "Invalid origin: 'https://example.com#frag'" |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should reject an origin with the scheme's default port specified", () => { |
| 79 | + // `new URL('https://example.com:443').href === 'https://example.com/'`, |
| 80 | + // so the round-trip strict-equality check rejects the input. |
| 81 | + expect(() => addRemoteOrigin('https://example.com:443')).toThrow( |
| 82 | + "Invalid origin: 'https://example.com:443'" |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should reject an origin with an uppercase scheme', () => { |
| 87 | + // The URL parser normalizes the scheme to lowercase, so the |
| 88 | + // round-trip strict-equality check rejects upper-case input. |
| 89 | + expect(() => addRemoteOrigin('HTTPS://example.com')).toThrow( |
| 90 | + "Invalid origin: 'HTTPS://example.com'" |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should reject an origin with a non-ASCII host', () => { |
| 95 | + // IDN hosts must be supplied in punycode form; otherwise the |
| 96 | + // URL parser normalizes the host and the round-trip check fails. |
| 97 | + expect(() => addRemoteOrigin('https://exämple.com')).toThrow( |
| 98 | + "Invalid origin: 'https://exämple.com'" |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should reject an origin with userinfo', () => { |
| 103 | + // `URL.origin` strips userinfo, so an origin with userinfo can |
| 104 | + // never match the iframe's `url.origin` at validation time. |
| 105 | + expect(() => |
| 106 | + addRemoteOrigin('https://user:pass@example.com') |
| 107 | + ).toThrow("Invalid origin: 'https://user:pass@example.com'"); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should reject a `ws://` origin', () => { |
| 111 | + expect(() => addRemoteOrigin('ws://example.com')).toThrow( |
| 112 | + "Invalid origin: 'ws://example.com'" |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should reject a `file://` origin', () => { |
| 117 | + expect(() => addRemoteOrigin('file:///tmp/x')).toThrow( |
| 118 | + "Invalid origin: 'file:///tmp/x'" |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should reject a `data:` URL', () => { |
| 123 | + expect(() => addRemoteOrigin('data:text/plain,hi')).toThrow( |
| 124 | + "Invalid origin: 'data:text/plain,hi'" |
| 125 | + ); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('accumulation', () => { |
| 130 | + it('should accumulate across calls in insertion order', () => { |
| 131 | + addRemoteOrigin('https://a.example.com'); |
| 132 | + addRemoteOrigin('https://b.example.com'); |
| 133 | + addRemoteOrigin('https://c.example.com'); |
| 134 | + expect(getRuntimeRemoteOrigins()).toEqual([ |
| 135 | + 'https://a.example.com', |
| 136 | + 'https://b.example.com', |
| 137 | + 'https://c.example.com', |
| 138 | + ]); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should not deduplicate when the same origin is added twice', () => { |
| 142 | + addRemoteOrigin('https://example.com'); |
| 143 | + addRemoteOrigin('https://example.com'); |
| 144 | + expect(getRuntimeRemoteOrigins()).toEqual([ |
| 145 | + 'https://example.com', |
| 146 | + 'https://example.com', |
| 147 | + ]); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should not register an origin when validation throws', () => { |
| 151 | + expect(() => addRemoteOrigin('not a url')).toThrow(); |
| 152 | + expect(getRuntimeRemoteOrigins()).toEqual([]); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should preserve previously registered origins when a later call is rejected', () => { |
| 156 | + addRemoteOrigin('https://first.example.com'); |
| 157 | + expect(() => addRemoteOrigin('not a url')).toThrow(); |
| 158 | + expect(getRuntimeRemoteOrigins()).toEqual([ |
| 159 | + 'https://first.example.com', |
| 160 | + ]); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments