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(ext/fs): truncate files when a ReadableStream is passed to writeFile #23330

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

char
Copy link

@char char commented Apr 11, 2024

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.

@CLAassistant
Copy link

CLAassistant commented Apr 11, 2024

CLA assistant check
All committers have signed the CLA.

@bartlomieju bartlomieju added this to the 1.43 milestone Apr 12, 2024
@bartlomieju
Copy link
Member

This will need some discussion from the core team. @char could you add a unit test for this particular case (or update existing one)?

@char
Copy link
Author

char commented Apr 12, 2024

I've added a test which ensures writeFile(..) returns the data you wrote into it, even with a larger pre-existing file

Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enablement condition is wrong. There should also be a test that appends correctly when using the append option (this is currently not working in this PR).

ext/fs/30_fs.js Outdated Show resolved Hide resolved
});
await Deno.writeFile(filename, stream);
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
// append false should also overwrite
await Deno.writeFile(filename, new Response(new Uint8Array([1, 2, 3])).body!, {append: false});
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2, 3]));
// append set should append
await Deno.writeFile(filename, new Response(new Uint8Array([4, 5])).body!, {append: true});
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2, 3, 4, 5]));
},

controller.enqueue(new Uint8Array([2]));
controller.close();
},
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
});
});
// append not set should overwrite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Deno.writeFile(readableStream) can result in "chimera" files (partially overwritten files)
5 participants