Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data corruption in protocol.handle #41931

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/browser/api/protocol.ts
Expand Up @@ -18,7 +18,7 @@ function makeStreamFromPipe (pipe: any): ReadableStream {
try {
const rv = await pipe.read(buf);
if (rv > 0) {
controller.enqueue(buf.subarray(0, rv));
controller.enqueue(buf.slice(0, rv));
} else {
controller.close();
}
Expand Down
43 changes: 43 additions & 0 deletions spec/api-protocol-spec.ts
Expand Up @@ -1379,6 +1379,49 @@ describe('protocol module', () => {
expect(body).to.equal(text);
});

it('can receive stream request body asynchronously', async () => {
let done: any;
const requestReceived: Promise<Buffer[]> = new Promise(resolve => { done = resolve; });
protocol.handle('http-like', async (req) => {
const chunks = [];
for await (const chunk of (req.body as any)) {
chunks.push(chunk);
}
done(chunks);
return new Response('ok');
});
defer(() => { protocol.unhandle('http-like'); });
const w = new BrowserWindow({ show: false });
w.loadURL('about:blank');
const expectedHashChunks = await w.webContents.executeJavaScript(`
const dataStream = () =>
new ReadableStream({
async start(controller) {
for (let i = 0; i < 10; i++) { controller.enqueue(Array(1024 * 128).fill(+i).join("\\n")); }
controller.close();
},
}).pipeThrough(new TextEncoderStream());
fetch(
new Request("http-like://host", {
method: "POST",
body: dataStream(),
duplex: "half",
})
);
(async () => {
const chunks = []
for await (const chunk of dataStream()) {
chunks.push(chunk);
}
return chunks;
})()
`);
const expectedHash = Buffer.from(await crypto.subtle.digest('SHA-256', Buffer.concat(expectedHashChunks))).toString('hex');
const body = Buffer.concat(await requestReceived);
const actualHash = Buffer.from(await crypto.subtle.digest('SHA-256', Buffer.from(body))).toString('hex');
expect(actualHash).to.equal(expectedHash);
});

it('can receive multi-part postData from loadURL', async () => {
protocol.handle('test-scheme', (req) => new Response(req.body));
defer(() => { protocol.unhandle('test-scheme'); });
Expand Down
1 change: 1 addition & 0 deletions spec/index.js
Expand Up @@ -40,6 +40,7 @@ protocol.registerSchemesAsPrivileged([
{ scheme: global.standardScheme, privileges: { standard: true, secure: true, stream: false } },
{ scheme: global.zoomScheme, privileges: { standard: true, secure: true } },
{ scheme: global.serviceWorkerScheme, privileges: { allowServiceWorkers: true, standard: true, secure: true } },
{ scheme: 'http-like', privileges: { standard: true, secure: true, corsEnabled: true, supportFetchAPI: true } },
{ scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
{ scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
{ scheme: 'no-cors', privileges: { supportFetchAPI: true } },
Expand Down