Repository navigation
fix(scripts): harden citation.py - #191
Conversation
Port the hardening of the citation helper from proTES (commit 2294b1c in elixir-cloud-aai/protes#244), so that the copies stay consistent: - Write through a temporary file with a new, unpredictable name (tempfile.mkstemp), so that a planted file or link is never written to and concurrent runs don't share it; keep the file's permissions. - Check that the version passed to check is a release version, also if the file has the same invalid one. - Refuse set --date with a date after today (UTC), which check rejects. - Describe these limits in the docstrings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideHardens citation updates by using secure, permission-preserving atomic writes and adds validation that versions are release versions and dates are not in the future, with focused regression tests and updated documentation. Sequence diagram for hardened citation updatesequenceDiagram
participant Caller
participant Main as citation.main
participant Temp as tempfile.mkstemp
participant File as CITATION.cff
Caller->>Main: main(argv, path)
Main->>Main: set_release(text, version, date)
Main->>Temp: mkstemp(prefix, suffix, dir)
Temp-->>Main: descriptor, name
Main->>Main: os.fdopen(descriptor).write(text)
Main->>Main: Path.chmod(mode)
Main->>File: temporary.replace(path)
Main->>Main: temporary.unlink(missing_ok=True)
Flow diagram for citation validationflowchart TD
Start([citation.main]) --> Command{command}
Command -->|set| Date[Select args.date or today]
Date --> Future{date > today}
Future -->|yes| RejectDate[Raise ValueError]
Future -->|no| Set[set_release]
Set --> Write[_write]
Command -->|check| Check[check_release]
Check --> Version[_check_version]
Version --> Release{Release version?}
Release -->|no| RejectVersion[Raise ValueError]
Release -->|yes| Compare[Validate version and date]
Compare --> Result([Return success])
Write --> Result
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #191 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 14
Lines 906 917 +11
=========================================
+ Hits 906 917 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="scripts/citation.py" line_range="212" />
<code_context>
- temporary.write_text(text, encoding="utf-8")
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ file.write(text)
+ temporary.chmod(mode)
temporary.replace(path)
finally:
</code_context>
<issue_to_address>
**Attacker changes arbitrary permissions**
When a privileged `set` runs while an attacker can modify the parent directory, `temporary.chmod(mode)` follows a substituted symlink, and `temporary.replace(path)` can publish the substituted entry, so the attacker changes an arbitrary target’s permissions or replaces the citation file.
Use descriptor-based permission changes and a replacement strategy that cannot follow or publish an attacker-substituted temporary path.
</issue_to_address>
### Comment 2
<location path="scripts/citation.py" line_range="213" />
<code_context>
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ file.write(text)
+ temporary.chmod(mode)
temporary.replace(path)
finally:
temporary.unlink(missing_ok=True)
</code_context>
<issue_to_address>
**Concurrent updates silently overwrite**
When two `set` processes read the same citation file and request different release metadata before either replaces it, `main` builds both outputs from the same snapshot, and `_write` replaces the file without serialization or checking for intervening updates; the later replacement discards the earlier successful update.
Serialize updates to each citation file or reject a replacement when its source snapshot is stale.
</issue_to_address>
### Comment 3
<location path="scripts/citation.py" line_range="213" />
<code_context>
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ file.write(text)
+ temporary.chmod(mode)
temporary.replace(path)
finally:
temporary.unlink(missing_ok=True)
</code_context>
<issue_to_address>
**File access metadata is lost**
When the original file has custom ownership, ACLs or extended attributes that differ from the temporary file, `_write` creates a new inode and copies only the mode bits, so replacing the original loses its owner, group, custom ACLs and extended attributes; collaborators relying on that metadata receive different access.
Preserve the original file’s ownership, ACLs and extended attributes on the temporary file before replacing it.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and if the new checks or write logic are wrong, set could leave an incorrect version or release date in CITATION.cff; that value is bounded and can be corrected in the file. Reverting the script would not undo a file it has already written.
Blocking findings: scripts/citation.py:212, scripts/citation.py:213, scripts/citation.py:213
Set the permissions of the temporary file with os.fchmod on its descriptor instead of by path, so that a link put in its place is not followed (Sourcery review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Ports the hardening of
scripts/citation.pyfrom proTES (commit 2294b1c, elixir-cloud-aai/protes#244), with its tests, so that the copies in proTES, FOCA and Cloud Registry stay consistent:setwrites through a temporary file with a new, unpredictable name (tempfile.mkstemp) in the same directory instead of the fixed.CITATION.cff.tmp, and keeps the file's permissions: a planted file or link is never written to, and concurrent runs don't collide.checkrejects versions that are not release versions, also if the file has the same one.set --daterefuses dates after today (UTC), whichcheckwould reject.Beyond proTES (from Sourcery's review, to be ported back to proTES): the temporary file's mode is set with
os.fchmodon its descriptor instead of by name.The module docstring and the
VERSIONcomment keep their wording here: unlike proTES, this repository has a release workflow, which checks the tag (scripts/release_version.py) and then runscitation.py check.Closes #190
Verification
Locally (Python 3.13):
coverage run --source cloud_registry,scripts -m pytest -W ignore::DeprecationWarning,coverage report -m: 859 passed, 210 skipped (no local MongoDB), 100% (scripts/citation.py100%)ruff check,ruff format --check,mypy: passast-grep scan,semgrep scan --config .semgrep --erroron the changed files: no findingspre-commit run --all-filescould not run (the sandbox cannot fetch the hook repositories); CI runs itCI: all checks pass on the last commit (tests with mongomock and MongoDB 7.0, static code analysis, docs, Helm chart, image scan, CodeQL, Codecov patch/project).
Declined review findings
--datehelp text that later dates are refused; and drop theassert not path.is_symlink()that cannot fail in the new test: both left as in proTES, to keep the copies identical (could be changed in all three later).CITATION.cffis replaced by a regular file: unchanged behaviour, the repository's file is no link.replacecould publish an entry substituted for the temporary file (whoever can do that can replaceCITATION.cffdirectly); concurrentsetruns overwrite each other (manual, one-off release step; the file is always complete andcheckverifies it); owner, ACLs and extended attributes are not kept (Git keeps none of them; the mode is kept).🤖 Generated with Claude Code