diff --git a/.github/scripts/read-1bc-ios-artifact.cjs b/.github/scripts/read-1bc-ios-artifact.cjs new file mode 100644 index 000000000..a034aa77f --- /dev/null +++ b/.github/scripts/read-1bc-ios-artifact.cjs @@ -0,0 +1,141 @@ +'use strict'; +// One existing iOS diagnostic artifact. This never starts native or release work. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const path = require('node:path'); +const { execFileSync } = require('node:child_process'); +const OWNER = 'KeeperCommunity', REPO = 'bitcoin-keeper', REPOSITORY = 470904030; +const RUN = 39858617273, JOB = 120103187854, ANDROID = 120125972406, ARTIFACT = 12462694619; +const SOURCE = '1bc34890cb8618b2fffd446a63924974e0fe15b5'; +const TREE = '2e9cd57bf9f78b92cac73e9343ddbd0eef25e86e'; +const WORKFLOW = '3c44ab6412a8118b183273c402dd08136db05b28'; +const DIGEST = '550142809a0faf812741a586630b17d6f458b2b6c935b5bab4b00f84da67c84f'; +const NAME = `keeper-diagnostic-native-IOS-${SOURCE}-${RUN}-1`; +const BRANCH = 'codex/r5-ios-startup-diagnosis-20261022'; + +function validateRun(value, jobs) { + assert.equal(value.id, RUN); assert.equal(value.run_attempt, 1); + assert.equal(value.repository.id, REPOSITORY); assert.equal(value.head_repository.id, REPOSITORY); + assert.equal(value.head_sha, SOURCE); assert.equal(value.event, 'pull_request'); + assert.equal(value.head_branch, BRANCH); + assert.equal(value.path, '.github/workflows/keeper-native-diagnostic.yml'); + assert.ok((value.status === 'in_progress' && value.conclusion === null) || + (value.status === 'completed' && value.conclusion === 'failure')); + assert.deepEqual(value.referenced_workflows, [{ + path: `${OWNER}/${REPO}/.github/workflows/keeper-native-platform.yml@${WORKFLOW}`, + ref: 'refs/pull/7116/merge', sha: WORKFLOW, + }]); + assert.equal(jobs.total_count, 3); assert.equal(jobs.jobs.length, 3); + assert.deepEqual(jobs.jobs.map(row => row.id).sort((a, b) => a - b), + [JOB, 120103189932, ANDROID].sort((a, b) => a - b)); + const android = jobs.jobs.find(row => row.id === ANDROID); + assert.equal(android.run_id, RUN); assert.equal(android.head_sha, SOURCE); + assert.equal(android.name, 'android / Native ANDROID build and probes'); + if (value.status === 'in_progress') { + // Only this already allocated serialized Android continuation may be active. + assert.ok(['queued', 'in_progress'].includes(android.status)); assert.equal(android.conclusion, null); + } else { + assert.equal(android.status, 'completed'); + assert.ok(['success', 'failure', 'cancelled', 'timed_out'].includes(android.conclusion)); + } +} + +function validateJob(value) { + assert.equal(value.id, JOB); assert.equal(value.run_id, RUN); assert.equal(value.head_sha, SOURCE); + assert.equal(value.name, 'ios / Native IOS build and probes'); + assert.equal(value.status, 'completed'); assert.equal(value.conclusion, 'failure'); + for (const [name, conclusion] of [ + ['Check out exact app source', 'success'], ['Verify current diagnostic source', 'success'], + ['Install immutable native dependencies', 'success'], ['Check disposable iOS driver readiness', 'success'], + ['Build development app', 'success'], ['Run disposable startup smoke', 'failure'], + ['Run disposable native persistence probe', 'skipped'], ['Run R5 production worker native scenarios', 'skipped'], + ['Measure five full-product startup and first-wallet runs', 'skipped'], ['Retain safe Phase A evidence', 'success'], + ]) { + const rows = value.steps.filter(step => step.name === name); + assert.equal(rows.length, 1); assert.equal(rows[0].status, 'completed'); + assert.equal(rows[0].conclusion, conclusion); + } +} + +function validateArtifact(value) { + assert.equal(value.id, ARTIFACT); assert.equal(value.name, NAME); + assert.equal(value.expired, false); assert.equal(value.size_in_bytes, 6884); + assert.equal(value.digest, `sha256:${DIGEST}`); + assert.equal(value.workflow_run.id, RUN); assert.equal(value.workflow_run.head_sha, SOURCE); + assert.equal(value.workflow_run.repository_id, REPOSITORY); + assert.equal(value.workflow_run.head_repository_id, REPOSITORY); + assert.equal(value.workflow_run.head_branch, BRANCH); +} + +async function run({ github, context, core }) { + let owned, primary = false, output; + try { + assert.equal(context.repo.owner, OWNER); assert.equal(context.repo.repo, REPO); + assert.equal(context.eventName, 'push'); + assert.equal(context.ref, 'refs/heads/codex/r5-1bc-ios-artifact-readout'); + const args = { owner: OWNER, repo: REPO, request: { timeout: 30000 } }; + async function snapshot() { + const [r, j, a, jobs] = await Promise.all([ + github.rest.actions.getWorkflowRun({ ...args, run_id: RUN }), + github.rest.actions.getJobForWorkflowRun({ ...args, job_id: JOB }), + github.rest.actions.getArtifact({ ...args, artifact_id: ARTIFACT }), + github.rest.actions.listJobsForWorkflowRun({ ...args, run_id: RUN, filter: 'latest', per_page: 100 }), + ]); + validateRun(r.data, jobs.data); validateJob(j.data); validateArtifact(a.data); + } + await snapshot(); + const [commit, pr] = await Promise.all([ + github.rest.repos.getCommit({ ...args, ref: SOURCE }), + github.rest.pulls.get({ ...args, pull_number: 7116 }), + ]); + assert.equal(commit.data.sha, SOURCE); assert.equal(commit.data.commit.tree.sha, TREE); + assert.equal(pr.data.number, 7116); assert.equal(pr.data.head.sha, SOURCE); + assert.equal(pr.data.head.ref, BRANCH); assert.equal(pr.data.head.repo.id, REPOSITORY); + assert.equal(pr.data.state, 'open'); assert.equal(pr.data.merged, false); + const download = await github.rest.actions.downloadArtifact({ ...args, artifact_id: ARTIFACT, archive_format: 'zip' }); + const bytes = Buffer.from(download.data); + assert.equal(bytes.length, 6884); + assert.equal(crypto.createHash('sha256').update(bytes).digest('hex'), DIGEST); + await snapshot(); // A later attempt or replacement cannot supply this readout. + const temp = process.env.RUNNER_TEMP; + assert.ok(temp && path.isAbsolute(temp)); assert.equal(fs.realpathSync(temp), path.resolve(temp)); + const filename = path.join(temp, 'r5-1bc-ios-fixed.zip'); + const descriptor = fs.openSync(filename, 'wx', 0o600); + owned = { filename }; // A failed stat still reports unresolved private cleanup. + try { + const info = fs.fstatSync(descriptor); Object.assign(owned, { dev: info.dev, ino: info.ino }); + fs.writeFileSync(descriptor, bytes); + } finally { fs.closeSync(descriptor); } + output = execFileSync('python3', ['-B', '.github/scripts/read-1bc-ios-artifact.py'], + { encoding: 'utf8', timeout: 30000, maxBuffer: 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] }); + const tag = 'SAFE_R5_IOS_READOUT '; + assert.ok(output.startsWith(tag)); + const result = JSON.parse(output.slice(tag.length)); + assert.equal(result.source_sha, SOURCE); assert.equal(result.workflow_sha, WORKFLOW); + assert.equal(result.run_id, String(RUN)); assert.equal(result.run_attempt, '1'); + assert.equal(result.artifact_id, String(ARTIFACT)); + assert.equal(result.readout_only, true); assert.equal(result.authorization_eligible, false); + assert.equal(result.trusted_native_acceptance, false); + output = tag + JSON.stringify(result); + } catch (_) { + primary = true; + throw new Error('FIXED_IOS_ARTIFACT_READOUT_FAILED'); + } finally { + if (owned) { + try { + const info = fs.lstatSync(owned.filename); + assert.ok(info.isFile() && !info.isSymbolicLink()); + assert.equal(info.dev, owned.dev); assert.equal(info.ino, owned.ino); + fs.unlinkSync(owned.filename); + } catch (_) { + try { core.warning('FIXED_IOS_ARTIFACT_PRIVATE_CLEANUP_FAILED'); } catch (_) { /* retain primary */ } + if (!primary) throw new Error('FIXED_IOS_ARTIFACT_PRIVATE_CLEANUP_FAILED'); + } + } + } + try { core.info(output); } + catch (_) { throw new Error('FIXED_IOS_ARTIFACT_READOUT_FAILED'); } +} + +module.exports = { run }; diff --git a/.github/scripts/read-1bc-ios-artifact.py b/.github/scripts/read-1bc-ios-artifact.py new file mode 100644 index 000000000..8c25aafbd --- /dev/null +++ b/.github/scripts/read-1bc-ios-artifact.py @@ -0,0 +1,274 @@ +#!/usr/bin/env python3 +"""Read one immutable iOS diagnostic artifact; this does not grant acceptance.""" +import hashlib +import io +import json +import math +import os +from pathlib import Path +import re +import stat +import sys +import zipfile + +SOURCE = '1bc34890cb8618b2fffd446a63924974e0fe15b5' +WORKFLOW = '3c44ab6412a8118b183273c402dd08136db05b28' +WORKFLOW_REF = 'KeeperCommunity/bitcoin-keeper/.github/workflows/keeper-native-diagnostic.yml@refs/pull/7116/merge' +RUN, ATTEMPT, ARTIFACT = '39858617273', '1', '12462694619' +ZIP_SIZE, LIMIT = 6884, 1024 * 1024 +ZIP_SHA256 = '550142809a0faf812741a586630b17d6f458b2b6c935b5bab4b00f84da67c84f' +ERROR_TYPES = ('ValueError', 'TypeError', 'OSError', 'FileNotFoundError', 'PermissionError', + 'TimeoutExpired', 'JSONDecodeError', 'RuntimeError', 'OTHER_ERROR') +IOS_CATEGORIES = ('JS_TYPE', 'JS_REFERENCE', 'JS_SYNTAX', 'JS_RANGE', 'JS_INVARIANT', + 'REACT_FATAL', 'OBJC_EXCEPTION', 'NATIVE_MODULE', 'NATIVE_LIBRARY', + 'HERMES_EXCEPTION', 'BUNDLE_LOAD', 'ATS_BLOCKED', 'CONNECTION') +HIERARCHY = ('credential_error', 'retry', 'logbox_controls', 'redbox_resource', 'loading', + 'login', 'welcome_back', 'logbox_error_header', 'logbox_warning_header', + 'logbox_syntax_footer', 'logbox_counter') +PREFLIGHT = ('runner_install_started', 'runner_install_launch_returned', 'status_check_started', + 'status_response_received', 'status_check_failed', 'driver_startup_timeout', + 'prebuilt_driver_selected', 'xcodebuild_driver_selected', 'xctestrun_config_missing', + 'ui_test_runner_missing', 'xcode_test_execute_failed', 'xcode_test_failed', + 'xcode_testing_failed', 'host_port_unavailable', 'requested_device_disconnected', + 'log_limit_reached') +DRIVER_TYPES = ('IOSDriverTimeoutException', 'FileNotFoundException', 'CliError', + 'IllegalStateException', 'IOException', 'ConnectException', 'SocketTimeoutException', + 'TimeoutException', 'RuntimeException', 'UnknownHostException', 'SSLHandshakeException') +METRO_ERRORS = ('SyntaxError', 'TypeError', 'ReferenceError', 'RangeError', 'TransformError', + 'UnableToResolveError', 'ResolutionError', 'AmbiguousModuleResolutionError', + 'NotFoundError', 'ResourceNotFoundError', 'GraphNotFoundError', 'RevisionNotFoundError', 'OTHER') +METRO_CATEGORIES = ('MODULE_RESOLUTION', 'SYNTAX', 'TYPE', 'REFERENCE', 'MEMORY', 'TRANSFORM', 'CONNECTION') +CLEANUP = ('startup_capture', 'metro_process', 'metro_output', 'device_shutdown', 'device_delete', + 'private_startup-logcat', 'private_startup-observer', 'private_startup-foreground', 'metadata') + + +def obj(value): + return value if type(value) is dict else {} + + +def enum(value, allowed=('PASS', 'FAIL', 'NOT RUN', 'RUNNING')): + return value if type(value) is str and value in allowed else 'UNKNOWN' + + +def boolean(value): + return value if type(value) is bool else None + + +def number(value, maximum=1000000): + return value if type(value) in (int, float) and 0 <= value <= maximum and math.isfinite(value) else None + + +def count(value, maximum=1000000): + return value if type(value) is int and 0 <= value <= maximum else None + + +def digest(value): + return value if type(value) is str and re.fullmatch('[a-f0-9]{64}', value) else None + + +def fixed_list(value, vocabulary): + selected = {item for item in value[:1024] if type(item) is str and item in vocabulary} if type(value) is list else set() + return [item for item in vocabulary if item in selected] + + +def flags(value, vocabulary): + return {key: boolean(obj(value).get(key)) for key in vocabulary} + + +def commands(value): + labels = {'build_ios': 'Build unsigned development simulator app', + 'install_ios': 'Install unsigned development app', + 'startup_ui': 'Assert startup and disposable passcode transition', + 'persistence_ui': 'Assert native persistence across process launches', + 'workers_ui': 'Assert R5 production workers over native storage'} + rows = value[:1000] if type(value) is list else [] + return {key: [{'exit_code': count(row.get('exit_code'), 255), + 'duration_seconds': number(row.get('duration_seconds'), 7200), + 'timeout_seconds': number(row.get('timeout_seconds'), 7200)} + for row in rows if type(row) is dict and row.get('label') == label][:5] + for key, label in labels.items()} + + +def metro_row(value): + value = obj(value) + result = {'stage': enum(value.get('stage'), ('BEFORE_APP', 'APP_WINDOW', 'UNKNOWN')), + 'kind': enum(value.get('kind'), ('BUILD', 'ERROR', 'REQUEST', 'RESPONSE'))} + if result['kind'] in ('BUILD', 'ERROR'): + allowed = (('bundle_build_started', 'bundle_build_done', 'bundle_build_failed') + if result['kind'] == 'BUILD' else + ('bundling_error', 'initialize_failed', 'transformer_load_failed', 'hmr_client_error', + 'cache_read_error', 'cache_write_error', 'worker_stderr_chunk')) + result.update(event=enum(value.get('event'), allowed), + error_type=enum(value.get('error_type'), METRO_ERRORS), + categories=fixed_list(value.get('categories'), METRO_CATEGORIES)) + if result['kind'] == 'BUILD': + result.update(platform=enum(value.get('platform'), ('ios', 'android', 'OTHER')), + **flags(value, ('dev', 'minify'))) + elif result['kind'] in ('REQUEST', 'RESPONSE'): + result['request'] = enum(value.get('request'), ('ENTRY_BUNDLE', 'SPLIT_BUNDLE', 'SYMBOLICATE')) + if result['kind'] == 'RESPONSE': + code = count(value.get('outer_http_status'), 599) + result['outer_http_status'] = code if code is not None and code >= 100 else None + else: + options = obj(value.get('options')) + result['options'] = {name: options.get(name) if type(options.get(name)) is bool else + None if options.get(name) is None else 'OTHER' + for name in ('dev', 'lazy', 'minify', 'modulesOnly', 'runModule', 'excludeSource')} + for name, allowed in {'platform': ('ios', 'android', 'OTHER'), + 'sourcePaths': ('url-server', 'absolute', 'OTHER'), + 'engine': ('hermes', 'jsc', 'OTHER'), 'app': ('EXPECTED', 'OTHER')}.items(): + result['options'][name] = enum(options.get(name), allowed) + return result + + +def observations(value): + value = obj(value) + metro, hierarchy = obj(value.get('metro')), obj(value.get('hierarchy')) + rows = metro.get('rows') + result = {'collection_errors': fixed_list(value.get('collection_errors'), ('metro', 'hierarchy', 'collector')), + 'process_state': 'UNKNOWN', + 'hierarchy': {**flags(hierarchy, HIERARCHY), + 'snapshot_count': count(hierarchy.get('snapshot_count'), 8), + 'observation_state': enum(hierarchy.get('observation_state'), ('UNKNOWN', 'SNAPSHOTS_PARSED'))}, + 'hierarchy_scope': 'ANY_RETAINED_SNAPSHOT_NOT_CURRENT_ROUTE_OR_TAP_DELIVERY', + 'metro': {'observation_state': enum(metro.get('observation_state'), ('UNKNOWN', 'ROWS_OBSERVED')), + 'coverage': enum(metro.get('coverage'), ('UNKNOWN', 'PARTIAL_OR_CAPPED', 'BOUNDED_INPUT_ONLY')), + 'rows': [metro_row(row) for row in rows[:512]] if type(rows) is list else [], + **flags(metro, ('input_truncated', 'producer_limit_may_be_reached', 'export_truncated', 'line_scan_truncated')), + **{key: count(metro.get(key), 1024) for key in + ('invalid_rows', 'producer_row_limit', 'export_row_limit', 'line_scan_limit', + 'lines_scanned', 'valid_rows_in_scanned_input', 'exported_rows')}, + 'scope': 'OUTER_HTTP_STATUS_NOT_BUNDLE_EXECUTION_OR_UI_ACCEPTANCE'}} + return result + + +def diagnostics(value): + value = obj(value) + ios, driver, maestro = (obj(value.get(key)) for key in ('ios_errors', 'maestro_preflight', 'maestro')) + hierarchy = obj(value.get('hierarchy_signals')) + errors = value.get('collection_errors') + return {'collection_errors': [ + {'stage': enum(obj(row).get('stage'), ('maestro', 'maestro_preflight', 'hierarchy_signals', + 'metro_errors', 'metro_progress', 'ios_errors', 'collector')), + 'error_type': enum(obj(row).get('error_type'), ERROR_TYPES)} + for row in errors[:32]] if type(errors) is list else [], + 'ios_errors': { + 'cause': 'UNDETERMINED', 'query_status': enum(ios.get('query_status'), ('SUCCESS', 'FAILED', 'TIMEOUT')), + 'parse_status': enum(ios.get('parse_status'), ('PARSED', 'PARTIAL', 'UNKNOWN')), + 'observation_state': enum(ios.get('observation_state'), ('OWN_RECORDS_OBSERVED', 'UNKNOWN')), + **flags(ios, ('input_truncated', 'replacement_character_observed', 'scan_limit_reached', 'record_limit_reached')), + **{key: count(ios.get(key)) for key in ('matching_process_records', 'malformed_record_lines', + 'oversized_record_lines', 'error_records', 'classified_error_records', 'unclassified_error_records')}, + 'categories': fixed_list(ios.get('categories'), IOS_CATEGORIES), + 'category_record_counts': {key: count(obj(ios.get('category_record_counts')).get(key)) for key in IOS_CATEGORIES}}, + 'maestro_preflight': {**flags(driver, PREFLIGHT), + **{key: count(driver.get(key), 6) for key in ('logs_read', 'truncated_logs')}, + 'exception_types': fixed_list(driver.get('exception_types'), DRIVER_TYPES), + 'collection_error_types': fixed_list(driver.get('collection_error_types'), ERROR_TYPES)}, + 'maestro': {'report': enum(maestro.get('report'), ('PRESENT', 'MISSING')), + 'testcase_statuses': fixed_list(maestro.get('testcase_statuses'), ('PASS', 'FAIL', 'ERROR', 'SKIPPED')), + 'first_create_passcode_assertion_reported': None if type(maestro.get('testcase_failures')) is not list else + 'Maestro assertion failed: Create a passcode' in + fixed_list(maestro.get('testcase_failures'), ('Maestro assertion failed: Create a passcode',))}, + 'hierarchy': {**flags(hierarchy, ('createPIN', 'confirmPIN', 'RNredbox', 'noScriptURL', + 'developmentServerError', 'unableLoadScript')), + 'snapshot_count': count(hierarchy.get('snapshot_count'), 9), + 'parse_error_types': fixed_list(hierarchy.get('parse_error_types'), ERROR_TYPES)}, + 'metro_progress': {**flags(value.get('metro_progress'), ('bundle_marker_observed',)), + **{key: count(obj(value.get('metro_progress')).get(key)) for key in + ('bundle_marker_count', 'prebundle_marker_count')}}} + + +def _decode(blob): + if type(blob) is not bytes or len(blob) > LIMIT + 4096: + raise ValueError('Artifact rejected.') + with zipfile.ZipFile(io.BytesIO(blob)) as archive: + entries = archive.infolist() + if len(entries) != 1: + raise ValueError('Artifact rejected.') + entry = entries[0] + if (entry.filename != 'ios/metadata.json' or entry.orig_filename != entry.filename or + entry.flag_bits & 1 or stat.S_IFMT(entry.external_attr >> 16) not in (0, stat.S_IFREG) or + entry.file_size > LIMIT or entry.compress_type not in (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED)): + raise ValueError('Artifact rejected.') + with archive.open(entry) as source: + raw = source.read(LIMIT + 1) + if len(raw) != entry.file_size or len(raw) > LIMIT: + raise ValueError('Artifact rejected.') + def pairs(items): + result = {} + for key, value in items: + if key in result: + raise ValueError('Artifact rejected.') + result[key] = value + return result + def finite(value): + result = float(value) + if not math.isfinite(result): + raise ValueError('Artifact rejected.') + return result + def reject(_): + raise ValueError('Artifact rejected.') + data = json.loads(raw, object_pairs_hook=pairs, parse_float=finite, parse_constant=reject) + expected = {'schema': 1, 'source_sha': SOURCE, 'workflow_sha': WORKFLOW, 'workflow_ref': WORKFLOW_REF, + 'run_id': RUN, 'run_attempt': ATTEMPT, 'platform': 'IOS', 'diagnostic_only': True} + if type(data) is not dict or any(type(data.get(key)) is not type(value) or data.get(key) != value for key, value in expected.items()): + raise ValueError('Artifact rejected.') + built, installed = obj(data.get('built_artifact')), obj(data.get('installed_artifact')) + built_sha, installed_sha = digest(built.get('executable_sha256')), digest(installed.get('executable_sha256')) + method = enum(installed.get('method'), ('FULL_SIMULATOR_BUNDLE_TREE_AND_EXECUTABLE_SHA256',)) + reported = boolean(data.get('installed_artifact_verified')) + matches = (built_sha == installed_sha) if reported is True and built_sha and installed_sha and method != 'UNKNOWN' else None + return {'tag': 'SAFE_R5_IOS_READOUT', 'source_sha': SOURCE, 'workflow_sha': WORKFLOW, + 'run_id': RUN, 'run_attempt': ATTEMPT, 'artifact_id': ARTIFACT, + 'diagnostic_only': True, 'readout_only': True, 'authorization_eligible': False, + 'trusted_native_acceptance': False, 'release_acceptance': 'NOT ESTABLISHED', 'cause': 'UNDETERMINED', + 'reported_phases': {key: enum(data.get(key)) for key in ('build', 'smoke', 'native_probe', 'r5_features', 'hardware', 'signing')}, + 'fixed_command_receipts': commands(data.get('commands')), + 'installed_artifact': {'reported_verified': reported, 'method': method, + 'built_executable_sha256': built_sha, 'installed_executable_matches_built': matches, + 'reported_inventory_sha256': digest(installed.get('inventory_sha256')), + 'inventory_digest_scope': 'REPORTED_FULL_INVENTORY_NOT_COMPARABLE_TO_BUILT_TREE_DIGEST', + 'scope': 'PRE_UI_REPORTED_PROOF_NOT_POST_FAILURE_OR_PROTECTED_ACCEPTANCE'}, + 'startup_diagnostics': diagnostics(data.get('startup_diagnostics')), + 'startup_observation': observations(data.get('startup_observation')), + 'cleanup': {'reported_errors': fixed_list(data.get('startup_cleanup_errors'), CLEANUP), + 'status': 'FAIL' if data.get('startup_cleanup_errors') else 'UNKNOWN', + 'observation_status': enum(data.get('startup_observation_cleanup'), ('FAIL',)), + 'scope': 'ABSENT_ERROR_FIELD_DOES_NOT_ESTABLISH_DISPOSAL'}} + + +def decode(blob): + try: + return _decode(blob) + except Exception: + raise ValueError('Artifact rejected.') from None + + +def main(argv=None): + try: + arguments = sys.argv[1:] if argv is None else argv + if arguments: + raise ValueError('Artifact rejected.') + temp = Path(os.environ['RUNNER_TEMP']) + if not temp.is_absolute() or temp.resolve(strict=True) != temp: + raise ValueError('Artifact rejected.') + path = temp / 'r5-1bc-ios-fixed.zip' + with os.fdopen(os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK), 'rb') as source: + info = os.fstat(source.fileno()) + if (not stat.S_ISREG(info.st_mode) or stat.S_IMODE(info.st_mode) != 0o600 or + info.st_size != ZIP_SIZE or info.st_uid != os.getuid() or info.st_nlink != 1): + raise ValueError('Artifact rejected.') + blob = source.read(ZIP_SIZE + 1) + if len(blob) != ZIP_SIZE or hashlib.sha256(blob).hexdigest() != ZIP_SHA256: + raise ValueError('Artifact rejected.') + result = decode(blob) + except Exception: + print('FIXED_IOS_ARTIFACT_READOUT_FAILED') + return 1 + print('SAFE_R5_IOS_READOUT ' + json.dumps(result, sort_keys=True, allow_nan=False)) + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/.github/tests/1bc-ios-artifact-readout.test.cjs b/.github/tests/1bc-ios-artifact-readout.test.cjs new file mode 100644 index 000000000..3e2ed51f7 --- /dev/null +++ b/.github/tests/1bc-ios-artifact-readout.test.cjs @@ -0,0 +1,154 @@ +'use strict'; +const assert = require('node:assert/strict'); +const test = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const { run } = require('../scripts/read-1bc-ios-artifact.cjs'); +const SOURCE = '1bc34890cb8618b2fffd446a63924974e0fe15b5'; +const WORKFLOW = '3c44ab6412a8118b183273c402dd08136db05b28'; +const DIGEST = '550142809a0faf812741a586630b17d6f458b2b6c935b5bab4b00f84da67c84f'; + +function fixture() { + const context = { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, + eventName: 'push', ref: 'refs/heads/codex/r5-1bc-ios-artifact-readout' }; + const runData = { id: 39858617273, run_attempt: 1, repository: { id: 470904030 }, + head_repository: { id: 470904030 }, head_sha: SOURCE, event: 'pull_request', + head_branch: 'codex/r5-ios-startup-diagnosis-20261022', path: '.github/workflows/keeper-native-diagnostic.yml', + status: 'in_progress', conclusion: null, referenced_workflows: [{ + path: `KeeperCommunity/bitcoin-keeper/.github/workflows/keeper-native-platform.yml@${WORKFLOW}`, + ref: 'refs/pull/7116/merge', sha: WORKFLOW }] }; + const jobData = { id: 120103187854, run_id: 39858617273, head_sha: SOURCE, + name: 'ios / Native IOS build and probes', status: 'completed', conclusion: 'failure', + steps: [['Check out exact app source', 'success'], ['Verify current diagnostic source', 'success'], + ['Install immutable native dependencies', 'success'], ['Check disposable iOS driver readiness', 'success'], + ['Build development app', 'success'], ['Run disposable startup smoke', 'failure'], + ['Run disposable native persistence probe', 'skipped'], ['Run R5 production worker native scenarios', 'skipped'], + ['Measure five full-product startup and first-wallet runs', 'skipped'], ['Retain safe Phase A evidence', 'success']] + .map(([name, conclusion]) => ({ name, conclusion, status: 'completed' })) }; + const jobs = { total_count: 3, jobs: [{ id: 120103187854 }, { id: 120103189932 }, + { id: 120125972406, run_id: 39858617273, head_sha: SOURCE, + name: 'android / Native ANDROID build and probes', status: 'in_progress', conclusion: null }] }; + const artifact = { id: 12462694619, name: `keeper-diagnostic-native-IOS-${SOURCE}-39858617273-1`, + expired: false, size_in_bytes: 6884, digest: `sha256:${DIGEST}`, workflow_run: { + id: 39858617273, head_sha: SOURCE, repository_id: 470904030, head_repository_id: 470904030, + head_branch: 'codex/r5-ios-startup-diagnosis-20261022' } }; + const commit = { sha: SOURCE, commit: { tree: { sha: '2e9cd57bf9f78b92cac73e9343ddbd0eef25e86e' } } }; + const pr = { number: 7116, head: { sha: SOURCE, ref: runData.head_branch, repo: { id: 470904030 } }, + state: 'open', merged: false }; + const calls = [], output = [], actions = {}; + for (const [name, data] of [['getWorkflowRun', runData], ['getJobForWorkflowRun', jobData], + ['getArtifact', artifact], ['listJobsForWorkflowRun', jobs]]) { + actions[name] = async args => { calls.push([name, args]); return { data }; }; + } + actions.downloadArtifact = async args => { calls.push(['downloadArtifact', args]); return { data: Buffer.alloc(6884) }; }; + const inputs = { github: { rest: { actions, + repos: { getCommit: async args => { calls.push(['getCommit', args]); return { data: commit }; } }, + pulls: { get: async args => { calls.push(['getPR', args]); return { data: pr }; } } } }, context, + core: { info: value => output.push(value), warning: value => output.push(value) } }; + return { context, runData, jobData, jobs, artifact, commit, pr, calls, output, inputs, invoke: () => run(inputs) }; +} + +test('current iOS provenance reaches only the fixed archive; unknown bytes supply no receipt', async () => { + const f = fixture(); + await assert.rejects(f.invoke, /^Error: FIXED_IOS_ARTIFACT_READOUT_FAILED$/); + assert.deepEqual(f.calls.at(-1), ['downloadArtifact', { + owner: 'KeeperCommunity', repo: 'bitcoin-keeper', request: { timeout: 30000 }, + artifact_id: 12462694619, archive_format: 'zip' }]); + assert.deepEqual(f.output, []); +}); + +test('foreign execution contexts cannot issue an API request', async () => { + for (const change of [f => f.context.repo.owner = 'foreign', f => f.context.repo.repo = 'foreign', + f => f.context.eventName = 'workflow_dispatch', f => f.context.ref = 'refs/heads/sprint']) { + const f = fixture(); change(f); await assert.rejects(f.invoke, /FIXED_IOS_ARTIFACT_READOUT_FAILED/); + assert.deepEqual(f.calls, []); assert.deepEqual(f.output, []); + } +}); + +test('stale, contradictory or cross-source API evidence fails before download', async () => { + for (const change of [ + f => f.runData.id++, f => f.runData.run_attempt++, f => f.runData.head_sha = WORKFLOW, + f => f.runData.head_repository.id++, f => f.runData.event = 'push', f => f.runData.conclusion = 'failure', + f => f.runData.path = 'other.yml', f => f.runData.referenced_workflows = [], + f => f.runData.referenced_workflows[0].sha = SOURCE, f => f.runData.head_branch += '-other', + f => f.jobData.id++, f => f.jobData.run_id++, f => f.jobData.head_sha = WORKFLOW, + f => f.jobData.status = 'in_progress', f => f.jobData.steps[5].conclusion = 'success', + f => f.jobData.steps[6].conclusion = 'success', f => f.jobData.steps[9].conclusion = 'failure', + f => f.jobData.steps.push(f.jobData.steps[9]), f => f.jobs.total_count++, + f => f.jobs.jobs[2].head_sha = WORKFLOW, f => f.jobs.jobs[2].id++, + f => f.jobs.jobs[2].status = 'completed', f => f.artifact.id++, f => f.artifact.name += '-other', + f => f.artifact.expired = true, f => f.artifact.size_in_bytes++, + f => f.artifact.digest = 'sha256:' + '0'.repeat(64), f => f.artifact.workflow_run.id++, + f => f.artifact.workflow_run.head_sha = WORKFLOW, f => f.artifact.workflow_run.repository_id++, + f => f.commit.sha = WORKFLOW, f => f.commit.commit.tree.sha = '0'.repeat(40), + f => f.pr.head.sha = WORKFLOW, f => f.pr.merged = true, f => f.pr.state = 'closed', + ]) { + const f = fixture(); change(f); await assert.rejects(f.invoke, /FIXED_IOS_ARTIFACT_READOUT_FAILED/); + assert.equal(f.calls.some(([name]) => name === 'downloadArtifact'), false); + assert.deepEqual(f.output, []); + } +}); + +function synthetic(f, options = {}) { + const events = []; + const receipt = { source_sha: SOURCE, workflow_sha: WORKFLOW, run_id: '39858617273', run_attempt: '1', + artifact_id: '12462694619', readout_only: true, authorization_eligible: false, trusted_native_acceptance: false }; + const sandbox = { module: { exports: {} }, Buffer, process: { env: { RUNNER_TEMP: '/owned' } }, require(name) { + if (name === 'node:assert/strict') return { ...assert, deepEqual(a, b) { + assert.deepEqual(JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b))); + } }; + if (name === 'node:path') return path; + if (name === 'node:crypto') return { createHash: () => ({ update() { return this; }, digest: () => DIGEST }) }; + if (name === 'node:fs') return { + realpathSync: x => x, openSync(filename, flags, mode) { + assert.equal(filename, '/owned/r5-1bc-ios-fixed.zip'); assert.equal(flags, 'wx'); assert.equal(mode, 0o600); + events.push('open'); return 42; + }, fstatSync() { if (options.stat) throw new Error('PRIVATE_SENTINEL'); return { dev: 1, ino: 2 }; }, + writeFileSync() { events.push('write'); if (options.write) throw new Error('PRIVATE_SENTINEL'); }, + closeSync: () => events.push('close'), + lstatSync: () => ({ dev: 1, ino: options.replaced ? 3 : 2, isFile: () => true, isSymbolicLink: () => false }), + unlinkSync() { events.push('unlink'); if (options.unlink) throw new Error('PRIVATE_SENTINEL'); }, + }; + if (name === 'node:child_process') return { execFileSync(command, args) { + assert.equal(command, 'python3'); assert.deepEqual(Array.from(args), ['-B', '.github/scripts/read-1bc-ios-artifact.py']); + events.push('decoder'); if (options.decoder) throw new Error('PRIVATE_SENTINEL'); + return options.raw ? 'PRIVATE_SENTINEL' : 'SAFE_R5_IOS_READOUT ' + JSON.stringify(receipt); + } }; + throw new Error('Unexpected synthetic import'); + } }; + vm.runInNewContext(fs.readFileSync(path.join(__dirname, '../scripts/read-1bc-ios-artifact.cjs'), 'utf8'), sandbox); + f.inputs.core = { info(value) { events.push('info'); f.output.push(value); }, warning(value) { events.push('warning'); f.output.push(value); } }; + return { events, invoke: () => sandbox.module.exports.run(f.inputs) }; +} + +test('post-download freshness rejects a later attempt before private write or decoding', async () => { + const f = fixture(); f.inputs.github.rest.actions.downloadArtifact = async () => { + f.runData.run_attempt++; return { data: Buffer.alloc(6884) }; + }; + const s = synthetic(f); await assert.rejects(s.invoke, /FIXED_IOS_ARTIFACT_READOUT_FAILED/); + assert.deepEqual(s.events, []); assert.deepEqual(f.output, []); +}); + +test('owned bytes purge before output; errors stay fixed and inode replacements are preserved', async () => { + for (const options of [{}, { decoder: true }, { write: true }, { raw: true }, { stat: true }, + { unlink: true }, { replaced: true }, { decoder: true, unlink: true }]) { + const f = fixture(), s = synthetic(f, options); + if (options.decoder || options.write || options.raw || options.stat) await assert.rejects(s.invoke, /^Error: FIXED_IOS_ARTIFACT_READOUT_FAILED$/); + else if (options.unlink || options.replaced) await assert.rejects(s.invoke, /^Error: FIXED_IOS_ARTIFACT_PRIVATE_CLEANUP_FAILED$/); + else await s.invoke(); + assert.equal(s.events.filter(x => x === 'unlink').length, options.replaced || options.stat ? 0 : 1); + assert.ok(s.events.indexOf('close') > s.events.indexOf('open')); + if (!Object.keys(options).length) assert.ok(s.events.indexOf('unlink') < s.events.indexOf('info')); + else assert.equal(s.events.includes('info'), false); + assert.equal(JSON.stringify(f.output).includes('PRIVATE_SENTINEL'), false); + } +}); + +test('naturally completed failed parent run retains this artifact without borrowing Android acceptance', async () => { + const f = fixture(); f.runData.status = 'completed'; f.runData.conclusion = 'failure'; + f.jobs.jobs[2].status = 'completed'; f.jobs.jobs[2].conclusion = 'failure'; + const s = synthetic(f); await s.invoke(); + assert.ok(f.output[0].startsWith('SAFE_R5_IOS_READOUT ')); + assert.equal(JSON.parse(f.output[0].slice('SAFE_R5_IOS_READOUT '.length)).trusted_native_acceptance, false); +}); diff --git a/.github/tests/test_1bc_ios_artifact_readout.py b/.github/tests/test_1bc_ios_artifact_readout.py new file mode 100644 index 000000000..c28482df5 --- /dev/null +++ b/.github/tests/test_1bc_ios_artifact_readout.py @@ -0,0 +1,238 @@ +"""Synthetic-only fixed iOS readout controls; no real artifact or native launch.""" +from contextlib import redirect_stdout +import hashlib +import importlib.util +import io +import json +import os +from pathlib import Path +import stat +import struct +import tempfile +import unittest +from unittest import mock +import warnings +import zipfile + +SPEC = importlib.util.spec_from_file_location('ios_readout', + Path(__file__).resolve().parents[1] / 'scripts/read-1bc-ios-artifact.py') +reader = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(reader) + + +def fixture(): + return {'schema': 1, 'source_sha': reader.SOURCE, 'workflow_sha': reader.WORKFLOW, + 'workflow_ref': reader.WORKFLOW_REF, 'run_id': reader.RUN, 'run_attempt': reader.ATTEMPT, + 'platform': 'IOS', 'diagnostic_only': True, 'build': 'PASS', 'smoke': 'FAIL', + 'native_probe': 'NOT RUN', 'r5_features': 'NOT RUN', 'hardware': 'NOT RUN', 'signing': 'NOT RUN', + 'built_artifact': {'executable_sha256': 'a' * 64, 'bundle_tree_sha256': 'b' * 64, + 'path': 'PRIVATE_SENTINEL', 'executable': 'PRIVATE_SENTINEL'}, + 'installed_artifact_verified': True, + 'installed_artifact': {'method': 'FULL_SIMULATOR_BUNDLE_TREE_AND_EXECUTABLE_SHA256', + 'executable_sha256': 'a' * 64, 'inventory_sha256': 'c' * 64}, + 'commands': [{'label': 'Assert startup and disposable passcode transition', + 'duration_seconds': 123.456, 'timeout_seconds': 420, 'exit_code': 1, + 'command': ['PRIVATE_SENTINEL']}], + 'startup_diagnostics': {'maestro': {'report': 'PRESENT', 'testcase_statuses': ['ERROR'], + 'testcase_failures': ['Maestro assertion failed: Create a passcode']}, + 'ios_errors': {'query_status': 'SUCCESS', 'parse_status': 'PARSED', + 'observation_state': 'OWN_RECORDS_OBSERVED', 'error_records': 616, + 'classified_error_records': 18, 'unclassified_error_records': 598, + 'categories': ['CONNECTION'], 'category_record_counts': {'CONNECTION': 18}}, + 'maestro_preflight': {'status_check_failed': True, 'exception_types': ['ConnectException']}}, + 'startup_observation': {'collection_errors': [], + 'hierarchy': {'login': True, 'retry': False, 'snapshot_count': 1, + 'observation_state': 'SNAPSHOTS_PARSED'}, + 'metro': {'coverage': 'BOUNDED_INPUT_ONLY', 'observation_state': 'ROWS_OBSERVED', + 'rows': [{'stage': 'APP_WINDOW', 'kind': 'RESPONSE', 'request': 'ENTRY_BUNDLE', + 'outer_http_status': 200}, + {'stage': 'BEFORE_APP', 'kind': 'REQUEST', 'request': 'ENTRY_BUNDLE', + 'options': {'dev': True, 'lazy': False, 'platform': 'ios', 'engine': 'hermes'}}, + {'stage': 'UNKNOWN', 'kind': 'BUILD', 'event': 'bundle_build_done', + 'platform': 'ios', 'dev': True, 'minify': False, 'categories': []}]}}} + + +def archive(data=None, raw=None, name='ios/metadata.json', attributes=None, extra=False): + stream = io.BytesIO() + with zipfile.ZipFile(stream, 'w', zipfile.ZIP_DEFLATED) as output: + entry = zipfile.ZipInfo(name) + entry.compress_type = zipfile.ZIP_DEFLATED + entry.external_attr = attributes if attributes is not None else (stat.S_IFREG | 0o600) << 16 + output.writestr(entry, raw if raw is not None else json.dumps(fixture() if data is None else data)) + if extra: + output.writestr('PRIVATE_SENTINEL', 'PRIVATE_SENTINEL') + return stream.getvalue() + + +class ReadoutTest(unittest.TestCase): + def rejected(self, blob): + with self.assertRaises(ValueError) as error: + reader.decode(blob) + self.assertEqual(str(error.exception), 'Artifact rejected.') + + def test_partial_failure_and_separate_inventory_algorithm_are_preserved(self): + result = reader.decode(archive()) + self.assertEqual(result['reported_phases']['smoke'], 'FAIL') + self.assertEqual(result['reported_phases']['native_probe'], 'NOT RUN') + self.assertTrue(result['installed_artifact']['installed_executable_matches_built']) + self.assertEqual(result['installed_artifact']['reported_inventory_sha256'], 'c' * 64) + self.assertNotIn('b' * 64, json.dumps(result)) + self.assertEqual(result['fixed_command_receipts']['startup_ui'][0]['duration_seconds'], 123.456) + ios = result['startup_diagnostics']['ios_errors'] + self.assertEqual(ios['unclassified_error_records'], 598) + self.assertEqual(ios['categories'], ['CONNECTION']) + self.assertTrue(result['startup_diagnostics']['maestro']['first_create_passcode_assertion_reported']) + self.assertEqual(result['startup_observation']['process_state'], 'UNKNOWN') + self.assertTrue(result['startup_observation']['hierarchy']['login']) + self.assertEqual(result['startup_observation']['metro']['rows'][0]['outer_http_status'], 200) + self.assertEqual(result['cleanup']['status'], 'UNKNOWN') + self.assertFalse(result['trusted_native_acceptance']) + self.assertFalse(result['authorization_eligible']) + self.assertEqual(result['cause'], 'UNDETERMINED') + + def test_exact_identity_and_scalar_types_are_mandatory(self): + for key in ('schema', 'source_sha', 'workflow_sha', 'workflow_ref', 'run_id', + 'run_attempt', 'platform', 'diagnostic_only'): + for replacement in (None, 'PRIVATE_SENTINEL'): + with self.subTest(key=key, replacement=replacement): + value = fixture() + value[key] = replacement + self.rejected(archive(value)) + value = fixture() + value['schema'] = True + self.rejected(archive(value)) + + def test_zip_paths_members_types_encryption_and_size_are_rejected(self): + for options in ({'name': '../ios/metadata.json'}, {'name': '/ios/metadata.json'}, + {'name': 'ios\\metadata.json'}, {'name': 'ios/metadata.json\0private'}, + {'attributes': (stat.S_IFLNK | 0o777) << 16}, + {'attributes': (stat.S_IFIFO | 0o600) << 16}, {'extra': True}, + {'raw': b'x' * (reader.LIMIT + 1)}): + with self.subTest(options=tuple(options)): + # ZipInfo truncates embedded NUL, so construct that invalid header below. + if '\0' not in options.get('name', ''): + self.rejected(archive(**options)) + stream = io.BytesIO() + with warnings.catch_warnings(), zipfile.ZipFile(stream, 'w') as output: + warnings.simplefilter('ignore', UserWarning) + output.writestr('ios/metadata.json', '{}') + output.writestr('ios/metadata.json', '{}') + self.rejected(stream.getvalue()) + encrypted = bytearray(archive()) + struct.pack_into('