Repository navigation
Description
Problem Description
A config file that devops-cli writes, the copies of it and the temporary file of a write get their mode from the default umask, usually 0644. A config file can hold secrets, so the files are readable by other users of the host.
Two cases show this, found while the change for #2645 was built:
- A write replaces the live file with a new file:
_write_contentinsrc/devops_cli/config/settings.pywrites<name>.yaml.tmpwithwrite_bytesand callsos.replace. The new file has the umask mode, so a config file that a person set to 0600 becomes 0644 after anydevops config setordevops config unset. The copy keeps the old mode, becauseshutil.copy2copies it, so the copy is then more closed than the live file. - The temporary file that
_copyinsrc/devops_cli/config/write_history.pywrites for a redacted copy exists with the umask mode untilshutil.copystatruns. For that moment the temporary file is more open than the live file it comes from. The same holds for the temporary file of_write_content.
Reproduction Steps
- Make a temporary
HOME. Writeai:andmodel: mto$HOME/.config/devops-cli/config.yaml. Runchmod 600on it. - Set
PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring. UnsetDEVOPS_CLI_CONFIG. Rundevops config unset ai.modelin a directory with no project config file. - Run
stat -c '%a %n' $HOME/.config/devops-cli/*.
Expected vs Actual Behavior
Expected: the live file keeps its mode (0600). A new config file, a copy and a temporary file are created with a mode of 0600 or the mode of the file they replace, never more open.
Actual: the live file is 0644 after the write, and the copy is 0600.
Environment Diagnostics
devops-cli 0.21.3 at origin/main ae446f0, Linux.
Logs, Traces, or Terminal Output
600 <tmp>/home/.config/devops-cli/config.yaml (before)
644 <tmp>/home/.config/devops-cli/config.yaml (after)
600 <tmp>/home/.config/devops-cli/config.yaml.bak-<stamp>
Pre-Submission Checklist
- I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
- I have checked existing issues and pull requests to ensure this is not a duplicate.
Proposed design
Problem statement
A config file that devops-cli writes gets the default umask mode, usually 0644, instead of the mode of the file it replaces. A config file can hold secrets, so other users of the host can read it. Two code paths cause this: _write_content in settings.py creates a tmp file with write_bytes and calls os.replace, so the live file loses its mode. _copy in write_history.py creates a tmp file with write_bytes and calls shutil.copystat after the write, so the tmp file is briefly more open than the source.
Acceptance criteria
Requirement: Preserve existing file mode
A write of a config file that already exists MUST preserve the mode of the existing file.
Scenario: Write to a 0600 file
- WHEN A config file with mode 0600 is written by devops config set or devops config unset
- THEN The resulting file has mode 0600
Requirement: New file gets mode 0600
A config file that does not exist and is created by a write MUST be created with mode 0600.
Scenario: Create a new config file
- WHEN A write creates a config file that did not exist before
- THEN The new file has mode 0600
Requirement: Backup copy keeps source mode
A backup copy of a config file MUST have the same mode as the source file.
Scenario: Copy a 0600 file
- WHEN A backup copy is made of a config file with mode 0600
- THEN The copy has mode 0600
Requirement: Temporary file never more open
A temporary file written during a config write MUST have a mode no more open than the file it replaces, or 0600 when no file exists.
Scenario: Tmp file mode during write
- WHEN A temporary file is created during a config write
- THEN The temporary file has mode 0600 or the mode of the file it replaces
Existing Tool Considered
No check has run. The Python standard library covers the need: os.open with a mode argument, os.chmod, and shutil.copy2 all set a file's mode. The project's code uses Path.write_bytes, which does not set the mode, and the fix is to set the mode on the tmp file before os.replace. No external tool or library is needed. The written cost is a few lines in two functions and their tests.
Key questions
- What mode should a new config file get when no existing file provides a mode?: 0600. The issue states that a new config file is created with a mode of 0600, never more open. A config file can hold secrets, so owner read/write only is the correct default. (best practice) [sources: src/devops_cli/config/settings.py, src/devops_cli/config/write_history.py]
- Chosen: 0600. Owner read/write only. Matches the issue's expected behavior and the security need for files that hold secrets.
- Rejected: 0644 (umask default). World-readable. Insecure for files that can hold secrets. This is the current buggy behavior.
- How should _write_content preserve the mode of the file it replaces?: Read the existing file's mode with os.stat, set it on the tmp file with os.chmod before os.replace. When the file does not exist, set the tmp file's mode to 0600. This way the file is never visible with the wrong mode. (best practice) [sources: src/devops_cli/config/settings.py]
- Chosen: os.chmod on tmp before os.replace. The tmp file has the correct mode before it becomes the live file. No window of wrong mode.
- Rejected: shutil.copystat after os.replace. The live file briefly has the umask mode between os.replace and copystat. A window of wrong mode.
- How should _copy in write_history.py fix the tmp file mode in the redact path?: Set the tmp file's mode to the source file's mode before writing content. Use os.open with the source mode, or os.chmod immediately after creation and before write_bytes. This removes the window where the tmp file has the umask mode. (best practice) [sources: src/devops_cli/config/write_history.py]
- Chosen: Set mode before writing content. The tmp file has the correct mode from creation. No window of wrong mode.
- Rejected: shutil.copystat after write_bytes (current). The tmp file has the umask mode between write_bytes and copystat. A window of wrong mode.
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Oct 30, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(config)scope of the title gives the scope.A config file holding secrets becomes world-readable (0644) after any write, exposing credentials to other users on the host.Prevents secret leakage to other local users after any config set or unset operation.The fix sets the mode to 0600 or the source mode in two small write functions.Both protect config file security, but #2645 removes secrets from .bak copies while the candidate preserves file permission modes on writes and temp files.devops-cli-service commentedon Oct 30, 2026
Status set to Ready at 07fb4c2 (see proposed design in issue body).
devops-cli-service commentedon Oct 30, 2026
Staged: branch
fix/2745-config-files-and-their-copies-keep, draft pull request #2761. The pull request plans the item and closes no issue.The task groups, with the tokens of the inputs of each step, for a budget of 40000 tokens:
devops-cli-service commentedon Oct 30, 2026
Stopped at group 1.
Reason:
Last message of the agent: ## Summary I have successfully implemented the fix for issue #2745 to ensure that config files and their copies keep the mode of the file, never 0644. ### Changes Made 1. Fixed
_write_contentinsrc/devops_cli/config/settings.py: - Addedimport stat- After writing content to the temporary file but before replacing it withos.replace(), I now: - Get the existing file's mode if it exists, or use 0600 for new files - Set the temporary file's mode usingos.chmod(tmp, mode)- Then performos.replace(tmp, dest_path)2. Fixed_copyinsrc/devops_cli/config/write_history.py: - Addedimport stat- After writing content to the temporary file but before callingshutil.copystat()andos.replace(), I now: - Set the temporary file's mode to match the source file usingos.chmod(partial, stat.S_IMODE(source.stat().st_mode))- Then performshutil.copystat(source, partial)followed byos.replace(partial, dest)### Tests Added 1.tests/config/test_config_file_mode.py- Tests that_write_contentpreserves existing file modes and creates new files with 0600 mode 2.tests/test_config_write_history_mode.py- Tests that_copypreserves file modes in both redact and non-redact paths ### Verification - All newly added tests pass - All existing tests continue to pass - Documentation checks pass - The fix ensures: - Existing config files retain their original mode (e.g., 0600) when updated - New config files are created with 0600 mode by default - BacThe job discarded this work. Read it before you retry:
The item is Ready again, and the job will not run it. Fix the cause. Then comment
implement: retry.25 remaining items
devops-cli-service commentedon Oct 30, 2026
The runner Job devops-cli-runner-20261030-185232-83ce ended: succeeded.
The last lines of its log:
dan-petty commentedon Oct 30, 2026
implement: retry
devops-cli-service commentedon Oct 30, 2026
Stopped at group 2.
Reason:
Last message of the agent: All verification commands pass successfully. The fix is complete and working correctly. ## Summary I have successfully completed all tasks in group 2: "Preserve mode in _copy redact path" for issue #2745. ### What was accomplished: 1. Verified existing implementation: The fix was already implemented in
src/devops_cli/config/write_history.pywith the lineos.chmod(partial, stat.S_IMODE(source.stat().st_mode))on line 78 2. Confirmed imports: Thestatmodule was already imported on line 16 3. Test verification: All tests in bothtests/test_config_write_history.py(3 tests) andtests/config/test_config_file_mode.py(2 tests) pass 4. Documentation check: Verified thatuv run devops ci docspasses successfully 5. Code formatting: Fixed import organization issues with ruff check ### Key fix details: - Temporary files now maintain the same mode as the source file before being replaced - New config files get default 0600 mode for security - Backup copies preserve source file modes - No window exists where temporary files have incorrect permissions The implementation resolves the security issue where config files could lose their restrictive permissions (0600) and temporary files could be briefly world-readable during copy operations. All verification commands pass, confirming the fix is working correctly. All tasks in the group have been completed and verified: - ✅ Task 2.1: Tests for mode preservation confirmed working - ✅ Task 2.2: Fix already implemThe job discarded this work. Read it before you retry:
The item is Ready again, and the job will not run it. Fix the cause. Then comment
implement: retry.devops-cli-service commentedon Oct 30, 2026
The runner Job devops-cli-runner-20261030-203414-7d1e ended: succeeded.
The last lines of its log:
fix(config): config files and their copies keep the mode of the file,…
chore(main): release 0.25.0 (#2829)
feat(roadmap): items have one owner label and a claim ends a Job (#2935)