From 55833cf799979e63c6b027fbbf018272308caf5c Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Wed, 1 Mar 2023 08:14:16 +0900 Subject: fix(core): introduce `SafeRegExp` to primordials (#17592) --- ext/fetch/21_formdata.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'ext/fetch/21_formdata.js') diff --git a/ext/fetch/21_formdata.js b/ext/fetch/21_formdata.js index 647c716b3..724511633 100644 --- a/ext/fetch/21_formdata.js +++ b/ext/fetch/21_formdata.js @@ -26,7 +26,9 @@ const { MapPrototypeGet, MapPrototypeSet, MathRandom, + ObjectFreeze, ObjectPrototypeIsPrototypeOf, + SafeRegExp, Symbol, StringFromCharCode, StringPrototypeTrim, @@ -277,19 +279,25 @@ webidl.mixinPairIterable("FormData", FormData, entryList, "name", "value"); webidl.configurePrototype(FormData); const FormDataPrototype = FormData.prototype; -const escape = (str, isFilename) => { - const escapeMap = { - "\n": "%0A", - "\r": "%0D", - '"': "%22", - }; +const ESCAPE_FILENAME_PATTERN = new SafeRegExp(/\r?\n|\r/g); +const ESCAPE_PATTERN = new SafeRegExp(/([\n\r"])/g); +const ESCAPE_MAP = ObjectFreeze({ + "\n": "%0A", + "\r": "%0D", + '"': "%22", +}); +function escape(str, isFilename) { return StringPrototypeReplace( - isFilename ? str : StringPrototypeReplace(str, /\r?\n|\r/g, "\r\n"), - /([\n\r"])/g, - (c) => escapeMap[c], + isFilename + ? str + : StringPrototypeReplace(str, ESCAPE_FILENAME_PATTERN, "\r\n"), + ESCAPE_PATTERN, + (c) => ESCAPE_MAP[c], ); -}; +} + +const FORM_DETA_SERIALIZE_PATTERN = new SafeRegExp(/\r(?!\n)|(?