Skip to content

Commit 2e6584e

Browse files
authored
fix: evaluates subdomains correctly when determining known hosts (#757)
* fix: subdomains on icp0.io correctly point to / * test cases for ports, verified on local deploy
1 parent ca2a1ad commit 2e6584e

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

docs/generated/changelog.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ <h1>Agent-JS Changelog</h1>
1111

1212
<section>
1313
<h2>Version x.x.x</h2>
14+
<ul>
15+
<li></li>
16+
</ul>
17+
<h2>Version 0.19.2</h2>
18+
<ul>
19+
<li>
20+
fix: subdomains on icp0.io and ic0.app were incorrectly sending requests to icp-api and
21+
encountering CSP issues
22+
</li>
23+
</ul>
24+
<h2>Version 0.19.1</h2>
1425
<ul>
1526
<li>fix: default host logic fixed and tests added</li>
1627
</ul>

packages/agent/src/agent/http/http.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ test('should fetch with given call options and fetch options', async () => {
763763
describe('default host', () => {
764764
it('should use a default host of icp-api.io', () => {
765765
const agent = new HttpAgent({ fetch: jest.fn() });
766-
window.location.hostname; //?
767766
expect((agent as any)._host.hostname).toBe('icp-api.io');
768767
});
769768
it('should use a default of icp-api.io if location is not available', () => {
@@ -783,4 +782,32 @@ describe('default host', () => {
783782
expect((agent as any)._host.hostname).toBe(host);
784783
}
785784
});
785+
it('should correctly handle subdomains on known hosts', () => {
786+
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
787+
for (const host of knownHosts) {
788+
delete window.location;
789+
window.location = {
790+
host: `foo.${host}`,
791+
hostname: `rrkah-fqaaa-aaaaa-aaaaq-cai.${host}`,
792+
protocol: 'https:',
793+
} as any;
794+
const agent = new HttpAgent({ fetch: jest.fn() });
795+
expect((agent as any)._host.hostname).toBe(host);
796+
}
797+
});
798+
it('should handle port numbers for localhost', () => {
799+
const knownHosts = ['localhost', '127.0.0.1'];
800+
for (const host of knownHosts) {
801+
delete window.location;
802+
// hostname is different from host when port is specified
803+
window.location = {
804+
host: `${host}:4943`,
805+
hostname: `${host}`,
806+
protocol: 'http:',
807+
port: '4943',
808+
} as any;
809+
const agent = new HttpAgent({ fetch: jest.fn() });
810+
expect((agent as any)._host.hostname).toBe(host);
811+
}
812+
});
786813
});

packages/agent/src/agent/http/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,17 @@ export class HttpAgent implements Agent {
212212
}
213213
// Mainnet and local will have the api route available
214214
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
215-
if (location && knownHosts.includes(location.hostname)) {
215+
const hostname = location?.hostname;
216+
let knownHost;
217+
if (hostname && typeof hostname === 'string') {
218+
knownHost = knownHosts.find(host => hostname.endsWith(host));
219+
}
220+
221+
if (location && knownHost) {
216222
// If the user is on a boundary-node provided host, we can use the same host for the agent
217-
this._host = new URL(location + '');
223+
this._host = new URL(
224+
`${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`,
225+
);
218226
} else {
219227
this._host = new URL('https://icp-api.io');
220228
console.warn(

packages/auth-client/src/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ describe('Auth Client', () => {
8484
(window as any).location = {
8585
reload: jest.fn(),
8686
fetch,
87+
hostname: 'localhost',
88+
protocol: 'http:',
89+
port: '4943',
8790
toString: jest.fn(() => 'http://localhost:4943'),
8891
};
8992

0 commit comments

Comments
 (0)