1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/fs): truncate files when a ReadableStream is passed to writeFile (#23330)

Closes #19697. This fixes a bug where the writeFile API can create
partially-overwritten files which may lead to invalid / corrupt files or
data leakage. It also aligns the behavior of writing a ReadableStream
and writing a Uint8Array to the disk.
This commit is contained in:
charlotte ✨ 2024-05-27 23:14:35 +01:00 committed by GitHub
parent 35e5159c8d
commit 506c275053
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View file

@ -926,6 +926,7 @@ async function writeFile(
append: options.append ?? false,
create: options.create ?? true,
createNew: options.createNew ?? false,
truncate: !(options.append ?? false),
write: true,
});
await data.pipeTo(file.writable, {

View file

@ -425,3 +425,21 @@ Deno.test(
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function overwriteFileWithStream() {
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeFile(filename, new Uint8Array([1, 2, 3, 4]));
const stream = new ReadableStream({
pull(controller) {
controller.enqueue(new Uint8Array([1]));
controller.enqueue(new Uint8Array([2]));
controller.close();
},
});
await Deno.writeFile(filename, stream);
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
},
);