-
Notifications
You must be signed in to change notification settings - Fork 96
fix(agent): check if canister is in ranges for certificates without delegation #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af1fd0b
test: explicit root key for mock replica
ilbertt 7f1f5e3
refactor: remove duplicate function
ilbertt 55a11ca
fix: check if canister is in ranges for certificate without delegation
ilbertt ee21b8a
Merge remote-tracking branch 'origin/main' into luca/test-root-key
ilbertt c48d800
chore: update changelog
ilbertt 7a35ef2
fix: return value
ilbertt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { vi, expect, it, beforeEach, describe } from 'vitest'; | ||
| import { CertificateNotAuthorizedErrorCode, HttpAgent, TrustError } from '@icp-sdk/core/agent'; | ||
| import { Principal } from '@icp-sdk/core/principal'; | ||
| import { | ||
| MockReplica, | ||
| prepareV3ReadStateResponse, | ||
| prepareV3ReadStateRootSubnetResponse, | ||
| } from '../utils/mock-replica.ts'; | ||
| import { randomIdentity, randomKeyPair } from '../utils/identity.ts'; | ||
|
|
||
| describe('fetchSubnetKeys (root key, no delegation)', () => { | ||
| const now = new Date('2025-12-16T06:34:56.789Z'); | ||
| const canisterId = Principal.fromText('v2nog-2aaaa-aaaab-p777q-cai'); | ||
|
|
||
| const rootSubnetKeyPair = randomKeyPair(); | ||
| const nodeIdentity = randomIdentity(); | ||
| const identity = randomIdentity(); | ||
|
|
||
| let mockReplica: MockReplica; | ||
|
|
||
| beforeEach(async () => { | ||
| mockReplica = await MockReplica.create(); | ||
|
|
||
| vi.setSystemTime(now); | ||
| }); | ||
|
|
||
| it('should verify that the canister is in the allowed canister ranges', async () => { | ||
| const agent = await HttpAgent.create({ | ||
| host: mockReplica.address, | ||
| rootKey: rootSubnetKeyPair.publicKeyDer, | ||
| identity, | ||
| }); | ||
|
|
||
| const { responseBody: readStateResponseBody } = await prepareV3ReadStateRootSubnetResponse({ | ||
| nodeIdentity, | ||
| canisterRanges: [[canisterId.toUint8Array(), canisterId.toUint8Array()]], | ||
| rootSubnetKeyPair, | ||
| date: now, | ||
| }); | ||
| mockReplica.setV3ReadStateSpyImplOnce(canisterId.toString(), (_req, res) => { | ||
| res.status(200).send(readStateResponseBody); | ||
| }); | ||
|
|
||
| const nodeKeys = await agent.fetchSubnetKeys(canisterId); | ||
|
|
||
| const expectedNodeKey = nodeIdentity.getPublicKey().toDer(); | ||
| expect(nodeKeys.get(nodeIdentity.getPrincipal().toText())).toEqual(expectedNodeKey); | ||
| expect(mockReplica.getV3ReadStateSpy(canisterId.toString())).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should throw if the canister is not in the allowed canister ranges', async () => { | ||
| const agent = await HttpAgent.create({ | ||
| host: mockReplica.address, | ||
| rootKey: rootSubnetKeyPair.publicKeyDer, | ||
| identity, | ||
| }); | ||
| const anotherCanisterId = Principal.fromText('jrlun-jiaaa-aaaab-aaaaa-cai'); | ||
|
|
||
| const { responseBody: readStateResponseBody } = await prepareV3ReadStateRootSubnetResponse({ | ||
| nodeIdentity, | ||
| canisterRanges: [[canisterId.toUint8Array(), canisterId.toUint8Array()]], | ||
| rootSubnetKeyPair, | ||
| date: now, | ||
| }); | ||
| mockReplica.setV3ReadStateSpyImplOnce(anotherCanisterId.toString(), (_req, res) => { | ||
| res.status(200).send(readStateResponseBody); | ||
| }); | ||
|
|
||
| await expect(agent.fetchSubnetKeys(anotherCanisterId)).rejects.toThrow( | ||
| TrustError.fromCode( | ||
| new CertificateNotAuthorizedErrorCode( | ||
| anotherCanisterId, | ||
| Principal.selfAuthenticating(rootSubnetKeyPair.publicKeyDer), | ||
| ), | ||
| ), | ||
| ); | ||
| expect(mockReplica.getV3ReadStateSpy(anotherCanisterId.toString())).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchSubnetKeys (delegated subnet)', () => { | ||
| const now = new Date('2025-12-16T06:34:56.789Z'); | ||
| const canisterId = Principal.fromText('v2nog-2aaaa-aaaab-p777q-cai'); | ||
|
|
||
| const rootSubnetKeyPair = randomKeyPair(); | ||
| const subnetKeyPair = randomKeyPair(); | ||
| const nodeIdentity = randomIdentity(); | ||
| const identity = randomIdentity(); | ||
|
|
||
| let mockReplica: MockReplica; | ||
|
|
||
| beforeEach(async () => { | ||
| mockReplica = await MockReplica.create(); | ||
|
|
||
| vi.setSystemTime(now); | ||
| }); | ||
|
|
||
| it('should verify that the canister is in the allowed canister ranges', async () => { | ||
| const agent = await HttpAgent.create({ | ||
| host: mockReplica.address, | ||
| rootKey: rootSubnetKeyPair.publicKeyDer, | ||
| identity, | ||
| }); | ||
|
|
||
| const { responseBody: readStateResponseBody } = await prepareV3ReadStateResponse({ | ||
| nodeIdentity, | ||
| canisterRanges: [[canisterId.toUint8Array(), canisterId.toUint8Array()]], | ||
| rootSubnetKeyPair, | ||
| keyPair: subnetKeyPair, | ||
| date: now, | ||
| }); | ||
| mockReplica.setV3ReadStateSpyImplOnce(canisterId.toString(), (_req, res) => { | ||
| res.status(200).send(readStateResponseBody); | ||
| }); | ||
|
|
||
| const nodeKeys = await agent.fetchSubnetKeys(canisterId); | ||
|
|
||
| const expectedNodeKey = nodeIdentity.getPublicKey().toDer(); | ||
| expect(nodeKeys.get(nodeIdentity.getPrincipal().toText())).toEqual(expectedNodeKey); | ||
| expect(mockReplica.getV3ReadStateSpy(canisterId.toString())).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should throw if the canister is not in the allowed canister ranges', async () => { | ||
| const agent = await HttpAgent.create({ | ||
| host: mockReplica.address, | ||
| rootKey: rootSubnetKeyPair.publicKeyDer, | ||
| identity, | ||
| }); | ||
| const anotherCanisterId = Principal.fromText('jrlun-jiaaa-aaaab-aaaaa-cai'); | ||
|
|
||
| const { responseBody: readStateResponseBody } = await prepareV3ReadStateResponse({ | ||
| nodeIdentity, | ||
| canisterRanges: [[canisterId.toUint8Array(), canisterId.toUint8Array()]], | ||
| rootSubnetKeyPair, | ||
| keyPair: subnetKeyPair, | ||
| date: now, | ||
| }); | ||
| mockReplica.setV3ReadStateSpyImplOnce(anotherCanisterId.toString(), (_req, res) => { | ||
| res.status(200).send(readStateResponseBody); | ||
| }); | ||
|
|
||
| await expect(agent.fetchSubnetKeys(anotherCanisterId)).rejects.toThrow( | ||
| TrustError.fromCode( | ||
| new CertificateNotAuthorizedErrorCode( | ||
| anotherCanisterId, | ||
| Principal.selfAuthenticating(subnetKeyPair.publicKeyDer), | ||
| ), | ||
| ), | ||
| ); | ||
| expect(mockReplica.getV3ReadStateSpy(anotherCanisterId.toString())).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.