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
45 changes: 35 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ nix = { version = "0.29", features = ["signal", "process", "user", "fs", "term"]
# Serialization
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
serde_yml = "0.0.12"

# HTTP client
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/openshell-policy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository.workspace = true
[dependencies]
openshell-core = { path = "../openshell-core" }
serde = { workspace = true }
serde_yaml = { workspace = true }
serde_yml = { workspace = true }
miette = { workspace = true }

[lints]
Expand Down
39 changes: 29 additions & 10 deletions crates/openshell-policy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ struct NetworkEndpointDef {
#[serde(default, skip_serializing_if = "String::is_empty")]
host: String,
/// Single port (backwards compat). Mutually exclusive with `ports`.
/// Uses `u16` to reject invalid values >65535 at parse time.
#[serde(default, skip_serializing_if = "is_zero")]
port: u32,
port: u16,
/// Multiple ports. When non-empty, this endpoint covers all listed ports.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
ports: Vec<u32>,
ports: Vec<u16>,
#[serde(default, skip_serializing_if = "String::is_empty")]
protocol: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
Expand All @@ -101,7 +102,7 @@ struct NetworkEndpointDef {
allowed_ips: Vec<String>,
}

fn is_zero(v: &u32) -> bool {
fn is_zero(v: &u16) -> bool {
*v == 0
}

Expand Down Expand Up @@ -169,10 +170,10 @@ fn to_proto(raw: PolicyFile) -> SandboxPolicy {
.map(|e| {
// Normalize port/ports: ports takes precedence, else
// single port is promoted to ports array.
let normalized_ports = if !e.ports.is_empty() {
e.ports
let normalized_ports: Vec<u32> = if !e.ports.is_empty() {
e.ports.into_iter().map(u32::from).collect()
} else if e.port > 0 {
vec![e.port]
vec![u32::from(e.port)]
} else {
vec![]
};
Expand Down Expand Up @@ -285,10 +286,12 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile {
.map(|e| {
// Use compact form: if ports has exactly 1 element,
// emit port (scalar). If >1, emit ports (array).
// Proto uses u32; YAML uses u16. Clamp at boundary.
let clamp = |v: u32| -> u16 { v.min(65535) as u16 };
let (port, ports) = if e.ports.len() > 1 {
(0, e.ports.clone())
(0, e.ports.iter().map(|&p| clamp(p)).collect())
} else {
(e.ports.first().copied().unwrap_or(e.port), vec![])
(clamp(e.ports.first().copied().unwrap_or(e.port)), vec![])
};
NetworkEndpointDef {
host: e.host.clone(),
Expand Down Expand Up @@ -358,7 +361,7 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile {

/// Parse a sandbox policy from a YAML string.
pub fn parse_sandbox_policy(yaml: &str) -> Result<SandboxPolicy> {
let raw: PolicyFile = serde_yaml::from_str(yaml)
let raw: PolicyFile = serde_yml::from_str(yaml)
.into_diagnostic()
.wrap_err("failed to parse sandbox policy YAML")?;
Ok(to_proto(raw))
Expand All @@ -371,7 +374,7 @@ pub fn parse_sandbox_policy(yaml: &str) -> Result<SandboxPolicy> {
/// and is round-trippable through `parse_sandbox_policy`.
pub fn serialize_sandbox_policy(policy: &SandboxPolicy) -> Result<String> {
let yaml_repr = from_proto(policy);
serde_yaml::to_string(&yaml_repr)
serde_yml::to_string(&yaml_repr)
.into_diagnostic()
.wrap_err("failed to serialize policy to YAML")
}
Expand Down Expand Up @@ -1207,4 +1210,20 @@ network_policies:
proto2.network_policies["test"].endpoints[0].host
);
}

#[test]
fn rejects_port_above_65535() {
let yaml = r#"
version: 1
network_policies:
test:
endpoints:
- host: example.com
port: 70000
"#;
assert!(
parse_sandbox_policy(yaml).is_err(),
"port >65535 should fail to parse"
);
}
}
2 changes: 1 addition & 1 deletion crates/openshell-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
serde_yaml = { workspace = true }
serde_yml = { workspace = true }
uuid = { workspace = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/openshell-router/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl RouterConfig {
path.display()
))
})?;
let config: Self = serde_yaml::from_str(&content).map_err(|e| {
let config: Self = serde_yml::from_str(&content).map_err(|e| {
RouterError::Internal(format!(
"failed to parse router config {}: {e}",
path.display()
Expand Down
2 changes: 1 addition & 1 deletion crates/openshell-sandbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ipnet = "2"

# Serialization
serde_json = { workspace = true }
serde_yaml = { workspace = true }
serde_yml = { workspace = true }

# Logging
tracing = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/openshell-sandbox/src/opa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ fn parse_process_policy(val: &regorus::Value) -> ProcessPolicy {

/// Preprocess YAML policy data: parse, normalize, validate, expand access presets, return JSON.
fn preprocess_yaml_data(yaml_str: &str) -> Result<String> {
let mut data: serde_json::Value = serde_yaml::from_str(yaml_str)
let mut data: serde_json::Value = serde_yml::from_str(yaml_str)
.map_err(|e| miette::miette!("failed to parse YAML data: {e}"))?;

// Normalize port → ports for all endpoints so Rego always sees "ports" array.
Expand Down
33 changes: 28 additions & 5 deletions crates/openshell-sandbox/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ use tracing::{debug, info, warn};
const MAX_HEADER_BYTES: usize = 8192;
const INFERENCE_LOCAL_HOST: &str = "inference.local";

/// Maximum total bytes for a streaming inference response body (32 MiB).
const MAX_STREAMING_BODY: usize = 32 * 1024 * 1024;

/// Idle timeout per chunk when relaying streaming inference responses.
const CHUNK_IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// Result of a proxy CONNECT policy decision.
struct ConnectDecision {
action: NetworkAction,
Expand Down Expand Up @@ -1045,18 +1051,35 @@ async fn route_inference_request(
let header_bytes = format_http_response_header(resp.status, &resp_headers);
write_all(tls_client, &header_bytes).await?;

// Stream body chunks as they arrive from the upstream.
// Stream body chunks with byte cap and idle timeout.
let mut total_bytes: usize = 0;
loop {
match resp.next_chunk().await {
Ok(Some(chunk)) => {
match tokio::time::timeout(CHUNK_IDLE_TIMEOUT, resp.next_chunk()).await {
Ok(Ok(Some(chunk))) => {
total_bytes += chunk.len();
if total_bytes > MAX_STREAMING_BODY {
warn!(
total_bytes = total_bytes,
limit = MAX_STREAMING_BODY,
"streaming response exceeded byte limit, truncating"
);
break;
}
let encoded = format_chunk(&chunk);
write_all(tls_client, &encoded).await?;
}
Ok(None) => break,
Err(e) => {
Ok(Ok(None)) => break,
Ok(Err(e)) => {
warn!(error = %e, "error reading upstream response chunk");
break;
}
Err(_) => {
warn!(
idle_timeout_secs = CHUNK_IDLE_TIMEOUT.as_secs(),
"streaming response chunk idle timeout, closing"
);
break;
}
}
}

Expand Down
Loading
Loading