修复 Release 的 CI 产物重定向回公网问题 #6

Merged
lingjhf merged 1 commits from fix/gitea-release-artifact-redirects into main 2026-10-06 08:56:35 +00:00
3 changed files with 40 additions and 1 deletions

No files matched your search

+3
View File
@@ -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 Checkout uses the same endpoint through its `github-server-url` input, retaining
the requested commit/tag and disabling persisted credentials. Release requests the requested commit/tag and disabling persisted credentials. Release requests
retain the public Host header so Gitea response links pass the public-origin checks. 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, Compare warm CI runs after the first cache fill. Measure checks, Android compile,
Windows compile, packaging and artifact transfer separately. Total duration follows Windows compile, packaging and artifact transfer separately. Total duration follows
+7 -1
View File
@@ -4,6 +4,7 @@ from concurrent.futures import ThreadPoolExecutor
import http.client import http.client
import json import json
import os import os
import re
import sys import sys
import time import time
import urllib.error import urllib.error
@@ -55,8 +56,13 @@ def retry_read(operation):
def request(url, data=None, method=None, content_type='application/json'): def request(url, data=None, method=None, content_type='application/json'):
endpoint = transport_url(url) endpoint = transport_url(url)
headers = {'Authorization': 'token ' + os.environ['GITEA_TOKEN']} 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. # 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' headers['Host'] = 'git.orangraph.com'
if data is not None: if data is not None:
headers['Content-Type'] = content_type headers['Content-Type'] = content_type
+30
View File
@@ -43,6 +43,36 @@ class TransferToolsTests(unittest.TestCase):
self.assertEqual(result['sha256'], hashlib.sha256(b'app').hexdigest()) self.assertEqual(result['sha256'], hashlib.sha256(b'app').hexdigest())
self.assertEqual(asset['browser_download_url'], 'https://git.orangraph.com/attachments/example') 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): def test_local_transport_rejects_unexpected_endpoint_or_origin_before_sending_credentials(self):
for internal, url, message in [ for internal, url, message in [
('http://example.com:3000', transfers.SERVER + '/api/v1/releases', 'internal Gitea endpoint'), ('http://example.com:3000', transfers.SERVER + '/api/v1/releases', 'internal Gitea endpoint'),