Repository navigation
fix: throw UnauthorizedAccessException when writing to read-only files - #1225
Conversation
|
Queue requested — the merge queue status continues in this comment ↓. |
[Medium impact] The PR appears safe to merge.
|
There was a problem hiding this comment.
All reported issues were addressed across 7 files
Reply to a comment to ask cubic a question or push back. It learns from your replies.
Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.
Re-trigger cubic
vbreuss
left a comment
There was a problem hiding this comment.
Thanks for the fix, @StefanGreve
❌ Left the queue ·
|
|
This is addressed in release v8.1.1. |
Opening a read-only file for writing throws
UnauthorizedAccessExceptionon Windows, Linux and macOS, andMockFileSystemalready does so for aFileStreamthroughFileModeHelper.File.WriteAllBytes,WriteAllText,WriteAllLines,AppendAllBytes,AppendAllTextandAppendAllLineswrite to the storage container directly and skip that check, so the mock replaced or extended a read-only file without an error. TheFile.WriteAllBytesreference listsUnauthorizedAccessExceptionfor the case where "pathspecified a file that is read-only".FileMocknow checks the attribute inAppendAllBytes,WriteAllBytesandWriteText, which every overload of these methods goes through, and throwsExceptionFactory.AccessToPathDeniedwith the full path and HResult-2147024891, as the real file system does. The check runs after the existing directory andNullContainerchecks, because aNullContainerreports every attribute flag as set.The new tests cover each method, plus
WriteAllTextwithnullcontents, which the real file system refuses as well because it opens the file before it writes. Each test also asserts that the file keeps its previous contents. They pass on every mock row and against the real file system on Windows, and fail on the four mock rows without the fix. Linux and macOS are verified by CI.