-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: escape special characters in filenames (#1147)
* fix: escape special characters in zip filenames * Applied filename substitution to individual asset exports, tweaked client regex when saving filename to prevent it from cutting off the suggested filename after a semicolon (semicolon is used in the filename character substitutions) * tests for filename reserved chars substitution * removed label property from test asset creation --------- Co-authored-by: csmig <csmig@csmig.com> Co-authored-by: cd-rite <61710958+cd-rite@users.noreply.github.com>
- Loading branch information
1 parent
30d8daf
commit 698a94f
Showing
6 changed files
with
499 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,31 +1,65 @@ | ||
|
||
|
||
/** | ||
* Regex matches characters that need to be escaped in XML. | ||
* @type {RegExp} | ||
* Escapes XML reserved characters with named entity references. | ||
* @param {string} value - The string to escape. | ||
* @returns {string} The escaped string. | ||
*/ | ||
const regexEscapeXml = /["&'<>]/g | ||
module.exports.escapeForXml = function (name, value) { | ||
/** | ||
* Regex matches characters that need to be escaped in XML. | ||
* @type {RegExp} | ||
*/ | ||
const regexEscapeXml = /["&'<>]/g | ||
|
||
/** | ||
* Map of characters to their corresponding named XML entities. | ||
* @type {Object.<string, string>} | ||
*/ | ||
const escapeMapXml = { | ||
'"': '"', | ||
'&': '&', | ||
'\'': ''', | ||
'<': '<', | ||
'>': '>' | ||
}; | ||
/** | ||
* Map of characters to their corresponding named XML entities. | ||
* @type {Object.<string, string>} | ||
*/ | ||
const escapeMapXml = { | ||
'"': '"', | ||
'&': '&', | ||
'\'': ''', | ||
'<': '<', | ||
'>': '>' | ||
} | ||
return value.toString().replace(regexEscapeXml, function ($0) { | ||
return escapeMapXml[$0] | ||
}) | ||
} | ||
|
||
/** | ||
* Escapes XML reserved characters with named entity references. | ||
* Escapes filesystem reserved characters with named entity references. | ||
* @param {string} value - The string to escape. | ||
* @returns {string} The escaped string. | ||
*/ | ||
module.exports.escapeForXml = function(name, value) { | ||
return value.toString().replace(regexEscapeXml, function($0) { | ||
return escapeMapXml[$0] | ||
}); | ||
module.exports.escapeFilename = function (value) { | ||
/** | ||
* Regexes match characters that need to be escaped in filenames. | ||
* @type {RegExp} | ||
*/ | ||
const osReserved = /[\/\\:\*"\?<>\|]/g | ||
const controlChars = /[\x00-\x1f]/g | ||
|
||
/** | ||
* Map of characters to their corresponding named HTML entities. | ||
* @type {Object.<string, string>} | ||
*/ | ||
const osReserveReplace = { | ||
'/': '/', | ||
'\\': '\', | ||
':': ':', | ||
'*': '*', | ||
'"': '"', | ||
'?': '?', | ||
'<': '<', | ||
'>': '>', | ||
'|': '|', | ||
} | ||
|
||
return value.toString() | ||
.replace(osReserved, (match) => osReserveReplace[match]) | ||
.replace(controlChars, (match) => `&#x${match.charCodeAt(0).toString().padStart(2,'0')};`) | ||
.substring(0, 255) | ||
} | ||
|
This file contains 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 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.