Skip to content

Commit ce82a75

Browse files
Potential fix for code scanning alert no. 5: Incomplete string escaping or encoding
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 847082c commit ce82a75

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

config/webpack-sprite-hash-plugin.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class SpriteHashPlugin {
2222
}
2323
}
2424

25+
/**
26+
* Escapes a string for safe use inside a PHP single-quoted string literal.
27+
*
28+
* @param {string} str Input string.
29+
* @return {string} Escaped string.
30+
*/
31+
_escapePhpSingleQuoted(str) {
32+
return String(str).replace(/\\/g, '\\\\').replace(/'/g, "\\'")
33+
}
34+
2535
/**
2636
* Formats a plain object as a PHP associative array string.
2737
*
@@ -30,8 +40,8 @@ class SpriteHashPlugin {
3040
*/
3141
formatPhpArray(obj) {
3242
const entries = Object.entries(obj).map(([key, value]) => {
33-
const escapedKey = key.replace(/'/g, "\\'")
34-
const escapedValue = String(value).replace(/'/g, "\\'")
43+
const escapedKey = this._escapePhpSingleQuoted(key)
44+
const escapedValue = this._escapePhpSingleQuoted(value)
3545
return `\t'${escapedKey}' => '${escapedValue}'`
3646
})
3747
return `array(\n${entries.join(',\n')}\n)`

0 commit comments

Comments
 (0)