fix(api): write session config with owner-only permissions#949
Closed
bluetoothbot wants to merge 3 commits into
Closed
fix(api): write session config with owner-only permissions#949bluetoothbot wants to merge 3 commits into
bluetoothbot wants to merge 3 commits into
Conversation
The session-cookie file (~/.config/ufp/unifi_protect.json) was created with aiofiles.open(..., 'wb') and no mode argument, so it landed with the process's default umask permissions — typically 0o644 on most Linux systems, making the bearer cookie readable to any local user on the host. On POSIX systems the new _write_session_config_atomic() helper: - Creates a temporary file via tempfile.mkstemp(), which uses mode 0o600 by default (owner read/write only). - Writes the config data to the temp file. - Atomically renames it into place with aos.replace(), so the live file is never world-readable even briefly. - Tightens the parent config directory to 0o700 on every write, which retroactively fixes directories created under older umask settings. Both _update_auth_config() and clear_session() now route through this helper. Windows keeps the direct write path (POSIX modes do not apply). Fixes uilibs#825
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
Collaborator
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.
What
The UniFi Protect session-cookie file (
~/.config/ufp/unifi_protect.json) was written withaiofiles.open(..., 'wb')and no mode argument, so it landed with the process's umask-derived permissions — typically0o644on most Linux hosts. Any other local user can read the file and copy the bearer cookie to impersonate the authenticated user against the NVR.Fixes #825
Why
On shared Home Assistant appliances or multi-user hosts,
0o644gives group and world read access to a file that contains long-lived session credentials. The fix eliminates that window entirely on POSIX systems.How
Added
_write_session_config_atomic():tempfile.mkstemp(), which creates with0o600(owner read/write only) by default on POSIX — no explicitchmodon the file itself is needed.aiofiles.open(async), then atomically renames into place withaos.replace(), so the live file is never world-readable even momentarily.0o700on every write viaasyncio.to_thread(os.chmod, …), retroactively fixing directories created under an older umask.sys.platform == "win32"guarded,# pragma: no cover).Both
_update_auth_config()andclear_session()now route through this helper. All blocking OS calls (mkstemp,os.close,os.chmod) are wrapped inasyncio.to_threadto satisfy the blockbuster async-purity checker.Testing
5 new tests in
tests/test_api.py(POSIX-only, skipped on Windows):0o6000o700.tmpfile after a successful writeaiofiles.openpatched to raiseOSError)clear_session()rewrites with0o600Full suite: 1683 passed (the one pre-existing failure in
test_public_schema_conformance.py::test_public_model_matches_spec[PublicSensor-sensor]is unrelated and already present onmain). Patch coverage: 100% on new lines.SECURITY — session token file written with world-readable permissions (0o644), allowing local credential theft. Fixed in this PR.
Quality Report
Changes: 2 files changed, 176 insertions(+), 6 deletions(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan