Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/uncompiled-workflows
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Workflows in .github/workflows/ that aren't compiled from workflows/,
# checked by `wfc check`. Each runs with none of the compiler's
# guarantees, so each needs its reason here.
ci.yml # this repository's own checks; runs no untrusted code with credentials (contents: read, no secrets), and calls the compiled workflows
5 changes: 5 additions & 0 deletions .github/workflows/actions-test.lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ jobs:
const runner = process.env.SUDO_USER;
if (!runner || runner === "root") throw new Error("must be started with sudo by the runner user");
const user = CONFIG.user;
if (user === runner || user === "root") throw new Error(`the sandbox user can't be ${user}: it must be another user than the runner and root`);

// A runner that ran a job before would hand this one the previous job's
// sandbox and workspace: refuse it. (The user itself may come with the
Expand All @@ -1103,6 +1104,10 @@ jobs:
}
const sandbox = passwd(user);
const runnerHome = passwd(runner).home;
// Also by uid: another name for root or the runner is still them.
if (sandbox.uid === 0 || sandbox.uid === passwd(runner).uid) {
throw new Error(`the sandbox user ${user} has uid ${sandbox.uid}, root's or the runner's`);
}

fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o755 });
// The sandbox works on its own copy of what the job hands it (the files git
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/agent-review.lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,7 @@ jobs:
const runner = process.env.SUDO_USER;
if (!runner || runner === "root") throw new Error("must be started with sudo by the runner user");
const user = CONFIG.user;
if (user === runner || user === "root") throw new Error(`the sandbox user can't be ${user}: it must be another user than the runner and root`);

// A runner that ran a job before would hand this one the previous job's
// sandbox and workspace: refuse it. (The user itself may come with the
Expand All @@ -1754,6 +1755,10 @@ jobs:
}
const sandbox = passwd(user);
const runnerHome = passwd(runner).home;
// Also by uid: another name for root or the runner is still them.
if (sandbox.uid === 0 || sandbox.uid === passwd(runner).uid) {
throw new Error(`the sandbox user ${user} has uid ${sandbox.uid}, root's or the runner's`);
}

fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o755 });
// The sandbox works on its own copy of what the job hands it (the files git
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/sandbox-test.lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ jobs:
const runner = process.env.SUDO_USER;
if (!runner || runner === "root") throw new Error("must be started with sudo by the runner user");
const user = CONFIG.user;
if (user === runner || user === "root") throw new Error(`the sandbox user can't be ${user}: it must be another user than the runner and root`);

// A runner that ran a job before would hand this one the previous job's
// sandbox and workspace: refuse it. (The user itself may come with the
Expand All @@ -1494,6 +1495,10 @@ jobs:
}
const sandbox = passwd(user);
const runnerHome = passwd(runner).home;
// Also by uid: another name for root or the runner is still them.
if (sandbox.uid === 0 || sandbox.uid === passwd(runner).uid) {
throw new Error(`the sandbox user ${user} has uid ${sandbox.uid}, root's or the runner's`);
}

fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o755 });
// The sandbox works on its own copy of what the job hands it (the files git
Expand Down
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ sha2 = "0.11"
# Fetches the action.yml of actions to lock; `check` never uses it.
ureq = "3"

[dev-dependencies]
tempfile = "3"

# nickel's evaluator is slow unoptimized, and `cargo test` and
# `cargo run -- compile` evaluate every source.
[profile.dev.package."*"]
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,17 @@ The compiler is `wfc`, a Rust program that embeds nickel

```sh
cargo run -- compile # write .github/workflows/*.lock.yml
cargo run -- check # fail on a stale lock file or an accepted reject test
cargo run -- check # fail on a stale lock file, an accepted reject test,
# or a workflow that isn't compiled (see below)
cargo test # wfc's own tests, and `check` of this repository
```

`check` also checks every workflow in `.github/workflows/`: a lock file
must have a source and the compiled shape (`src/shape.rs`: the generated
enter and seal steps, byte for byte, and nothing between them that
leaves the wrapper), and anything else must be listed, with its reason,
in `.github/uncompiled-workflows`.

`compile` also fetches new actions' metadata from GitHub; `check` never
uses the network.

