Skip to content

Commit da5be58

Browse files
oggy-dfinkrpeacock
andauthored
fix(agent): Check subnet canister ranges (#580)
* Verify the canister subnet ranges for a certificate * Don't check subnet ranges for reading management canister state * Update usage of Certificate.verify in e2e test * Fix how root keys are determined * Change the expected error in the MITM test Co-authored-by: Kyle Peacock <kylpeacock@gmail.com>
1 parent 6eb97f2 commit da5be58

File tree

10 files changed

+332
-110
lines changed

10 files changed

+332
-110
lines changed

docs/generated/changelog.html

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

1212
<section>
13+
<h2>Version 0.12.0</h2>
14+
<ul>
15+
<li>
16+
Changed the certificate verification interface and fixed its logic. The public constructor
17+
is now static and asynchronous. There is no separate verification method, the check is
18+
done automatically in the constructor and newly also checks that the delegation is
19+
authoritative for the given canister ID, as required by the Internet Computer interface
20+
specification.
21+
</li>
22+
</ul>
1323
<h2>Version 0.11.2</h2>
1424
<ul>
1525
<li>

e2e/node/basic/basic.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ test('read_state', async () => {
1010
const resolvedAgent = await agent;
1111
const now = Date.now() / 1000;
1212
const path = [new TextEncoder().encode('time')];
13-
const response = await resolvedAgent.readState(Principal.fromHex('00000000000000000001'), {
13+
const canisterId = Principal.fromHex('00000000000000000001');
14+
const response = await resolvedAgent.readState(canisterId, {
1415
paths: [path],
1516
});
16-
const cert = new Certificate(response, resolvedAgent);
17-
18-
expect(() => cert.lookup(path)).toThrow(/Cannot lookup unverified certificate/);
19-
expect(await cert.verify()).toBe(true);
17+
if (resolvedAgent.rootKey == null) throw new Error(`The agent doesn't have a root key yet`);
18+
const cert = await Certificate.create({
19+
certificate: response.certificate,
20+
rootKey: resolvedAgent.rootKey,
21+
canisterId: canisterId,
22+
});
2023
expect(cert.lookup([new TextEncoder().encode('Time')])).toBe(undefined);
2124
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2225
const rawTime = cert.lookup(path)!;

e2e/node/basic/mitm.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ if (!process.env['MITM']) {
1111
jest.setTimeout(30000);
1212
mitmTest('mitm greet', async () => {
1313
const { actor: counter } = await counterCanister();
14-
await expect(counter.greet('counter')).rejects.toThrow(/Fail to verify certificate/);
14+
await expect(counter.greet('counter')).rejects.toThrow(/Invalid certificate/);
1515
expect(await counter.queryGreet('counter')).toEqual('Hullo, counter!');
1616
});

package-lock.json

Lines changed: 58 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/agent/src/canisterStatus/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,11 @@ export const request = async (options: {
8787
const response = await agent.readState(canisterId, {
8888
paths: [encodedPaths[index]],
8989
});
90-
const cert = new Certificate(response, agent);
91-
const verified = await cert.verify();
92-
if (!verified) {
93-
throw new Error(
94-
'There was a problem certifying the response data. Please verify your connection to the mainnet, or be sure to call fetchRootKey on your agent if you are developing locally',
95-
);
96-
}
90+
const cert = await Certificate.create({
91+
certificate: response.certificate,
92+
rootKey: agent.rootKey,
93+
canisterId: canisterId,
94+
});
9795

9896
const data = cert.lookup(encodePath(uniquePaths[index], canisterId));
9997
if (!data) {

0 commit comments

Comments
 (0)