Skip to content

Enforce mailroom access control on TLS serve paths - #1941

Merged
DanGould merged 2 commits into
payjoin:masterfrom
DanGould:mailroom-geoip-connect-info
Oct 2, 2026
Merged

DanGould merged 2 commits into
payjoin:masterfrom
DanGould:mailroom-geoip-connect-info

Conversation

@DanGould

@DanGould DanGould commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

payjoin-mailroom's access-control middleware never rejected a request when the mailroom served over TLS. check_geoip read the client address only from a ConnectInfo<MaybePeerIp> request extension, and only the plain serve path installs that type. serve_acme and serve_manual_tls install ConnectInfo<SocketAddr>. The lookup found nothing and the middleware let the request through. An operator who sets blocked_regions or blocked_ips together with acme gets a policy that admits every client, and nothing in the logs says so. ACME is the default deployment, so this affects the usual setup.

Fix

check_geoip now falls back to ConnectInfo<SocketAddr> when MaybePeerIp is absent. The serve paths are unchanged.

The alternative was to install MaybePeerIp on every serve path. That needs two new Connected impls and moves MaybePeerIp out from behind the access-control feature, and all of it would be deleted again if the plain path stops accepting Unix socket listeners. MaybePeerIp carries an Option only because of those listeners. Without them every serve path can install SocketAddr, and the middleware needs a single lookup. This PR keeps the change to the middleware so it can ship in a patch release.

Testing

geoip_policy_is_enforced_on_the_manual_serve_paths drives both branches of serve_manual_tls. The TLS branch reports its peer the same way serve_acme does, since ACME can't run in a test. Each branch is checked twice. An instance blocking every source address must answer 403, and one blocking an unrelated range must answer 200. Removing the fallback makes the test fail with 200 where 403 is expected.

self-reviewed over multiple iterations concerning who actually uses the unix feature, how this bug was introduced in the first place, and whether it can be done with a more minimal design.

Found by a self-hosted run of project-loupe/loupe over the tree.

Disclosure: co-authored by Claude Code

@DanGould
DanGould marked this pull request as draft October 2, 2026 05:42
@coveralls

coveralls commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 36992725278

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.2%) to 87.494%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (60 of 61 lines covered, 98.36%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
payjoin-mailroom/src/middleware.rs 12 11 91.67%
Total (2 files) 61 60 98.36%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 17848
Covered Lines: 15616
Line Coverage: 87.49%
Coverage Strength: 319.54 hits per line

💛 - Coveralls

@DanGould
DanGould force-pushed the mailroom-geoip-connect-info branch from 0b1bce8 to ac86b7f Compare October 2, 2026 05:56
`check_geoip` read the client address only from
`ConnectInfo<MaybePeerIp>`, which only the plain `serve` path installs.
`serve_acme` and both branches of `serve_manual_tls` install
`ConnectInfo<SocketAddr>`, so on those paths the lookup found nothing,
the middleware fell through, and every request was admitted. An
operator who configured `blocked_regions` or `blocked_ips` alongside
ACME got a policy that never rejected anything, with no error and no
log line, and ACME is the default deployment.

Fall back to `ConnectInfo<SocketAddr>` when `MaybePeerIp` is absent.
This keeps the change to the middleware. Installing one type on every
path would mean new `Connected` impls and ungating `MaybePeerIp`, all of
which goes away once the plain path no longer accepts non-IP listeners
and every path can install `SocketAddr`.

The regression test drives both branches of `serve_manual_tls`. The TLS
branch reports its peer the same way as `serve_acme`, which cannot run
in a test. Each branch must refuse a client when every source address
is blocked and serve it when an unrelated range is blocked, so a path
whose address never reaches the middleware fails one of the two checks
whichever way a missing address is treated. Without the fix the
blocked instance answers 200 where 403 is required.
@DanGould
DanGould force-pushed the mailroom-geoip-connect-info branch from ac86b7f to da33408 Compare October 2, 2026 07:05
@DanGould

DanGould commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

My plan after this lands is to drop tokio_listener from payjoin-mailroom in 0.2.0. MaybePeerIp carries an Option only because tokio_listener lets serve bind a Unix socket, and I don't know of anyone who still runs the mailroom that way (it was a request from Obscura in the early days). With TCP only:

  • Config.listener becomes a SocketAddr, and every serve path installs ConnectInfo<SocketAddr>.
  • MaybePeerIp, its Connected impl and the fallback added here go away, and check_geoip has one lookup.
  • The checks in serve_manual_tls and serve_acme that reject non-TCP listener addresses go away, because the config can't express one.
  • axum's own TCP listener already backs off when accepting fails, so the sleep_on_errors setup in serve may go too. That needs checking against the earlier file descriptor exhaustion before we rely on it.

It removes about a dozen crates from the lockfile, tokio-listener and an old nix among them.

It's a breaking change. The Config.listener type changes, and configs that use the Unix, systemd or inetd listener forms stop working. Plain addresses like "[::]:8080" still parse.

Separately, blocked_regions and blocked_ips only see the address of whatever connects to the mailroom. Behind a reverse proxy that's the proxy, so these settings only protect a mailroom that accepts client connections itself, as it does with ACME. That belongs in docs whichever way this goes.

This is issue territory because it doesn't block anyone but it is simplification and therefore better hygiene.

@DanGould
DanGould requested a review from benalleng October 2, 2026 07:13
@DanGould
DanGould marked this pull request as ready for review October 2, 2026 07:13
Opens the Unreleased section that release crates keep from now on,
with this change's line in it. The next mailroom bump renames the
heading to its version.

@spacebear21 spacebear21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Comment on lines +546 to +564
async fn health_status(tls: bool, blocked_ips: &[&str]) -> axum::http::StatusCode {
let tempdir = tempdir().unwrap();
let mut config = test_config(tempdir.path());
config.access_control = Some(crate::config::AccessControlConfig {
blocked_ips: blocked_ips.iter().map(|ip| ip.to_string()).collect(),
..Default::default()
});

let cert = local_cert_key();
let cert_der = cert.cert.der().to_vec();
let key_der = cert.signing_key.serialize_der();
let tls_config = match tls {
true => Some(RustlsConfig::from_der(vec![cert_der.clone()], key_der).await.unwrap()),
false => None,
};
let mut root_store = RootCertStore::empty();
root_store.add(CertificateDer::from(cert_der.clone())).unwrap();

let (port, _handle) = serve_manual_tls(config, tls_config, root_store, None).await.unwrap();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper seems kinda selective for just the newly added test, when there are already existing tests with shared code that could be refactored as well.

@DanGould
DanGould merged commit ad90f02 into payjoin:master Oct 2, 2026
15 checks passed
@DanGould
DanGould deleted the mailroom-geoip-connect-info branch October 2, 2026 15:09
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.

3 participants