Skip to content

Commit 1a9bcb0

Browse files
committed
fix(sandbox,podman): finish the fail-closed upstream proxy credential contract
The upstream proxy URL already had a single shared validator, but the credential did not: the Podman driver rejected only CR/LF/NUL while the supervisor rejected every control character, so a credential accepted at sandbox-create time (e.g. one containing a tab) could still be rejected in-container. Present-but-whitespace reserved OPENSHELL_UPSTREAM_* values were also silently treated as unset, quietly downgrading the operator's egress boundary to direct dialing. Add parse_upstream_proxy_credential to openshell-core as the single source of truth for the documented user:pass credential form (non-empty user, no control characters, trimmed) and use it in both the Podman driver's secret staging and the supervisor's Proxy-Authorization header construction. Error variants carry no payload so credential content can never leak into messages. Make a present-but-empty reserved variable fatal to supervisor proxy startup instead of meaning "unset"; only fully unset variables disable the proxy. The driver correspondingly rejects an empty no_proxy at config time so it can never inject a value the supervisor refuses. Addresses the remaining fail-closed credential/config review item on Signed-off-by: Philippe Martin <[email protected]> #2245.
1 parent a82cf26 commit 1a9bcb0

6 files changed

Lines changed: 211 additions & 62 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,23 @@ variables are unaffected — they are always rewritten to point at the local
119119
policy proxy.
120120

121121
The configuration is fail-closed: a reserved variable that is present but
122-
invalid — an unsupported or malformed proxy URL, an unreadable auth file, or a
123-
malformed credential — is fatal to supervisor startup instead of being treated
124-
as unset, so a misconfiguration can never silently degrade to direct dialing
125-
or unauthenticated proxy access. The driver validates the same rules at
126-
sandbox-create time through a validator shared with the supervisor
127-
(`openshell_core::driver_utils::parse_upstream_proxy_url`).
122+
invalid — a present-but-empty value, an unsupported or malformed proxy URL, an
123+
unreadable auth file, or a malformed credential — is fatal to supervisor
124+
startup instead of being treated as unset, so a misconfiguration can never
125+
silently degrade to direct dialing or unauthenticated proxy access. Only a
126+
fully unset variable means "no proxy". The driver validates the same rules at
127+
sandbox-create time through validators shared with the supervisor
128+
(`openshell_core::driver_utils::parse_upstream_proxy_url` and
129+
`parse_upstream_proxy_credential`).
128130

129131
Proxy credentials are never embedded in the URL: an inline `user:pass@` is
130132
rejected because it would be stored in `gateway.toml` and exposed in container
131133
metadata. Operators supply credentials via `proxy_auth_file`; the driver
132134
stages them as a root-only secret mounted at a fixed path and exports only
133135
that path in `OPENSHELL_UPSTREAM_PROXY_AUTH_FILE`. The supervisor reads the
134-
file and builds the `Proxy-Authorization: Basic` header; an empty credential
135-
or one containing control characters is fatal. The reserved proxy variables —
136+
file and builds the `Proxy-Authorization: Basic` header; a credential that is
137+
empty, contains control characters, or is not in `user:pass` form is fatal on
138+
both sides. The reserved proxy variables —
136139
including the auth-file path — are stripped from workload child processes.
137140

138141
## Credentials

‎crates/openshell-core/src/driver_utils.rs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,60 @@ pub fn parse_upstream_proxy_url(raw: &str) -> Result<UpstreamProxyAddr, Upstream
170170
})
171171
}
172172

