The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(release): R2 client — Content-Length signing + ranged-GET size check
ethernet80233f7b8e71f39d
diff --git a/scripts/msix-shared.mjs b/scripts/msix-shared.mjsindex 61a47acd0d3..606876709c8 100644--- a/scripts/msix-shared.mjs+++ b/scripts/msix-shared.mjs@@ -28,18 +28,36 @@ export const OUT_OF_STORE_PUBLISHER =const CONTENT_TYPES = {'.appinstaller': 'application/appinstaller','.msixbundle': 'application/msixbundle',- '.msix': 'application/msix'+ '.msix': 'application/msix',+ // Termux APT repo artifacts (uploaded under releases/termux/<channel>/).+ // InRelease/Release/Packages are extensionless; match by exact basename+ // too so apt gets text/plain instead of octet-stream.+ '.deb': 'application/vnd.debian.binary-package',+ '.gz': 'application/gzip',+ '.asc': 'text/plain',+ 'release.gpg': 'application/pgp-signature',+ inrelease: 'text/plain',+ release: 'text/plain',+ packages: 'text/plain'}/*** The Content-Type to store for a staged release artifact, if any.- * @param {string} filename the artifact filename- * @returns {string | undefined}+ *+ * Keys starting with '.' (or containing one, like 'release.gpg') match by+ * filename suffix. Extensionless keys (inrelease/release/packages — the apt+ * repo metadata) match by exact basename only, so 'foo-release' or+ * 'xrelease' never collide with the apt 'Release' file.*/export function contentTypeFor(filename) {const lower = String(filename).toLowerCase()- for (const [suffix, mime] of Object.entries(CONTENT_TYPES)) {- if (lower.endsWith(suffix)) return mime+ const base = lower.slice(lower.lastIndexOf('/') + 1)+ for (const [key, mime] of Object.entries(CONTENT_TYPES)) {+ if (key.includes('.')) {+ if (lower.endsWith(key)) return mime+ } else if (base === key) {+ return mime+ }}return undefined}diff --git a/scripts/r2-release.mjs b/scripts/r2-release.mjsindex 46d2a78a38c..ac64b792d74 100644--- a/scripts/r2-release.mjs+++ b/scripts/r2-release.mjs@@ -149,7 +149,7 @@ function requiredEnv(name) {return value}-function r2Headers(method, host, path, query, bodyHash, now, creds, contentType, contentLength) {+function r2Headers(method, host, path, query, bodyHash, now, creds, contentType, contentLength, extraHeaders) {const headers = {host,'x-amz-date': now,@@ -162,6 +162,9 @@ function r2Headers(method, host, path, query, bodyHash, now, creds, contentType,// Stream bodies (files >2GiB) need an explicit Content-Length — R2// rejects a bodyless-length PUT with 411 MissingContentLength.if (contentLength != null) headers['Content-Length'] = String(contentLength)+ // Extra request headers (e.g. Range) join BEFORE signing so they are+ // covered by SignedHeaders like every other header we send.+ if (extraHeaders) Object.assign(headers, extraHeaders)headers.authorization = authHeader({method,host,@@ -176,10 +179,10 @@ function r2Headers(method, host, path, query, bodyHash, now, creds, contentType,return headers}-async function signedFetch(method, url, { body, bodyHash, creds, now, contentType, contentLength }) {+async function signedFetch(method, url, { body, bodyHash, creds, now, contentType, contentLength, extraHeaders }) {const { host, pathname, search } = new URL(url)const query = search.replace(/^\?/, '')- const headers = r2Headers(method, host, pathname, query, bodyHash, now, creds, contentType, contentLength)+ const headers = r2Headers(method, host, pathname, query, bodyHash, now, creds, contentType, contentLength, extraHeaders)const res = await fetch(url, {method,headers,@@ -195,6 +198,12 @@ async function signedFetch(method, url, { body, bodyHash, creds, now, contentTypreturn { res, text }}+/** Signed ranged GET: HEAD responses lose content-length through some+ * proxies; Content-Range on a 1-byte GET is the reliable size oracle. */+async function signedFetchRange(method, url, rangeHeaders, creds, now) {+ return signedFetch(method, url, { bodyHash: EMPTY_SHA, creds, now, extraHeaders: rangeHeaders })+}+async function retry(fn, tries = 3) {let lastErrorfor (let attempt = 1; attempt <= tries; attempt++) {@@ -361,13 +370,31 @@ async function putObject(creds, base, bucket, key, payload, now, contentType) {const { res, text } = await signedFetch('PUT', url, { body, bodyHash, contentLength: size, creds, now, contentType })if (!res.ok) throw new Error(`PUT ${key} -> ${res.status}${text ? `: ${text.slice(0, 300)}` : ''}`)})- const { res: headRes, text: headText } = await retry(async () => {+ // HEAD can come back without content-length (intermediaries strip it on+ // HEAD more readily than on ranged GETs). Fall back to a 1-byte ranged+ // GET, whose Content-Range carries the authoritative total size.+ const { res: headRes } = await retry(async () => {const r = await signedFetch('HEAD', url, { bodyHash: EMPTY_SHA, creds, now })if (!r.res.ok) throw new Error(`HEAD ${key} -> ${r.res.status}`)return r})- const remoteSize = headRes.headers.get('content-length')- if (remoteSize === null || String(remoteSize) !== String(size)) {+ let remoteSize = headRes.headers.get('content-length')+ if (remoteSize === null) {+ const rangeHeaders = { range: 'bytes=0-0' }+ const range = await retry(async () => {+ const r = await signedFetchRange('GET', url, rangeHeaders, creds, now)+ if (!r.res.ok) throw new Error(`GET range ${key} -> ${r.res.status}`)+ return r+ })+ const contentRange = range.res.headers.get('content-range')+ const total = contentRange ? contentRange.match(/bytes 0-0\/(\d+)/) : null+ if (!total) {+ console.error(`::error::R2 ${key}: could not determine remote size (HEAD had no content-length, ranged GET had no content-range: ${contentRange})`)+ process.exit(1)+ }+ remoteSize = total[1]+ }+ if (String(remoteSize) !== String(size)) {console.error(`::error::R2 HEAD ${key}: size mismatch (remote ${remoteSize}, local ${size})`)process.exit(1)}