Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request restricts the permissions of the saved credentials file and its parent directory to the current user (using modes 0o600 and 0o700 respectively) and adds a corresponding unit test. The feedback suggests using tempfile.TemporaryDirectory instead of tempfile.mkdtemp() in the test to avoid leaking temporary directories.
Comment on lines
+140
to
+164
| credentials_tmpdir = tempfile.mkdtemp() | ||
| credentials_path = os.path.join( | ||
| credentials_tmpdir, "new-directory", "credentials.json" | ||
| ) | ||
| result = runner.invoke( | ||
| cli.main, | ||
| [ | ||
| "--client-secrets", | ||
| CLIENT_SECRETS_FILE, | ||
| "--scope", | ||
| "somescope", | ||
| "--credentials", | ||
| credentials_path, | ||
| "--save", | ||
| ], | ||
| ) | ||
| local_server_mock.assert_called_with(mock.ANY) | ||
| assert not result.exception | ||
| assert result.exit_code == 0 | ||
| # The saved file holds the refresh token and client secret, so neither | ||
| # it nor the directory created for it may be group or world accessible. | ||
| file_mode = stat.S_IMODE(os.stat(credentials_path).st_mode) | ||
| dir_mode = stat.S_IMODE(os.stat(os.path.dirname(credentials_path)).st_mode) | ||
| assert file_mode & 0o077 == 0 | ||
| assert dir_mode & 0o077 == 0 |
Contributor
There was a problem hiding this comment.
The temporary directory created with tempfile.mkdtemp() is never cleaned up, which leads to directory leaks in the test environment. Using tempfile.TemporaryDirectory as a context manager ensures that the directory and its contents are automatically cleaned up after the test completes, even if assertions fail.
with tempfile.TemporaryDirectory() as credentials_tmpdir:
credentials_path = os.path.join(
credentials_tmpdir, "new-directory", "credentials.json"
)
result = runner.invoke(
cli.main,
[
"--client-secrets",
CLIENT_SECRETS_FILE,
"--scope",
"somescope",
"--credentials",
credentials_path,
"--save",
],
)
local_server_mock.assert_called_with(mock.ANY)
assert not result.exception
assert result.exit_code == 0
# The saved file holds the refresh token and client secret, so neither
# it nor the directory created for it may be group or world accessible.
file_mode = stat.S_IMODE(os.stat(credentials_path).st_mode)
dir_mode = stat.S_IMODE(os.stat(os.path.dirname(credentials_path)).st_mode)
assert file_mode & 0o077 == 0
assert dir_mode & 0o077 == 0
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.
google-oauthlib-tool --save writes credentials.json, which holds the OAuth refresh token and client secret, through a plain open() and creates its config directory with os.makedirs(), so both take the process umask and on a typical host end up 0644 and 0755, readable by every local user. Create the file with mode 0600 and the directory with 0700 inside the tool instead, matching how the mTLS helper in google-auth writes key material, and add a test that checks neither has group or other bits. The test is skipped on Windows, where these mode bits do not apply.