Skip to content

fix(gateway): A grant worker survives the files the C library's malloc reads - #63

Merged
sodre merged 4 commits into
mainfrom
fix/62-malloc-reads
Nov 25, 2026
Merged

sodre merged 4 commits into
mainfrom
fix/62-malloc-reads

Conversation

@sodre

@sodre sodre commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Closes #62

Summary

A grant worker no longer dies, and its grant is no longer fenced, when a connection frees a large response or when nine or more connections run at once. The C library's malloc used to open a file at those moments, and the worker sandbox killed the grant worker for it.

glibc's malloc opens a file the first time it needs one of two values. openat is outside the allow-list, and ADR 0001's decision 5 makes every such call a kill:

When File glibc opens
A thread's heap shrinks for the first time: a connection thread freed more than the trim threshold, 128 KiB /proc/sys/vm/overcommit_memory
A ninth thread needs a malloc arena: nine connections at once /sys/devices/system/cpu/online

confine() now settles both before any layer goes on, so decision 5 stands with no new exception:

 confine()
+  settle_malloc()                 # while files can still be read
+    mallopt(M_ARENA_MAX, 8 × CPUs in the affinity mask)   # glibc's own limit: it never counts CPUs
+    mallopt(M_TRIM_THRESHOLD, 128 KiB)                     # glibc's default, held fixed
+    a warm-up thread frees 1 MiB  # one heap shrinks, and glibc caches the overcommit policy
   non-dumpable, no_new_privs, Landlock, seccomp allow-list

The warm-up thread ends before the first layer, since each layer binds only the calling thread. docs/develop.md's section "A grant worker died of SIGSYS" now names this symptom.

Extras for the owner

  • The trim threshold is held at glibc's default. A grant worker is killed when the C library's malloc opens a file #62's decided fix sets M_ARENA_MAX and frees a block on a warm-up thread; it does not set M_TRIM_THRESHOLD. Review round 1 found that a block of 1 MiB or more freed before confinement raises glibc's moving trim threshold above the warm-up, so the warm-up then caches nothing. It was measured: 2 MiB freed before confine(), then 8 MiB freed after it, killed the process (exit 159). Holding the threshold at 128 KiB closes that, and also stops glibc's moving mmap threshold for the grant worker, so a block of 128 KiB or more is always mapped on its own. Today a grant worker calls confine() before it allocates anything large (main.rs), so the gap needs future code to open. No source settles it.

Evidence

Criterion Ticked Shown by
A confined stand-in survives a thread that allocates and frees more than the trim threshold, and goes on serving yes a_thread_that_frees_a_large_heap_leaves_the_grant_worker_serving
A confined stand-in survives sixteen threads allocating at once, and goes on serving yes sixteen_threads_at_once_leave_the_grant_worker_serving
After a block of 2 MiB is freed before confinement, a confined stand-in still survives a thread that frees more than the trim threshold: the threshold is held at glibc's default yes a_large_block_freed_before_confinement_leaves_the_warm_up_working
Both checks are in the containment test, so they run on x86-64 and arm64 and under the filter sandbox's container yes CI on 20ba1c4: workspace (ubuntu-latest) and gateway-filter-sandbox (ubuntu-24.04) on x86-64, 28 passed, 0 failed; gateway-filter-sandbox (ubuntu-24.04-arm), 26 passed, 0 failed; each printing both tests ... ok
The runtime test passes under both worker sandboxes yes runtime.py with --worker-sandbox namespaces and --worker-sandbox filter
Test Red Green
a_thread_that_frees_a_large_heap_leaves_the_grant_worker_serving Against main (c612768), and again at 4ab3b5a with settle_malloc stubbed out: stand-in 0, `free-heap`: expected a reply, got Killed(31) ok
sixteen_threads_at_once_leave_the_grant_worker_serving The same two ways: stand-in 0, `threads 16`: expected a reply, got Killed(31) ok
The trim threshold held (review) A throwaway program, with 2 MiB freed before confine() and 8 MiB freed on a thread after it: exit 159 (SIGSYS) at 24ea0ea exit 0
a_large_block_freed_before_confinement_leaves_the_warm_up_working (widening) With M_TRIM_THRESHOLD left unset on purpose: stand-in 0, `free-heap`: expected a reply, got Killed(31), 26 passed, 1 failed ok

On f4b53ed, the widening's test added, Linux arm64: cargo test --locked -p countersign-gateway, every suite ok, the containment test 27 passed, 0 failed; scripts/filter-sandbox-container.sh, exit 0 in 75 s, its containment test 27 passed, 0 failed; cargo fmt --check and clippy clean.

Runs on 20ba1c4, Linux arm64, glibc 2.39, kernel 6.17:

  • cargo test --locked -p countersign-gateway: every suite ok. The containment test: 26 passed, 0 failed.
  • cargo fmt --all -- --check and cargo clippy --locked -p countersign-gateway --all-targets -- -D warnings: clean.
  • python3 crates/countersign-gateway/tests/runtime.py target/debug/countersign-gateway --worker-sandbox namespaces, and the same with filter: exit 0.
  • scripts/filter-sandbox-container.sh: exit 0 in 71 s.
  • A throwaway program that confines itself with confine(), as the diagnosis did, then exits 0 in each of these cases. Before the fix, 9 threads, 16 × 16 KiB freed and the 2 MiB pre-free case each died with exit 159.
    • 9, 16 and 32 threads allocating at once;
    • one thread freeing 64 × 16 KiB;
    • 32 threads with one CPU in the affinity mask (taskset -c 0);
    • the 2 MiB pre-free case.
  • The diagnosis: strace of the same cases unconfined names the two files. The kernel's audit line from the failed CI run, run 45536441028, names syscall=257 (openat, x86-64) for comm="worker".

CI on 20ba1c4: all 28 checks passed, among them the end-to-end curl job on x86-64 whose failure on #61 was this bug, and the Documentation workflow's mkdocs build --strict.

