mirror of
https://github.com/openai/codex.git
synced 2026-10-30 19:18:49 +00:00
Add MITM hooks for host-specific HTTPS request clamping
This commit is contained in:
@@ -670,6 +670,27 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"InjectedHeaderConfigSchema": {
|
||||
"properties": {
|
||||
"name": {
|
||||
"default": "",
|
||||
"type": "string"
|
||||
},
|
||||
"prefix": {
|
||||
"default": null,
|
||||
"type": "string"
|
||||
},
|
||||
"secret_env_var": {
|
||||
"default": null,
|
||||
"type": "string"
|
||||
},
|
||||
"secret_file": {
|
||||
"default": null,
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"MemoriesToml": {
|
||||
"additionalProperties": false,
|
||||
"description": "Memories settings loaded from config.toml.",
|
||||
@@ -724,6 +745,101 @@
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"MitmHookActionsConfigSchema": {
|
||||
"properties": {
|
||||
"inject_request_headers": {
|
||||
"default": [],
|
||||
"items": {
|
||||
"$ref": "#/definitions/InjectedHeaderConfigSchema"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"strip_request_headers": {
|
||||
"default": [],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"MitmHookConfigSchema": {
|
||||
"properties": {
|
||||
"actions": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MitmHookActionsConfigSchema"
|
||||
}
|
||||
],
|
||||
"default": {
|
||||
"inject_request_headers": [],
|
||||
"strip_request_headers": []
|
||||
}
|
||||
},
|
||||
"host": {
|
||||
"default": "",
|
||||
"type": "string"
|
||||
},
|
||||
"match": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MitmHookMatchConfigSchema"
|
||||
}
|
||||
],
|
||||
"default": {
|
||||
"body": null,
|
||||
"headers": {},
|
||||
"methods": [],
|
||||
"path_prefixes": [],
|
||||
"query": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"MitmHookMatchConfigSchema": {
|
||||
"properties": {
|
||||
"body": {
|
||||
"default": null
|
||||
},
|
||||
"headers": {
|
||||
"additionalProperties": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"default": {},
|
||||
"type": "object"
|
||||
},
|
||||
"methods": {
|
||||
"default": [],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"path_prefixes": {
|
||||
"default": [],
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"query": {
|
||||
"additionalProperties": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"default": {},
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ModelAvailabilityNuxConfig": {
|
||||
"additionalProperties": {
|
||||
"format": "uint32",
|
||||
@@ -869,6 +985,15 @@
|
||||
"enabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"mitm": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"mitm_hooks": {
|
||||
"items": {
|
||||
"$ref": "#/definitions/MitmHookConfigSchema"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"mode": {
|
||||
"$ref": "#/definitions/NetworkModeSchema"
|
||||
},
|
||||
|
||||
@@ -13,6 +13,11 @@ use crate::config_loader::RequirementSource;
|
||||
use crate::features::Feature;
|
||||
use assert_matches::assert_matches;
|
||||
use codex_config::CONFIG_TOML_FILE;
|
||||
use codex_network_proxy::InjectedHeaderConfig;
|
||||
use codex_network_proxy::MitmHookActionsConfig;
|
||||
use codex_network_proxy::MitmHookConfig;
|
||||
use codex_network_proxy::MitmHookMatchConfig;
|
||||
use codex_network_proxy::NetworkMode;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
@@ -217,7 +222,27 @@ enabled = true
|
||||
proxy_url = "http://127.0.0.1:43128"
|
||||
enable_socks5 = false
|
||||
allow_upstream_proxy = false
|
||||
mode = "full"
|
||||
mitm = true
|
||||
allowed_domains = ["openai.com"]
|
||||
|
||||
[[permissions.workspace.network.mitm_hooks]]
|
||||
host = "api.github.com"
|
||||
|
||||
[permissions.workspace.network.mitm_hooks.match]
|
||||
methods = ["POST", "PUT"]
|
||||
path_prefixes = ["/repos/openai/"]
|
||||
|
||||
[permissions.workspace.network.mitm_hooks.match.headers]
|
||||
"x-github-api-version" = ["2022-11-28"]
|
||||
|
||||
[permissions.workspace.network.mitm_hooks.actions]
|
||||
strip_request_headers = ["authorization"]
|
||||
|
||||
[[permissions.workspace.network.mitm_hooks.actions.inject_request_headers]]
|
||||
name = "authorization"
|
||||
secret_env_var = "CODEX_GITHUB_TOKEN"
|
||||
prefix = "Bearer "
|
||||
"#;
|
||||
let cfg: ConfigToml =
|
||||
toml::from_str(toml).expect("TOML deserialization should succeed for permissions profiles");
|
||||
@@ -253,11 +278,34 @@ allowed_domains = ["openai.com"]
|
||||
allow_upstream_proxy: Some(false),
|
||||
dangerously_allow_non_loopback_proxy: None,
|
||||
dangerously_allow_all_unix_sockets: None,
|
||||
mode: None,
|
||||
mode: Some(NetworkMode::Full),
|
||||
allowed_domains: Some(vec!["openai.com".to_string()]),
|
||||
denied_domains: None,
|
||||
allow_unix_sockets: None,
|
||||
allow_local_binding: None,
|
||||
mitm: Some(true),
|
||||
mitm_hooks: Some(vec![MitmHookConfig {
|
||||
host: "api.github.com".to_string(),
|
||||
matcher: MitmHookMatchConfig {
|
||||
methods: vec!["POST".to_string(), "PUT".to_string()],
|
||||
path_prefixes: vec!["/repos/openai/".to_string()],
|
||||
query: BTreeMap::new(),
|
||||
headers: BTreeMap::from([(
|
||||
"x-github-api-version".to_string(),
|
||||
vec!["2022-11-28".to_string()],
|
||||
)]),
|
||||
body: None,
|
||||
},
|
||||
actions: MitmHookActionsConfig {
|
||||
strip_request_headers: vec!["authorization".to_string()],
|
||||
inject_request_headers: vec![InjectedHeaderConfig {
|
||||
name: "authorization".to_string(),
|
||||
secret_env_var: Some("CODEX_GITHUB_TOKEN".to_string()),
|
||||
secret_file: None,
|
||||
prefix: Some("Bearer ".to_string()),
|
||||
}],
|
||||
},
|
||||
}]),
|
||||
}),
|
||||
},
|
||||
)]),
|
||||
@@ -265,6 +313,40 @@ allowed_domains = ["openai.com"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permissions_profile_network_to_proxy_config_preserves_mitm_hooks() {
|
||||
let network = NetworkToml {
|
||||
enabled: Some(true),
|
||||
mode: Some(NetworkMode::Full),
|
||||
mitm: Some(true),
|
||||
mitm_hooks: Some(vec![MitmHookConfig {
|
||||
host: "api.github.com".to_string(),
|
||||
matcher: MitmHookMatchConfig {
|
||||
methods: vec!["POST".to_string()],
|
||||
path_prefixes: vec!["/repos/openai/".to_string()],
|
||||
..MitmHookMatchConfig::default()
|
||||
},
|
||||
actions: MitmHookActionsConfig {
|
||||
strip_request_headers: vec!["authorization".to_string()],
|
||||
inject_request_headers: vec![InjectedHeaderConfig {
|
||||
name: "authorization".to_string(),
|
||||
secret_env_var: Some("CODEX_GITHUB_TOKEN".to_string()),
|
||||
secret_file: None,
|
||||
prefix: Some("Bearer ".to_string()),
|
||||
}],
|
||||
},
|
||||
}]),
|
||||
..NetworkToml::default()
|
||||
};
|
||||
|
||||
let config = network.to_network_proxy_config();
|
||||
|
||||
assert!(config.network.enabled);
|
||||
assert_eq!(config.network.mode, NetworkMode::Full);
|
||||
assert!(config.network.mitm);
|
||||
assert_eq!(config.network.mitm_hooks, network.mitm_hooks.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permissions_profiles_network_populates_runtime_network_proxy_spec() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use codex_network_proxy::MitmHookConfig;
|
||||
use codex_network_proxy::NetworkMode;
|
||||
use codex_network_proxy::NetworkProxyConfig;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
@@ -73,6 +74,9 @@ pub struct NetworkToml {
|
||||
pub denied_domains: Option<Vec<String>>,
|
||||
pub allow_unix_sockets: Option<Vec<String>>,
|
||||
pub allow_local_binding: Option<bool>,
|
||||
pub mitm: Option<bool>,
|
||||
#[schemars(with = "Option<Vec<MitmHookConfigSchema>>")]
|
||||
pub mitm_hooks: Option<Vec<MitmHookConfig>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
|
||||
@@ -82,6 +86,46 @@ enum NetworkModeSchema {
|
||||
Full,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
|
||||
#[serde(default)]
|
||||
struct MitmHookConfigSchema {
|
||||
pub host: String,
|
||||
#[serde(rename = "match", default)]
|
||||
pub matcher: MitmHookMatchConfigSchema,
|
||||
#[serde(default)]
|
||||
pub actions: MitmHookActionsConfigSchema,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
|
||||
#[serde(default)]
|
||||
struct MitmHookMatchConfigSchema {
|
||||
pub methods: Vec<String>,
|
||||
pub path_prefixes: Vec<String>,
|
||||
pub query: BTreeMap<String, Vec<String>>,
|
||||
pub headers: BTreeMap<String, Vec<String>>,
|
||||
pub body: Option<MitmHookBodyConfigSchema>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
|
||||
#[serde(default)]
|
||||
struct MitmHookActionsConfigSchema {
|
||||
pub strip_request_headers: Vec<String>,
|
||||
pub inject_request_headers: Vec<InjectedHeaderConfigSchema>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
|
||||
#[serde(default)]
|
||||
struct InjectedHeaderConfigSchema {
|
||||
pub name: String,
|
||||
pub secret_env_var: Option<String>,
|
||||
pub secret_file: Option<String>,
|
||||
pub prefix: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)]
|
||||
#[serde(transparent)]
|
||||
struct MitmHookBodyConfigSchema(pub serde_json::Value);
|
||||
|
||||
impl NetworkToml {
|
||||
pub(crate) fn apply_to_network_proxy_config(&self, config: &mut NetworkProxyConfig) {
|
||||
if let Some(enabled) = self.enabled {
|
||||
@@ -126,6 +170,12 @@ impl NetworkToml {
|
||||
if let Some(allow_local_binding) = self.allow_local_binding {
|
||||
config.network.allow_local_binding = allow_local_binding;
|
||||
}
|
||||
if let Some(mitm) = self.mitm {
|
||||
config.network.mitm = mitm;
|
||||
}
|
||||
if let Some(mitm_hooks) = self.mitm_hooks.as_ref() {
|
||||
config.network.mitm_hooks = mitm_hooks.clone();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_network_proxy_config(&self) -> NetworkProxyConfig {
|
||||
|
||||
@@ -319,7 +319,16 @@ mod tests {
|
||||
default_permissions = "workspace"
|
||||
|
||||
[permissions.workspace.network]
|
||||
mode = "full"
|
||||
mitm = true
|
||||
allowed_domains = ["lower.example.com"]
|
||||
|
||||
[[permissions.workspace.network.mitm_hooks]]
|
||||
host = "lower.example.com"
|
||||
|
||||
[permissions.workspace.network.mitm_hooks.match]
|
||||
methods = ["POST"]
|
||||
path_prefixes = ["/repos/openai/"]
|
||||
"#,
|
||||
)
|
||||
.expect("lower layer should parse");
|
||||
@@ -328,7 +337,16 @@ allowed_domains = ["lower.example.com"]
|
||||
default_permissions = "workspace"
|
||||
|
||||
[permissions.workspace.network]
|
||||
mode = "full"
|
||||
mitm = true
|
||||
allowed_domains = ["higher.example.com"]
|
||||
|
||||
[[permissions.workspace.network.mitm_hooks]]
|
||||
host = "api.github.com"
|
||||
|
||||
[permissions.workspace.network.mitm_hooks.match]
|
||||
methods = ["PUT"]
|
||||
path_prefixes = ["/repos/openai/"]
|
||||
"#,
|
||||
)
|
||||
.expect("higher layer should parse");
|
||||
@@ -346,6 +364,11 @@ allowed_domains = ["higher.example.com"]
|
||||
.expect("higher layer should apply");
|
||||
|
||||
assert_eq!(config.network.allowed_domains, vec!["higher.example.com"]);
|
||||
assert_eq!(config.network.mode, codex_network_proxy::NetworkMode::Full);
|
||||
assert!(config.network.mitm);
|
||||
assert_eq!(config.network.mitm_hooks.len(), 1);
|
||||
assert_eq!(config.network.mitm_hooks[0].host, "api.github.com");
|
||||
assert_eq!(config.network.mitm_hooks[0].matcher.methods, vec!["PUT"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user