Skip to content

fix: add permission checks to MemMapFs Open/OpenFile#573

Open
veeceey wants to merge 1 commit intospf13:masterfrom
veeceey:fix/issue-150-memmap-permission-checks
Open

fix: add permission checks to MemMapFs Open/OpenFile#573
veeceey wants to merge 1 commit intospf13:masterfrom
veeceey:fix/issue-150-memmap-permission-checks

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 23, 2026

Fixes #150

Right now MemMapFs ignores file permissions completely -- you can chmod a file to 0 and still open/read it without any error. This makes it pretty much impossible to use MemMapFs for testing code that cares about file permissions.

This PR adds basic permission checking:

  • Open() checks read permission bits (0444) before returning a handle
  • openWrite() checks write permission bits (0222) before returning a handle
  • Create() now defaults to 0666 permissions, matching what os.Create() does
  • Stat() bypasses the permission check (uses internal open()) since real stat(2) doesn't require read permission either
  • Auto-created parent directories in registerWithParent now get 0755 instead of 0, so they stay accessible

The permission model is simplified -- it checks any read/write bit being set rather than doing full uid/gid owner/group/other matching. This keeps things straightforward while still catching the common case of chmod 0 or testing read-only vs write-only scenarios.

MemMapFs was ignoring file permissions entirely -- you could chmod a file
to 0 and still open it without error. This made it hard to use MemMapFs
for testing code that depends on permission-based behavior.

Changes:
- Open() now checks read permission bits before returning a file handle
- openWrite() now checks write permission bits
- Create() sets default permission to 0666, matching os.Create() docs
- Stat() uses internal open() to avoid permission checks (matching real
  stat(2) behavior which doesn't require read permission)
- registerWithParent uses 0755 for auto-created parent directories
  instead of 0, so they remain accessible

Fixes spf13#150
@CLAassistant
Copy link

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

@veeceey
Copy link
Author

veeceey commented Feb 23, 2026

Test Results

Full test suite passes:

$ go test -count=1 ./...
ok  	github.com/spf13/afero	4.358s
ok  	github.com/spf13/afero/mem	0.156s
ok  	github.com/spf13/afero/tarfs	0.454s
ok  	github.com/spf13/afero/zipfs	0.603s

New test TestMemMapFsPermissionChecks covers:

$ go test -v -run TestMemMapFsPermissionChecks ./...
=== RUN   TestMemMapFsPermissionChecks
--- PASS: TestMemMapFsPermissionChecks (0.00s)
PASS

Verified manually:

  • Creating a file gives 0666 permissions (matching os.Create docs)
  • Chmod to 0 then Open -> permission denied
  • Chmod to 0444 then Open for read -> ok, Open for write -> denied
  • Chmod to 0222 then Open for read -> denied
  • Stat works regardless of permissions (matching real stat behavior)

@veeceey
Copy link
Author

veeceey commented Mar 12, 2026

hey, any thoughts on this?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File permissions don't get checked by Open

2 participants