173+
/// Why an upstream proxy credential was rejected by
174+
/// [`parse_upstream_proxy_credential`].
175+
///
176+
/// Variants carry no payload so an error can never leak credential content
177+
/// into logs or user-facing messages.
178+
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
179+
pub enum UpstreamProxyCredentialError {
180+
/// The credential is empty or whitespace-only.
181+
#[error("credential is empty")]
182+
Empty,
183+
/// The credential contains control characters (CR, LF, NUL, tab, ...)
184+
/// that could inject additional HTTP headers.
185+
#[error("credential contains control characters")]
186+
ControlCharacters,
187+
/// The credential has no `:` separating user from password.
188+
#[error("credential must use the user:pass form (missing ':')")]
189+
MissingSeparator,
190+
/// The credential has an empty user before the `:` separator.
191+
#[error("credential must use the user:pass form (empty user)")]
192+
EmptyUser,
193+
}
194+
195+
/// Validate a corporate upstream-proxy credential read from the proxy auth
196+
/// file, returning the trimmed `user:pass` value.
197+
///
198+
/// Single source of truth for what counts as a valid proxy credential: the
199+
/// compute driver applies it at sandbox-create time (before staging the
200+
/// secret) and the in-container supervisor applies it again before building
201+
/// the `Proxy-Authorization: Basic` header, so a credential one side accepts
202+
/// is never rejected by the other.
203+
///
204+
/// Surrounding whitespace (including the conventional trailing newline) is
205+
/// trimmed. The user part must be non-empty; the password may be empty and
206+
/// may itself contain `:` (per RFC 7617 the first `:` is the separator).
207+
///
208+
/// # Errors
209+
///
210+
/// Returns an [`UpstreamProxyCredentialError`] describing the first rule the
211+
/// value violates. Errors never contain the credential itself.
212+
pub fn parse_upstream_proxy_credential(raw: &str) -> Result<&str, UpstreamProxyCredentialError> {
213+
let credential = raw.trim();
214+
if credential.is_empty() {
215+
return Err(UpstreamProxyCredentialError::Empty);
216+
}
217+
if credential.contains(|c: char| c.is_control()) {
218+
return Err(UpstreamProxyCredentialError::ControlCharacters);
219+
}
220+
match credential.split_once(':') {
221+
None => Err(UpstreamProxyCredentialError::MissingSeparator),
222+
Some(("", _)) => Err(UpstreamProxyCredentialError::EmptyUser),
223+
Some(_) => Ok(credential),
224+
}
225+
}
226+
173227
/// Return the XDG state path for a driver's sandbox JWT token file.
174228
///
175229
/// The resulting path is `$XDG_STATE_HOME/openshell/<driver_subdir>[/<namespace>]/<sandbox_id>/sandbox.jwt`.
@@ -318,4 +372,50 @@ mod tests {
318372
));
319373
assert!(parse_upstream_proxy_url("http://").is_err());
320374
}
375+
376+
#[test]
377+
fn upstream_proxy_credential_accepts_user_pass_and_trims() {
378+
assert_eq!(
379+
parse_upstream_proxy_credential("user:pass\n"),
380+
Ok("user:pass")
381+
);
382+
// The password may be empty and may contain further colons.
383+
assert_eq!(parse_upstream_proxy_credential("user:"), Ok("user:"));
384+
assert_eq!(
385+
parse_upstream_proxy_credential("user:p@:ss"),
386+
Ok("user:p@:ss")
387+
);
388+
}
389+
390+
#[test]
391+
fn upstream_proxy_credential_rejects_empty() {
392+
for raw in ["", " ", "\n"] {
393+
assert_eq!(
394+
parse_upstream_proxy_credential(raw),
395+
Err(UpstreamProxyCredentialError::Empty)
396+
);
397+
}
398+
}
399+
400+
#[test]
401+
fn upstream_proxy_credential_rejects_control_characters() {
402+
for raw in ["user:pa\r\nss", "user:pa\0ss", "user:pa\tss"] {
403+
assert_eq!(
404+
parse_upstream_proxy_credential(raw),
405+
Err(UpstreamProxyCredentialError::ControlCharacters)
406+
);
407+
}
408+
}
409+
410+
#[test]
411+
fn upstream_proxy_credential_rejects_malformed_user_pass_form() {
412+
assert_eq!(
413+
parse_upstream_proxy_credential("userpass"),
414+
Err(UpstreamProxyCredentialError::MissingSeparator)
415+
);
416+
assert_eq!(
417+
parse_upstream_proxy_credential(":pass"),
418+
Err(UpstreamProxyCredentialError::EmptyUser)
419+
);
420+
}
321421
}

