Reading the source¶
A guided tour for someone about to change appjail. Architecture maps every concept onto modules;
this page follows the code itself: where a command starts, what it calls in which order, where state is kept, and how
to add something without breaking the rules the rest of the code relies on. Function names are given as
module.function; all modules are under src/appjail/.
Rules the code relies on¶
Five conventions hold everywhere. Knowing them makes most modules easy to read, and a change that breaks one is almost certainly wrong.
| Rule | Where it lives | What it means for a change |
|---|---|---|
| Standard library only | pyproject.toml has no dependencies |
no imports from PyPI, in the package or in the window |
| Untrusted work happens in a jail | jail.py (helper jails), runtimejail.py (the app jail) |
a new step that touches downloaded bytes or runs a helper program gets a jail built from jail.common_args, never a plain subprocess call |
| Policy is pure | grants.py, valid.py, manifest.py, updater.blocks |
decisions about what is allowed take values and return reasons; they do no I/O, so they are easy to test exhaustively |
| Every line printed goes through the console | console.Console, textsafe.clean |
commands never call print; untrusted text cannot reach the terminal unfiltered |
| One command changes an app's files at a time | store.app_lock (a flock on locks/NAME.lock, released by the kernel if the process dies) |
taken by install.swap_in, runner.prepare (extraction included, which can take minutes), remove and reset; never while a download runs or an app is running. A second command waits up to store.LOCK_TIMEOUT, then fails with StoreBusy |
| Nothing is deleted under a running app | store.running (a shared flock on locks/NAME.run) and store.refuse_if_running |
runner.launch takes it before prepare releases the app lock and holds it until the jail ends; remove, reset and install.swap_in (for an installed app) check it under the app lock (decision) |
| Every write is atomic, every temporary directory is cleaned up | store.atomic_write, store.staging, store.remove_tree |
no open(path, "w") on a file appjail keeps; staging goes in a with store.staging() block |
From the command line to a command¶
__main__.py → cli.main(argv) → cli.build_parser() → COMMANDS[name](args, client, console, app_args) → cmd_*.py
cli.mainsplits off everything after--(it belongs to the app), parses the rest, creates theConsole, and routes the warnings of the jail layer to it (jail.warn_with).cli.COMMANDSmaps each command to a small handler that calls the real function in acmd_*.pymodule.cli.pyholds no command logic.- Errors are exceptions, turned into exit codes in one place:
| Exception | Raised for | Exit status |
|---|---|---|
console.Abort |
the user declined, or the command cannot go on (message shown as is) | 1 |
errors.JailError |
a helper jail failed or was refused | 1 |
errors.RunError |
an app cannot be started as asked | 1 |
valid.ManifestError |
a malformed manifest or a refused value | 1 |
github.GitHubError, catalog.IndexProblem, OSError |
network, catalog and file errors | 1 |
KeyboardInterrupt |
Ctrl-C | 130 |
| argument errors (argparse) | a usage error | 2 |
Walk-through: appjail add owner/repo¶
cmd_add.cmd_add, top to bottom. Every network access and download comes after every check that can be made without
it.
install.new_app_name: the name, which must be free.permargs.permissions_from_args: every permission option is validated now (paths exist and are allowed, D-Bus names and portals are permitted), before any network access.permargs.require_network_isolationrefusesnetworkwithoutpastaandnetwork_hostwithout Landlock.github.Client.repo_info, thencmd_add.show_review: the repository and its owner.cmd_add.choose_release_asset(github.appimage_candidates,install.choose_asset) andcmd_add.show_asset_evidence: the release, the file, its digest, whether an attestation exists.permargs.show_permissions: what the app will be granted, with warnings.install.ask_extractor: ifunsquashfsis missing, whether to accept the AppImage's own extractor.console.confirm: nothing has been downloaded yet.- Inside
with store.staging() as stage:install.download_verified→Services.download(jail.download):fetcher.pyin the download jail, then the size, the SHA-256 against the digest and the AppImage type, on appjail's side;install.gather_evidence→Services.verify(attest.verify): tier 1 if an attestation verifies;- a
manifest.Manifestis built and checked (install.check_record); install.swap_in→Services.extract(appimage.extract): extraction in its jail, then the tree, the markers (root.sha256,root.tree.sha256) and the manifest, last. On any failure everything placed is removed again.
install.finish_add: the report, the history commit (history.record) and the optional menu entry.
add --file and add --url (cmd_addimage.py) follow the same steps with imagesrc.py instead of the resolver.
Walk-through: appjail run NAME¶
runner.launch is the heart of the app jail. Its order matters: everything that can refuse comes before anything is
started, and everything started is registered on one contextlib.ExitStack, so it is stopped in reverse order when the
app ends or a step fails.
runner.prepare: the manifest; the AppImage must hash to the pin (otherwiseRunError); the tree is reused only if its markers match, else extracted again.history.modified: a warning if the manifest was changed outside appjail.runner._open_grants→runtimejail.Grants: each granted folder and file opened once and checked through its descriptor (grants.sensitive_reason,grants.directory_reason).network_hostwithout Landlock is refused (landlock.available).- Display:
wayland.RestrictedSocket(a restricted socket when the compositor offers one), thenrunner._check_wayland_socket(wayland.privileged): the registry rule, before anything else is started. runner._private_audio(audiomode.resolve,audiojail.PrivateAudio): the private audio graph, or the fallback.runner._seccomp_filter(seccomp.program): the filter, in memory or, behindpasta, in a private file; and the lifeline (jail.Lifeline).runner._portal_and_bus(portal.Instance,dbusproxy.check_owners,dbusproxy.Proxy): the portal identity and the filtered bus.runtimejail.runtime_args: the completebwrapargument list. This is the one place to read to know what an app sees.- Wrapping: behind
pasta,jail.net_wrapwithreopen.wrap(the grants opened again in the namespace); otherwiselandlock.wrap. runner._run_appandrunner._wait_group: start, publish the jail's pid to the portal, forward signals to the process group, and kill what is left when the app exits.
Walk-through: appjail update¶
cmd_update.cmd_update loops over the apps (updater.installed) and calls cmd_update.update_app for each, which
dispatches by source kind:
github:→cmd_update.update_github:repo_infoand the latest release,cmd_update.pick_asset,cmd_update.is_current; thenupdater.blocks(owner change, lower tier: stop) andupdater.cautions;updater.note_seenandupdater.quarantine_ends(the quarantine;cmd_update.hash_candidatefor tier 4);install.show_change, the confirmation, andcmd_update.install_release, which reusesinstall.download_verified,install.gather_evidenceandinstall.swap_in; finallyinstall.finish_update.url:→cmd_updateimage.update_url(jail.check_url,fetcher.check: the conditional and ranged requests).file:→cmd_updateimage.update_file.
An update never changes permissions: the new Manifest is built from the old one with the new pins.
Helper jails¶
All of them are built in jail.py from jail.common_args (namespaces, no nested user namespaces, no capabilities, a
cleared environment, the read-only system) plus the few binds of the step, for example jail.download_args,
jail.extract_args, jail.local_git_args. jail.run adds the seccomp filter and, for jails with a network,
jail.net_wrap (pasta); jail.supervise runs the command with a timeout, a watch on disk use and capped output. A
new helper jail follows the same pattern: a *_args builder that binds only what the step needs, run through
jail.run.
Where state lives¶
Path (store.py) |
Holds |
|---|---|
config_dir()/apps/NAME.toml (~/.config/appjail) |
the manifests; the directory is the manifest-history git repository |
app_dir(NAME) (~/.local/share/appjail/apps/NAME) |
app.AppImage, the extracted root/, the markers root.sha256 and root.tree.sha256, the private home/, the generated etc/, pending.json during a quarantine |
tools_dir() |
the pinned helper tools |
staging_root() |
temporary directories of store.staging, removed when the block ends |
data_dir()/index/ |
the catalogs (indexstore.py) |
APPJAIL_DATA_DIR and APPJAIL_CONFIG_DIR override the two roots; the tests use them for a private tree per test.
Testing a change¶
- The
Servicesseam.services.Servicesgathers whataddandupdatedo to the outside world (download, extraction, attestation check). Tests pass their own, so a wholeaddorupdateruns without a network or a jail. The GitHub client is passed in the same way (helpers.FakeClient). - Fixtures (
tests/conftest.py):handsandboxgive a private data and config tree;runtimea fake runtime directory with real sockets;no_historyandno_portal_store(automatic) keep tests away from git and the real portal store. Shared helpers are intests/helpers.py: scripted consoles (Script,Answers), fake releases and assets, a local URL server, fake compositor registries. - Integration tests start real jails and skip unless
APPJAIL_INTEGRATION=1, whichtests/vm/test.shsets in the guest. Opt-in suites that download needAPPJAIL_E2E=1orAPPJAIL_LIVE=1. - Checks that guard the documentation and the style:
test_docs.py(every command and option documented),test_lint.py(ruff),test_reuse.py(licence headers),test_assessment.py(the compositor tables).
Recipes¶
A new command. A cmd_<name>.py module with a cmd_<name>(args, console) function; its parser entry in
cli.build_parser and its handler in cli.COMMANDS; a menu or form in the window only if it is useful there
(gui/argv.py builds its command line); the command and its options in the guide and the reference.
A new permission. Follow an existing one such as linger through the code:
manifest.py: the field inPermissions(default denied), its name inBOOL_PERMSfor a switch (the--allowand--denychoices are taken from it) or inPERM_KEYSfor another kind of value, the writer (dumps), and any consistency rule (_network_consistencyis an example);permargs.py:permissions_from_args, the summary lines, and a warning inRISKY_GRANTSif it widens the jail;- its effect, almost always in
runtimejail.runtime_argsorrunner.launch; - the window's argument builder (
gui/argv.py); - tests in
test_manifest.py,test_cli_add.py,test_runner.pyandtest_gui_argv.py; - the permission reference, the guide, the concept page it belongs to, and a decision page if a choice was made.
A refused path or D-Bus name. Add it to the lists in grants.py (SECRET_HOME_DIRS, PERSIST_HOME_DIRS,
DBUS_BLOCKED, ...) with a comment saying why, a test in test_manifest.py or test_dbus.py, and the reason in the
matching decision page.
A new helper program. If it runs on untrusted input, it runs in a helper jail (above). If it is not packaged by the
supported distributions, it is pinned in tools.py by URL, size and hash, installed only on request.