[GH-ISSUE #728] [Issue]: File-native recovery can overwrite a valid .bak with corrupt JSON #347

Closed
opened 2026-10-20 17:27:52 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @NeoKazuya on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/728

Summary

File-native storage currently uses matching .bak files to recover corrupted table and manifest JSON. If the primary file is malformed but still contains ordinary bytes (for example, after a truncated write or manual edit), Marinara completes the recovery from .bak, but the repair save also overwrites the valid .bak with the corrupt file it just recovered from.

The result is that the main file looks fixed, but the fallback backup is no longer usable until a later normal save refreshes it.

Expected behavior

After Marinara recovers a table or manifest from .bak, the repair write should keep that known-good .bak in place while the main file is being repaired.

Actual behavior

If tables/<name>.json or manifest.json contains malformed JSON that is not NUL-filled, such as a truncated file containing {"id":, Marinara currently recovers from .bak and marks the file for rewrite.

On the next flush, atomicWriteFile() sees that the primary file exists and is not NUL-filled, so it refreshes .bak from that still-corrupt primary before writing the recovered data back to the main file.

After a successful repair, the main file is valid again, but .bak can contain the old malformed JSON.

Steps to reproduce

  1. Start from a file-native storage directory.
  2. Write malformed non-NUL JSON to a table file, for example tables/chats.json containing {"id":.
  3. Write valid JSON to tables/chats.json.bak, for example [].
  4. Start Marinara with FILE_STORAGE_DIR pointed at that storage directory.
  5. Let startup recover from .bak, then close Marinara or otherwise let file-native storage flush.
  6. Inspect both files.

Observed on current main:

tables/chats.json     -> []
tables/chats.json.bak -> {"id":

The primary was repaired, but the valid backup was replaced with malformed JSON.

The same pattern reproduces with manifest.json:

manifest.json     -> valid JSON after self-heal
manifest.json.bak -> {"version":

Environment

  • Marinara Engine version: current main at ed0285f52011f3e89e87263a76c6b2168d2e8827
  • Install type: source
  • OS + version: Windows 11 Pro 10.0.26200
  • Browser or app shell: Reproduced through the server file-storage path; browser UI is not involved

Logs, screenshots, or video

Startup logs show the expected recovery path:

[file-storage] .../tables/chats.json is corrupt; recovering from .../tables/chats.json.bak (backup is less than a second older). Edits made since the backup are unrecoverable.

After startup and flush, the files were:

{
  "afterMain": "[]",
  "afterBackup": "{\"id\":",
  "mainParses": true,
  "backupParses": false,
  "backupWasReplacedWithCorruptPrimary": true
}

Control checks showed the existing NUL/empty guard still works: when the primary starts with NUL bytes or is empty, the main file is repaired and .bak remains parseable.

Additional context

This is a narrow follow-up to #631 / #634.

#634 made .bak refresh durable and skips backup refresh when the existing primary is NUL-filled or empty. This remaining case is a malformed primary that starts with printable bytes, so looksNulFilled() returns false even though startup already proved the file is not valid JSON.

The repair path can preserve the valid .bak for files that were just recovered from backup, then allow later normal saves to refresh .bak from the repaired primary.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @NeoKazuya on GitHub (May 12, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/728 ## Summary File-native storage currently uses matching `.bak` files to recover corrupted table and manifest JSON. If the primary file is malformed but still contains ordinary bytes (for example, after a truncated write or manual edit), Marinara completes the recovery from `.bak`, but the repair save also overwrites the valid `.bak` with the corrupt file it just recovered from. The result is that the main file looks fixed, but the fallback backup is no longer usable until a later normal save refreshes it. ## Expected behavior After Marinara recovers a table or manifest from `.bak`, the repair write should keep that known-good `.bak` in place while the main file is being repaired. ## Actual behavior If `tables/<name>.json` or `manifest.json` contains malformed JSON that is not NUL-filled, such as a truncated file containing `{"id":`, Marinara currently recovers from `.bak` and marks the file for rewrite. On the next flush, `atomicWriteFile()` sees that the primary file exists and is not NUL-filled, so it refreshes `.bak` from that still-corrupt primary before writing the recovered data back to the main file. After a successful repair, the main file is valid again, but `.bak` can contain the old malformed JSON. ## Steps to reproduce 1. Start from a file-native storage directory. 2. Write malformed non-NUL JSON to a table file, for example `tables/chats.json` containing `{"id":`. 3. Write valid JSON to `tables/chats.json.bak`, for example `[]`. 4. Start Marinara with `FILE_STORAGE_DIR` pointed at that storage directory. 5. Let startup recover from `.bak`, then close Marinara or otherwise let file-native storage flush. 6. Inspect both files. Observed on current `main`: ```txt tables/chats.json -> [] tables/chats.json.bak -> {"id": ``` The primary was repaired, but the valid backup was replaced with malformed JSON. The same pattern reproduces with `manifest.json`: ```txt manifest.json -> valid JSON after self-heal manifest.json.bak -> {"version": ``` ## Environment - Marinara Engine version: current `main` at `ed0285f52011f3e89e87263a76c6b2168d2e8827` - Install type: source - OS + version: Windows 11 Pro 10.0.26200 - Browser or app shell: Reproduced through the server file-storage path; browser UI is not involved ## Logs, screenshots, or video Startup logs show the expected recovery path: ```txt [file-storage] .../tables/chats.json is corrupt; recovering from .../tables/chats.json.bak (backup is less than a second older). Edits made since the backup are unrecoverable. ``` After startup and flush, the files were: ```json { "afterMain": "[]", "afterBackup": "{\"id\":", "mainParses": true, "backupParses": false, "backupWasReplacedWithCorruptPrimary": true } ``` Control checks showed the existing NUL/empty guard still works: when the primary starts with NUL bytes or is empty, the main file is repaired and `.bak` remains parseable. ## Additional context This is a narrow follow-up to #631 / #634. #634 made `.bak` refresh durable and skips backup refresh when the existing primary is NUL-filled or empty. This remaining case is a malformed primary that starts with printable bytes, so `looksNulFilled()` returns false even though startup already proved the file is not valid JSON. The repair path can preserve the valid `.bak` for files that were just recovered from backup, then allow later normal saves to refresh `.bak` from the repaired primary. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:27:52 +07:00
  • closed this issue
  • added the
    bug
    label
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#347
No description provided.