diff --git a/.ci-scripts/install-pony-tools.py b/.ci-scripts/install-pony-tools.py index 6166b58..71b0476 100644 --- a/.ci-scripts/install-pony-tools.py +++ b/.ci-scripts/install-pony-tools.py @@ -1,6 +1,7 @@ import argparse import hashlib import json +import os import re import subprocess import tempfile @@ -21,11 +22,12 @@ def get(url, token=None, accept=None): def release(tool, filename): base = f"https://api.github.com/repos/ponylang/{tool}/releases" + token = os.environ.get("GITHUB_TOKEN") url = base + "/latest" while url: if url == base + "/latest": try: - with get(url) as response: + with get(url, token) as response: releases = [json.load(response)] except urllib.error.HTTPError as err: if err.code != 404: @@ -33,10 +35,12 @@ def release(tool, filename): releases = [] url = base + "?per_page=20" else: - with get(url) as response: + with get(url, token) as response: releases = json.load(response) link = re.search(r'<([^>]+)>; rel="next"', response.headers.get("Link", "")) url = link[1] if link else None + if url and not url.startswith("https://api.github.com/"): + raise ValueError(f"Unexpected release pagination URL for {tool}") for item in releases: if item["prerelease"] or item["draft"]: continue diff --git a/.ci-scripts/test-install-pony-tools.py b/.ci-scripts/test-install-pony-tools.py new file mode 100644 index 0000000..c3f9441 --- /dev/null +++ b/.ci-scripts/test-install-pony-tools.py @@ -0,0 +1,54 @@ +import io +import json +import os +import runpy +import unittest +import urllib.request +from pathlib import Path +from unittest.mock import patch + + +release = runpy.run_path(str(Path(__file__).with_name("install-pony-tools.py")))["release"] +base = "https://api.github.com/repos/ponylang/corral/releases" +first = base + "?per_page=20" +next_page = "https://api.github.com/repositories/96358645/releases?page=2" +archive = "https://github.com/ponylang/corral/releases/download/1/corral.zip" + + +def page(items, next_url=None): + response = io.BytesIO(json.dumps(items).encode()) + response.headers = {"Link": f'<{next_url}>; rel="next"'} if next_url else {} + return response + + +class ReleaseRequests(unittest.TestCase): + def test_credentials_on_metadata_requests(self): + assets = [ + {"name": "corral.zip" + suffix, "state": "uploaded", "browser_download_url": archive + suffix} + for suffix in ("", ".sha512") + ] + item = {"draft": False, "prerelease": False, "assets": assets} + for token in ("", "job-token"): + with self.subTest(token=token), patch.dict(os.environ, GITHUB_TOKEN=token): + responses = [page(dict(item, assets=[])), page([], next_page), page([item]), io.BytesIO(b"a" * 128)] + with patch.object(urllib.request, "urlopen", side_effect=responses) as fetch: + url, _, _, download_token = release("corral", "corral.zip") + self.assertEqual(url, archive) + self.assertIsNone(download_token) + auth = f"Bearer {token}" if token else None + requests = [(r.args[0].full_url, r.args[0].get_header("Authorization")) for r in fetch.call_args_list] + self.assertEqual(requests, [ + (base + "/latest", auth), (first, auth), (next_page, auth), (archive + ".sha512", None) + ]) + + def test_rejects_off_site_release_pagination(self): + responses = [page({"draft": True, "prerelease": False}), page([], "https://example.org/releases?page=2")] + with patch.dict(os.environ, GITHUB_TOKEN="job-token"): + with patch.object(urllib.request, "urlopen", side_effect=responses) as fetch: + with self.assertRaisesRegex(ValueError, "Unexpected release pagination URL"): + release("corral", "corral.zip") + self.assertEqual(fetch.call_count, 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/workflows/breakage-against-windows-ponyc-latest.yml b/.github/workflows/breakage-against-windows-ponyc-latest.yml index 9d5a0ac..7b412f9 100644 --- a/.github/workflows/breakage-against-windows-ponyc-latest.yml +++ b/.github/workflows/breakage-against-windows-ponyc-latest.yml @@ -14,6 +14,8 @@ jobs: steps: - uses: actions/checkout@v6.0.2 - name: Install pony tools + env: + GITHUB_TOKEN: ${{ github.token }} run: python .ci-scripts/install-pony-tools.py x86-64-pc-windows-msvc nightly release - name: Test with most recent ponyc nightly env: diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 814d200..6696584 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -112,6 +112,8 @@ jobs: runs-on: windows-2025-vs2026 steps: - uses: actions/checkout@v6.0.2 + - name: Test CI installer release lookups + run: python .ci-scripts/test-install-pony-tools.py - name: install pony tools run: python .ci-scripts/install-pony-tools.py x86-64-pc-windows-msvc nightly nightly - name: Test with most recent ponyc release diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 574e92f..306ed27 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -87,6 +87,8 @@ jobs: GITHUB_REPOSITORY: ${{ github.repository }} x86-64-apple-darwin-release: + permissions: + contents: read name: Build and upload x86-64-apple-darwin runs-on: macos-15-intel needs: @@ -94,6 +96,8 @@ jobs: steps: - uses: actions/checkout@v6.0.2 - name: install pony tools + env: + GITHUB_TOKEN: ${{ github.token }} run: python3 .ci-scripts/install-pony-tools.py x86-64-apple-darwin nightly release - name: brew install dependencies run: | @@ -108,6 +112,8 @@ jobs: GITHUB_REPOSITORY: ${{ github.repository }} arm64-apple-darwin-release: + permissions: + contents: read name: Build and upload arm64-apple-darwin runs-on: macos-26 needs: @@ -115,6 +121,8 @@ jobs: steps: - uses: actions/checkout@v6.0.2 - name: install pony tools + env: + GITHUB_TOKEN: ${{ github.token }} run: python3 .ci-scripts/install-pony-tools.py arm64-apple-darwin nightly release - name: brew install dependencies run: | @@ -130,6 +138,8 @@ jobs: GITHUB_REPOSITORY: ${{ github.repository }} x86-64-pc-windows-msvc-release: + permissions: + contents: read name: Build and upload x86-64-pc-windows-msvc-release runs-on: windows-2025-vs2026 needs: @@ -137,6 +147,8 @@ jobs: steps: - uses: actions/checkout@v6.0.2 - name: install pony tools + env: + GITHUB_TOKEN: ${{ github.token }} run: python .ci-scripts/install-pony-tools.py x86-64-pc-windows-msvc nightly release - name: Build and upload run: | @@ -160,6 +172,8 @@ jobs: GITHUB_REPOSITORY: ${{ github.repository }} arm64-pc-windows-msvc-release: + permissions: + contents: read name: Build and upload arm64-pc-windows-msvc-release runs-on: windows-11-arm needs: @@ -167,6 +181,8 @@ jobs: steps: - uses: actions/checkout@v6.0.2 - name: install pony tools + env: + GITHUB_TOKEN: ${{ github.token }} run: python .ci-scripts/install-pony-tools.py arm64-pc-windows-msvc nightly release - name: Build and upload run: |