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

Secure writing #94

Open
cupill opened this issue Oct 13, 2017 · 3 comments
Open

Secure writing #94

cupill opened this issue Oct 13, 2017 · 3 comments

Comments

@cupill
Copy link

cupill commented Oct 13, 2017

Hi,

Let's say we run in an electrically unstable environment, and the system we are running on may be shut down unexpectedly, i.e. power loss. AND we were in the middle of the writeFile/writeFileSync. As far as I understand, the resulting storage file, on the next reboot, will consist in an incomplete JSON file. And so, all its content is lost.
OK, chances are small, but thanks to Murphy, it WILL happen.
So, would you consider securing the writing step of the file (in persistKey* functions). It could be an additional option, disabled by default.

IMO, it would be as simple as writing the ${md5(key)}.tmp file instead of md5(key), then renaming the temporary file with the actual file name.
In that case, previous content would still be safe.

Thanks!

@MattJustMatt
Copy link

I actually had this happen to me. json file was wiped. Nothing critical in there, but now I've got a corrupt file!

@vladimiry
Copy link

vladimiry commented Jul 15, 2018

Look like what is needed is enabling atomic writing (safe write). Means you write to the temp file first, and then rename it to the destination file. On Unix systems renaming is atomic operation, and Win OS has ReplaceFile() method.

This module can be a start point https://github.com/npm/write-file-atomic But it has a flaw with EPERM errors handling on Win OS that is going to be handled here isaacs/node-graceful-fs#119

#31 is somehow related issue.

@Stormpass
Copy link

I actually had this happen to me. json file was wiped. Nothing critical in there, but now I've got a corrupt file!

it is happen to me too!

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

No branches or pull requests

5 participants