Repository navigation
Conversation
save_credentials() wrote the token with Path.write_text() and narrowed the file to 0600 afterwards. write_text() creates the file at the process umask, so with the common 0022 the token lands at 0644 and is readable by any local user until the chmod runs. ~/.czdev itself was created with mkdir() and no mode, so it stayed 0755. Open the file with O_CREAT and an explicit 0600 instead, so it is never wider than 0600 at any point, and narrow the directory to 0700 on every save rather than only at creation -- a directory left 0755 by an earlier login is otherwise never repaired. The explicit chmod is kept because O_CREAT does not change the mode of a file that already exists, which is the case on re-login.
Four cases, run by the existing unittest discovery in test-czdev.yml. Three of them fail against the previous implementation: the directory mode, the repair of a directory left wide by an earlier login, and the mode the file is created with before any chmod. The last one neutralises only the credentials-file chmod so the test observes the creation mode directly; asserting the end state alone cannot see the window, because the old code did reach 0600 eventually. setUp installs a 0000 umask, since a restrictive umask would mask the very thing being tested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
save_credentials()inscripts/czdev/auth.pywrote the token and then narrowed the file:Two problems:
write_text()creates the file at the process umask. With the common0022the tokenlands at
0644and is readable by any local user until thechmodon the next line runs.The end state is correct; the creation is not.
~/.czdevis created with no mode, so it stays0755— and becausemkdir(exist_ok=True)never revisits an existing directory, a directory left wide by an earlier login is never
repaired.
Measured on macOS with
umask 000, to show what the umask would otherwise hide:The change
Open with
O_CREATand an explicit0600so the file is never wider than0600at anypoint, and narrow the directory on every save rather than only at creation. The trailing
chmodis kept deliberately:O_CREATdoes not change the mode of a file that alreadyexists, which is exactly the re-login case.
The token is a GitHub OAuth token with push scope on the developer's repositories, so the
window is worth closing even though it's short.
Tests
Added
tests/test_auth_credentials.py, picked up by the existingpython3 -m unittest discover -s testsin.github/workflows/test-czdev.yml.Validated in both directions — against the previous implementation:
and against this branch:
test_file_is_created_narrow_without_relying_on_chmodneutralises only the credentials-filechmod, so it observes the mode the file was created with. Asserting the final mode alonecan't catch this, because the old code did reach
0600eventually — which is whytest_token_is_never_group_or_world_readablepasses against both versions and is there as aplain guard rather than a regression test.
setUpinstalls a0000umask; a restrictive umask on the test machine would mask thebehaviour under test.
No hardware involved — this is all host-side Python.