Skip to content

czdev: never let the GitHub token exist on disk world-readable - #23

Open
at0m-b0mb wants to merge 2 commits into
CardputerZero:mainfrom
at0m-b0mb:fix/credentials-never-world-readable
Open

at0m-b0mb wants to merge 2 commits into
CardputerZero:mainfrom
at0m-b0mb:fix/credentials-never-world-readable

Conversation

@at0m-b0mb

Copy link
Copy Markdown

save_credentials() in scripts/czdev/auth.py wrote the token and then narrowed the file:

path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(creds, indent=2))
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)

Two problems:

  1. write_text() creates the file at the process umask. With the common 0022 the token
    lands at 0644 and is readable by any local user until the chmod on the next line runs.
    The end state is correct; the creation is not.
  2. ~/.czdev is created with no mode, so it stays 0755 — and because mkdir(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:

creation mode:  write_text -> 0o666    os.open(..., 0o600) -> 0o600

The change

Open with O_CREAT and an explicit 0600 so the file is never wider than 0600 at any
point, and narrow the directory on every save rather than only at creation. The trailing
chmod is kept deliberately: O_CREAT does not change the mode of a file that already
exists, 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 existing
python3 -m unittest discover -s tests in .github/workflows/test-czdev.yml.

Validated in both directions — against the previous implementation:

FAIL: test_directory_is_owner_only
FAIL: test_existing_wide_login_is_repaired
FAIL: test_file_is_created_narrow_without_relying_on_chmod
Ran 9 tests
FAILED (failures=3)

and against this branch:

Ran 9 tests
OK

test_file_is_created_narrow_without_relying_on_chmod neutralises only the credentials-file
chmod, so it observes the mode the file was created with. Asserting the final mode alone
can't catch this, because the old code did reach 0600 eventually — which is why
test_token_is_never_group_or_world_readable passes against both versions and is there as a
plain guard rather than a regression test.

setUp installs a 0000 umask; a restrictive umask on the test machine would mask the
behaviour under test.

No hardware involved — this is all host-side Python.

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.
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.

1 participant