fix issue where auth:export doesn't escape double quotes for CSV format#10563
Conversation
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where auth:export does not properly escape double quotes for the CSV format. It renames escapeComma to escapeCsv and updates it to handle both commas and double quotes (by doubling them and wrapping the field in quotes). A corresponding unit test has been added to src/accountExporter.spec.ts. The reviewer pointed out that fields containing line breaks (\n or \r) must also be enclosed in double quotes to prevent breaking the CSV structure, and provided a code suggestion to address this.
| function escapeCsv(str: string): string { | ||
| if (str.includes(",") || str.includes('"')) { | ||
| // Encapsulate the string with quotes if it contains a comma. | ||
| return `"${str}"`; | ||
| // Also escape any existing quotes by doubling them. | ||
| return `"${str.replace(/"/g, '""')}"`; | ||
| } | ||
| return str; | ||
| } |
There was a problem hiding this comment.
In CSV format, fields containing line breaks (newlines like \n or \r) must also be enclosed in double quotes, just like fields containing commas or double quotes. If a user's display name contains a newline, not escaping it will break the CSV structure by introducing an unexpected new row. We should update escapeCsv to also check for \n and \r.
| function escapeCsv(str: string): string { | |
| if (str.includes(",") || str.includes('"')) { | |
| // Encapsulate the string with quotes if it contains a comma. | |
| return `"${str}"`; | |
| // Also escape any existing quotes by doubling them. | |
| return `"${str.replace(/"/g, '""')}"`; | |
| } | |
| return str; | |
| } | |
| function escapeCsv(str: string): string { | |
| if (str.includes(",") || str.includes('"') || str.includes("\n") || str.includes("\r")) { | |
| // Encapsulate the string with quotes if it contains a comma, quote, or newline. | |
| // Also escape any existing quotes by doubling them. | |
| return "\"" + str.replace(/"/g, '""') + "\""; | |
| } | |
| return str; | |
| } |
Description
Fixes #3484. from what I can tell, the issue was just double quotes
", since we wrap the entire string in double quotes, if thedisplayNamehas double quotes it would be escaped earlyScenarios Tested
Ran
firebase auth:export --format csv --project PROJECT_ID users.csvand verified that user data is exported correctlySample Commands