From 4346d9330a0f978011aac0b06d94ad8b70a99455 Mon Sep 17 00:00:00 2001 From: lingjhf Date: Tue, 6 Oct 2026 16:41:45 +0800 Subject: [PATCH] fix(release): keep signed CI archive redirects on loopback --- .gitea/CI_PERFORMANCE.md | 3 +++ tool/gitea/release_transport.py | 8 +++++++- tool/gitea/test_transfer_tools.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/.gitea/CI_PERFORMANCE.md b/.gitea/CI_PERFORMANCE.md index 311a8f1..fb0607c 100644 --- a/.gitea/CI_PERFORMANCE.md +++ b/.gitea/CI_PERFORMANCE.md @@ -52,6 +52,9 @@ configure a reachable private endpoint before using runners on other hosts. Checkout uses the same endpoint through its `github-server-url` input, retaining the requested commit/tag and disabling persisted credentials. Release requests retain the public Host header so Gitea response links pass the public-origin checks. +CI artifact ZIP download requests retain the loopback Host instead: Gitea returns +a signed redirect to the raw archive, and its Host must remain local for that +second request to avoid the public tunnel. Release attachment links stay public. Compare warm CI runs after the first cache fill. Measure checks, Android compile, Windows compile, packaging and artifact transfer separately. Total duration follows diff --git a/tool/gitea/release_transport.py b/tool/gitea/release_transport.py index 7384f0d..a66eaa7 100644 --- a/tool/gitea/release_transport.py +++ b/tool/gitea/release_transport.py @@ -4,6 +4,7 @@ from concurrent.futures import ThreadPoolExecutor import http.client import json import os +import re import sys import time import urllib.error @@ -55,8 +56,13 @@ def retry_read(operation): def request(url, data=None, method=None, content_type='application/json'): endpoint = transport_url(url) headers = {'Authorization': 'token ' + os.environ['GITEA_TOKEN']} - if endpoint != url: + local_archive = (data is None and method in (None, 'GET') + and re.fullmatch(r'/api/v1/repos/[^/]+/[^/]+/actions/artifacts/\d+/zip', + urllib.parse.urlparse(url).path)) + if endpoint != url and not local_archive: # Gitea constructs response links from Host, even when reached locally. + # CI ZIP downloads redirect to a signed raw URL. Keep their local Host + # so urllib follows that redirect locally instead of returning to FRP. headers['Host'] = 'git.orangraph.com' if data is not None: headers['Content-Type'] = content_type diff --git a/tool/gitea/test_transfer_tools.py b/tool/gitea/test_transfer_tools.py index 69f3d66..6d7cd55 100644 --- a/tool/gitea/test_transfer_tools.py +++ b/tool/gitea/test_transfer_tools.py @@ -43,6 +43,36 @@ class TransferToolsTests(unittest.TestCase): self.assertEqual(result['sha256'], hashlib.sha256(b'app').hexdigest()) self.assertEqual(asset['browser_download_url'], 'https://git.orangraph.com/attachments/example') + def test_local_ci_archive_request_keeps_local_host_for_signed_redirect(self): + class Response(io.BytesIO): + headers = {'Content-Type': 'application/zip'} + + with patch.dict('os.environ', {'CUTDEX_GITEA_INTERNAL_URL': 'http://127.0.0.1:3000', + 'GITEA_TOKEN': 'test-token'}), \ + patch.object(transfers.urllib.request, 'urlopen', return_value=Response(b'archive')) as open_url: + result = transfers.request(transfers.SERVER + '/api/v1/repos/owner/repo/actions/artifacts/40/zip') + request = open_url.call_args.args[0] + self.assertEqual(result, b'archive') + self.assertEqual(request.full_url, 'http://127.0.0.1:3000/api/v1/repos/owner/repo/actions/artifacts/40/zip') + self.assertIsNone(request.get_header('Host')) + self.assertEqual(request.get_header('Authorization'), 'token test-token') + + def test_local_host_exception_does_not_change_other_api_response_links(self): + for path, method in [ + ('/api/v1/repos/owner/repo/actions/artifacts/40/zip', 'POST'), + ('/api/v1/repos/owner/repo/actions/artifacts/not-a-number/zip', None), + ('/api/v1/repos/owner/repo/releases/40/zip', None), + ]: + class Response(io.BytesIO): + headers = {'Content-Type': 'application/json'} + + with self.subTest(path=path, method=method), \ + patch.dict('os.environ', {'CUTDEX_GITEA_INTERNAL_URL': 'http://127.0.0.1:3000', + 'GITEA_TOKEN': 'test-token'}), \ + patch.object(transfers.urllib.request, 'urlopen', return_value=Response(b'{}')) as open_url: + transfers.request(transfers.SERVER + path, method=method) + self.assertEqual(open_url.call_args.args[0].get_header('Host'), 'git.orangraph.com') + def test_local_transport_rejects_unexpected_endpoint_or_origin_before_sending_credentials(self): for internal, url, message in [ ('http://example.com:3000', transfers.SERVER + '/api/v1/releases', 'internal Gitea endpoint'),