-
Notifications
You must be signed in to change notification settings - Fork 58
fix(imported-tokens): validate the entered canisters anonymously #8039
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
Open
yhabib
wants to merge
3
commits into
main
Choose a base branch
from
fix/import-token-anonymous-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
150 changes: 150 additions & 0 deletions
150
frontend/src/tests/e2e/import-token-anonymous-validation.spec.ts
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,150 @@ | ||
| import { AppPo } from "$tests/page-objects/App.page-object"; | ||
| import { PlaywrightPageObjectElement } from "$tests/page-objects/playwright.page-object"; | ||
| import { | ||
| closeHighlight, | ||
| dfxCanisterId, | ||
| disableCssAnimations, | ||
| signInWithNewUser, | ||
| step, | ||
| } from "$tests/utils/e2e.test-utils"; | ||
| import { expect, test, type Request } from "@playwright/test"; | ||
|
|
||
| const TEST_TOKEN_NAME = "ckRED"; | ||
|
|
||
| // The IC request envelope is CBOR. These two markers read the envelope without | ||
| // a CBOR parser. | ||
| // | ||
| // ANONYMOUS_SENDER is the field `sender` set to the anonymous principal: | ||
| // text(6) "sender" (0x66 + "sender") followed by bytes(1) 0x04. | ||
| const ANONYMOUS_SENDER = "6673656e6465724104"; | ||
| // agent-js adds `sender_pubkey` and `sender_sig` only when an identity signs | ||
| // the envelope. An anonymous envelope carries neither field. | ||
| const SIGNED_MARKER = Buffer.from("sender_pubkey", "utf8").toString("hex"); | ||
|
|
||
| type IcRequest = { | ||
| url: string; | ||
| bodyHex: string; | ||
| }; | ||
|
|
||
| const isSigned = (request: IcRequest): boolean => | ||
| request.bodyHex.includes(SIGNED_MARKER); | ||
|
|
||
| // This test covers finding 22: an import-token deep link must not send the user | ||
| // principal to the canisters that the URL names. The user chose neither | ||
| // canister ID, and the modal calls both of them with no click. | ||
| test("Import token deep link validates the URL canisters anonymously", async ({ | ||
| page, | ||
| context, | ||
| }) => { | ||
| const ledgerCanisterId = await dfxCanisterId("ckred_ledger"); | ||
| const indexCanisterId = await dfxCanisterId("ckred_index"); | ||
| const nnsDappCanisterId = await dfxCanisterId("nns-dapp"); | ||
|
|
||
| await page.goto("/tokens"); | ||
| await disableCssAnimations(page); | ||
| await signInWithNewUser({ page, context }); | ||
|
|
||
| const requests: IcRequest[] = []; | ||
| const recordRequest = (request: Request) => { | ||
| const url = request.url(); | ||
| if (!url.includes("/api/v")) return; | ||
| requests.push({ | ||
| url, | ||
| bodyHex: (request.postDataBuffer() ?? Buffer.alloc(0)).toString("hex"), | ||
| }); | ||
| }; | ||
| const requestsTo = (canisterId: string): IcRequest[] => | ||
| requests.filter((request) => | ||
| request.url.includes(`/canister/${canisterId}/`) | ||
| ); | ||
|
|
||
| step("Open the import token deep link"); | ||
|
|
||
| page.on("request", recordRequest); | ||
|
|
||
| await page.goto( | ||
| `/tokens/?import-ledger-id=${ledgerCanisterId}&import-index-id=${indexCanisterId}` | ||
| ); | ||
| await disableCssAnimations(page); | ||
| await closeHighlight(page); | ||
|
|
||
| const pageElement = PlaywrightPageObjectElement.fromPage(page); | ||
| const appPo = new AppPo(pageElement); | ||
| const importTokenModalPo = appPo | ||
| .getTokensPo() | ||
| .getTokensPagePo() | ||
| .getImportTokenModalPo(); | ||
| const reviewPo = importTokenModalPo.getImportTokenReviewPo(); | ||
|
|
||
| step("The review step opens without a click"); | ||
|
|
||
| await importTokenModalPo.waitFor(); | ||
| await reviewPo.waitFor(); | ||
|
|
||
| expect(await reviewPo.getTokenName()).toBe(TEST_TOKEN_NAME); | ||
| expect(await reviewPo.getLedgerCanisterIdPo().getCanisterIdText()).toBe( | ||
| ledgerCanisterId | ||
| ); | ||
| expect(await reviewPo.getIndexCanisterIdPo().getCanisterIdText()).toBe( | ||
| indexCanisterId | ||
| ); | ||
|
|
||
| // Everything above happened without a click. Stop recording here, because | ||
| // the calls after the confirmation are allowed to carry the user identity. | ||
| page.off("request", recordRequest); | ||
|
|
||
| step("The zero click calls reach both URL canisters"); | ||
|
|
||
| const ledgerRequests = requestsTo(ledgerCanisterId); | ||
| const indexRequests = requestsTo(indexCanisterId); | ||
| expect(ledgerRequests.length).toBeGreaterThan(0); | ||
| expect(indexRequests.length).toBeGreaterThan(0); | ||
|
|
||
| step("The session is signed in, and a signed envelope is detectable"); | ||
|
|
||
| // Control for the two assertions below. The dapp loads the imported tokens | ||
| // from its own backend canister in the same window, with the user identity. | ||
| // If this call carried no signature, the checks below would pass for the | ||
| // wrong reason. | ||
| const nnsDappRequests = requestsTo(nnsDappCanisterId); | ||
| expect(nnsDappRequests.length).toBeGreaterThan(0); | ||
| expect(nnsDappRequests.filter(isSigned).length).toBeGreaterThan(0); | ||
|
|
||
| step("No zero click call to the URL canisters carries the user identity"); | ||
|
|
||
| const urlCanisterRequests = [...ledgerRequests, ...indexRequests]; | ||
| expect(urlCanisterRequests.filter(isSigned).map(({ url }) => url)).toEqual( | ||
| [] | ||
| ); | ||
| expect( | ||
| urlCanisterRequests | ||
| .filter(({ bodyHex }) => !bodyHex.includes(ANONYMOUS_SENDER)) | ||
| .map(({ url }) => url) | ||
| ).toEqual([]); | ||
|
|
||
| step("Confirm still imports the token with the user identity"); | ||
|
|
||
| await reviewPo.getConfirmButtonPo().click(); | ||
|
|
||
| const walletPo = appPo.getWalletPo().getIcrcWalletPo(); | ||
| await walletPo.waitFor(); | ||
| expect( | ||
| await walletPo.getWalletPageHeaderPo().getUniverseSummaryPo().getTitle() | ||
| ).toEqual(TEST_TOKEN_NAME); | ||
|
|
||
| step("The imported token is present in the tokens table"); | ||
|
|
||
| await appPo.goBack(); | ||
| await appPo | ||
| .getTokensPo() | ||
| .getTokensPagePo() | ||
| .getImportedTokensTable() | ||
| .waitFor(); | ||
| expect( | ||
| await appPo | ||
| .getTokensPo() | ||
| .getTokensPagePo() | ||
| .getImportedTokensTable() | ||
| .getTokenNames() | ||
| ).toContain(TEST_TOKEN_NAME); | ||
| }); |
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.