Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Polyfills/XMLHttpRequest/Source/XMLHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ namespace Babylon::Polyfills::Internal
{
if (m_request.ResponseType() == UrlLib::UrlResponseType::String)
{
return Napi::Value::From(Env(), m_request.ResponseString().data());
const std::string_view responseString{m_request.ResponseString()};
return Napi::String::New(Env(), responseString.data(), responseString.size());
}
else
{
Expand All @@ -134,7 +135,12 @@ namespace Babylon::Polyfills::Internal

Napi::Value XMLHttpRequest::GetResponseText(const Napi::CallbackInfo&)
{
return Napi::Value::From(Env(), m_request.ResponseString().data());
// The body may legitimately contain embedded nulls: Emscripten's EXPORT_ES6 output, for
// example, inlines the .wasm payload as a JavaScript string literal. Passing .data()
// alone would hand a const char* to Napi and truncate at the first null, so the length
// has to be supplied explicitly.
const std::string_view responseString{m_request.ResponseString()};
return Napi::String::New(Env(), responseString.data(), responseString.size());
}

Napi::Value XMLHttpRequest::GetResponseType(const Napi::CallbackInfo&)
Expand Down
Binary file added Tests/UnitTests/Assets/embedded_nulls.txt
Binary file not shown.
8 changes: 8 additions & 0 deletions Tests/UnitTests/Scripts/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ describe("XMLHTTPRequest", function () {
const vertexCount = parseInt(/element vertex (\d+)\n/.exec(header)![1]);
expect(vertexCount).to.equal(18713);
});

it("should not truncate responseText or response at an embedded null byte", async function () {
const xhr = await createRequest("GET", "app:///Assets/embedded_nulls.txt");
expect(xhr.status).to.equal(200);
expect(xhr.responseText).to.equal("start\0middle\0end");
expect(xhr.responseText.length).to.equal(16);
expect(xhr.response).to.equal(xhr.responseText);
});
});

describe("fetch", function () {
Expand Down
Loading