Skip to content

Commit

Permalink
fix(core): fix execSync
Browse files Browse the repository at this point in the history
Plain text values are able to escape from the JSON string and break it. If we're going to move a string value we should escape it for now. Later we should just return two different array buffers for {stdout,stderr}
  • Loading branch information
heapwolf committed Apr 6, 2024
1 parent e83dd19 commit c3fe2c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions api/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ export function execSync (command, options) {
code: typeof code === 'string' ? code : null,
signal: errorSignal || null,
status: Number.isFinite(code) ? code : null,
output: [null, stdout.join('\n'), stderr.join('\n')]
output: [null, stdout, stderr]
})

// @ts-ignore
Expand All @@ -673,9 +673,9 @@ export function execSync (command, options) {
throw error
}

const output = options?.encoding === 'utf8'
? stdout.join('\n')
: Buffer.from(stdout.join('\n'))
const output = stdout && options?.encoding === 'utf8'
? stdout
: Buffer.from(stdout)

return output
}
Expand Down
16 changes: 8 additions & 8 deletions src/core/child_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ namespace SSC {
const auto command = args.size() > 0 ? args.at(0) : String("");
const auto argv = join(args.size() > 1 ? Vector<String>{ args.begin() + 1, args.end() } : Vector<String>{}, " ");

auto stdoutBuffer = new JSON::Array{};
auto stderrBuffer = new JSON::Array{};
StringStream* stdoutBuffer = new StringStream;
StringStream* stderrBuffer = new StringStream;

const auto onStdout = [=](const String& output) mutable {
if (!options.allowStdout || output.size() == 0) {
return;
}

if (stdoutBuffer != nullptr) {
stdoutBuffer->push(output);
*stdoutBuffer << String(output);
}
};

Expand All @@ -92,7 +92,7 @@ namespace SSC {
}

if (stderrBuffer != nullptr) {
stderrBuffer->push(output);
*stderrBuffer << String(output);
}
};

Expand All @@ -112,8 +112,8 @@ namespace SSC {
{"data", JSON::Object::Entries {
{"id", std::to_string(id)},
{"pid", std::to_string(pid)},
{"stdout", *stdoutBuffer},
{"stderr", *stderrBuffer},
{"stdout", encodeURIComponent(stdoutBuffer->str())},
{"stderr", encodeURIComponent(stderrBuffer->str())},
{"code", code}
}}
};
Expand Down Expand Up @@ -156,8 +156,8 @@ namespace SSC {
{"err", JSON::Object::Entries {
{"id", std::to_string(id)},
{"pid", std::to_string(pid)},
{"stdout", *stdoutBuffer},
{"stderr", *stderrBuffer},
{"stdout", encodeURIComponent(stdoutBuffer->str())},
{"stderr", encodeURIComponent(stderrBuffer->str())},
{"code", "ETIMEDOUT"}
}}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ipc/bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static void initRouterTable (Router *router) {

if (args.size() == 0 || args.at(0).size() == 0) {
auto json = JSON::Object::Entries {
{"source", "child_process.spawn"},
{"source", "child_process.exec"},
{"err", JSON::Object::Entries {
{"message", "Spawn requires at least one argument with a length greater than zero"},
}}
Expand Down

0 comments on commit c3fe2c8

Please sign in to comment.