Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions .github/native/hosted_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ def ios_command_observation():
spec.loader.exec_module(observer)
return observer.collect(PRIVATE, device)
collect('command_observation', ios_command_observation)
def ios_replacement_observation():
spec = importlib.util.spec_from_file_location('ios_replacement_observation',
Path(__file__).with_name('ios_replacement_observation.py'))
observer = importlib.util.module_from_spec(spec)
spec.loader.exec_module(observer)
return observer.collect(SimpleNamespace(PRIVATE=PRIVATE, PLATFORM=PLATFORM,
read=read, artifact_identity=artifact_identity,
_bundle_inventory=_bundle_inventory, owned_ios_bundle_path=owned_ios_bundle_path), device)
collect('replacement_observation', ios_replacement_observation)
if PLATFORM == 'ANDROID' and device:
def android_details():
args = ['adb', '-s', device, 'logcat', '-d', '-t', '300', '-v', 'brief',
Expand Down Expand Up @@ -720,6 +729,21 @@ def _bundle_inventory(bundle):
return tree, executable


def owned_ios_bundle_path(installed, device, built):
"""The same exact owned container policy for acceptance and observations."""
uuid = r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}'
if not re.fullmatch(uuid, device):
raise ValueError('Expected owned simulator UUID.')
installed = Path(installed)
base = Path.home() / 'Library/Developer/CoreSimulator/Devices' / device / 'data/Containers/Bundle/Application'
if (not installed.is_absolute() or installed.name != built.name or
not re.fullmatch(uuid, installed.parent.name) or
installed.parent.parent.resolve() != base.resolve() or
installed.resolve(strict=True).parent.parent != base.resolve()):
raise ValueError('Unexpected installed app container.')
return installed


def verify_installed_artifact(device):
"""Attest owned installed bytes, never installation argv or app assertions."""
data = read()
Expand All @@ -738,12 +762,7 @@ def verify_installed_artifact(device):
original, executable = _bundle_inventory(built)
installed = Path(command(['xcrun', 'simctl', 'get_app_container', device, APP_IDS['IOS'], 'app'],
'Locate owned installed app bundle', capture=True, error_details=False))
base = Path.home() / 'Library/Developer/CoreSimulator/Devices' / device / 'data/Containers/Bundle/Application'
if (not installed.is_absolute() or installed.name != built.name or
not re.fullmatch(uuid, installed.parent.name) or
installed.parent.parent.resolve() != base.resolve() or
installed.resolve(strict=True).parent.parent != base.resolve()):
raise ValueError('Unexpected installed app container.')
installed = owned_ios_bundle_path(installed, device, built)
actual, installed_executable = _bundle_inventory(installed)
if original != actual or executable != installed_executable:
raise ValueError('Installed bundle differs; no installer normalization is accepted.')
Expand Down
128 changes: 128 additions & 0 deletions .github/native/ios_replacement_observation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""Read-only post-failure bundle observations; never repair or acceptance.

