Skip to content

fix: throw UnauthorizedAccessException when writing to read-only files - #1225

Merged
vbreuss merged 1 commit into
Testably:mainfrom
StefanGreve:fix/readonly-file-write
Nov 22, 2026
Merged

vbreuss merged 1 commit into
Testably:mainfrom
StefanGreve:fix/readonly-file-write

Conversation

@StefanGreve

Copy link
Copy Markdown
Contributor

Opening a read-only file for writing throws UnauthorizedAccessException on Windows, Linux and macOS, and MockFileSystem already does so for a FileStream through FileModeHelper. File.WriteAllBytes, WriteAllText, WriteAllLines, AppendAllBytes, AppendAllText and AppendAllLines write to the storage container directly and skip that check, so the mock replaced or extended a read-only file without an error. The File.WriteAllBytes reference lists UnauthorizedAccessException for the case where "path specified a file that is read-only".

FileMock now checks the attribute in AppendAllBytes, WriteAllBytes and WriteText, which every overload of these methods goes through, and throws ExceptionFactory.AccessToPathDenied with the full path and HResult -2147024891, as the real file system does. The check runs after the existing directory and NullContainer checks, because a NullContainer reports every attribute flag as set.

The new tests cover each method, plus WriteAllText with null contents, 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.

@mergify

mergify Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Queue requested — the merge queue status continues in this comment ↓.

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR makes bulk write and append methods reject read-only files without changing their contents.

  • Mock file writes and appends reject read-only files before changing them.

Reviews (1) · Last reviewed commit: "fix: throw UnauthorizedAccessException w..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 vbreuss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, @StefanGreve

@mergify

mergify Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

❌ Left the queue · Testably/Conventional-Commits fails with this pull request

1 min in the queue, less than a minute of it CI.

So far

  • ✅ Entered the queue Sun 22 Nov, 16:55 · rule default · triggered by @vbreuss with the merge queue checkbox
  • ❌ Checks failed · draft #1226
  • 🚫 Left the queue 16:56 · 2c41198

The failure is in this pull request. It failed tested alone on top of main.

Reason

The merge conditions cannot be satisfied due to failing checks

  • Testably/Conventional-Commits

Failing checks:

  • ❌ Testably/Conventional-Commits

To get back in the queue

  • Push a fix: the pull request is queued again once its queue conditions match.
  • If the failure is a flake, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/API tests
    • check-skipped = @github-actions/API tests
    • check-success = @github-actions/API tests
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/Unit tests (macos-latest)
    • check-skipped = @github-actions/Unit tests (macos-latest)
    • check-success = @github-actions/Unit tests (macos-latest)
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/Unit tests (ubuntu-latest)
    • check-skipped = @github-actions/Unit tests (ubuntu-latest)
    • check-success = @github-actions/Unit tests (ubuntu-latest)
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/Unit tests (windows-latest)
    • check-skipped = @github-actions/Unit tests (windows-latest)
    • check-success = @github-actions/Unit tests (windows-latest)
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = Testably/Conventional-Commits
    • check-skipped = Testably/Conventional-Commits
    • check-success = Testably/Conventional-Commits
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/AOT smoke tests (macos-latest)
    • check-skipped = @github-actions/AOT smoke tests (macos-latest)
    • check-success = @github-actions/AOT smoke tests (macos-latest)
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/AOT smoke tests (ubuntu-latest)
    • check-skipped = @github-actions/AOT smoke tests (ubuntu-latest)
    • check-success = @github-actions/AOT smoke tests (ubuntu-latest)
  • any of · required by GitHub repository ruleset rule main:
    • check-neutral = @github-actions/AOT smoke tests (windows-latest)
    • check-skipped = @github-actions/AOT smoke tests (windows-latest)
    • check-success = @github-actions/AOT smoke tests (windows-latest)

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request
2 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub repository ruleset rule main
  • github-review-approved · required by GitHub repository ruleset rule main

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added the queued label Nov 22, 2026
@mergify mergify Bot mentioned this pull request Nov 22, 2026
20 of 100 tasks
@mergify mergify Bot added dequeued and removed queued labels Nov 22, 2026
@vbreuss
vbreuss merged commit 0f105c2 into Testably:main Nov 22, 2026
16 checks passed
@StefanGreve
StefanGreve deleted the fix/readonly-file-write branch November 22, 2026 17:00
@github-actions

Copy link
Copy Markdown

This is addressed in release v8.1.1.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants