-
Notifications
You must be signed in to change notification settings - Fork 58
fix(reporting): escape +, -, tab, CR and LF prefixes in the CSV export #8038
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
4
commits into
main
Choose a base branch
from
fix/csv-formula-injection-escaping
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb877e8
fix(reporting): escape '+', '-' and tab prefixes in the CSV export
yhabib 524059e
test(reporting): add an e2e spec for the CSV formula escaping
yhabib 658e7b0
fix(reporting): escape carriage return and line feed prefixes in the …
yhabib 085856f
test(reporting): guard a null download path in the CSV escaping e2e spec
yhabib 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { AppPo } from "$tests/page-objects/App.page-object"; | ||
| import { PlaywrightPageObjectElement } from "$tests/page-objects/playwright.page-object"; | ||
| import { ReportingTransactionsPo } from "$tests/page-objects/ReportingTransactions.page-object"; | ||
| import { | ||
| disableCssAnimations, | ||
| signInWithNewUser, | ||
| step, | ||
| } from "$tests/utils/e2e.test-utils"; | ||
| import { expect, test } from "@playwright/test"; | ||
| import { readFileSync } from "fs"; | ||
|
|
||
| // A spreadsheet reads a cell that starts with one of these characters as a | ||
| // formula. The CSV export must break the formula with a single quote. | ||
| const FORMULA_CHARACTERS = ["=", "+", "-", "@", "|", "\t", "\r", "\n"]; | ||
|
|
||
| // The account name is the shortest path from the user interface to a CSV cell. | ||
| // The canister limits the name to 24 bytes, so this payload is 22 characters. | ||
| const PAYLOAD_ACCOUNT_NAME = "-2+3+cmd|' /C calc'!A0"; | ||
|
|
||
| // A plain signed number. A spreadsheet reads it as a number, so the export | ||
| // leaves it as it is. | ||
| const SIGNED_NUMBER = /^[+-]\d[\d'.]*$/; | ||
|
|
||
| // The wrapper that the export uses for a neuron id. | ||
| const EXCEL_STRING_FORMULA = /^="\d+"$/; | ||
|
|
||
| /** | ||
| * Splits CSV text into rows of unquoted cells, as a spreadsheet does. | ||
| */ | ||
| const parseCsv = (text: string): string[][] => { | ||
| const rows: string[][] = []; | ||
| let row: string[] = []; | ||
| let cell = ""; | ||
| let inQuotes = false; | ||
|
|
||
| for (let index = 0; index < text.length; index++) { | ||
| const character = text[index]; | ||
|
|
||
| if (inQuotes) { | ||
| if (character === '"') { | ||
| if (text[index + 1] === '"') { | ||
| cell += '"'; | ||
| index++; | ||
| } else { | ||
| inQuotes = false; | ||
| } | ||
| } else { | ||
| cell += character; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (character === '"') { | ||
| inQuotes = true; | ||
| } else if (character === ",") { | ||
| row.push(cell); | ||
| cell = ""; | ||
| } else if (character === "\n") { | ||
| row.push(cell); | ||
| rows.push(row); | ||
| row = []; | ||
| cell = ""; | ||
| } else if (character !== "\r") { | ||
| cell += character; | ||
| } | ||
| } | ||
|
|
||
| row.push(cell); | ||
| rows.push(row); | ||
|
|
||
| return rows; | ||
| }; | ||
|
|
||
| test("Test the CSV export escapes formula characters", async ({ | ||
| page, | ||
| context, | ||
| }) => { | ||
| await page.goto("/accounts"); | ||
| await disableCssAnimations(page); | ||
| await signInWithNewUser({ page, context }); | ||
|
|
||
| const pageElement = PlaywrightPageObjectElement.fromPage(page); | ||
| const appPo = new AppPo(pageElement); | ||
| const accountsPo = appPo.getAccountsPo(); | ||
| const nnsAccountsPo = accountsPo.getNnsAccountsPo(); | ||
| const tokensTablePo = nnsAccountsPo.getTokensTablePo(); | ||
|
|
||
| step("Wait for the main account"); | ||
|
|
||
| const mainAccountRow = await tokensTablePo.getRowByName("Main"); | ||
| await mainAccountRow.waitFor(); | ||
|
|
||
| step("Create a linked account whose name is a spreadsheet formula"); | ||
|
|
||
| await nnsAccountsPo.clickAddAccount(); | ||
|
|
||
| const addAccountModalPo = accountsPo.getAddAccountModalPo(); | ||
| expect(await addAccountModalPo.isPresent()).toBe(true); | ||
|
|
||
| await addAccountModalPo.addAccount(PAYLOAD_ACCOUNT_NAME); | ||
| await addAccountModalPo.waitForClosed(); | ||
|
|
||
| const payloadRow = await tokensTablePo.getRowByName(PAYLOAD_ACCOUNT_NAME); | ||
| await payloadRow.waitFor(); | ||
|
|
||
| step("Get ICP so that the export holds a signed amount"); | ||
|
|
||
| // The accounts page has no menu button. | ||
| await appPo.goBack(); | ||
| await appPo.getIcpTokens(20); | ||
|
|
||
| step("Export the transactions to CSV"); | ||
|
|
||
| await page.goto("/reporting"); | ||
|
|
||
| const reportingTransactionsPo = ReportingTransactionsPo.under(pageElement); | ||
| await reportingTransactionsPo.waitFor(); | ||
|
|
||
| const exportButtonPo = | ||
| reportingTransactionsPo.getReportingTransactionsButtonPo(); | ||
| await exportButtonPo.waitFor(); | ||
|
|
||
| const downloadPromise = page.waitForEvent("download", { timeout: 120_000 }); | ||
| await exportButtonPo.click(); | ||
| const download = await downloadPromise; | ||
|
|
||
| const downloadPath = await download.path(); | ||
| const csvText = readFileSync(downloadPath, "utf-8"); | ||
| const cells = parseCsv(csvText).flat(); | ||
|
|
||
| step("Check that the export escapes every formula cell"); | ||
|
|
||
| // The account name reaches the CSV with a single quote in front of it. | ||
| expect(cells).toContain(`'${PAYLOAD_ACCOUNT_NAME}`); | ||
| expect(cells).not.toContain(PAYLOAD_ACCOUNT_NAME); | ||
|
|
||
| // No cell starts with a formula character, unless it is a plain signed | ||
| // number or the neuron id wrapper. | ||
| const unescaped = cells.filter( | ||
| (cell) => | ||
| FORMULA_CHARACTERS.includes(cell[0]) && | ||
| !SIGNED_NUMBER.test(cell) && | ||
| !EXCEL_STRING_FORMULA.test(cell) | ||
| ); | ||
| expect(unescaped).toEqual([]); | ||
|
|
||
| step("Check that the amount column keeps its sign and no quote"); | ||
|
|
||
| // The export holds the credit of 20 ICP, with its sign and no quote. | ||
| const amounts = cells.filter((cell) => SIGNED_NUMBER.test(cell)); | ||
| expect(amounts.length).toBeGreaterThan(0); | ||
| expect(amounts.some((amount) => /^\+20(\.0+)?$/.test(amount))).toBe(true); | ||
|
|
||
| // No amount cell carries the quote prefix. | ||
| expect(cells.filter((cell) => /^'[+-]\d/.test(cell))).toEqual([]); | ||
| }); | ||
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.