fix: SonarCloud's findings on the updater-json job
- scripts/updater_json.py takes no file path from the command line any more, only the tag; it reads latest.json, assets.json and the .sig files from the working directory and rewrites latest.json there (pythonsecurity:S8707, path traversal from CLI arguments). The job runs it inside its work directory. New test for main(). - release-desktop.yml asks for nothing at workflow level and gives each of its two jobs the `contents: write` it needs (githubactions:S8233: with a second job, a workflow-level write reached both by default). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37f1783e90
commit
3145ed2f1a
3 files changed
+38
-15
No files matched your search
@@ -11,11 +11,14 @@ on:
|
||||
required: true
|
||||
default: 'desktop-v1.0.0'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
# Nothing by default; each job asks for the write it needs (both write to the
|
||||
# release: the build its artefacts, updater-json latest.json).
|
||||
permissions: {}
|
||||
|
||||
jobs:
|
||||
build:
|
||||
permissions:
|
||||
contents: write
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -119,6 +122,8 @@ jobs:
|
||||
# refuses an entry whose signature is not its file's .sig.
|
||||
updater-json:
|
||||
needs: build
|
||||
permissions:
|
||||
contents: write
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
@@ -135,6 +140,5 @@ jobs:
|
||||
--pattern latest.json --pattern '*.sig'
|
||||
gh release view "$TAG" --repo "$GITHUB_REPOSITORY" --json assets \
|
||||
--jq '[.assets[] | {id: (.apiUrl | split("/") | last), name}]' > "$work/assets.json"
|
||||
python3 scripts/updater_json.py "$work/latest.json" "$work/assets.json" "$work" "$TAG" "$work/fixed.json"
|
||||
mv "$work/fixed.json" "$work/latest.json"
|
||||
(cd "$work" && python3 "$GITHUB_WORKSPACE/scripts/updater_json.py" "$TAG")
|
||||
gh release upload "$TAG" "$work/latest.json" --repo "$GITHUB_REPOSITORY" --clobber
|
||||
@@ -1,4 +1,6 @@
|
||||
"""How updater_json.py points a desktop release's latest.json at its downloads."""
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
import updater_json
|
||||
@@ -46,3 +48,16 @@ def test_a_second_run_changes_nothing():
|
||||
def test_anything_the_updater_feed_would_refuse_fails_loudly(doc, why):
|
||||
with pytest.raises(ValueError, match=why):
|
||||
updater_json.rewrite(doc, TAG, NAMES, SIGS)
|
||||
|
||||
|
||||
def test_main_rewrites_latest_json_in_the_working_directory(tmp_path, monkeypatch):
|
||||
(tmp_path / "latest.json").write_text(json.dumps(_doc(**{
|
||||
"linux-x86_64": {"signature": "sig-appimage", "url": API + "11"},
|
||||
})), encoding="utf-8")
|
||||
(tmp_path / "assets.json").write_text(json.dumps([{"id": k, "name": v} for k, v in NAMES.items()]), encoding="utf-8")
|
||||
for name, sig in SIGS.items():
|
||||
(tmp_path / f"{name}.sig").write_text(sig, encoding="utf-8")
|
||||
monkeypatch.chdir(tmp_path)
|
||||
assert updater_json.main([TAG]) == 0
|
||||
out = json.loads((tmp_path / "latest.json").read_text(encoding="utf-8"))
|
||||
assert out["platforms"]["linux-x86_64"]["url"] == DL + "F2P.Tracker_2.2.1_amd64.AppImage"
|
||||
+15
-11
@@ -10,14 +10,15 @@ https://github.com/<repo>/releases/download/<tag>/<file> and answers 204 -
|
||||
never offered to an installed app. desktop-v2.2.1 was the first release built
|
||||
with v1 and had to be fixed by hand.
|
||||
|
||||
release-desktop.yml runs this once every platform has added its entries:
|
||||
release-desktop.yml runs this once every platform has added its entries, in a
|
||||
directory holding the release's latest.json, its .sig files and assets.json
|
||||
(the release's assets as [{"id": ..., "name": ...}]):
|
||||
|
||||
python scripts/updater_json.py <latest.json> <assets.json> <sig dir> <tag> <out>
|
||||
python scripts/updater_json.py <tag> # rewrites ./latest.json
|
||||
|
||||
assets.json is the release's assets as [{"id": ..., "name": ...}]; the sig dir
|
||||
holds its .sig files. Every entry's signature must be the content of the .sig
|
||||
of the file its new URL names. An entry that already points at this release's
|
||||
download is kept, so a second run changes nothing. Standard library only.
|
||||
Every entry's signature must be the content of the .sig of the file its new
|
||||
URL names. An entry that already points at this release's download is kept,
|
||||
so a second run changes nothing. Standard library only.
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
@@ -66,12 +67,15 @@ def rewrite(doc: dict, tag: str, names_by_id: dict, signatures: dict) -> dict:
|
||||
|
||||
|
||||
def main(argv=None) -> int:
|
||||
latest, assets, sig_dir, tag, out = argv if argv is not None else sys.argv[1:]
|
||||
doc = json.loads(Path(latest).read_text(encoding="utf-8"))
|
||||
names_by_id = {str(a["id"]): a["name"] for a in json.loads(Path(assets).read_text(encoding="utf-8"))}
|
||||
signatures = {p.name.removesuffix(".sig"): p.read_text(encoding="utf-8") for p in Path(sig_dir).glob("*.sig")}
|
||||
# No file path comes from the command line, only the tag: the files are
|
||||
# the fixed names in the working directory (SonarCloud pythonsecurity:S8707).
|
||||
(tag,) = argv if argv is not None else sys.argv[1:]
|
||||
here = Path.cwd()
|
||||
doc = json.loads((here / "latest.json").read_text(encoding="utf-8"))
|
||||
names_by_id = {str(a["id"]): a["name"] for a in json.loads((here / "assets.json").read_text(encoding="utf-8"))}
|
||||
signatures = {p.name.removesuffix(".sig"): p.read_text(encoding="utf-8") for p in here.glob("*.sig")}
|
||||
fixed = rewrite(doc, tag, names_by_id, signatures)
|
||||
Path(out).write_text(json.dumps(fixed, indent=2) + "\n", encoding="utf-8", newline="\n")
|
||||
(here / "latest.json").write_text(json.dumps(fixed, indent=2) + "\n", encoding="utf-8", newline="\n")
|
||||
for key, entry in fixed["platforms"].items():
|
||||
print(f"{key:24} {entry['url']}")
|
||||
return 0
|
||||
|
||||
Reference in new issue
Block a user