-
Notifications
You must be signed in to change notification settings - Fork 51
fix: restrict file permissions on auth, config, and log files to prevent local credential theft #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aviatco
merged 13 commits into
microsoft:main
from
iemejia:fix/restrict-file-permissions-sensitive-data
Jun 18, 2026
Merged
fix: restrict file permissions on auth, config, and log files to prevent local credential theft #244
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
154e0d3
fix: restrict file permissions on auth, config, and log files to prev…
iemejia 3aef7ad
chore: add changie entry for file permissions fix
iemejia b415dc1
fix: address review comments - fd leak guard, chmod for existing file…
iemejia cb185e3
fix: address round 2 review - chmod on log dir, warning on chmod fail…
iemejia 8ac5965
refactor: extract file permission helpers to utils module
iemejia c8ec9be
fix: specify utf-8 encoding in write_restricted_file
iemejia a77187c
fix: address review round 3 - rename to fab_secure_io, add log rotati…
iemejia 20256ba
fix: address review round 4 - chmod before write, public IS_POSIX, ha…
iemejia f649865
fix: move [tool.black] config from tox.toml to pyproject.toml
iemejia 6b80102
Merge branch 'main' into fix/restrict-file-permissions-sensitive-data
iemejia 8685849
fix: address review round 5 - encapsulate IS_POSIX in fab_secure_io h…
iemejia 174ad89
Merge branch 'fix/restrict-file-permissions-sensitive-data' of https:…
iemejia 3a73c1a
Revert "fix: move [tool.black] config from tox.toml to pyproject.toml"
iemejia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| kind: fixed | ||
| body: Restrict file and directory permissions on auth, config, context, and log paths to prevent local credential exposure on multi-user systems | ||
| time: 2026-06-07T13:40:43+02:00 | ||
| custom: | ||
| Author: iemejia | ||
| AuthorLink: https://github.com/iemejia | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
|
aviatco marked this conversation as resolved.
|
||
| """Shared helpers for enforcing restrictive file and directory permissions. | ||
|
|
||
| On POSIX systems (Linux/macOS), these helpers ensure that sensitive files are | ||
| created with owner-only access (0o600 for files, 0o700 for directories) and | ||
| tighten permissions on pre-existing paths from older CLI versions. | ||
|
|
||
| On Windows, POSIX permission bits are a no-op — Windows uses ACLs instead, | ||
| and default user-profile ACLs already restrict access to the owner. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| # True on Linux/macOS; False on Windows where POSIX permission bits are a no-op. | ||
| IS_POSIX = os.name != "nt" | ||
|
|
||
|
|
||
| def chmod_if_posix(path: str, mode: int) -> None: | ||
| """Best-effort chmod on POSIX; no-op on Windows. | ||
|
|
||
| Logs at debug level when chmod fails (e.g. permission denied on | ||
| restrictive filesystems) since the user cannot act on it. | ||
| """ | ||
| if IS_POSIX: | ||
| try: | ||
| os.chmod(path, mode) | ||
| except OSError as e: | ||
| _logger.debug("Failed to set permissions %o on %s: %s", mode, path, e) | ||
|
|
||
|
|
||
| def write_restricted_file(file_path: str, content: str) -> None: | ||
| """Write content to a file with owner-only permissions (0o600). | ||
|
|
||
| Handles both new file creation and tightening permissions on | ||
| pre-existing files from older CLI versions. Permissions are | ||
| tightened *before* truncation so that sensitive content is never | ||
| written to a world-readable file descriptor. | ||
| """ | ||
| # Tighten permissions on pre-existing files before writing, so | ||
| # the truncate+write never exposes new content through a permissive fd. | ||
| if os.path.exists(file_path): | ||
| chmod_if_posix(file_path, 0o600) | ||
| fd = os.open(file_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) | ||
| try: | ||
| with os.fdopen(fd, "w", encoding="utf-8") as file: | ||
| file.write(content) | ||
| except Exception: | ||
| # os.fdopen may fail before wrapping fd; close to avoid leak. | ||
| # If os.fdopen succeeded, fd is already closed by the with block. | ||
| try: | ||
| os.close(fd) | ||
| except OSError: | ||
| pass | ||
| raise | ||
|
|
||
|
|
||
| def get_restricted_file_opener(): | ||
| """Return a file opener that creates files with 0o600 on POSIX, or None on Windows. | ||
|
|
||
| Intended for use as the ``opener`` argument to :func:`open`. Returns | ||
| ``None`` on Windows so that the default opener is used. | ||
| """ | ||
| if IS_POSIX: | ||
| return lambda path, flags: os.open(path, flags, 0o600) | ||
| return None | ||
|
|
||
|
|
||
| def create_restricted_dir(dir_path: str) -> None: | ||
| """Create a directory with owner-only permissions (0o700). | ||
|
|
||
| Uses exist_ok=True to avoid TOCTOU races and tightens permissions | ||
| on pre-existing directories from older CLI versions. | ||
| """ | ||
| os.makedirs(dir_path, mode=0o700, exist_ok=True) | ||
| # Enforce permissions on pre-existing directories from older versions | ||
| chmod_if_posix(dir_path, 0o700) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.