Expand Down Expand Up @@ -261,7 +268,6 @@ the task compiler that will produce this compiler's input):
- [#36](https://github.com/cgwalters-forge/workflow-compiler/issues/36) `agent_run` with real inference through praxis run tokens (P1)
- [#4](https://github.com/cgwalters-forge/workflow-compiler/issues/4) agent-run as a compiler macro or a composite action (P1)
- [#5](https://github.com/cgwalters-forge/workflow-compiler/issues/5) use `cgwalters-forge/actions/secure-host-setup` (P1)
- [#13](https://github.com/cgwalters-forge/workflow-compiler/issues/13) the stale-lock check doesn't prove every workflow is compiled (P1)
- [#8](https://github.com/cgwalters-forge/workflow-compiler/issues/8) using the compiler from other repositories (P1)
- [#19](https://github.com/cgwalters-forge/workflow-compiler/issues/19) `runner_steps` that execute a pull request's checkout (P1)
- [#20](https://github.com/cgwalters-forge/workflow-compiler/issues/20) caches saved from sandbox output and restored in `runner_steps` (P1)
Expand Down
47 changes: 39 additions & 8 deletions docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,47 @@ a step's `shell:` or `working-directory`, a job's `container:`, `defaults`
or `services`, workflow- or job-level `env:`, and a `uses:` in `steps` that
the shim can't run.

**Status:** holds, with the gap below.
**Status:** holds. Besides matching each lock file to its source, `wfc
check` checks the output itself (`src/shape.rs`), so a
source that builds its own record instead of going through `gha.compile`
fails too. It requires exactly what the compiler emits rather than
refusing what it knows to be dangerous: every job has exactly one enter
step, byte for byte what `runtime/` makes it, with the configuration the
compiler writes (a sandbox user that is neither the runner nor root, the
workspace, a valid hand-off, the sudo lock whenever steps follow the
seal), after the generated secure-host step; every step up to the one
generated seal step runs through the step wrapper, has only the keys such
a step has and references no secret or token; every step after the seal
requires it to have succeeded and is held to what gha.ncl's `PublishStep`
refuses (unknown keys, unpinned actions, sandboxed steps' outputs in
`run`, `working-directory`, `with` or `env`, untrusted text in scripts);
and jobs and workflows have only the keys gha.ncl's contracts let through,
so a key GitHub adds later is refused until the compiler knows it. Any
other file in `.github/workflows/` must be listed, with its reason, in
`.github/uncompiled-workflows` (here, `ci.yml`), and every lock file needs
a source. This proves the repository's own tree; that the tree `ci` runs
the check from is trustworthy is
[#26](https://github.com/cgwalters-forge/workflow-compiler/issues/26).

**Proof:** the reject tests in `tests/reject/` (`shell-override`,
`working-directory`, `job-container`, `job-defaults`, and the others), which
`wfc check` requires to fail with their expected message,
`job-services`, `job-env` and `workflow-env` among them. Gap:
`wfc check` proves each lock file matches its source, not that every
workflow in `.github/workflows/` is a lock file or that a source went
through `gha.compile` unmodified
([#13](https://github.com/cgwalters-forge/workflow-compiler/issues/13)).
`working-directory`, `job-container`, `job-defaults`, and the others),
which `wfc check` requires to fail with their expected
message, `job-services`, `job-env` and `workflow-env` among them; and
`tests/shape.rs`, where a hand-built workflow of the
compiled shape passes and each way out is found: an action, a plain shell,
`working-directory` or an unknown key mid-sandbox, a `--stage` that runs
more or carries `env`, `NODE_OPTIONS` for the wrapper, a second seal id,
publish steps that skip or escape the seal condition, a job or workflow
`env`, job `container`, `services`, `defaults`, `uses` or an unknown key,
a secret or the token in a sandboxed step, a publish step that is
unpinned, has an unknown key, uses sandboxed steps' outputs or expands
event text, a changed, skippable or missing enter, secure-host or seal
step, a seal before entering, and a CONFIG with the runner or root as
sandbox user, another workspace, a hand-off out of it, no sudo lock before
publish steps or an extra key; and, for the tree, an unlisted or `.YML`
workflow, an orphan lock file, and a listed workflow without its reason or
that doesn't exist. `tests/reject/sandbox-user-*.ncl` refuse the runner
and root as `sandbox.user`.

### R2. A sandboxed step runs as another user, in a session of its own

Expand Down
8 changes: 6 additions & 2 deletions lib/gha.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ in
# as runner under node before anything is validated, so names that change
# how a program starts or where it finds others are refused here, along
# with the ones runner-sandbox-run refuses anyway.
let DENIED_ENV_RE = "^(ACTIONS_|RUNNER_|GITHUB_(TOKEN|ENV|PATH|OUTPUT|STATE|STEP_SUMMARY)$|LD_|NODE_|(BASH_ENV|ENV|PATH|HOME|USER|LOGNAME|SHELL)$)" in
let DENIED_ENV_RE = "^(ACTIONS_|RUNNER_|GITHUB_(TOKEN|ENV|PATH|OUTPUT|STATE|STEP_SUMMARY)$|LD_|NODE_|OPENSSL_(CONF|MODULES)$|(BASH_ENV|ENV|PATH|HOME|USER|LOGNAME|SHELL)$)" in
let SafeEnvNames = validator
(fun env => std.array.all (fun n => !(std.string.is_match DENIED_ENV_RE n)) (std.record.fields env))
(fun env =>
Expand Down Expand Up @@ -305,7 +305,11 @@ let PublishStep = fun publish_ids => std.contract.Sequence [
] in

let Sandbox = {
user | String | validator (fun s => std.string.is_match USER_RE s) (fun s => "invalid user name `%{s}`") | default = "runner-sandbox",
# Never the runner (whose tokens the sandbox must not reach) nor root.
user | String
| validator (fun s => std.string.is_match USER_RE s) (fun s => "invalid user name `%{s}`")
| validator (fun s => !(std.array.elem s ["root", "runner"])) (fun s => "sandbox.user can't be `%{s}`: the sandbox must be another user than the runner and root")
| default = "runner-sandbox",
# Take root away from the runner user too, once the sandbox is entered;
# its only sudo rule left is the wrapper.
lock_runner_sudo | Bool | default = true,
Expand Down
5 changes: 5 additions & 0 deletions runtime/install.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (run("sh", ["-c", "command -v run0"], { check: false }).status !== 0) {
const runner = process.env.SUDO_USER;
if (!runner || runner === "root") throw new Error("must be started with sudo by the runner user");
const user = CONFIG.user;
if (user === runner || user === "root") throw new Error(`the sandbox user can't be ${user}: it must be another user than the runner and root`);

// A runner that ran a job before would hand this one the previous job's
// sandbox and workspace: refuse it. (The user itself may come with the
Expand All @@ -58,6 +59,10 @@ if (run("getent", ["passwd", user], { check: false }).status !== 0) {
}
const sandbox = passwd(user);
const runnerHome = passwd(runner).home;
// Also by uid: another name for root or the runner is still them.
if (sandbox.uid === 0 || sandbox.uid === passwd(runner).uid) {
throw new Error(`the sandbox user ${user} has uid ${sandbox.uid}, root's or the runner's`);
}

fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o755 });
// The sandbox works on its own copy of what the job hands it (the files git
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
pub mod actions;
pub mod nickel;
pub mod repo;
pub mod shape;

use std::collections::{BTreeMap, btree_map};
use std::path::Path;
Expand All @@ -26,6 +27,8 @@ pub const REJECT_DIR: &str = "tests/reject";
/// actions from tests/actions/, for one) check its output without making
/// a workflow of it.
pub const ACCEPT_DIR: &str = "tests/accept";
/// Workflows in [`LOCKS_DIR`] that aren't compiled, each with its reason.
pub const UNCOMPILED: &str = ".github/uncompiled-workflows";
/// How to regenerate what `compile` writes, from the repository root.
pub const REGENERATE: &str = "cargo run -- compile";
/// The prefix of a reject test's expected message.
Expand Down Expand Up @@ -149,6 +152,7 @@ fn check(root: &Repo) -> Result<Vec<String>> {
}
}
}
problems.extend(shape::check_tree(root)?);
let rejects = root.list(REJECT_DIR, ".ncl")?;
for source in &rejects {
let text = root.read(source)?.unwrap_or_default();
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ enum Command {
/// in actions.lock.json.
Compile(Repo),
/// Fail if a lock file or expected output is stale or hand-edited, if
/// actions.lock.json doesn't match the action.yml files it holds, or
/// if a source in tests/reject/ compiles.
/// actions.lock.json doesn't match the action.yml files it holds, if
/// a source in tests/reject/ compiles, or if a workflow in
/// .github/workflows/ is neither a lock file with the compiled shape
/// nor listed in .github/uncompiled-workflows.
Check(Repo),
}

Expand Down
10 changes: 10 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ impl Repo {
}
}

/// Whether `rel` is a regular file, not following symlinks; false if
/// it doesn't exist.
pub fn is_file(&self, rel: &str) -> Result<bool> {
match std::fs::symlink_metadata(self.path(rel)) {
Ok(meta) => Ok(meta.is_file()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e).with_context(|| format!("reading {rel}")),
}
}

/// The names in the directory `rel` that end in `suffix`, as paths
/// relative to the root, sorted; none if it doesn't exist.
pub fn list(&self, rel: &str, suffix: &str) -> Result<Vec<String>> {
Expand Down
Loading
Loading