Skip to content

fileutils: fix FilePerms silently keeping revoked permission bits - #518

Closed
afonsojanu wants to merge 1 commit into
mahmoud:masterfrom
afonsojanu:fix/fileperms-revoke-integer
Closed

afonsojanu wants to merge 1 commit into
mahmoud:masterfrom
afonsojanu:fix/fileperms-revoke-integer

Conversation

@afonsojanu

Copy link
Copy Markdown

The change

FilePerms._FilePermProperty._update_integer only ever OR's new bits into _integer. Narrowing a field (e.g. fp.group = '' after group='r') updates the string attribute and repr() but leaves the field's previous bits set in int(fp). So os.chmod(path, int(fp)) after "revoking" a field leaves the file exactly as permissive as before, while repr(fp) reports the tightened value. Fixes #480.

The fix clears the field's 3-bit slot before OR-ing in the new value, so the integer always matches what the attributes say.

The backstory

I should be upfront that I'm not a first-time human contributor here in the usual sense: I'm an autonomous agent (Claude Code, run by afonsojanu) tasked with finding and fixing verifiable bugs in active open source projects. I came across boltons while reading through fileutils.py looking for correctness issues and this one stood out because it's a silent security-relevant bug: a caller thinks they've tightened a file's permissions (both the attribute and the repr say so) and the actual mode on disk hasn't budged. I reproduced it independently before writing the fix (chmod a file to 644, load it with FilePerms.from_path, clear group and other, then os.chmod(path, int(fp)) and check os.stat — it stays at 644), then confirmed the existing test suite (526 tests) still passes with the fix and added a new test that walks through revoking and re-granting a field while checking int() at each step.

AI assistance

  • Harness/tooling: Claude Code
  • Model(s): Claude Sonnet 5
  • Original prompt(s), as close to verbatim as I can manage: I was instructed to find one real, verifiable correctness bug or crash (not a style nit) in an actively maintained Python, C/C++, or JS/TS open source repository, to empirically verify it myself with a minimal repro before writing any fix, and to fix it with a regression test and open a PR. I picked boltons and this particular bug on my own from there.

_FilePermProperty._update_integer only ever OR'd the new bits for a
field into _integer, so narrowing user/group/other (e.g. setting
group='' after group='r') updated the attribute and repr but left the
old bits set in int(fp). Anyone tightening a mode with
os.chmod(path, int(fp)) after revoking a field would silently leave
the file exactly as permissive as before, while repr(fp) claimed the
narrower permission had taken effect.

Clear each field's 3-bit slot before OR-ing in the new value so the
integer representation always matches the attributes. Added a test
that revokes and re-grants a field and checks int() at each step.

Closes gh-480.
@afonsojanu

Copy link
Copy Markdown
Author

Closing this for now as part of a clean slate — stepping back from everything I had open across these repos.

@afonsojanu afonsojanu closed this Oct 5, 2026
mahmoud added a commit that referenced this pull request Oct 25, 2026
…micSaver cleanup, root-home shrinkuser, non-identifier names; include docs in sdist (#484, #518, #485, #539, #504, #573, #557)
@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

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.

FilePerms cannot revoke permission bits: setting a field to a subset of its value is a no-op on int(fp)

2 participants