‎crates/openshell-driver-podman/src/config.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ impl PodmanComputeConfig {
250250
})?;
251251
}
252252

253+
// The supervisor treats a present-but-empty reserved variable as a
254+
// fatal misconfiguration, so never accept (and later inject) one.
255+
if self
256+
.no_proxy
257+
.as_deref()
258+
.is_some_and(|list| list.trim().is_empty())
259+
{
260+
return Err(crate::client::PodmanApiError::InvalidInput(
261+
"no_proxy must not be empty when set; omit it instead".to_string(),
262+
));
263+
}
264+
253265
if let Some(path) = self.proxy_auth_file.as_deref() {
254266
if path.trim().is_empty() {
255267
return Err(crate::client::PodmanApiError::InvalidInput(
@@ -526,6 +538,17 @@ mod tests {
526538
assert!(err.to_string().contains("http_proxy"), "{err}");
527539
}
528540

541+
#[test]
542+
fn validate_proxy_config_rejects_empty_no_proxy() {
543+
let cfg = PodmanComputeConfig {
544+
https_proxy: Some("http://proxy.corp.com:8080".to_string()),
545+
no_proxy: Some(" ".to_string()),
546+
..PodmanComputeConfig::default()
547+
};
548+
let err = cfg.validate_proxy_config().unwrap_err();
549+
assert!(err.to_string().contains("no_proxy"), "{err}");
550+
}
551+
529552
#[test]
530553
fn validate_proxy_config_rejects_inline_credentials() {
531554
for url in [

‎crates/openshell-driver-podman/src/driver.rs‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ async fn cleanup_sandbox_token_secret(client: &PodmanClient, secret_name: &str)
118118
/// Podman secret, so the credentials reach the supervisor through a root-only
119119
/// mount rather than the container environment.
120120
///
121-
/// Fails closed: when `proxy_auth_file` is configured but cannot be read or is
122-
/// empty/malformed, the sandbox is not created.
121+
/// Fails closed: when `proxy_auth_file` is configured but cannot be read or
122+
/// does not hold a valid `user:pass` credential, the sandbox is not created.
123+
/// Credential validation is shared with the in-container supervisor through
124+
/// [`openshell_core::driver_utils::parse_upstream_proxy_credential`], so a
125+
/// credential staged here can never be rejected at supervisor startup.
123126
async fn create_sandbox_proxy_auth_secret(
124127
client: &PodmanClient,
125128
config: &PodmanComputeConfig,
@@ -132,19 +135,10 @@ async fn create_sandbox_proxy_auth_secret(
132135
let raw = tokio::fs::read_to_string(path).await.map_err(|e| {
133136
ComputeDriverError::Message(format!("failed to read proxy_auth_file '{path}': {e}"))
134137
})?;
135-
let credential = raw.trim();
136-
if credential.is_empty() {
137-
return Err(ComputeDriverError::InvalidArgument(format!(
138-
"proxy_auth_file '{path}' is empty"
139-
)));
140-
}
141-
// A credential containing CR/LF/NUL would allow HTTP header injection once
142-
// the supervisor places it in the Proxy-Authorization header.
143-
if credential.contains(['\r', '\n', '\0']) {
144-
return Err(ComputeDriverError::InvalidArgument(format!(
145-
"proxy_auth_file '{path}' must not contain control characters"
146-
)));
147-
}
138+
let credential =
139+
openshell_core::driver_utils::parse_upstream_proxy_credential(&raw).map_err(|err| {
140+
ComputeDriverError::InvalidArgument(format!("proxy_auth_file '{path}': {err}"))
141+
})?;
148142

149143
let secret_name = container::proxy_auth_secret_name(&sandbox.id);
150144
client

0 commit comments

Comments
 (0)