CI on f4b53ed: all 28 checks passed. The new test printed ... ok in workspace (ubuntu-latest) and gateway-filter-sandbox (ubuntu-24.04) on x86-64, 29 passed, 0 failed, and in gateway-filter-sandbox (ubuntu-24.04-arm), 27 passed, 0 failed.

Not run: nothing.

Merge Danger

Door: two-way. A revert brings the kill back, nothing stored changes.

Blast Radius: every grant worker's memory allocator.

Nothing deploys on the merge: the countersign-gateway crate and the ghcr.io/zeroae/countersign-gateway image ship at the next version tag. No flag. After the merge, #61 is rebased onto it and its CI runs again, since the x86-64 failure there was this bug.

Reviews

Correctness, Standards, Spec and Security, three rounds: 15 findings in round 1, 2 in round 2, none in round 3; the widening's commit changes only a test, so no round ran on it

Correctness, round 1 (at medium, on 24ea0ea against origin/main c612768)

  1. The warm-up caches the overcommit policy only if its free shrinks the heap, and a raised trim threshold stops that without a word. Fixed: the trim threshold is held at glibc's default before the warm-up (measured; an extra for the owner, under Summary).
  2. A settle_malloc failure is reported as the seccomp allow-list layer. Pushed back: the warm-up exists for the allow-list, and its errors carry their own words ("malloc refused option …", "the malloc warm-up thread panicked"), as no_new_privs' error does under the same layer.
  3. std::thread::spawn panics when the kernel refuses a thread. Fixed: std::thread::Builder::spawn, whose error confine() returns.
  4. The arena limit was not glibc's own on every glibc version. Fixed: in round 2, the CPUs in the affinity mask, which glibc 2.36 in the Gateway's image counts.
  5. Answer openat with an error, as clone3 is answered, rather than settle each read. Pushed back: the owner chose the warm-up on A grant worker is killed when the C library's malloc opens a file #62, which keeps ADR 0001's decision 5 with no new exception.
  6. On glibc 2.35 to 2.38, the sixteen-thread test passes without the arena limit. Pushed back: CI and this host run glibc 2.39, where the test is red without the fix.
  7. The regression test copied the warm-up's shape. Fixed: it frees 8 MiB in 4 KiB blocks.
  8. confine() did not say why the warm-up thread must end before the first layer. Fixed: its comment says each layer binds only the calling thread.

Standards, round 1

  1. Possible Mysterious Name: the warm-up's errors under the allow-list layer. Pushed back, as finding 2.
  2. Possible Duplicated Code: the test and the warm-up. Fixed, as finding 7.
  3. Possible Primitive Obsession: the warm-up's numbers. Fixed: TRIM_THRESHOLD and WARM_UP_BLOCK.
  4. A SAFETY comment also held what mallopt returns. Fixed: that note has its own line.
  5. The 16 in the sixteen-thread test. Pushed back: the test's comment ties it to the ninth and later arenas.
  6. The guide's new rule might belong in an ADR. Fixed: the paragraph names the symptom and cites ADR 0001's decision 5, with no rule of its own.

Spec, round 1

  1. The paragraph in docs/develop.md was not asked for. Pushed back: the guide's section on a grant worker killed by SIGSYS is where the next person looks for this symptom, and the paragraph now holds only that.

Security, round 1: no findings.

Spec, round 2 (fix round at low, on 4ab3b5a against 24ea0ea; Correctness, Standards and Security found nothing)

  1. Holding M_TRIM_THRESHOLD is not in A grant worker is killed when the C library's malloc opens a file #62's decided fix, and it stops glibc's moving thresholds. Fixed: the owner widened A grant worker is killed when the C library's malloc opens a file #62 to include it (Extras for the owner), and a containment test now holds it.
  2. available_parallelism applies the cgroup quota, so the limit was not glibc's own. Fixed: the CPUs in the affinity mask, then the online CPUs.

Round 3 (fix round at low, on 20ba1c4 against 4ab3b5a): Correctness, Standards, Spec and Security found nothing.

Tracking

Epic: none
Spec: none
Ticket: #62 A grant worker is killed when the C library's malloc opens a file
Flag: none

🤖 Generated with Claude Code

sodre and others added 3 commits November 24, 2026 22:23
…c reads

