Files
werk/scripts/android_apk_check.py
waoziandClaude Opus 5.5 0e718b1fff Add Gallery, a local photo browser for desktop and Android
Gallery is a separate portable tool: it browses local folders and albums,
opens a photo, moves between pictures and zooms. Werkzeug keeps the
folder grant in its private data and serves only Gallery; navigation
never leaves the granted root, and image bytes and pixels are bounded.
On Android it reads through the Storage Access Framework, read-only.
Nothing is synced.

Gallery has its own Linux launcher and icon, is bundled in the APK and on
the web build, and is covered by gallery-test (native fixtures),
gallery-gui-test (private Xvfb) and gallery-android-test (private
emulator).

Local tests (on the final tree of this series, published pins, no local
overrides, through build-queue): make test including ziran fmt --check,
gui-smoke, gui-test, gallery-gui-test and web-smoke on private displays.
Not run on these pins: the private-emulator Android gate. On an earlier
pin the x86_64 APK built and validated and android-input-test passed
until Entity's add-node page, which needs Kryon's density damage fix
(78af4090); it runs with the next Kryon pin update.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-17 01:10:42 -03:00

110 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Reject app-owned bytecode; permit shared Kryon account consent and resource constants."""
import re
import struct
import sys
from zipfile import ZipFile
def dex_classes(data):
assert data.startswith(b"dex\n"), "Invalid DEX header"
word = lambda offset: struct.unpack_from("<I", data, offset)[0]
strings, types = word(60), word(68)
count, definitions = word(96), word(100)
for index in range(count):
descriptor = word(types + word(definitions + index * 32) * 4)
offset = word(strings + descriptor * 4)
# Skip the ULEB128 string length, then read its ASCII descriptor.
while data[offset] & 128:
offset += 1
offset += 1
yield data[offset:data.index(0, offset)].decode("ascii")
def elf_section_names(data):
assert data[:4] == b"\x7fELF" and data[5] == 1, "Expected a little-endian ELF library"
wide = data[4] == 2
assert data[4] in (1, 2), "Unsupported ELF class"
table = struct.unpack_from("<Q" if wide else "<I", data, 40 if wide else 32)[0]
size, count, strings_index = struct.unpack_from("<HHH", data, 58 if wide else 46)
strings_entry = table + strings_index * size
offset, length = struct.unpack_from("<QQ" if wide else "<II", data, strings_entry + (24 if wide else 16))
strings = data[offset:offset + length]
for index in range(count):
name = struct.unpack_from("<I", data, table + index * size)[0]
yield strings[name:strings.index(0, name)].decode("ascii")
def elf_exports(data):
wide = data[4] == 2
table = struct.unpack_from("<Q" if wide else "<I", data, 40 if wide else 32)[0]
size, count = struct.unpack_from("<HH", data, 58 if wide else 46)
sections = []
for index in range(count):
entry = table + index * size
kind = struct.unpack_from("<I", data, entry + 4)[0]
offset, length = struct.unpack_from("<QQ" if wide else "<II", data, entry + (24 if wide else 16))
link = struct.unpack_from("<I", data, entry + (40 if wide else 24))[0]
stride = struct.unpack_from("<Q" if wide else "<I", data, entry + (56 if wide else 36))[0]
sections.append((kind, offset, length, link, stride))
for kind, offset, length, link, stride in sections:
if kind != 11: # SHT_DYNSYM: symbols visible to Android's loader.
continue
_, strings_offset, strings_length, _, _ = sections[link]
strings = data[strings_offset:strings_offset + strings_length]
for entry in range(offset, offset + length, stride):
name = struct.unpack_from("<I", data, entry)[0]
info, other, defined = struct.unpack_from("<BBH", data, entry + (4 if wide else 12))
if info >> 4 in (1, 2) and other & 3 == 0 and defined != 0:
yield strings[name:strings.index(0, name)].decode("ascii")
HANDOFF = {
"Lorg/kryonlabs/platform/" + name
for name in ("AccountHandoff", "AccountHandoffConsentActivity",
"AccountHandoffNameProvider", "AccountHandoffRequestActivity")
}
def allowed_class(item: str) -> bool:
return bool(re.fullmatch(r"Lxyz/waozi/pass/R(?:\$[^;]+)?;", item)) or any(
item == base + ";" or bool(re.fullmatch(
re.escape(base) + r"\$\$ExternalSynthetic(?:Lambda|ApiModelOutline)[0-9]+;", item))
for base in HANDOFF)
def check_apk(path):
with ZipFile(path) as archive:
names = archive.namelist()
# Account policy stays in Ziran. Kryon's consent flow is the only
# platform Java allowed alongside Android's resource constants.
packaged_classes = set()
for name in names:
if name.endswith(".dex"):
classes = list(dex_classes(archive.read(name)))
packaged_classes.update(classes)
unexpected = [item for item in classes if not allowed_class(item)]
assert not unexpected, f"{path}: packaged application bytecode: {unexpected}"
assert {base + ";" for base in HANDOFF} <= packaged_classes, f"{path}: missing shared account consent classes"
for font in ["ui.ttf", "emoji.ttf"]:
assert "assets/fonts/" + font in names, f"{path}: missing {font}"
bundles = {f"assets/apps/{app}.zx" for app in ["apps", "entity", "pass", "auth", "calculator", "clock", "workbook", "gallery"]}
packaged = {name for name in names if name.startswith("assets/apps/") and not name.endswith("/")}
assert packaged == bundles, f"{path}: stale or missing app bundles: {packaged ^ bundles}"
for name in bundles:
assert archive.read(name)[:4] == b"ZX\0\0", f"{path}: {name} has an obsolete bundle format"
assert "assets/pfp.webp" in names, f"{path}: missing profile pictures"
assert "assets/tools/pass.webp" in names, f"{path}: missing tool artwork"
native = [name for name in names if name.endswith("/libmain.so")]
assert native, f"{path}: no NativeActivity payload"
for name in native:
assert "JNI_OnLoad" in set(elf_exports(archive.read(name))), f"{path}: {name} hides account JNI registration"
assert ".note.gnu.build-id" not in set(elf_section_names(archive.read(name))), \
f"{path}: {name} contains a build-path-dependent build ID"
print(f"{path}: native Ziran app with shared Kryon account consent")
if __name__ == "__main__":
for path in sys.argv[1:]:
check_apk(path)