From 8f50e00806bed7996788e490c0d46636ad6f62fe Mon Sep 17 00:00:00 2001 From: Willie Chalmers III Date: Sun, 11 Oct 2026 11:54:40 -0700 Subject: [PATCH 1/2] feat(cli): seed retained OpenNext cache with bucket-scoped S3 Use the existing account-owned deployment token to derive ephemeral in-memory S3 signing credentials after fixed account self-verification. This avoids requiring an account-wide R2 REST grant for bucket-selected object policies. Require an explicit transport for future cache candidates, preserve the native REST option without fallback, and bind transport, endpoint and byte inventory to candidate-scoped journals. Bound HTTP exchanges and cache inventories, verify full readback digests, reconcile resumed objects, and recheck approval immediately before every PUT attempt. Preserve immutable v0.1.8 consumers. Document the DNS/deadline boundary and pending signing/provider acceptance. Migrate four existing REST fixture declarations without changing their cases or assertions. Ruff, Python compilation and diff checks passed; no tests ran under the task restriction. No release version or dependencies changed. Co-authored-by: Codex --- docs/rollout.md | 34 ++++ docs/websites.md | 90 +++++++-- studio/r2_s3.py | 248 ++++++++++++++++++++++++ studio/websites.py | 104 +++++++++- tests/test_opennext_cache.py | 4 +- tests/test_workers_framework_runtime.py | 4 +- 6 files changed, 460 insertions(+), 24 deletions(-) create mode 100644 studio/r2_s3.py diff --git a/docs/rollout.md b/docs/rollout.md index aeb86b4..79110cc 100644 --- a/docs/rollout.md +++ b/docs/rollout.md @@ -188,3 +188,37 @@ on Node 24.20.0, together with four Swift tests, actionlint, runtime assembly an fix. Hosted workflow execution, real signing, private package delivery and provider promotion remain external acceptance gates; no push or release was performed for these changes. + + +## Bucket-scoped R2 S3 transport: implementation pending acceptance + +The next runtime source adds a stdlib-only, query-free SigV4 R2 PUT/GET adapter +and requires an explicit remote OpenNext cache transport. This addresses the +published v0.1.8 Wrangler REST path's incompatibility with documented S3-only +bucket-selected Object Read/Write tokens. Existing v0.1.8 runtime bytes, release +pins and artifacts remain immutable. No release version is bumped here. + +S3 derives credentials only in memory from the existing account-owned CI token, +after fixed account self-verification. It records nonsecret identity/time +metadata, verifies exact raw object bytes and lengths, bounds inventory and +HTTP reads, and rechecks completed objects on resume. The absolute 90-second +exchange timer begins after connection; native TCP/TLS connect and socket +operations use a 30-second timeout, while OS DNS resolution remains outside +the timer. Candidate-scoped journal identity, staging/production isolation and +authorization before each PUT/retry +and final Worker upload remain enforced. Explicit wrangler-rest compatibility +has no automatic fallback or account-wide grant. + +This is source preparation only. Per the task restriction no tests were added +or run. +Four existing fixture declaration literals were migrated to explicit +`wrangler-rest`, preserving every case and assertion; this is configuration +migration, not test acceptance. Future product callers must explicitly select +transport before adopting this runtime. Signing vectors and independent signer +comparison, fault/resume/bounds cases, local native cache acceptance and actual +narrowly scoped token S3 PUT/GET remain +unperformed. Static syntax/diff checks are recorded separately in the migration +acceptance receipt. No provider write, credential creation/read, dependency +installation, release or product adoption occurred during implementation. Context7 was already +monthly-quota blocked; current official Cloudflare and AWS source documents +supplied this protocol proposal, not a successful Context7 retrieval. diff --git a/docs/websites.md b/docs/websites.md index 574b3b4..3925585 100644 --- a/docs/websites.md +++ b/docs/websites.md @@ -154,8 +154,19 @@ Pages/Vercel cutovers retain their old provider deployment separately. Local moc ## Retained OpenNext R2 cache -Declare `deploy.opennext_cache.directory: .open-next/cache` when a Workers site -uses OpenNext's `NEXT_INC_CACHE_R2_BUCKET`. Keep the complete native cache tree +A cache-enabled candidate using this runtime must declare its remote transport: + +```yaml +deploy: + opennext_cache: + directory: .open-next/cache + transport: s3 +``` + +Use this when a Workers site uses OpenNext's `NEXT_INC_CACHE_R2_BUCKET`. Missing +or unknown transport fails before network writes; this is an explicit contract +change for future candidates. Already published v0.1.8 candidates retain their +immutable runtime and original declaration contract. Keep the complete native cache tree in the reviewed archive. Each environment must declare that binding exactly once with a separate bucket name. Production and staging bucket identities, including any preview bucket aliases, cannot overlap. An optional environment @@ -171,21 +182,66 @@ configuration (`images: {"binding": "IMAGES"}` with optional boolean `remote`) is accepted and must be explicit in both environments. The cache build ID must match the native `BUILD_ID` file in the retained asset directory. -Before uploading the Worker, pinned Wrangler uploads the exact retained bytes -with `r2 object put --remote`, reads each object with `r2 object get --remote`, -and checks SHA-256. This path never invokes OpenNext's source dependency -resolver, builds source, or provisions buckets. A missing bucket or missing -object permission fails before Worker upload. Jurisdiction is a bounded opaque -identifier passed unchanged, matching the pinned native optional string field; -provider support is checked by the native object operations. Three attempts cover ambiguous -native command failures by repeating the same bucket, key and bytes. +For `s3`, the shared runtime uploads exact retained bytes to the bucket's R2 +S3 endpoint, then streams a GET and verifies both byte length and SHA-256 before +Worker upload. It performs only PUT/GET on the declared bucket and native keys. +There is no OpenNext resolver, source build, bucket creation, multipart upload, +object deletion or automatic REST fallback. Fixed endpoints support default, +EU, US and FedRAMP jurisdictions; unsupported jurisdictions fail before writes. +Region is `auto`. Cache inventory is bounded to 10,000 objects, 64 MiB each and +512 MiB total, with a separate 40,000 filesystem-entry traversal bound. These +are runtime safeguards, not Cloudflare service limits. Native TCP/TLS connect +and socket operations use a 30-second timeout. After connection, an absolute +90-second shutdown timer bounds the exchange, including header parsing, writes +and streamed reads. OS DNS resolution is outside that timer; this is not a +whole-operation DNS deadline. HTTP responses reject redirects, oversized bodies, +malformed framing, truncated metadata and unexpected content encoding. ETags do not substitute for full byte readback. + +The existing staging or production `CLOUDFLARE_API_TOKEN` must be account-owned +and have Editor for the selected existing Worker plus **Workers R2 Storage +Bucket Item Write** for only that environment's bucket. The bucket resource is +`com.cloudflare.edge.r2.bucket.__`; +it is not the public bucket ID. Bootstrap owns routing: changing routes during +deployment requires separate zone permissions and is not granted by bucket +scope. Environment credentials must match the reviewed provider policy; a token +activity response alone does not prove scope. + +The runtime verifies the token at the fixed account-owned +`GET /accounts//tokens/verify`, requires an active ID and valid optional +time bounds, and derives S3 credentials in memory: access key ID = token ID; +secret key = lowercase SHA-256 hex of the exact token value. It does not persist +additional AWS secrets or place them in command arguments, runtime bindings or +artifacts. Nonsecret execution receipts record token ID, account and validity +metadata so the actual credential can be correlated with its reviewed policy. +Authorization headers, derived keys and response payloads are never diagnostic +output. Verification errors do not trigger a user-token endpoint fallback or +API Tokens Read/Edit grant. + +`transport: wrangler-rest` is available only as an explicit compatibility choice +for independently reviewed existing credentials that authorize the native REST +object operations. Bucket Object Read/Write is documented as S3-only and cannot +be assumed to authorize REST. That option retains pinned Wrangler object +PUT/GET and jurisdiction handling; it never receives an automatic permission +upgrade. The local target continues to use Wrangler's local store with a +separate target identity, regardless of the declared remote transport. + +S3 operations retry transport ambiguity, HTTP 408/429 and 5xx at most three +times with bounded backoff, repeating the same bucket/key/bytes. Authorization, +malformed responses, redirects and readback mismatches fail closed. No +persistent additional S3 credential is needed. R2 temporary credentials are an +optional later delegation from an existing parent token, not a native GitHub +OIDC exchange; this runtime does not mint them. Production records every cache object in the existing durable promotion journal. Receipt slots are namespaced by immutable candidate ID and component, allowing dependent candidates in the same journal. The identity binds the account, Worker, environment, artifact metadata, -bucket, jurisdiction and complete cache inventory. Interrupted runs retain -completed object receipts and retry failed or running operations. A changed +bucket, jurisdiction, transport, endpoint and complete cache inventory including +lengths and digests. Interrupted runs retain completed object receipts and retry +failed or running operations. S3 resume verifies completed objects again; a +missing or mismatched object is repaired only by a newly authorized PUT of the +same retained bytes followed by fresh GET verification. Rotatable token IDs are +audit metadata and do not change the immutable plan identity. A changed identity cannot reuse those receipts. Promotion rechecks the current human authorization immediately before each remote object upload or retry and before the final Worker upload; revocation stops further writes and preserves receipts. Staging keeps its receipt beneath the @@ -199,3 +255,13 @@ Local acceptance uses the same adapter with an explicit local target and a separate receipt identity. The native tests round-trip raw objects through Wrangler, compare keys with pinned OpenNext, and run real Next archives with R2 incremental cache after extraction. + + +The credential and protocol contracts above follow current +[Cloudflare R2 authentication](https://developers.cloudflare.com/r2/api/tokens/), +[account-token verification](https://developers.cloudflare.com/api/resources/accounts/subresources/tokens/methods/verify/), +[Worker authorization](https://developers.cloudflare.com/workers/authorization/) +and [AWS SigV4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html). +Implementation source and static validation are not signing-vector, native S3, +or provider acceptance. The scoped account-token PUT/GET path must be accepted +with the real reviewed staging credential before adoption. diff --git a/studio/r2_s3.py b/studio/r2_s3.py new file mode 100644 index 0000000..1d2cc30 --- /dev/null +++ b/studio/r2_s3.py @@ -0,0 +1,248 @@ +"""Bounded R2 PUT/GET using an account-owned token, without stored S3 keys.""" +from datetime import datetime, timezone +import hashlib +import hmac +import http.client +import json +import os +import re +import socket +import ssl +import threading +import time +from urllib.parse import quote + +MAX_OBJECT_BYTES = 64 * 1024 * 1024 +MAX_CACHE_BYTES = 512 * 1024 * 1024 +MAX_CACHE_ENTRIES = 10_000 +MAX_METADATA_BYTES = 64 * 1024 +REQUEST_SECONDS = 90 +SOCKET_SECONDS = 30 + + +class R2TransportError(RuntimeError): + """Safe diagnostics never include headers, credentials, or response bodies.""" + + def __init__(self, message, *, retryable=False, status=None): + super().__init__(message) + self.retryable = retryable + self.status = status + + +class R2ReadbackMismatch(R2TransportError): + pass + + +def endpoint(account, jurisdiction=None): + if not isinstance(account, str) or not re.fullmatch(r'[a-f0-9]{32}', account): + raise ValueError('R2 S3 requires a reviewed account identity') + if jurisdiction not in (None, 'default', 'eu', 'us', 'fedramp'): + raise ValueError('R2 S3 jurisdiction is not supported by this runtime') + suffix = '' if jurisdiction in (None, 'default') else '.' + jurisdiction + return account + suffix + '.r2.cloudflarestorage.com' + + +def _path(bucket, key): + if not isinstance(bucket, str) or not re.fullmatch(r'[a-z0-9][a-z0-9-]{1,61}[a-z0-9]', bucket): + raise ValueError('R2 S3 requires an explicit bucket identity') + if (not isinstance(key, str) or not key or len(key.encode()) > 1024 + or any(part in ('', '.', '..') for part in key.split('/')) + or '\\' in key or any(ord(char) < 32 or ord(char) == 127 for char in key)): + raise ValueError('R2 S3 requires a safe bounded object key') + return '/' + bucket + '/' + quote(key, safe='/-_.~') + + +def _headers(method, host, path, payload_sha256, access_key_id, secret, now): + """SigV4 for the fixed, query-free R2 object endpoint; sign the raw payload.""" + timestamp = now.strftime('%Y%m%dT%H%M%SZ') + day = now.strftime('%Y%m%d') + scope = day + '/auto/s3/aws4_request' + signed = 'host;x-amz-content-sha256;x-amz-date' + canonical_headers = f'host:{host}\nx-amz-content-sha256:{payload_sha256}\nx-amz-date:{timestamp}\n' + if method == 'PUT': + signed = 'content-type;' + signed + canonical_headers = 'content-type:application/octet-stream\n' + canonical_headers + canonical = '\n'.join((method, path, '', canonical_headers, signed, payload_sha256)) + to_sign = '\n'.join(('AWS4-HMAC-SHA256', timestamp, scope, + hashlib.sha256(canonical.encode()).hexdigest())) + signing_key = ('AWS4' + secret).encode() + for value in (day, 'auto', 's3', 'aws4_request'): + signing_key = hmac.new(signing_key, value.encode(), hashlib.sha256).digest() + signature = hmac.new(signing_key, to_sign.encode(), hashlib.sha256).hexdigest() + return {'Host': host, 'x-amz-date': timestamp, 'x-amz-content-sha256': payload_sha256, + 'Authorization': f'AWS4-HMAC-SHA256 Credential={access_key_id}/{scope}, SignedHeaders={signed}, Signature={signature}', + 'Accept-Encoding': 'identity'} + + +def _remaining(deadline): + remaining = deadline - time.monotonic() + if remaining <= 0: + raise R2TransportError('R2 request deadline exceeded', retryable=True) + return min(SOCKET_SECONDS, remaining) + + +def _read(transport_socket, response, count, deadline): + timeout = _remaining(deadline) + if response.isclosed(): + return b'' + transport_socket.settimeout(timeout) + # read1 returns after one underlying read, so slow chunked delivery cannot + # postpone the deadline check until an entire requested block arrives. + data = response.read1(count) + _remaining(deadline) + return data + + +def _exchange(host, method, path, headers, body, consume, *, before_send=None): + deadline = time.monotonic() + REQUEST_SECONDS + connection = http.client.HTTPSConnection(host, timeout=SOCKET_SECONDS, + context=ssl.create_default_context()) + response = None + timer = None + try: + connection.connect() + transport_socket = connection.sock + + def expire(): + try: + transport_socket.shutdown(socket.SHUT_RDWR) + except OSError: + pass + + # Bound header parsing and writes too, including slow peer delivery. + _remaining(deadline) + timer = threading.Timer(max(0, deadline - time.monotonic()), expire) + timer.daemon = True + timer.start() + if before_send is not None: + before_send() + transport_socket.settimeout(_remaining(deadline)) + connection.request(method, path, body=body, headers=headers) + transport_socket.settimeout(_remaining(deadline)) + response = connection.getresponse() + if response.status != 200: + status = response.status + raise R2TransportError(f'R2 {method} rejected (HTTP {status})', + retryable=status in (408, 429) or 500 <= status <= 599, + status=status) + length = response.getheader('Content-Length') + transfer = response.getheader('Transfer-Encoding') + if (length is not None and (not re.fullmatch(r'[0-9]{1,20}', length) + or int(length) > MAX_OBJECT_BYTES) + or transfer is not None and transfer.strip().lower() != 'chunked' + or length is not None and transfer is not None): + raise R2TransportError('R2 returned malformed HTTP framing') + encoding = response.getheader('Content-Encoding', 'identity').strip().lower() + if encoding not in ('', 'identity'): + raise R2TransportError('R2 returned an unexpected content encoding') + return consume(transport_socket, response, deadline) + except R2TransportError: + raise + except (OSError, http.client.RemoteDisconnected): + raise R2TransportError('R2 request did not complete', retryable=True) from None + except http.client.HTTPException: + if time.monotonic() >= deadline: + raise R2TransportError('R2 request deadline exceeded', retryable=True) from None + raise R2TransportError('R2 returned malformed HTTP framing') from None + finally: + if timer is not None: + timer.cancel() + if response is not None: + response.close() + connection.close() + + +def _bounded_body(transport_socket, response, deadline): + body = bytearray() + while block := _read(transport_socket, response, MAX_METADATA_BYTES - len(body) + 1, deadline): + body.extend(block) + if len(body) > MAX_METADATA_BYTES: + raise R2TransportError('R2 metadata response exceeds bound') + length = response.getheader('Content-Length') + if length is not None and int(length) != len(body): + raise R2TransportError('R2 metadata response is truncated') + return bytes(body) + + +def _date(value): + if not isinstance(value, str): + raise R2TransportError('Account token time metadata is malformed') + try: + parsed = datetime.fromisoformat(value.replace('Z', '+00:00')) + except ValueError: + raise R2TransportError('Account token time metadata is malformed') from None + if parsed.tzinfo is None: + raise R2TransportError('Account token time metadata requires a timezone') + return parsed + + +class R2S3Client: + def __init__(self, account, jurisdiction=None, *, token=None): + self.host = endpoint(account, jurisdiction) + token = os.environ.get('CLOUDFLARE_API_TOKEN') if token is None else token + if not isinstance(token, str) or not re.fullmatch(r'[A-Za-z0-9_-]+', token): + raise ValueError('An account-owned CLOUDFLARE_API_TOKEN is required for S3 seeding') + raw = _exchange('api.cloudflare.com', 'GET', f'/client/v4/accounts/{account}/tokens/verify', + {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', + 'Accept-Encoding': 'identity'}, None, _bounded_body) + try: + payload = json.loads(raw) + if not isinstance(payload, dict): + raise ValueError('invalid verification response') + record = payload['result'] + if (payload.get('success') is not True or not isinstance(record, dict) + or not re.fullmatch(r'[a-f0-9]{32}', str(record.get('id', ''))) + or record.get('status') != 'active'): + raise ValueError('invalid verification response') + except (ValueError, TypeError, KeyError, UnicodeError): + raise R2TransportError('Account-owned token verification failed') from None + now = datetime.now(timezone.utc) + if (record.get('not_before') is not None and _date(record['not_before']) > now + or record.get('expires_on') is not None and _date(record['expires_on']) <= now): + raise R2TransportError('Account-owned token is outside its valid time interval') + self._access_key_id = record['id'] + self._secret = hashlib.sha256(token.encode()).hexdigest() + self.credential_receipt = {'kind': 'account-owned', 'account_id': account, + 'token_id': record['id'], 'status': 'active', + 'verified_at': now.isoformat(), + 'not_before': record.get('not_before'), + 'expires_on': record.get('expires_on')} + + def put(self, bucket, key, source, expected_size, expected_sha256, *, authorize=None): + if not 0 <= expected_size <= MAX_OBJECT_BYTES: + raise ValueError('R2 object exceeds retained cache bound') + with source.open('rb') as file: + data = file.read(expected_size + 1) + if len(data) != expected_size or hashlib.sha256(data).hexdigest() != expected_sha256: + raise ValueError('OpenNext cache source changed before S3 upload') + path = _path(bucket, key) + headers = _headers('PUT', self.host, path, expected_sha256, self._access_key_id, + self._secret, datetime.now(timezone.utc)) + headers.update({'Content-Type': 'application/octet-stream', 'Content-Length': str(len(data))}) + _exchange(self.host, 'PUT', path, headers, data, _bounded_body, before_send=authorize) + + def verify(self, bucket, key, expected_size, expected_sha256): + if not 0 <= expected_size <= MAX_OBJECT_BYTES: + raise ValueError('R2 object exceeds retained cache bound') + path = _path(bucket, key) + headers = _headers('GET', self.host, path, hashlib.sha256(b'').hexdigest(), + self._access_key_id, self._secret, datetime.now(timezone.utc)) + + def consume(connection, response, deadline): + length = response.getheader('Content-Length') + if length is not None and length != str(expected_size): + raise R2ReadbackMismatch('OpenNext cache readback length mismatch') + digest = hashlib.sha256() + received = 0 + while True: + block = _read(connection, response, min(64 * 1024, expected_size - received + 1), deadline) + if not block: + break + received += len(block) + if received > expected_size: + raise R2ReadbackMismatch('OpenNext cache readback exceeds retained length') + digest.update(block) + if received != expected_size or digest.hexdigest() != expected_sha256: + raise R2ReadbackMismatch('OpenNext cache readback digest mismatch') + + _exchange(self.host, 'GET', path, headers, None, consume) diff --git a/studio/websites.py b/studio/websites.py index d7423f4..a689b9e 100644 --- a/studio/websites.py +++ b/studio/websites.py @@ -7,10 +7,13 @@ import re import subprocess import tempfile +import time from urllib.parse import urlsplit from urllib.error import HTTPError, URLError from urllib.request import HTTPRedirectHandler, Request, build_opener from .candidate import PromotionJournal +from .r2_s3 import (MAX_CACHE_BYTES, MAX_CACHE_ENTRIES, MAX_OBJECT_BYTES, + R2ReadbackMismatch, R2S3Client, R2TransportError, endpoint) # This bounded profile deliberately excludes filesystem/build options that need @@ -813,6 +816,22 @@ def workers_site_inputs(component, artifact, environment, build_root, bindings_r return command, metadata +def _cache_digest(source, size): + if source.is_symlink() or not source.is_file() or size > MAX_OBJECT_BYTES: + raise ValueError('OpenNext cache requires bounded regular files') + digest = hashlib.sha256() + received = 0 + with source.open('rb') as file: + while block := file.read(min(1024 * 1024, size - received + 1)): + received += len(block) + if received > size: + raise ValueError('OpenNext cache source length changed') + digest.update(block) + if received != size: + raise ValueError('OpenNext cache source length changed') + return digest.hexdigest() + + def _opennext_cache_plan(component, metadata, environment, root, config): """Map OpenNext 1.20.10 retained cache paths to native R2 object keys. @@ -828,8 +847,9 @@ def _opennext_cache_plan(component, metadata, environment, root, config): for scope in scopes) if declared is None and not uses_cache: return None - if not isinstance(declared, dict) or set(declared) != {'directory'}: - raise ValueError('OpenNext R2 cache requires a retained directory declaration') + if (not isinstance(declared, dict) or set(declared) != {'directory', 'transport'} + or declared['transport'] not in ('s3', 'wrangler-rest')): + raise ValueError('OpenNext R2 cache requires a retained directory and explicit s3 or wrangler-rest transport') cache = _inside(root, declared['directory'], 'OpenNext cache', directory=True) selected = scopes[0 if environment == 'staging' else 1] for scope in scopes: @@ -851,9 +871,18 @@ def _opennext_cache_plan(component, metadata, environment, root, config): raise ValueError('OpenNext cache prefix must be a bounded relative object prefix') entries = [] build_ids = set() - for file in sorted(cache.rglob('*')): + total = 0 + for index, file in enumerate(cache.rglob('*')): + if index >= MAX_CACHE_ENTRIES * 4 or file.is_symlink() or not file.resolve().is_relative_to(cache): + raise ValueError('OpenNext cache inventory exceeds its safe bound') if not file.is_file(): + if not file.is_dir(): + raise ValueError('OpenNext cache requires regular files and directories') continue + size = file.stat().st_size + total += size + if len(entries) >= MAX_CACHE_ENTRIES or size > MAX_OBJECT_BYTES or total > MAX_CACHE_BYTES: + raise ValueError('OpenNext cache byte or object count exceeds bound') parts = file.relative_to(cache).parts if any('\\' in part for part in parts) or parts[0].startswith('__fetch') and parts[0] != '__fetch': raise ValueError('Invalid retained OpenNext cache path') @@ -873,14 +902,68 @@ def _opennext_cache_plan(component, metadata, environment, root, config): raise ValueError('OpenNext cache object key exceeds R2 limits') build_ids.add(build_id) entries.append({'file': str(file.relative_to(root)), 'key': object_key, - 'sha256': hashlib.sha256(file.read_bytes()).hexdigest()}) + 'size': size, 'sha256': _cache_digest(file, size)}) + entries.sort(key=lambda entry: entry['file']) build_id_file = _inside(root, str(Path(deploy['assets']) / 'BUILD_ID'), 'OpenNext build ID') build_id = build_id_file.read_text().strip() if build_ids != {build_id} or len({row['key'] for row in entries}) != len(entries): raise ValueError('OpenNext cache requires the retained asset build ID and distinct object keys') + host = endpoint(config['account_id'], bucket.get('jurisdiction')) if declared['transport'] == 's3' else None return {'artifact': metadata, 'account_id': config['account_id'], 'worker': selected['name'], 'environment': environment, 'wrangler_environment': deploy['environments'][environment], - 'bucket': bucket['bucket_name'], 'jurisdiction': bucket.get('jurisdiction'), 'entries': entries} + 'bucket': bucket['bucket_name'], 'jurisdiction': bucket.get('jurisdiction'), + 'transport': declared['transport'], 'endpoint': host, 'entries': entries} + + +def _s3_attempts(operation): + for attempt in range(3): + try: + return operation() + except R2TransportError as error: + if not error.retryable or attempt == 2: + raise + time.sleep(0.5 * 2 ** attempt) + + +def _seed_s3_cache(plan, identity, slot, root, journal, authorize): + client = R2S3Client(plan['account_id'], plan['jurisdiction']) + journal.data.setdefault('cache_credentials', {})[slot] = client.credential_receipt + journal._save() + for entry in plan['entries']: + source = _inside(root, entry['file'], 'OpenNext cache file') + if _cache_digest(source, entry['size']) != entry['sha256']: + raise ValueError('OpenNext cache source digest changed before upload') + operation = f'website-cache:{slot}:{entry["key"]}' + + def verify(entry=entry): + client.verify(plan['bucket'], entry['key'], entry['size'], entry['sha256']) + + # Reconcile completed receipts with actual bytes. A removed/corrupted + # object is repaired only with the same authorized retained content. + if journal.data['operations'].get(operation, {}).get('state') == 'completed': + try: + _s3_attempts(verify) + continue + except R2TransportError as error: + if not isinstance(error, R2ReadbackMismatch) and error.status != 404: + raise + journal.data['operations'][operation] = {'state': 'pending'} + journal._save() + + def upload(entry=entry, source=source): + def put_and_verify(): + client.put(plan['bucket'], entry['key'], source, entry['size'], entry['sha256'], + authorize=authorize) + verify() + return {'state': 'completed', 'bucket': plan['bucket'], 'key': entry['key'], + 'size': entry['size'], 'sha256': entry['sha256'], 'identity': identity, + 'token_id': client.credential_receipt['token_id']} + return _s3_attempts(put_and_verify) + + journal.perform(operation, upload) + return {'state': 'completed', 'identity': identity, 'bucket': plan['bucket'], + 'transport': 's3', 'entries': len(plan['entries']), + 'credential': client.credential_receipt} def seed_workers_site_cache(component, metadata, environment, root, config_path, cli, *, runner, @@ -905,13 +988,15 @@ def seed_workers_site_cache(component, metadata, environment, root, config_path, raise ValueError('OpenNext cache receipt identity differs from this deployment') journal.data['website_caches'][slot] = identity journal._save() + if target == 'remote' and plan['transport'] == 's3': + return _seed_s3_cache(plan, identity, slot, root, journal, authorize) flags = ['--' + target, '--config', str(config_path), '--env', plan['wrangler_environment'], '--env-file', os.devnull] if plan['jurisdiction']: flags.extend(['--jurisdiction', plan['jurisdiction']]) for entry in plan['entries']: source = _inside(root, entry['file'], 'OpenNext cache file') - if hashlib.sha256(source.read_bytes()).hexdigest() != entry['sha256']: + if _cache_digest(source, entry['size']) != entry['sha256']: raise ValueError('OpenNext cache source digest changed before upload') def upload(entry=entry, source=source): @@ -927,7 +1012,8 @@ def upload(entry=entry, source=source): downloaded = Path(temporary) / 'object' runner([*cli, 'r2', 'object', 'get', f'{plan["bucket"]}/{entry["key"]}', '--file', str(downloaded), *flags], cwd=root) - if not downloaded.is_file() or hashlib.sha256(downloaded.read_bytes()).hexdigest() != entry['sha256']: + if (not downloaded.is_file() or downloaded.stat().st_size != entry['size'] + or _cache_digest(downloaded, entry['size']) != entry['sha256']): raise ValueError('OpenNext cache readback digest mismatch') return {'state': 'completed', 'bucket': plan['bucket'], 'key': entry['key'], 'sha256': entry['sha256'], 'identity': identity} @@ -935,4 +1021,6 @@ def upload(entry=entry, source=source): if attempt == 2: raise journal.perform(f'website-cache:{slot}:{entry["key"]}', upload) - return {'state': 'completed', 'identity': identity, 'bucket': plan['bucket'], 'entries': len(plan['entries'])} + return {'state': 'completed', 'identity': identity, 'bucket': plan['bucket'], + 'transport': 'wrangler-local' if target == 'local' else 'wrangler-rest', + 'entries': len(plan['entries'])} diff --git a/tests/test_opennext_cache.py b/tests/test_opennext_cache.py index 0e6b77d..b85c71a 100644 --- a/tests/test_opennext_cache.py +++ b/tests/test_opennext_cache.py @@ -37,7 +37,7 @@ def setUp(self): for env in ('staging', 'production')}} self.component = {'id': 'web', 'path': '.', 'deploy': { 'provider': 'workers', 'config': 'wrangler.json', 'entrypoint': 'worker.js', 'assets': 'assets', - 'opennext_cache': {'directory': '.open-next/cache'}, + 'opennext_cache': {'directory': '.open-next/cache', 'transport': 'wrangler-rest'}, 'environments': {'staging': 'staging', 'production': 'production'}, 'health_urls': {env: f'https://{env}.example.com/__studio/release' for env in ('staging','production')}}} self.artifact = {'source_sha': 'a' * 40, 'sha256': 'b' * 64, 'candidate_id': '123-1'} @@ -108,7 +108,7 @@ def test_rejects_unreviewed_cache_and_binding_shapes_before_any_write(self): with self.subTest(change=change): saved = json.loads(json.dumps(self.config)); deploy = dict(self.component['deploy']) bad = self.cache / 'invalid.txt' - if change == 'escape': self.component['deploy']['opennext_cache'] = {'directory': '../outside'} + if change == 'escape': self.component['deploy']['opennext_cache'] = {'directory': '../outside', 'transport': 'wrangler-rest'} if change == 'shared-preview': self.config['env']['staging']['r2_buckets'][0]['preview_bucket_name'] = 'cache-production' if change == 'missing-binding': self.config['env']['production']['r2_buckets'] = [] if change == 'invalid-file': bad.write_text('invalid') diff --git a/tests/test_workers_framework_runtime.py b/tests/test_workers_framework_runtime.py index fc5d43f..def6ea8 100644 --- a/tests/test_workers_framework_runtime.py +++ b/tests/test_workers_framework_runtime.py @@ -106,7 +106,7 @@ def test_both_framework_archives_keep_identity_html_modules_assets_and_404s(self 'config': 'wrangler.json', 'entrypoint': 'worker.js', 'assets': 'assets' if framework == 'astro' else '.open-next/assets', 'environments': {'staging': 'staging', 'production': 'production'}, - **({'opennext_cache': {'directory': '.open-next/cache'}} if framework == 'next' else {}), + **({'opennext_cache': {'directory': '.open-next/cache', 'transport': 'wrangler-rest'}} if framework == 'next' else {}), }} # Two independently extracted shipping roots use the same zip bytes. # Promotion never invokes a framework build or uses node_modules. @@ -149,7 +149,7 @@ def test_next_server_rendering_keeps_viewers_and_forwarded_request_state_separat unpack(archive, build) component = {'id': 'next', 'path': '.', 'deploy': { 'config': 'wrangler.json', 'entrypoint': 'worker.js', 'assets': '.open-next/assets', - 'opennext_cache': {'directory': '.open-next/cache'}, + 'opennext_cache': {'directory': '.open-next/cache', 'transport': 'wrangler-rest'}, 'environments': {'staging': 'staging', 'production': 'production'}, }} artifact = {'source_sha': 'b' * 40, 'sha256': sha256(archive), 'candidate_id': '124-1'} From 9adbeeb9427b49b96fec995695646cfa196bdda6 Mon Sep 17 00:00:00 2001 From: Willie Chalmers III Date: Sun, 11 Oct 2026 12:02:53 -0700 Subject: [PATCH 2/2] fix(cli): unwrap paginated Worker deployment recovery responses Cloudflare deployment lists retain result_info, so the shared transport returns an envelope instead of the direct deployment object expected by recovery. Normalize this endpoint without changing generic pagination. Reject malformed rollout rows and ambiguous envelopes before selecting one complete version. Static Ruff, Python syntax and diff checks passed. No tests were added or manually run, and provider recovery acceptance remains pending. Co-authored-by: Codex --- docs/rollout.md | 17 +++++++++++++++++ studio/websites.py | 26 ++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/rollout.md b/docs/rollout.md index 79110cc..394f0ae 100644 --- a/docs/rollout.md +++ b/docs/rollout.md @@ -222,3 +222,20 @@ acceptance receipt. No provider write, credential creation/read, dependency installation, release or product adoption occurred during implementation. Context7 was already monthly-quota blocked; current official Cloudflare and AWS source documents supplied this protocol proposal, not a successful Context7 retrieval. + +## Worker recovery deployment envelope correction + +Recovery capture run 38165763297 stopped before producing a recovery record: +the deployment reader expected a direct `deployments` object, while the +Cloudflare transport preserves `result` and `result_info` when pagination +metadata is present. The [official list deployments response](https://developers.cloudflare.com/api/resources/workers/subresources/scripts/subresources/deployments/methods/list/) +has exactly that envelope. The endpoint reader now accepts the transport's +envelope as well as direct results, rejects ambiguous or malformed envelopes +and rollout rows, and still requires one active version at 100%. Generic +transport and custom-domain pagination are unchanged. + +This correction is unreleased source. Static checks are separate from recovery +acceptance; no tests were added or manually run, and no provider request, release, +tag or consumer pin change was performed for this correction. The failed +capture remains retained evidence; a future authorized hosted capture must +prove recovery against the real complete deployment. diff --git a/studio/websites.py b/studio/websites.py index a689b9e..f34d0e8 100644 --- a/studio/websites.py +++ b/studio/websites.py @@ -125,19 +125,33 @@ def _api_mutate(api, method, path, data, headers=None): def _deployment_version(api, account, worker): payload = _api_get(api, f'/accounts/{account}/workers/scripts/{worker}/deployments') + # The transport retains result_info for paginated callers. This endpoint's + # result is itself an object, rather than the lists returned by those APIs. + if isinstance(payload, dict) and ('result' in payload or 'result_info' in payload): + if ('deployments' in payload or not isinstance(payload.get('result'), dict) + or not isinstance(payload.get('result_info'), dict)): + raise ValueError('Cloudflare deployment response envelope is malformed') + payload = payload['result'] deployments = payload.get('deployments') if isinstance(payload, dict) else None if not isinstance(deployments, list) or not deployments or not isinstance(deployments[0], dict): raise ValueError('Cloudflare has no complete Worker deployment to recover') versions = deployments[0].get('versions') - if not isinstance(versions, list): + if not isinstance(versions, list) or not versions: raise ValueError('Cloudflare deployment version state is malformed') - active = [v for v in versions if isinstance(v, dict) and v.get('percentage', 0) > 0] + for row in versions: + if not isinstance(row, dict): + raise ValueError('Cloudflare deployment version state is malformed') + percentage = row.get('percentage') + if (isinstance(percentage, bool) or not isinstance(percentage, (int, float)) + or not 0 <= percentage <= 100): + raise ValueError('Cloudflare deployment percentage is malformed') + version = row.get('version_id') + if not isinstance(version, str) or not re.fullmatch(r'[A-Fa-f0-9-]{16,64}', version): + raise ValueError('Cloudflare deployment has an invalid version identity') + active = [v for v in versions if v['percentage'] > 0] if len(active) != 1 or active[0].get('percentage') != 100: raise ValueError('Workers recovery requires one complete 100% deployment') - version = active[0].get('version_id') - if not isinstance(version, str) or not re.fullmatch(r'[A-Fa-f0-9-]{16,64}', version): - raise ValueError('Cloudflare deployment has an invalid version identity') - return version + return active[0]['version_id'] def _version_artifact_digest(api, account, worker, version):