Maestro clearState replaces the first installed instance. Inspect the remaining
owned instance after failure without a launch, retry or metadata promotion.
"""
import importlib.util
from pathlib import Path
import plistlib
import re
import subprocess
import tempfile

APP = 'io.hexawallet.hexakeeper.dev'
LIMIT = 1024 * 1024
UUID = r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}'


def query(private, args):
"""Keep output in an unnamed private file; retain no command or raw text."""
try:
with tempfile.TemporaryFile(dir=private) as output:
process = subprocess.run(args, stdout=output, stderr=subprocess.STDOUT,
timeout=20, check=False)
output.seek(0)
payload = output.read(LIMIT + 1)
if len(payload) > LIMIT:
return 'OVERSIZED', None
if process.returncode != 0:
return 'NONZERO_EXIT', None
return 'EXIT_ZERO', payload
except subprocess.TimeoutExpired:
return 'TIMEOUT', None
except OSError:
return 'UNAVAILABLE', None


def entitlement_state(payload):
"""Compare types and complete effective values to the fixed signing policy."""
try:
if type(payload) is not bytes or len(payload) > LIMIT:
return 'UNKNOWN'
begin, end = payload.find(b'<?xml'), payload.find(b'</plist>')
if (begin < 0 or end < begin or payload.count(b'<?xml') != 1 or
payload.count(b'</plist>') != 1):
return 'UNKNOWN'
effective = plistlib.loads(payload[begin:end + len(b'</plist>')])
spec = importlib.util.spec_from_file_location('observed_simulator_policy',
Path(__file__).with_name('simulator_signing.py'))
policy = importlib.util.module_from_spec(spec)
spec.loader.exec_module(policy)
if type(effective) is not dict:
return 'UNKNOWN'
return ('MATCH' if plistlib.dumps(effective, sort_keys=True) ==
plistlib.dumps(policy.ENTITLEMENTS, sort_keys=True) else 'MISMATCH')
except Exception:
return 'UNKNOWN'


def unknown(stage='NOT_RUN'):
return {'schema': 1, 'state': 'UNKNOWN', 'stage': stage,
'bytes': 'UNKNOWN', 'signature': 'UNKNOWN', 'identity': 'UNKNOWN',
'entitlements': 'UNKNOWN', 'built_internal_links': None,
'installed_internal_links': None, 'queries': {},
'scope': 'POST_FAILURE_CURRENT_INSTANCE_NOT_LAUNCH_CAUSE_OR_ACCEPTANCE'}


def collect(native, device):
result = unknown()
stage = 'DEVICE_POLICY'
try:
if (native.PLATFORM != 'IOS' or type(device) is not str or
not re.fullmatch(UUID, device)):
return unknown(stage)
stage = 'BUILT_ARTIFACT_STABILITY'
expected = native.read()['built_artifact']
if native.artifact_identity() != expected:
return unknown(stage)
built = native.PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app'
stage = 'BUILT_INVENTORY'
original, executable = native._bundle_inventory(built)
get_container = ['xcrun', 'simctl', 'get_app_container', device, APP, 'app']
stage = 'INSTALLED_CONTAINER_QUERY'
status, payload = query(native.PRIVATE, get_container)
result['queries']['container'] = status
if status != 'EXIT_ZERO':
return {**result, 'stage': stage}
stage = 'INSTALLED_CONTAINER_POLICY'
installed = native.owned_ios_bundle_path(payload.decode('utf-8').strip(), device, built)
stage = 'INSTALLED_INVENTORY'
actual, actual_executable = native._bundle_inventory(installed)
result['bytes'] = 'MATCH' if (original, executable) == (actual, actual_executable) else 'MISMATCH'
result['built_internal_links'] = sum(value[0] == 'link' for value in original.values())
result['installed_internal_links'] = sum(value[0] == 'link' for value in actual.values())
stage = 'SIGNATURE_QUERIES'
status, _ = query(native.PRIVATE, ['codesign', '--verify', '--deep', '--strict', str(installed)])
result['queries']['signature'] = status
result['signature'] = ('VERIFIED' if status == 'EXIT_ZERO' else
'NOT_VERIFIED' if status == 'NONZERO_EXIT' else 'UNKNOWN')
status, payload = query(native.PRIVATE, ['codesign', '--display', '--verbose=4', str(installed)])
result['queries']['identity'] = status
if status == 'EXIT_ZERO':
lines = payload.decode('utf-8').splitlines()
result['identity'] = ('ADHOC' if 'Signature=adhoc' in lines and
not any(line.startswith('Authority=') for line in lines) else 'OTHER')
status, payload = query(native.PRIVATE, ['codesign', '--display', '--entitlements', ':-', str(installed)])
result['queries']['entitlements'] = status
if status == 'EXIT_ZERO':
result['entitlements'] = entitlement_state(payload)
# All conclusions refer to one unchanged current instance and built input.
stage = 'FINAL_CONTAINER_QUERY'
status, payload = query(native.PRIVATE, get_container)
result['queries']['final_container'] = status
if status != 'EXIT_ZERO':
return unknown(stage)
final = native.owned_ios_bundle_path(payload.decode('utf-8').strip(), device, built)
stage = 'INSTALLED_STABILITY'
if final != installed or native._bundle_inventory(final) != (actual, actual_executable):
return unknown(stage)
stage = 'BUILT_ARTIFACT_STABILITY'
if (native._bundle_inventory(built) != (original, executable) or
native.artifact_identity() != expected):
return unknown(stage)
result.update(state='OBSERVED', stage='COMPLETE')
return result
except Exception:
# Unsupported/missing bundles and failed collectors supply no pass and
# never replace the original startup exception or export its message.
return unknown(stage)
39 changes: 37 additions & 2 deletions .github/native/ios_startup_command_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@
'SIMCTL_OPERATION_TEXT': r'An error was encountered processing the command',
'ASSERTION_TEXT': r'Assertion is false:|Element not found:',
}
ERROR_DOMAINS = frozenset(('FBSOpenApplicationServiceErrorDomain', 'FBSOpenApplicationErrorDomain',
'RBSRequestErrorDomain', 'RBSErrorDomain', 'NSPOSIXErrorDomain', 'NSCocoaErrorDomain',
'NSOSStatusErrorDomain', 'NSMachErrorDomain'))


def launch_error_codes(error):
"""Project exact simctl header pairs, without descriptions or interpretation."""
result = {'state': 'UNKNOWN', 'pairs': [], 'scope': 'RECORDED_CODES_NOT_INTERPRETED_CAUSE'}
pairs = []
if (type(error) is not dict or not set(error) <= {'message', 'debugMessage'} or
any(type(value) is not str or len(value) > 65536 for value in error.values())):
return result
pattern = (r'(?:An error was encountered processing the command|Underlying error) '
r'\(domain=([A-Za-z][A-Za-z0-9.]{0,95}), code=(-?(?:0|[1-9][0-9]{0,9}))\):')
for text in error.values():
for line in text.splitlines():
if 'domain=' not in line and 'Error Domain=' not in line:
continue
match = re.fullmatch(pattern, line.strip(' \t'))
if not match or match[1] not in ERROR_DOMAINS:
return result
code = int(match[2])
if not -(2 ** 31) <= code < 2 ** 31:
return result
pair = {'domain': match[1], 'code': code}
if pair not in pairs:
pairs.append(pair)
if len(pairs) > 8:
return result
if pairs:
result.update(state='RECORDED', pairs=pairs)
return result


def unknown():
Expand Down Expand Up @@ -87,10 +119,13 @@ def commands(directory):
if (type(evaluated) is not dict or set(evaluated) != {key} or
type(evaluated[key]) is not dict or evaluated[key].get('appId') != APP):
return result
rows.append({'sequence': sequence, 'kind': kind,
row = {'sequence': sequence, 'kind': kind,
'status': metadata['status'],
'error_markers': [label for label, pattern in ERROR_MARKERS.items()
if re.search(pattern, text, re.IGNORECASE)]})
if re.search(pattern, text, re.IGNORECASE)]}
if kind == 'LAUNCH_APP' and metadata['status'] == 'FAILED':
row['launch_error_codes'] = launch_error_codes(error)
rows.append(row)
result.update(state='RECORDED', rows=rows)
except (OSError, ValueError, TypeError, RecursionError):
pass
Expand Down
Loading