fix(release): Gitea 写接口必须主动带 Authorization 头

症状:GET 全绿、POST 一律 401「token is required」。
根因:本仓 Gitea 的**读接口允许匿名访问**(GET 不带凭据也过),
而写接口返回的 401 **不带 WWW-Authenticate 挑战头**
⇒ urllib 的 HTTPBasicAuthHandler 永远不触发重试 ⇒ 压根没把凭据带上。
(第一版用 BasicAuthHandler,看着像「凭据不对」,实则是「没带」。)

修法:所有请求显式带 `Authorization: Basic <b64>` 头(含资产下载算 sha256)。
顺带去掉 remote_asset_index 里每个资产多打一次元数据接口的冗余请求。
This commit is contained in:
admin committed 2026-11-10 14:32:29 +08:00
1 parent e00da151e4
commit a35f9fa077
1 file changed
+34 -31
+34 -31
View File
@@ -29,6 +29,7 @@
"""
import argparse
import base64
import hashlib
import json
import os
@@ -89,10 +90,18 @@ def git_credential():
def api(method, path, user, pwd, data=None, raw=None, ctype=None):
"""调用 Gitea API。data = json body;raw = 二进制 body。"""
"""调用 Gitea API。data = json body;raw = 二进制 body。
★ 必须**主动带 Authorization 头**,不能用 urllib 的 HTTPBasicAuthHandler:
本仓 Gitea 的读接口允许匿名访问(GET 不带凭据也过),
而写接口返回的 401 **不带 WWW-Authenticate 挑战头** ⇒
BasicAuthHandler 永远不会触发重试 ⇒ POST 一律 401「token is required」。
症状:GET 全绿、POST 全 401,看起来像「凭据不对」,其实是「压根没带上」。
"""
url = API + path
body = None
headers = {}
headers = {"Authorization": "Basic " + base64.b64encode(
("%s:%s" % (user, pwd)).encode()).decode()}
if data is not None:
body = json.dumps(data).encode()
headers["Content-Type"] = "application/json"
@@ -101,11 +110,8 @@ def api(method, path, user, pwd, data=None, raw=None, ctype=None):
if ctype:
headers["Content-Type"] = ctype
req = urllib.request.Request(url, data=body, headers=headers, method=method)
mgr = urllib.request.HTTPBasicAuthHandler()
mgr.add_password(realm=None, uri=url, user=user, passwd=pwd)
opener = urllib.request.build_opener(mgr)
try:
with opener.open(req, timeout=300) as r:
with urllib.request.urlopen(req, timeout=300) as r:
payload = r.read()
if not payload:
return {}
@@ -123,47 +129,44 @@ def list_releases(user, pwd):
def remote_asset_index(user, pwd):
"""扫远端所有 Release 的资产 ⇒ {sha256: {name, tag, id, size}}。
"""扫远端所有 Release 的资产 ⇒ {sha256: {name, tag, id, size, url}}。
★ 这是「只发一次」的唯一判据:内容寻址,不靠文件名。
★ 代价:要把每个资产下载一遍算 sha256(依赖资产有 50MB 级的)。
资产数量不大(每版十来个),可接受;换来的是「判据唯一且不会错」。
"""
index = {}
for rel in list_releases(user, pwd):
tag = rel.get("tag_name")
for a in rel.get("assets", []):
aid = a.get("id")
url = a.get("browser_download_url") or ""
dl = a.get("browser_download_url") or ""
# API 给的是 localhost,要换 127.0.0.1 才连得上
url = url.replace("http://localhost:13300", "http://127.0.0.1:13300")
idx_url = "%s/api/v1/repos/%s/releases/%s/assets/%s" % (API, REPO, rel["id"], aid)
meta = api("GET", idx_url, user, pwd)
dl = meta.get("browser_download_url") or url
dl = dl.replace("http://localhost:13300", "http://127.0.0.1:13300")
try:
data = api("GET", "/repos/%s/releases/%s/assets/%s" % (REPO, rel["id"], aid),
user, pwd)
digest = sha256_file_from_url(dl, user, pwd)
except Exception:
digest = None
if digest:
index[digest] = {
"name": a.get("name"),
"tag": tag,
"id": aid,
"size": a.get("size"),
"url": dl,
}
except Exception as e:
print("WARN: 资产 %s 算 sha256 失败(%s)" % (a.get("name"), e))
continue
index[digest] = {
"name": a.get("name"),
"tag": tag,
"id": a.get("id"),
"size": a.get("size"),
"url": dl,
}
return index
def sha256_file_from_url(url, user, pwd):
"""下载并算 sha256(流式,不把大文件整个读进内存)。"""
req = urllib.request.Request(url)
mgr = urllib.request.HTTPBasicAuthHandler()
mgr.add_password(realm=None, uri=url, user=user, passwd=pwd)
opener = urllib.request.build_opener(mgr)
"""下载并算 sha256(流式,不把大文件整个读进内存)。
★ 同样必须主动带 Authorization 头(见 api() 的说明)。
"""
h = hashlib.sha256()
with opener.open(req, timeout=600) as r:
headers = {"Authorization": "Basic " + base64.b64encode(
("%s:%s" % (user, pwd)).encode()).decode()}
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req, timeout=600) as r:
for chunk in iter(lambda: r.read(1024 * 1024), b""):
h.update(chunk)
return h.hexdigest()