Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ <h1>Agent-JS Changelog</h1>

<section>
<h2>Version x.x.x</h2>
<ul>
<li></li>
</ul>
<h2>Version 0.19.2</h2>
<ul>
<li>
fix: subdomains on icp0.io and ic0.app were incorrectly sending requests to icp-api and
encountering CSP issues
</li>
</ul>
<h2>Version 0.19.1</h2>
<ul>
<li>fix: default host logic fixed and tests added</li>
</ul>
Expand Down
29 changes: 28 additions & 1 deletion packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ test('should fetch with given call options and fetch options', async () => {
describe('default host', () => {
it('should use a default host of icp-api.io', () => {
const agent = new HttpAgent({ fetch: jest.fn() });
window.location.hostname; //?
expect((agent as any)._host.hostname).toBe('icp-api.io');
});
it('should use a default of icp-api.io if location is not available', () => {
Expand All @@ -783,4 +782,32 @@ describe('default host', () => {
expect((agent as any)._host.hostname).toBe(host);
}
});
it('should correctly handle subdomains on known hosts', () => {
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
host: `foo.${host}`,
hostname: `rrkah-fqaaa-aaaaa-aaaaq-cai.${host}`,
protocol: 'https:',
} as any;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe(host);
}
});
it('should handle port numbers for localhost', () => {
const knownHosts = ['localhost', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
// hostname is different from host when port is specified
window.location = {
host: `${host}:4943`,
hostname: `${host}`,
protocol: 'http:',
port: '4943',
} as any;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe(host);
}
});
});
12 changes: 10 additions & 2 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,17 @@ export class HttpAgent implements Agent {
}
// Mainnet and local will have the api route available
const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1'];
if (location && knownHosts.includes(location.hostname)) {
const hostname = location?.hostname;
let knownHost;
if (hostname && typeof hostname === 'string') {
knownHost = knownHosts.find(host => hostname.endsWith(host));
}

if (location && knownHost) {
// If the user is on a boundary-node provided host, we can use the same host for the agent
this._host = new URL(location + '');
this._host = new URL(
`${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`,
);
} else {
this._host = new URL('https://icp-api.io');
console.warn(
Expand Down
3 changes: 3 additions & 0 deletions packages/auth-client/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ describe('Auth Client', () => {
(window as any).location = {
reload: jest.fn(),
fetch,
hostname: 'localhost',
protocol: 'http:',
port: '4943',
toString: jest.fn(() => 'http://localhost:4943'),
};

Expand Down