Repository navigation
fix(scripts): harden citation.py - #431
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 GuideThis PR ports citation hardening into the repository: it makes writes safer and permission-preserving via descriptor-based temporary files and atomic replacement, and aligns set/check validation around release versions and UTC date limits, with focused regression tests. Sequence diagram for hardened citation updatesequenceDiagram
participant Caller
participant Main as citation.main
participant File as CITATION.cff
participant Temp as TemporaryFile
Caller->>Main: main(argv, path)
Main->>File: read_text()
Main->>Main: set_release()
Main->>Temp: tempfile.mkstemp()
Main->>Temp: os.fchmod()
Main->>Temp: write()
Main->>Temp: replace(path)
Main->>Temp: unlink(missing_ok=True)
Flow diagram for citation validation limitsflowchart TD
A["citation.main()"] --> B["Read CITATION.cff"]
B --> C{"set or check?"}
C -->|set| D["Validate date is not after today UTC"]
D --> E["set_release()"]
E --> F["Atomic permission-preserving write"]
C -->|check| G["_check_version()"]
G --> H["check_release()"]
H --> I["Validate version and released date"]
File-Level Changes
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 #431 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 2869 2869
=========================================
Hits 2869 2869
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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]>
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="scripts/citation.py" line_range="179" />
<code_context>
+ differs or is missing, or `date-released` is missing, invalid or
+ later than `today`.
"""
+ _check_version(version)
actual = _value(text, "version")
if actual != version:
</code_context>
<issue_to_address>
**Accepted release tags fail citation check**
When a release tag has a leading-zero version component or prerelease number, such as `v1.2.3rc01`, `_check_version(version)` rejects the tag even though the release workflow accepts it, so `citation.py check` fails and stops the release job.
Make `_check_version` accept the versions allowed by the release tag gate, or align the two validations.
</issue_to_address>
### Comment 2
<location path="scripts/citation.py" line_range="212" />
<code_context>
try:
- temporary.write_text(text, encoding="utf-8")
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ os.fchmod(file.fileno(), mode)
+ file.write(text)
temporary.replace(path)
</code_context>
<issue_to_address>
**Windows set command crashes**
When the documented `set` command runs on Windows, `os.fchmod` raises `AttributeError` because it is unavailable on Windows, and `main` does not catch that exception, so the command exits with a traceback without updating the citation file.
Avoid calling `os.fchmod` on Windows and use a platform-supported way to handle the file mode.
</issue_to_address>
### Comment 3
<location path="scripts/citation.py" line_range="214" />
<code_context>
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ os.fchmod(file.fileno(), mode)
+ file.write(text)
temporary.replace(path)
finally:
temporary.unlink(missing_ok=True)
</code_context>
<issue_to_address>
**Concurrent sets overwrite citation state**
When two `set` processes target the same citation file with different versions or dates, both `main` calls can read the same starting file, compute different release metadata, and then replace it in succession; the earlier caller still returns success even though its requested version or date is gone.
Serialize `set` operations or verify the file has not changed since it was read before replacing it.
</issue_to_address>
### Comment 4
<location path="scripts/citation.py" line_range="212" />
<code_context>
try:
- temporary.write_text(text, encoding="utf-8")
+ with os.fdopen(descriptor, "w", encoding="utf-8") as file:
+ os.fchmod(file.fileno(), mode)
+ file.write(text)
temporary.replace(path)
</code_context>
<issue_to_address>
**Setuid or setgid bits are lost**
When the existing citation file has setuid or setgid bits, `_write` applies the original mode before writing, and writing can clear those bits on POSIX; `temporary.replace(path)` then publishes a file without them.
Apply the original mode after writing the temporary file and before replacing the citation file.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and if the script writes an incorrect version or release date, that value remains in the citation file after reverting the code, but it is bounded and can be corrected or regenerated. The new checks and safer atomic replacement do not introduce an apparent irreversible or externally consequential action.
Blocking findings: scripts/citation.py:179, scripts/citation.py:212, scripts/citation.py:214, scripts/citation.py:212
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 of the same change in elixir-cloud-aai/cloud-registry#191, 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 and then runscitation.py check. FOCA's import order anddatetime.UTCare kept.Closes #430
Verification
Locally (Python 3.12):
pytest tests/test_citation.py: 45 passedcoverage run -m pytest -m "not mongodb",coverage report: 1738 passed, 100% (no local MongoDB for themongodbtests; CI runs them)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 the lint jobCI: all checks pass on the last commit (lint, unit tests on Python 3.11-3.14 with MongoDB 7.0 and 8.0, lowest direct dependencies, integration tests, image scans, charts on kind, docs, 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).checkrejects tags with leading zeros (v1.2.3rc01) that the tag pattern of the release workflow accepts (intended: non-canonical,setnever writes them, and the workflows are out of scope);os.fchmodis missing on Windows before Python 3.13 (release steps are documented for a POSIX shell); setuid/setgid bits may be lost (Git keeps none); concurrentsetruns (as above).🤖 Generated with Claude Code
Summary by Sourcery
Harden citation metadata updates and validation to make release-file handling safer and more consistent.
Bug Fixes:
Enhancements:
Documentation:
Tests: