p0wny-shell
p0wny-shell copied to clipboard
An error is thrown if a file contains non UTF-8 characters
File and directory names on Windows might not be UTF-8 encoded and json_encode() fails. This is what I have come up with (around line 100):
$encoded = json_encode($response);
if(!$encoded) {
//replace "unknown" charaters with ?
$i = 0;
foreach($response['stdout'] as $v) {
$response['stdout'][$i] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '?', $v);
$i++;
}
$encoded = json_encode($response);
}
header("Content-Type: application/json");
echo $encoded;
die();
Do not hesitate to make a PR to fix the encoding on Windows :)
A possible workaround to this problem would be to encode the stdout result with base64 or hex to ensure that no invalid characters are encoded with json. The client side would have to decode the json field stdout with base64 or hex and could then output it again. The same procedure is already performed with the files.
@flozz Would this be a desired function as I described?
Yeah probably encoding stdout/stderr to base64 could save us here... Do you think paths (cwd) should be encoded too? :)
Not sure, could be a potential problem. Would this be worth a PR from my side?
Yes please make a PR if you want to implement a fix for this issue :)
Fix released in v2023.05.28
thanks to @cli-ish :)