glibc's malloc opens a file the first time it needs one of two values,
and under the worker sandbox's allow-list that openat killed the grant
worker and fenced its grant (#62):

- /proc/sys/vm/overcommit_memory, when a thread's heap first shrinks,
  after a connection thread freed more than the trim threshold;
- /sys/devices/system/cpu/online, when a ninth thread needs an arena.

confine() now settles both before Landlock and the allow-list: it sets
M_ARENA_MAX to glibc's own limit, eight arenas per CPU, and shrinks one
heap on a warm-up thread, so glibc caches the overcommit policy. ADR
0001's decision 5 stands: every call outside the allow-list still kills.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 1 of #62's fix:

- the trim threshold is set by hand to glibc's default, so a large
  block freed before confinement cannot raise it above the warm-up;
- the arena limit counts the CPUs the process may use;
- the warm-up thread is started with Builder::spawn, so a refused
  thread is an error and not a panic, and confine() says why it ends
  before any layer goes on;
- the regression test frees 8 MiB in 4 KiB blocks, not the warm-up's
  own shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 2 of #62's fix: glibc 2.36, in the Gateway's image,
limits arenas to eight per CPU in the process's affinity mask, and
available_parallelism also applied the cgroup quota. The limit now
counts that mask, as glibc does, falling back to the online CPUs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sed trim threshold

#62 widened by the owner: confinement holds glibc's trim threshold at
its default, so a block freed before confinement cannot raise it above
the warm-up. A stand-in now frees 2 MiB before it confines itself, and
a heap it frees after confinement must not kill it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sodre
sodre marked this pull request as ready for review November 25, 2026 04:08
@sodre
sodre merged commit e17331f into main Nov 25, 2026
38 checks passed
sodre added a commit that referenced this pull request Nov 25, 2026
…ser stories (#61)

Closes #60

## Summary

Nothing changes for anyone using the product. The Gateway's runtime test
now proves seven of #49's user stories that it left unproved, so every
story of the egress proxy spec has a test or a check before the spec
closes.

A check of #49's 53 user stories against `main` found seven where
breaking the Gateway on purpose left the runtime test green. Each now
has a case at the real Gateway binary (#49's seam 1,
`crates/countersign-gateway/tests/runtime.py`):

| Story | New case |
|---|---|
| 8, a wildcard rule through the egress proxy | A proxied rule
`*.wild.test:<port>` reaches `a.wild.test`, and the proxy records that
name |
| 11, every undecided private rule stops startup | A private rule after
a proxied rule is refused by `env` and `serve`, naming that rule |
| 17, the proxy's name resolved on every connection | The name
`proxy.test` moves to a second egress proxy on 127.0.0.2 and back; each
CONNECT lands where the name points |
| 21, a silent egress proxy | A proxy that reads the CONNECT and never
answers: 502 `proxy_unavailable` at the Gateway's 120-second deadline,
and the site sees nothing |
| 37, the rule's roots before any signature | A rule whose `roots` is
another CA: the request fails through an allowed tunnel, with no
signature asked for |
| 38, a rule at the Gateway and a grant at the Broker | A name with no
rule, and one with no grant, are refused with 403; neither the egress
proxy nor the site sees a connection |
| 42, proxy environment variables ignored | In the proxied passes the
six proxy variables name a dead address, and every request still goes
through `egress_proxy` |

The silent case waits out the fixed deadline, so it runs behind
`--silent-egress-proxy`, in a step of its own in the `gateway-arm64`
job:

```diff
       - name: Exercise shared and sidecar workers and lease cutoff
         run: timeout 120 python crates/countersign-gateway/tests/runtime.py target/debug/countersign-gateway
+      - name: Fail a CONNECT through a silent egress proxy at the Gateway's deadline
+        run: |   # under both worker sandboxes at once: about two minutes
```

The Gateway does not change. The table of all 53 stories, with each
one's verdict, goes into the Evidence of the pull request that closes
#49.

### Extras for the owner

- **A race in the runtime test's `alive()`, on `main` since f13433d.**
The push-event `curl` job on f1c80b1 failed after the filter-killed
grant worker was fenced as it should be: `/proc/<pid>/stat` was opened
while a worker existed and read after it was collected, which raises
`ProcessLookupError` (ESRCH), not the `FileNotFoundError` the helper
caught. The pull-request run of the same commit passed. The helper now
reads both as ended.
- [x] Fix here (recommended: one line in the file this pull request
changes, and the release tag runs the same job)
  - [ ] A new ticket
  - [ ] Record only
- Applied: dbdb8c9, which changes only the test, so no review round ran
on it

## Evidence

| Criterion | Ticked | Shown by |
|---|---|---|
| A wildcard rule with no `private_networks` reaches a host under it
through the egress proxy, and the proxy records that host's name (story
8) | yes | Story 8: a wildcard rule through the egress proxy |
| With `egress_proxy` set, a private rule with no route after a proxied
rule stops startup, and the error names that rule (story 11) | yes |
Story 11: a second private rule stops startup |
| When the egress proxy's name moves between two connections, the next
`CONNECT` goes to the new address, and moved back, the one after
succeeds (story 17) | yes | Story 17: the egress proxy's name moves and
back |
| An egress proxy that reads the `CONNECT` and never answers gives 502
`proxy_unavailable` and the request ID, and the site sees no connection
(story 21) | yes | Story 21: a silent egress proxy |
| That case runs in a CI step of its own on every pull request (story
21) | yes | CI on f1c80b1: `gateway-arm64`'s step "Fail a CONNECT
through a silent egress proxy at the Gateway's deadline" passed in 121
s, under both worker sandboxes |
| Through an allowed tunnel to a site outside the rule's `roots`, the
request fails and no signature is asked for (story 37) | yes | Story 37:
a site outside the rule's roots |
| A `CONNECT` to a name with no Gateway rule, and to one with no grant,
are each refused with 403, and neither the egress proxy nor the site
sees a connection (story 38) | yes | Story 38: no rule at the Gateway;
Story 38: no grant at the Broker |
| With `egress_proxy` set and the proxy variables naming another
address, every proxied request still goes through `egress_proxy` (story
42) | yes | Story 42: proxy variables beside `egress_proxy` |
| Each new case was red once against a Gateway broken on purpose, and
the runtime test runs all of them wherever it runs today, under both
worker sandboxes | yes | The red runs below; CI on f1c80b1: the runtime
test passed in `curl` (x86-64), `gateway-arm64` and
`gateway-filter-sandbox` on both architectures |

Each break alone, the Gateway rebuilt from it, against the test at
39c0d0b (and 17 and 11 again at b995bc4):

| Test | Red | Green |
|---|---|---|
| Story 8: a wildcard rule through the egress proxy | A proxied wildcard
rule refused at request time: `AssertionError: ('alice', b'curl: (22)
The requested URL returned error: 403\n')`; refused at load:
`RuntimeError: Gateway exited 1` | ok |
| Story 11: a second private rule stops startup | The check limited to
the first rule (`.iter().take(1)`): `subprocess.TimeoutExpired: Command
'[… 'serve', '…/egress.json']' timed out after 10 seconds`, at the new
case | ok |
| Story 17: the egress proxy's name moves and back | The proxy's address
resolved once and cached: `AssertionError: The Gateway did not follow
the egress proxy` | ok |
| Story 21: a silent egress proxy | The deadline mapped to
`upstream_failure`: `AssertionError: ('silent.test:33833', b'HTTP/1.1
502 Bad Gateway\r\ncountersign-error: upstream_failure\r\n…')` | ok, in
121 s |
| Story 37: a site outside the rule's roots | Trust loaded from the
first rule's roots: `AssertionError: ('untrusted', 0,
b'cfafcd6910720f1b…')`, a successful read | ok |
| Story 38: no rule at the Gateway | An unknown name falls back to the
first rule, scope check skipped: `AssertionError:
('unlisted.test:41337', b'HTTP/1.1 200 OK\r\n…')` | ok |
| Story 38: no grant at the Broker | A tunnel opened when admission is
refused: `AssertionError: ('ungranted.test:41185', 'The egress proxy saw
a connection', b'HTTP/1.1 403 Forbidden\r\ncountersign-error:
denied\r\n…')` | ok |
| Story 42: proxy variables beside `egress_proxy` | `egress::tunnel`
prefers `HTTPS_PROXY`: `AssertionError: ('alice', b'curl: (22) The
requested URL returned error: 502\n')` | ok |
| `--landlock` reaches the silent case (review) | Before the fix the
option was dropped. After it, `--landlock unavailable
--silent-egress-proxy` on this host, which has Landlock, fails its
startup check in 5 s, as it should | ok |

Runs on b995bc4 (`origin/main` c612768 and this branch), Linux arm64:

- `python3 crates/countersign-gateway/tests/runtime.py
target/debug/countersign-gateway`: exit 0 in 27 s, printing sidecar and
shared `Gateway mTLS passed`, `Gateway mTLS through the egress proxy
passed`, `Gateway without a usable egress proxy failed closed`, and
`filter-killed grant worker fenced`.
- The new CI step's script, taken from `e2e.yml` and run under `bash -e`
as Actions runs it: both runs printed `sidecar Gateway with a silent
egress proxy failed closed`, one under each worker sandbox, and the step
exited 0 in 121 s. It exits 1 when either run fails.
- `scripts/filter-sandbox-container.sh`: exit 0 in 131 s, every step:
the containment test (24 passed, 0 failed), the runtime test under the
filter sandbox, both named startup failures, the filter sandbox without
Landlock, and the `close_range` unit (1 passed).

Rebased onto #63 (e17331f), whose fix the x86-64 `curl` job on b995bc4
was waiting for. CI on f1c80b1: all 26 pull-request checks passed, among
them `curl` on x86-64 and the silent step in `gateway-arm64`. On
dbdb8c9, the race fix: `runtime.py` exit 0 on Linux arm64, printing
`filter-killed grant worker fenced`. CI on dbdb8c9: all 26 pull-request
checks passed, and the push-event runs, `curl` among them, passed too.

Not run: `cargo test` and the Compose demo, since no Rust or demo file
changed.

## Merge Danger

**Door:** two-way. A revert removes the new cases and the CI step;
nothing ships.

**Blast Radius:** CI only.

The merge ships no crate, image or deploy. The `gateway-arm64` job of
the end-to-end workflow takes about two minutes longer.

## Reviews

<details>
<summary>Correctness, Standards, Spec and Security, two rounds: 15
findings in round 1, each with its outcome, and none in round
2</summary>

**Correctness, round 1** (at `medium`, on 39c0d0b against `origin/main`
c612768)

1. The silent case never times the failure, so it does not show the
failure comes at the deadline. Pushed back: story 21 and #60's criterion
ask for `proxy_unavailable`, the request ID and no connection at the
site, not when; a Gateway that answered sooner would meet the story.
2. `--silent-egress-proxy` drops `--landlock`. Fixed: the silent pass
takes `landlock` as the other passes do.
3. The `untrusted` case cannot tell a roots mismatch from another
failure. Pushed back: the case asserts that the egress proxy recorded
the tunnel, and the rule's trust loads before the route is chosen
(`connect_request`), so the request reached the site's certificate; with
another rule's roots it succeeded (the red row).
4. The dead proxy port, found by binding and closing a socket, can race
another listener. Pushed back: the file finds its listen port and
`nothing` the same way, and a false pass would need another process on
that port acting as a working CONNECT proxy to the site.
5. The step that moved the proxy's name to 127.0.0.2 accepted any
`proxy_unavailable`. Fixed: the name moves to a second, working egress
proxy, which must record the CONNECT while the first records none.
6. `\bsecond\b` also matches the rule's pattern `second.site.test`.
Fixed: the pattern is `extra.site.test:<port>`, so only the rule's id
matches.
7. The request-and-fingerprint check is repeated. Fixed: `reads_site`
serves the direct and wildcard routes.
8. `timeout 200` leaves little room around the 120-second wait. Pushed
back: the run takes 121 s, of which setup is about 1 s, and the
130-second socket timeout is per read, not added to the deadline.
9. The silent case runs only under the default worker sandbox. Fixed, as
finding 15.

**Standards, round 1**

10. Possible Divergent Change: `silent` selects a whole scenario inside
`do_test`. Pushed back: the file selects each scenario by an argument of
`do_test` (`filter_kill`, `startup_fails`, `unusable`), and a function
of its own would repeat its setup.
11. Possible Mysterious Name: `write_hosts`' parameter `proxy` shadows
the Gateway URL `proxy`. Fixed: renamed `proxy_address`.
12. Possible Duplicated Code: the bind-to-port-0 shape. Pushed back: two
of its three uses predate this change, and a helper would change lines
outside this change's scope.
13. Possible Duplicated Code: `refused_before_egress` and
`fails_through_egress`. Pushed back: they assert different answers, a
403 against a 502 with an error value and a log line, and only the first
checks the egress proxy.
14. Possible Shotgun Surgery: a new name is listed in several places.
Pushed back: that is how the file already lists its proxy names,
certificate names and startup line.

**Spec, round 1**

15. #60: "the runtime test runs all of them wherever it runs today,
under both worker sandboxes", but the silent case ran under the default
sandbox only. Fixed: the CI step runs it under both worker sandboxes, in
parallel.

**Security, round 1** (the path list could not be read, so every changed
path was judged as a control): no findings.

**Round 2** (fix round on b995bc4 against 39c0d0b; Correctness at
`low`): Correctness, Standards, Spec and Security found nothing.

</details>

## Tracking

Epic: E2 Reach protected sites through an operator's egress proxy
Spec: #49 Reach protected sites through an operator's egress proxy
Ticket: #60 Every user story of the egress proxy spec has a test that
proves it
Flag: none

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
sodre added a commit that referenced this pull request Nov 25, 2026
Closes #64

## Summary

Countersign 0.2.2 brings the Gateway's egress proxy (#55): an operator
can send the Gateway's upstream connections through an HTTP CONNECT
proxy, rule by rule. It also fixes a grant worker killed when glibc's
malloc opened a file (#63). Neither changes an existing setup:
`egress_proxy` is opt-in, and the fix needs no setting.

Every version source moves from 0.2.1 to 0.2.2, as in #45:

```diff
 Cargo.toml                       workspace version, and the `=` pins on countersign-pki and countersign-protocol
 Cargo.lock                       the five workspace crates only
 packages/widget/package.json
 packages/companion/package.json  the version publish_desktop.py checks against the tag
 recipe/recipe.yaml
-0.2.1
+0.2.2
```

It is a patch release, as 0.2.1 was for its features: every addition is
opt-in. After the merge, the squash commit on `main` is tagged `v0.2.2`,
which starts the registry, distribution and documentation workflows. All
of v0.2.1's tag workflows passed, so no publisher needs setting up
first.

## Evidence

| Criterion | Ticked | Shown by |
|---|---|---|
| Every version source is at 0.2.2 | yes | `python3
scripts/registry_release.py check --tag v0.2.2` |
| `registry_release.py check` passes and `cargo metadata --locked`
accepts the lockfile | yes | `python3 scripts/registry_release.py check
--tag v0.2.2`; `cargo metadata --locked` |
| The squash commit on `main` is tagged `v0.2.2`, and the tag's
workflows pass | no | Not run: the tag follows the merge |

On dae7a8c: `python3 scripts/registry_release.py check --tag v0.2.2`,
exit 0; `cargo metadata --locked --format-version 1`, exit 0.
`pnpm-lock.yaml` holds no workspace versions, so it does not change;
CI's `pnpm install --frozen-lockfile` checks that.

Not run: the tag's workflows, which start when the tag is pushed after
the merge.

## Merge Danger

**Door:** two-way for the merge, which only changes version strings. The
tag after it is one-way: crates.io and npm keep 0.2.2 once uploaded.

**Blast Radius:** every published package.

The merge ships nothing. The tag publishes the five crates,
`@zeroae/countersign-widget`, the npm desktop packages, the desktop
installers, and the `countersign-gateway` and `countersign-broker`
images tagged `v0.2.2`.

## Reviews

<details>
<summary>No review ran: the change is version strings only, as in
#45</summary>

</details>

## Tracking

Epic: none
Spec: none
Ticket: #64 Countersign 0.2.2 is released
Flag: none

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
sodre added a commit that referenced this pull request Nov 25, 2026
…rator's egress proxy is in git (#66)

Closes #49

## Summary

The text of the spec "Reach protected sites through an operator's egress
proxy" is in the repository at
`docs/specs/E2-49-reach-protected-sites-through-an-operator-s-egress-proxy.md`,
as the issue held it when the spec closed. Nothing changes for anyone
using the product.

```text
docs/specs/
└── E2-49-reach-protected-sites-through-an-operator-s-egress-proxy.md   # accepted, exported 2026-11-24
```

Before this closing, each of the spec's 53 user stories was mapped to
the test, static check or live run that proves it (the table under
Evidence). On `d5b15ba`, the squash of #55, seven stories (8, 11, 17,
21, 37, 38 and 42) held but were only partly tested: a Gateway broken on
purpose still passed every test. #61 added their cases, building #60, a
sub-issue of this spec that is now done. Story 52's Fargate step stays
out of scope, as the spec's Out of Scope says.

#61's body named "the pull request that closes #49", and GitHub read
that as a closing keyword: #49 closed when #61 merged. It was reopened
so that this pull request closes it, as `docs/agents/issue-tracker.md`
("Closing a spec") asks.

The verification also found #62: on x86-64, a grant worker was killed
when glibc's malloc opened a file, which failed #61's `curl` job. #63
fixed it, and both are in Countersign 0.2.2 (`v0.2.2`). #62 has no
milestone, so it never held this spec open.

## Evidence

| Criterion | Ticked | Shown by |
|---|---|---|
| Every sub-issue of #49 is closed | yes | `export-spec.py 49` |
| No issue but the spec is open in E2 | yes | `export-spec.py 49` |
| The spec has no release flag | yes | The spec's Further Notes:
"**Flag: none.**" |
| The file's text after its header is the issue's body | yes | The
comparison below |
| Every user story is proven by a test, a static check or a live run |
yes | The user-story table below |

No test or guard is new here: the seven cases #61 added were each shown
red there against a Gateway broken on purpose, and green against the
real one.

`python3 .claude/plugins/foresight/scripts/export-spec.py 49`, run on
59f95a9 after #61 and #65 merged: `wrote … as accepted: every sub-issue
is closed and only specs are open in E2 Reach protected sites through an
operator's egress proxy, so the pull request that adds this file closes
#49`. The sub-issues are #50, #51, #52, #53 and #60, all closed. The
text it wrote after the header is the same as the committed file's; only
its export day differed, so the committed file, exported 2026-11-24,
stands. A comparison of the file's text after its header with `gh issue
view 49 --json body`, CRLF read as LF: 20104 bytes in each, the same.

Not run: nothing.

<details>
<summary>The 53 user stories of #49: 39 Test, 12 Static, 10 Live run
(eight stories have two verdicts), 0 Partial, 0 Gap</summary>

Checked on `main` at 59f95a9, the squash of #65 (Countersign 0.2.2),
which holds #55's egress proxy and #61's cases for the stories marked
(#61).

- **The runtime test** is `crates/countersign-gateway/tests/runtime.py`.
Line numbers below are at 59f95a9. On 59f95a9, CI's *Sidecar mTLS* run
[45638935969](https://github.com/zeroae/countersign/actions/runs/45638935969)
passed it in all four jobs (`gateway-arm64`, `curl`, and
`gateway-filter-sandbox` on x86-64 and arm64). Each job printed
`sidecar/shared Gateway mTLS passed`, `sidecar/shared Gateway mTLS
through the egress proxy passed` and `sidecar/shared Gateway without a
usable egress proxy failed closed`, and `gateway-arm64` ran the silent
egress proxy's step under both worker sandboxes.
- **The Compose run** is the `compose_demo.py --squid --verify` output
posted on #52, from 5d76a8b with Squid 6.14. #55's body records a pass
on d84d66d too, but that run's output was not posted.
- **Documents:** a Static row whose proof is a document was checked by
reading that document on `main`. CI's `mkdocs build --strict` checks
only its links, not its words.

| # | Story (shortened) | Verdict | What proves it | Notes |
|---|---|---|---|---|
| 1 | Set `egress_proxy` once and every site goes through it | Test |
Proxied pass. `egress.take() == through_egress` (734). Every failing
rule's CONNECT is recorded by the test proxy (760–783). | One
`egress_proxy` line; every rule but `direct` goes through it. |
| 2 | Every destination uses the egress proxy by default | Test | Same
pass. The rules `site` and `refused` … `wrong` have no `egress` field
(516) and reach the proxy (734, 760–783). | |
| 3 | `"egress": "direct"` on one rule; one Gateway serves both routes |
Test | `res = request(user, to=direct)` returns the fingerprint, then
`assert egress.take() == [], 'A direct destination went through the
egress proxy'` (735–738). | #55's red run: route choice broken to always
use the proxy → 502. |
| 4 | No egress proxy: configuration and behaviour unchanged | Test |
Direct passes `sidecar/shared Gateway mTLS passed`. Their assertions are
unchanged from before #55; the diff only refactors the grant fixture.
The shared pass adds `"egress": "direct"` (535–538). | |
| 5 | The Gateway sends the site's name to the egress proxy | Test |
`assert egress.take() == [f'CONNECT {host} HTTP/1.1'] * len(users)`
(731–734), with `host = 'site.test:<port>'`. | |
| 6 | No resolution on a proxied route, for a network with no external
DNS | Test | `assert resolved.returncode == 2, ('The Gateway could
resolve the site itself', resolved)` (571–575) runs under the Gateway's
own NSS environment, and the proxied pass then succeeds. | Proves the
Gateway cannot resolve `site.test` and works anyway. It does not prove
the Gateway never tries a lookup, which the story does not need. |
| 7 | A private site through the proxy, with a rule that has no
`private_networks` | Test; Live run | Runtime: the `site` rule loses
`private_networks` (512) and the site is at the host's interface
address. Compose: the site is at 172.31.0.2 and the rule has no
`private_networks` (#52 run, `docs/develop.md`). | Whether the runtime
test's address is private depends on the host. The Compose run's address
always is. |
| 8 | A wildcard rule works through the egress proxy | Test (#61) | A
proxied rule `*.wild.test:<port>` without `private_networks`:
`reads_site(user, wild)` for each user, then `assert egress.take() ==
[f'CONNECT {wild} HTTP/1.1'] * len(users)` (740–742). | #61's red runs:
a wildcard rule refused on a proxied route at request time (403) and at
load (the Gateway exits). |
| 9 | Startup refused when a proxied rule has `private_networks` | Test
| `egress_configuration`: `error = refused('http://proxy.test:3128',
**base)` (438). `refuses()` asserts a non-zero exit and no stdout from
both `env` and `serve` (403–412). | #55's red run: `serve` started
(`TimeoutExpired`). |
| 10 | That error names the rule and both fixes | Test | `assert
re.search(r'\bsite\b', error) and 'private_networks' in error` and
`assert 'remove' in error and '"egress": "direct"' in error` (439–440).
| |
| 11 | Adding `egress_proxy`: every undecided private rule stops startup
| Test (#61) | `egress_configuration`: `[rule, second]`, the private
rule second. `refuses()` from `env` and `serve`, then `assert
re.search(r'\bsecond\b', error) and 'private_networks' in error`
(441–446). | #61's red run: the check limited to the first rule let
`serve` start. |
| 12 | An `https` egress proxy URL refused at startup | Test | `error =
refused('https://proxy.test:3128')`, then `assert 'egress_proxy' in
error and 'https' in error` (418–419). | |
| 13 | An egress proxy URL with credentials refused at startup | Test |
`assert 'egress_proxy' in error and 'credentials' in error and 'secret'
not in error` for `user:secret@` and `user@` (420–422). | The error also
does not echo the secret. |
| 14 | An unknown `egress` value fails loading | Test | `refused(...,
egress=value)` for `'proxy', 'default', 'Direct', 'none', '', None,
True, ['direct']`, with and without an egress proxy (447–449). | |
| 15 | `env` refuses what `serve` refuses | Test | `refuses()`: `assert
results[0].stderr == results[1].stderr` across `env` and `serve` (409).
Every refusal above goes through it, and so does the
wildcard-with-`private_networks` rule (430–433). | |
| 16 | Startup logs which destinations leave address policy to the proxy
| Test | `wait_for_log(rf'Gateway egress proxy localhost:<port> carries
the address policy of destinations: ' + ', '.join(['site', *failing,
'wild', 'ungranted']))`, a full match that leaves out `direct`
(620–622). | |
| 17 | The egress proxy's name resolved on every connection | Test (#61)
| `egress_proxy` is `http://proxy.test:<port>`, resolved only through
the Gateway's hosts file. The name moves to a second egress proxy on
127.0.0.2: `assert moved.take() == [f'CONNECT {host} HTTP/1.1'], 'The
Gateway did not follow the egress proxy'`. Moved back: `'The Gateway did
not follow the egress proxy back'` (748–758). | #61's red run: resolving
once and caching fails at `The Gateway did not follow the egress proxy`.
|
| 18 | The egress proxy allowed on a private address | Test; Live run |
The test proxy listens on 127.0.0.1, the address the Gateway's hosts
file gives `proxy.test`, inside the special-use blocks, and is reached
with no `private_networks` (511, 549). In Compose, Squid is on the
private backend network, where Squid logs the Gateway as 192.168.32.5. |
|
| 19 | `denied_networks` still applied to the proxy's address | Test |
Unusable pass with `denied_networks: ['127.0.0.0/8', '::1/128']` (930):
`proxy_unavailable`, then `assert not egress.accepted_since(seen), 'The
egress proxy saw a connection'` (682–685). | #55's red run: with the
filter removed, the egress proxy saw a connection. |
| 20 | A refusal by the proxy reported as `proxy_denied` | Test; Live
run | `fails_through_egress(failing['refused'], 'proxy_denied')` asserts
`headers.get('countersign-error') == error` (656, 760). In Compose:
`TCP_DENIED/403` → `proxy_denied`. | |
| 21 | An unreachable or silent proxy reported as `proxy_unavailable` |
Test (#61) | Unreachable: unusable pass. Closed, not HTTP, kept open:
`closed`, `garbled`, `banner`, `stub`. Silent: `runtime.py
--silent-egress-proxy`, `fails_through_egress(failing['silent'],
'proxy_unavailable', timeout=130)` (675–680). It runs in `e2e.yml`'s
`gateway-arm64` step "Fail a CONNECT through a silent egress proxy at
the Gateway's deadline", under both worker sandboxes. | Takes the
120-second deadline. #61's red run: with the deadline mapped to
`upstream_failure`, the case fails. |
| 22 | The proxy's "cannot reach the site" (5xx) reported as
`upstream_failure` | Test; Live run |
`fails_through_egress(failing['unreachable'], 'upstream_failure')`
(773). In Compose, Squid's `TCP_TUNNEL/503` with the site stopped →
`upstream_failure`. | |
| 23 | Each failure carries `Countersign-Request-ID` | Test |
`fails_through_egress`: `assert request_id, (to, answer)`, then
`wait_for_log(rf'Gateway CONNECT {request_id} {error}')` (657–661). The
Compose run matches each ID to the Gateway's log line. | |
| 24 | The operator guide says how to set the egress proxy | Static |
`docs/operate.md`, "Reach sites through an egress proxy": the example
configuration and its fields. | |
| 25 | The guide says where resolution and address policy happen on each
route | Static | The same section's table, with rows "Who resolves the
site's name" and "Who checks the site's addresses". | |
| 26 | Example Squid ACLs, blocking metadata, link-local and loopback |
Static | The same section's `squid` block: `to_metadata`,
`to_link_local`, `to_loopback`, each denied before any allow. | `squid
-k parse` on these rules was a one-off run in #55's review, recorded in
its body. The demo's `squid.conf` has the same rules, so the Compose run
exercises them. |
| 27 | The guide shows how to pin a private site to its prefixes |
Static | The same block: `billing_prefixes` and `billing_outside`, with
`http_access deny billing billing_outside`, and the paragraph on how
`dst` matches. | The test with mixed answers was a one-off run in #55's
review on Squid 6.14. |
| 28 | The configuration reference describes `egress_proxy` and `egress`
| Static | `docs/reference/config.md`: the `egress_proxy` row (Gateway
fields) and the `egress` row (destination fields). | |
| 29 | The troubleshooting table covers the two new error values |
Static | `docs/operate.md`, troubleshooting row "CONNECT returns 502":
`proxy_denied`, `proxy_unavailable`, and `upstream_failure` on a proxied
route. | |
| 30 | The egress proxy works in shared mode as in a sidecar | Test |
`for shared in (False,True)` runs the proxied and unusable passes in
both modes (922–931). CI prints `shared Gateway mTLS through the egress
proxy passed`. | |
| 31 | Workload client configuration unchanged on both routes | Test |
One `request()` (same `--proxy`, `--proxy-cacert`, `--cacert`) reads
`site.test` (proxied) and `direct.test` (direct) in one pass (623–628,
725–738). | |
| 32 | A proxied failure ends with a status and an error value, never a
hang, once the proxy has answered | Test | `banner`, `stub` and `lf`
answer and keep the connection open. Each must give its 502 inside
`connect`'s 8-second socket timeout (768–771). | #55's red run: against
the old read loop, each one timed out. |
| 33 | The proxy's own error page kept from the job | Test | `assert
body == b'' and b'egress proxy' not in answer` (660), on every negative
case. The test proxy sends `ERROR_PAGE` with each 403 and 503 (250–251,
289, 303). | |
| 34 | Prompt, grant and cutoff work the same on a proxied route | Test;
Live run | Grant: the site returns the approved certificate's
fingerprint (727–730). Cutoff: the lease cutoff runs in the proxied pass
(805–827). Prompt: in the Compose run, the Companion approval reaches
the site through Squid. | The runtime test's grant authority stands in
for the prompt, so only the Compose run covers the prompt itself. |
| 35 | Revoking a grant cuts a proxied connection as a direct one | Test
| Shared proxied pass: `broker.retired['bob'].set()`, then `assert
site.cutoff.wait(1)` (831–840), before the proxied pass returns
(841–843). | |
| 36 | Never a fallback from the egress proxy to a direct connection |
Test | `assert not site.accepted_since(seen), (to, 'The site saw a
connection', answer)` in every `fails_through_egress` case (651). The
unusable pass gives the Gateway a working direct route and checks that
it went unused (549–550, 573). | #55's red run: a direct fallback added
on purpose was caught. A silent proxy is story 21's case, which #61
added. |
| 37 | Site certificate checked against the hostname and the rule's
roots before any signature | Test (#61) | `for name in ('wrong',
'untrusted')`: the request fails, `egress.take()` records the allowed
tunnel, and `assert failing[name] not in broker.signatures` (778–782).
`untrusted`'s `roots` is another CA. | #61's red run: with trust loaded
from another rule's roots, the request succeeded. |
| 38 | A site still needs its destination rule at the Gateway and the
Broker | Test (#61) | `refused_before_egress(unlisted)` (a grant, no
rule) and `refused_before_egress(ungranted)` (a rule, no grant). Each
asserts `HTTP/1.1 403`, then `'The egress proxy saw a connection'` and
`'The site saw a connection'` are both false (667–673, 745–747). | #61's
red runs: the rule check bypassed gives 200; a tunnel opened on a
refused admission is seen by the egress proxy. |
| 39 | The egress proxy carries only encrypted bytes | Test; Live run |
The test proxy pumps raw bytes to a TLS site that requires a client
certificate (`site_tls.verify_mode=ssl.CERT_REQUIRED`), and the grant
authority asserts `body['tls'] == 'tls13'` on each signature (218). A
clear-text byte after the CONNECT head would break that handshake. In
Compose: Squid has no `ssl-bump`, logs `TCP_TUNNEL/200`, and the site
saw the approved fingerprint. | |
| 40 | Grant worker and worker sandbox unchanged | Static; Test | #55's
diff touches no file under `runtime/worker.rs` or `worker_sandbox.rs`.
The proxied leg is in the supervisor (`egress::tunnel` from
`connect_request`). The proxied passes run under both sandboxes in CI
(`gateway-filter-sandbox`). | #63 later changed `worker_sandbox.rs` for
a glibc malloc read (#62), which the egress proxy did not cause: the
same kill could come on a direct route. |
| 41 | ADR 0003 says which checks move to the proxy and which stay |
Static | `docs/adr/0003-…md`, `status: accepted`. Decisions 3 (move), 4
(stay) and 6 (the proxy's own address). | |
| 42 | The Gateway ignores proxy environment variables on this leg |
Test (#61) | `ignored` pass, as before. In the proxied passes, the six
variables name a dead address beside `egress_proxy` (563–569), and every
proxied request still succeeds. | #61's red run: `egress::tunnel`
preferring `HTTPS_PROXY` fails the first proxied request with 502. |
| 43 | The design record's resolution paragraph names both routes |
Static | `docs/specs/2026-11-17-countersign-design.md`: "A destination
takes one of two routes. On a direct route … On a route through the
operator's egress proxy (ADR 0003) …". | |
| 44 | The runtime test sends a request through a test-only CONNECT
proxy on every pull request | Static | `.github/workflows/e2e.yml` (`on:
[push, pull_request]`), step `timeout 120 python
crates/countersign-gateway/tests/runtime.py …` and
`scripts/filter-sandbox-container.sh`. The proxied passes always run
(`main`, 922–931). | |
| 45 | That test proves the site saw nothing when the proxy refused or
was unreachable | Test | As story 36: line 651 for `refused`, `closed`,
`garbled`, `banner`, `stub`, `lf` and `unreachable`, and both unusable
passes. | |
| 46 | That test proves the proxy got the site's name, not an address |
Test | As stories 5 and 6 (575, 734). | |
| 47 | That test runs one Gateway with a proxied and a direct
destination | Test | As story 3 (735–738). | |
| 48 | That test rejects a wrong-hostname certificate through an allowed
tunnel | Test | `wrong` (778–782). `assert egress.take() == [f'CONNECT
{failing["wrong"]} HTTP/1.1'], 'The egress proxy refused the tunnel'`
proves that the tunnel was allowed. | #55's red run: with the site
certified for `wrong.test`, a signature was made. |
| 49 | A Compose variant with a real Squid to run before a release |
Live run | `compose_demo.py --squid --verify`, the run posted on #52
(5d76a8b, Squid 6.14, exit 0). `docs/develop.md`, "Qualify the Gateway
against a real Squid". | |
| 50 | That variant correlates Squid's log, the site's certificate and
the Workload's read from one run | Live run | #52 run: `Squid tunneled
the Workload's CONNECT: … TCP_TUNNEL/200 … CONNECT site.test:9444 …
HIER_DIRECT/172.31.0.2`, `Compose mTLS accepted selected SHA-256
5d01b2…` and `Job page matched the fingerprint the site received with
the approval`. The checks are `Squid.logged` and `verify` in
`compose_demo.py`. | The posted run is on 5d76a8b. Between it and
d84d66d, `compose_demo.py` did not change on the default path. |
| 51 | The glossary defines egress proxy | Static | `GLOSSARY.md`:
**Egress proxy**, _Avoid_: upstream proxy, outbound proxy, Squid. |
Landed with #53. |
| 52 | #46's reporter has a documented, passing Compose configuration |
Live run | The #52 run, plus `docs/develop.md` and
`examples/demo-integrator/{compose.squid.yaml,squid.conf}`. | Repeating
it on Fargate is out of scope: "Repeating the run on Fargate 1.4.0 with
the filter sandbox. The reporter does that from the documented Compose
configuration; Countersign's part ends at Compose for now." #52's live
step (a comment on #46) is a separate matter. |
| 53 | The demo walkthrough is unchanged unless the Squid variant is
asked for | Static; Live run | `examples/demo-integrator/compose.yaml`
is unchanged by #55, and `--squid` is an opt-in `store_true` flag. #55's
body: the default `--verify` passed on 17525c6, and the default `docker
compose config` was identical before and after. | After 17525c6,
`compose_demo.py` changed only inside `Squid.configure`. |

**Summary:** 53 stories, each proved: by test, statically, by the live
run, or by a mix of these. No story is excluded whole. Only story 52's
Fargate step is out of scope. Stories 8, 11, 17, 21, 37, 38 and 42 were
Partial or Gaps on d5b15ba, the squash of #55; #61 added their cases,
each red once against a Gateway broken on purpose.

</details>

## Merge Danger

**Door:** two-way. A revert removes the file, and #49 is reopened by
hand.

**Blast Radius:** none.

The merge ships no image, release or infrastructure, and no flag. The
handbook excludes `docs/specs/` (`mkdocs.yml`, `exclude_docs`).

## Reviews

<details>
<summary>No review ran: the file is the issue's text, checked byte for
byte</summary>

</details>

## Tracking

Epic: E2 Reach protected sites through an operator's egress proxy
Spec: #49 Reach protected sites through an operator's egress proxy
Ticket: none
Flag: none

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A grant worker is killed when the C library's malloc opens a file

1 participant