@@ -34,6 +34,10 @@ test("should fetch alternative origins file from expected URL", async () => {
3434 iiUrl : "https://identity.ic0.app" ,
3535 fetchUrl : `https://${ TEST_CANISTER_ID } .icp0.io/.well-known/ii-alternative-origins` ,
3636 } ,
37+ {
38+ iiUrl : "https://id.ai" ,
39+ fetchUrl : `https://${ TEST_CANISTER_ID } .icp0.io/.well-known/ii-alternative-origins` ,
40+ } ,
3741 {
3842 iiUrl : "https://identity.raw.ic0.app" ,
3943 fetchUrl : `https://${ TEST_CANISTER_ID } .icp0.io/.well-known/ii-alternative-origins` ,
@@ -86,16 +90,6 @@ test("should fetch alternative origins file from expected URL", async () => {
8690 iiUrl : "https://127.0.0.1/?canisterId=222ew-7aaaa-aaaar-akaia-cai" ,
8791 fetchUrl : `https://127.0.0.1/.well-known/ii-alternative-origins?canisterId=${ TEST_CANISTER_ID } ` ,
8892 } ,
89- {
90- iiUrl :
91- "https://totally-custom.com/?canisterId=222ew-7aaaa-aaaar-akaia-cai" ,
92- fetchUrl : `https://totally-custom.com/.well-known/ii-alternative-origins?canisterId=${ TEST_CANISTER_ID } ` ,
93- } ,
94- {
95- iiUrl :
96- "http://totally-custom.com:8080/?canisterId=222ew-7aaaa-aaaar-akaia-cai" ,
97- fetchUrl : `https://totally-custom.com:8080/.well-known/ii-alternative-origins?canisterId=${ TEST_CANISTER_ID } ` ,
98- } ,
9993 ] ;
10094
10195 for ( const { iiUrl, fetchUrl } of testCases ) {
@@ -117,27 +111,6 @@ test("should fetch alternative origins file from expected URL", async () => {
117111 }
118112} ) ;
119113
120- test ( "should fetch alternative origins file using non-raw URL" , async ( ) => {
121- const fetchMock = setupMocks ( {
122- iiUrl : "https://identity.ic0.app" ,
123- response : Response . json ( {
124- alternativeOrigins : [ `https://${ TEST_CANISTER_ID } .raw.ic0.app` ] ,
125- } ) ,
126- } ) ;
127-
128- const result = await validateDerivationOrigin ( {
129- requestOrigin : `https://${ TEST_CANISTER_ID } .raw.ic0.app` ,
130- derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
131- resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
132- } ) ;
133-
134- expect ( result ) . toEqual ( { result : "valid" } ) ;
135- expect ( fetchMock ) . toHaveBeenLastCalledWith (
136- `https://${ TEST_CANISTER_ID } .icp0.io/.well-known/ii-alternative-origins` ,
137- FETCH_OPTS ,
138- ) ;
139- } ) ;
140-
141114test ( "should not validate if canister id resolution fails" , async ( ) => {
142115 const result = await validateDerivationOrigin ( {
143116 requestOrigin : "https://example.com" ,
@@ -147,69 +120,98 @@ test("should not validate if canister id resolution fails", async () => {
147120 expect ( result . result ) . toBe ( "invalid" ) ;
148121} ) ;
149122
150- test ( "should not validate if origin not allowed" , async ( ) => {
151- setupMocks ( {
152- iiUrl : "https://identity.ic0.app" ,
153- response : Response . json ( {
154- alternativeOrigins : [ "https://not-example.com" ] ,
155- } ) ,
156- } ) ;
123+ const validIIUrls = [
124+ "https://identity.ic0.app" ,
125+ "https://identity.internetcomputer.org" ,
126+ "https://id.ai" ,
127+ ] ;
157128
158- const result = await validateDerivationOrigin ( {
159- requestOrigin : "https://example.com" ,
160- derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
161- resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
162- } ) ;
129+ for ( const iiUrl of validIIUrls ) {
130+ test ( "should fetch alternative origins file using non-raw URL" , async ( ) => {
131+ const fetchMock = setupMocks ( {
132+ iiUrl,
133+ response : Response . json ( {
134+ alternativeOrigins : [ `https://${ TEST_CANISTER_ID } .raw.ic0.app` ] ,
135+ } ) ,
136+ } ) ;
163137
164- expect ( result . result ) . toBe ( "invalid" ) ;
165- } ) ;
138+ const result = await validateDerivationOrigin ( {
139+ requestOrigin : `https://${ TEST_CANISTER_ID } .raw.ic0.app` ,
140+ derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
141+ resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
142+ } ) ;
166143
167- test ( "should not validate if alternative origins file malformed" , async ( ) => {
168- setupMocks ( {
169- iiUrl : "https://identity.ic0.app" ,
170- response : Response . json ( {
171- notAlternativeOrigins : [ "https://example.com" ] ,
172- } ) ,
144+ expect ( result ) . toEqual ( { result : "valid" } ) ;
145+ expect ( fetchMock ) . toHaveBeenLastCalledWith (
146+ `https://${ TEST_CANISTER_ID } .icp0.io/.well-known/ii-alternative-origins` ,
147+ FETCH_OPTS ,
148+ ) ;
173149 } ) ;
174150
175- const result = await validateDerivationOrigin ( {
176- requestOrigin : "https://example.com" ,
177- derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
178- resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
179- } ) ;
151+ test ( "should not validate if origin not allowed" , async ( ) => {
152+ setupMocks ( {
153+ iiUrl,
154+ response : Response . json ( {
155+ alternativeOrigins : [ "https://not-example.com" ] ,
156+ } ) ,
157+ } ) ;
180158
181- expect ( result . result ) . toBe ( "invalid" ) ;
182- } ) ;
159+ const result = await validateDerivationOrigin ( {
160+ requestOrigin : "https://example.com" ,
161+ derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
162+ resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
163+ } ) ;
183164
184- test ( "should not validate on alternative origins redirect" , async ( ) => {
185- setupMocks ( {
186- iiUrl : "https://identity.ic0.app" ,
187- response : Response . redirect ( "https://some-evil-url.com" ) ,
165+ expect ( result . result ) . toBe ( "invalid" ) ;
188166 } ) ;
189167
190- const result = await validateDerivationOrigin ( {
191- requestOrigin : "https://example.com" ,
192- derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
193- resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
194- } ) ;
168+ test ( "should not validate if alternative origins file malformed" , async ( ) => {
169+ setupMocks ( {
170+ iiUrl,
171+ response : Response . json ( {
172+ notAlternativeOrigins : [ "https://example.com" ] ,
173+ } ) ,
174+ } ) ;
195175
196- expect ( result . result ) . toBe ( "invalid" ) ;
197- } ) ;
176+ const result = await validateDerivationOrigin ( {
177+ requestOrigin : "https://example.com" ,
178+ derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
179+ resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
180+ } ) ;
198181
199- test ( "should not validate on alternative origins error" , async ( ) => {
200- setupMocks ( {
201- iiUrl : "https://identity.ic0.app" ,
202- response : new Response ( undefined , { status : 404 } ) ,
182+ expect ( result . result ) . toBe ( "invalid" ) ;
203183 } ) ;
204184
205- const result = await validateDerivationOrigin ( {
206- requestOrigin : "https://example.com" ,
207- derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
208- resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
185+ test ( "should not validate on alternative origins redirect" , async ( ) => {
186+ setupMocks ( {
187+ iiUrl,
188+ response : Response . redirect ( "https://some-evil-url.com" ) ,
189+ } ) ;
190+
191+ const result = await validateDerivationOrigin ( {
192+ requestOrigin : "https://example.com" ,
193+ derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
194+ resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
195+ } ) ;
196+
197+ expect ( result . result ) . toBe ( "invalid" ) ;
209198 } ) ;
210199
211- expect ( result . result ) . toBe ( "invalid" ) ;
212- } ) ;
200+ test ( "should not validate on alternative origins error" , async ( ) => {
201+ setupMocks ( {
202+ iiUrl,
203+ response : new Response ( undefined , { status : 404 } ) ,
204+ } ) ;
205+
206+ const result = await validateDerivationOrigin ( {
207+ requestOrigin : "https://example.com" ,
208+ derivationOrigin : "https://some-url.com" , // different from requestOrigin so that we need to fetch the alternative origins
209+ resolveCanisterId : ( ) => Promise . resolve ( { ok : TEST_CANISTER_ID } ) ,
210+ } ) ;
211+
212+ expect ( result . result ) . toBe ( "invalid" ) ;
213+ } ) ;
214+ }
213215
214216const setupMocks = ( {
215217 iiUrl,
0 commit comments