fix(fs): reject malformed write_file options instead of ignoring them (#3622)

In plugins/fs/src/commands.rs write_file_inner, a malformed or non-ASCII
`options` header was dropped with .ok(), so baseDir/append/createNew were
silently ignored and the call became a truncating write to the raw path;
the JSON array body fallback truncated values above 255 and dropped
non-numbers.

The header is parsed with parse_write_file_options: empty, `undefined`
(what fetch sends for JSON.stringify(undefined)) and `null` mean no
options, anything else must parse. Array bodies must only contain bytes.
Unit test for the header parser; e2e spec for writes without options,
which is the case the `undefined` handling keeps working.
This commit is contained in:
Lucas Fernandes Nogueira
2026-09-23 12:57:49 -03:00
committed by GitHub
parent 0c84c917e3
commit b46a88ff55
3 changed files with 64 additions and 5 deletions
+15
View File
@@ -70,6 +70,21 @@ describePlugin('fs', () => {
expect(read).toBe('first second')
})
it('writeFile and writeTextFile work without options', async () => {
const result = await tauri(async (api, dir) => {
const path = await api.path.join(
await api.path.appDataDir(),
dir,
'no-options.txt'
)
await api.fs.writeTextFile(path, 'text')
const text = await api.fs.readTextFile(path)
await api.fs.writeFile(path, new Uint8Array([98, 121, 116, 101, 115]))
return { text, bytes: await api.fs.readTextFile(path) }
}, dir)
expect(result).toEqual({ text: 'text', bytes: 'bytes' })
})
it('writeFile and readFile round-trip binary data', async () => {
const bytes = [0, 1, 2, 3, 250, 251, 252, 253, 254, 255]
const read = await tauri(