Skip to content

Commit

Permalink
escapeshellarg: Add Windows support (fixes #395)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvz committed Apr 5, 2024
1 parent b819bb6 commit 10fb066
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Released: TBA. [Diff](/~https://github.com/locutusjs/locutus/compare/v2.0.30...mai
- [x] dx: Add Stale Action
- [x] bin2hex: Add support for multi-byte characters (fixes #427)
- [x] var_dump: Detect circular references (fixes #305)
- [x] escapeshellarg: Add Windows support (fixes #395)
- [x] fmod: Fix Uncaught RangeError: toFixed() digits argument must be between 0 and 100 (thx @dekairi, fixes #417)

## v2.0.30
Expand Down
20 changes: 16 additions & 4 deletions src/php/exec/escapeshellarg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ module.exports = function escapeshellarg(arg) {
throw new Error('escapeshellarg(): Argument #1 ($arg) must not contain any null bytes')
}

let ret = ''

ret = arg.replace(/'/g, "'\\''")
// Check if the script is running on Windows
let isWindows = false
if (typeof process !== 'undefined' && process.platform) {
isWindows = process.platform === 'win32'
}
if (typeof window !== 'undefined' && window.navigator.platform) {
isWindows = window.navigator.platform.indexOf('Win') !== -1
}

return "'" + ret + "'"
if (isWindows) {
// Windows escaping strategy
// Double quotes need to be escaped and the whole argument enclosed in double quotes
return '"' + arg.replace(/(["%])/g, '^$1') + '"'
} else {
// Unix-like escaping strategy
return "'" + arg.replace(/'/g, "'\\''") + "'"
}
}

0 comments on commit 10fb066

Please sign in to comment.