mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 07:04:45 -04:00
Fix DoS in CSV parser (#1467)
* Fix DoS in CSV parser Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
553e9f8ead
commit
90ae3c1f76
1 changed files with 17 additions and 1 deletions
|
@ -4,11 +4,27 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a given text as comma separated values (CSV).
|
||||||
|
*
|
||||||
|
* @param csvText The raw csv text
|
||||||
|
* @param csvColumnDelimiter The delimiter for the columns
|
||||||
|
* @return the values splitted by rows and columns
|
||||||
|
*/
|
||||||
export const parseCsv = (csvText: string, csvColumnDelimiter: string): string[][] => {
|
export const parseCsv = (csvText: string, csvColumnDelimiter: string): string[][] => {
|
||||||
const rows = csvText.split('\n')
|
const rows = csvText.split('\n')
|
||||||
if (!rows || rows.length === 0) {
|
if (!rows || rows.length === 0) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
const splitRegex = new RegExp(`${csvColumnDelimiter}(?=(?:[^"]*"[^"]*")*[^"]*$)`)
|
const splitRegex = new RegExp(`${escapeRegexCharacters(csvColumnDelimiter)}(?=(?:[^"]*"[^"]*")*[^"]*$)`)
|
||||||
return rows.filter((row) => row !== '').map((row) => row.split(splitRegex))
|
return rows.filter((row) => row !== '').map((row) => row.split(splitRegex))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escapes regex characters in the given string so it can be used as literal string in another regex.
|
||||||
|
* @param unsafe The unescaped string
|
||||||
|
* @return The escaped string
|
||||||
|
*/
|
||||||
|
const escapeRegexCharacters = (unsafe: string): string => {
|
||||||
|
return unsafe.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue