I found a file manager sitting in a scratch directory left behind by an AI coding tool. It was nine months old, finished, about 2,200 lines of React and Express, and as far as I could tell it had never been run once. Two fixes later it worked: the server's files as a tree in a browser, with rename, upload, compress and an editor. Open a file, change a line, hit save, and the change is there the next time you look.
It was also quietly turning every file it saved into something anyone on the machine could read and write.
Saved, every time
The same day I put it back into service, I edited a file and noticed its permissions had changed. I checked a few more. Every file the editor touched came back as 666. A secret that had been 600, readable by me alone, was now readable by every account on the box. A 755 script lost its execute bit and stopped running. The editor said saved each time, because from where it stood, the save had worked.
A create option on a file that already exists
The editor wrote files over SFTP using the ssh2 library, with one call: sftp.createWriteStream(path), no options. That looks harmless. The catch is in how SFTP opens a file. The open request carries the attributes the file should get if it has to be created, and ssh2 fills in a mode of 0o666 when you don't pass one. The server on the other end applies those attributes even when the file already exists and nothing is being created. Because the mode is set explicitly, no umask trims it down either. So the result was exactly 666, on every save, for every file.
Nothing in that chain broke. Every piece did what it was written to do, and the report that came back only covered the part I had asked about, which was whether the contents landed.
The fix, and the check that caught it
The fix reads the file's current mode before writing and writes with that same mode, masked with 0o7777 so setuid, setgid and sticky bits survive. A file that really is new gets 0o644. Permissions now change only when someone changes them on purpose.
The part I trust more is the check. The app has two ways of writing files, one over SSH for other machines and one straight to the local disk for the machine it runs on, and scripts/check-permissions.sh covers both. It writes files at 600, 640, 700 and 755 through the app's real API and compares each mode before and after. It also confirms the content actually arrived, so a write that failed outright can't pass as "mode preserved." It runs after any change to either driver.
One case it treats as expected: a 444 file can't be saved. I own it, so I could change its mode, but the editor won't write to a read-only file, and it shouldn't.
If a tool writes files for you, you can test it in a minute:
stat -c '%a %n' notes.txt
# edit and save notes.txt in the tool
stat -c '%a %n' notes.txt
The same number twice means it left your permissions alone. Since 0o666 is the library's default and not something this app chose, anything else of mine that writes over SFTP with ssh2 is on the list to check.
Half of my original request is still open. I asked for the editor to keep a file's permissions unless I change them in the app, and the app has no way to change them yet. It never touches a mode now, and it can't be asked to.