trusty_code/http_credential.rs
1//! The client-side credential for the daemon's HTTP listener (#5439, #6637).
2//!
3//! Why this is its own module now: it used to live in
4//! `crate::tui_client::discovery`, alongside the `TCODE_DAEMON_URL` lookup the
5//! TUI used to find a daemon. #6637 moved the TUI onto the daemon's Unix
6//! socket, where the peer-uid check replaces the token outright, and deleted
7//! that module. [`crate::session::connector::TcodeConnector`] still speaks
8//! HTTP, so the credential half outlives the discovery half and moves here
9//! rather than being deleted with it.
10//!
11//! **This retires with the TCP listener in PR 2** — `trusty-code-gui` serves
12//! its own webview then, and no client in this crate dials HTTP.
13//!
14//! What: [`DAEMON_TOKEN_ENV`](crate::http_credential::DAEMON_TOKEN_ENV) and
15//! [`daemon_credential_for`](crate::http_credential::daemon_credential_for),
16//! unchanged.
17//!
18//! Test: `http_credential_tests`.
19
20/// Environment variable naming the daemon credential directly, ahead of the
21/// token file (#5439).
22///
23/// Why: a client may run where it cannot read the daemon's data directory — a
24/// container, a different account, an operator driving a remote daemon over a
25/// tunnel. This is a CLIENT-side override only: `tcode serve --http` never
26/// reads it, because a server that took its credential from the environment
27/// would accept whatever a caller could arrange to export.
28pub const DAEMON_TOKEN_ENV: &str = "TCODE_DAEMON_TOKEN";
29
30/// The credential to send to `base_url`, or `None` when there is none to send
31/// or none that may be sent.
32///
33/// Why: the token authenticates a caller to the LOCAL daemon, and nothing
34/// else. A base URL can legitimately name a non-loopback address (an operator
35/// forwarding a port, a remote daemon over a tunnel), and attaching the local
36/// machine's credential to a request leaving loopback would hand it to
37/// whatever answers. This crate resolves it in one place so no request site
38/// grows its own answer.
39/// What: a thin naming of `trusty_common::daemon_token::credential_for`, which
40/// owns the loopback gate, the override precedence, and the file read.
41///
42/// The gate lives THERE and not here for a reason worth stating: the first
43/// version of this function called `server::origin_is_loopback`, an
44/// `Origin`-HEADER parser, which reads
45/// `http://127.0.0.1:7882@attacker.example` as loopback and shipped the token
46/// off-machine. `trusty-code-gui` had the identical bug in its own copy. One
47/// implementation, parsing the way the client that dials the URL parses.
48/// Test: `http_credential_tests::credential_is_withheld_from_a_non_loopback_url`,
49/// `http_credential_tests::credential_env_override_wins_for_a_loopback_url`.
50pub fn daemon_credential_for(base_url: &str) -> Option<String> {
51 trusty_common::daemon_token::credential_for(
52 crate::serve::http::TOKEN_APP_NAME,
53 base_url,
54 DAEMON_TOKEN_ENV,
55 )
56}
57
58#[cfg(test)]
59mod http_credential_tests {
60 use super::*;
61 use serial_test::serial;
62 use trusty_common::credentials::test_sandbox::{CredentialSandbox, assert_secret_eq};
63
64 /// #5439's credential-exfiltration guard: the local token authenticates a
65 /// caller to the LOCAL daemon and must never leave loopback, however the
66 /// operator points a client.
67 ///
68 /// This is the arm that fails open if the gate is dropped — with a token
69 /// available in the environment, a non-loopback base URL must still
70 /// resolve to `None`.
71 /// #9123: inside the credential sandbox, so the token file the resolver
72 /// falls back to is the sandbox's empty data dir, never the real one.
73 #[test]
74 #[serial]
75 fn credential_is_withheld_from_a_non_loopback_url() {
76 let mut sandbox = CredentialSandbox::enter();
77 sandbox.set(DAEMON_TOKEN_ENV, "a".repeat(64));
78 let remote = [
79 "http://example.test:7882",
80 "https://10.0.0.5:7882",
81 "http://192.168.1.4:7882",
82 // The userinfo family. An `Origin`-header parser splits the
83 // authority at the FIRST `:`, so it reads the host of these as
84 // `127.0.0.1` and calls them loopback; WHATWG URL parsing splits
85 // userinfo at the LAST `@`, so the real host is `attacker.example`
86 // and every request goes there.
87 "http://127.0.0.1:7882@attacker.example",
88 "http://127.0.0.1:7882@attacker.example/rpc",
89 "http://localhost@attacker.example",
90 "http://user:pass@attacker.example",
91 ];
92 for url in remote {
93 assert_secret_eq(
94 daemon_credential_for(url).as_deref(),
95 None,
96 &format!("{url} must get no credential"),
97 );
98 }
99 assert_secret_eq(
100 daemon_credential_for("http://127.0.0.1:7882").as_deref(),
101 Some("a".repeat(64).as_str()),
102 "loopback must get the credential",
103 );
104 }
105
106 /// The env override must beat the token file, so a client that cannot read
107 /// the daemon's data directory can still be pointed at a credential.
108 /// #9123: sandboxed as above.
109 #[test]
110 #[serial]
111 fn credential_env_override_wins_for_a_loopback_url() {
112 let mut sandbox = CredentialSandbox::enter();
113 sandbox.set(DAEMON_TOKEN_ENV, "b".repeat(64));
114 assert_secret_eq(
115 daemon_credential_for("http://localhost:7882").as_deref(),
116 Some("b".repeat(64).as_str()),
117 "the env override must win",
118 );
119 }
120
121 /// A blank override must be ignored rather than becoming an empty
122 /// credential — an empty bearer is a malformed header, not "no header".
123 /// #9123: the sandbox's data dir holds no token file, so falling through
124 /// lands on `None`, not on whatever the real daemon wrote.
125 #[test]
126 #[serial]
127 fn blank_credential_override_falls_through() {
128 let mut sandbox = CredentialSandbox::enter();
129 sandbox.set(DAEMON_TOKEN_ENV, " ");
130 assert_secret_eq(
131 daemon_credential_for("http://127.0.0.1:7882").as_deref(),
132 None,
133 "a blank override must fall through to the (absent) token file",
134 );
135 }
136}