Files
imeai-aKode/akode-rs/http-client/src/openai_endpoints.rs
andrea.difonzo a354f0f00c Residui OpenAI: nessun nome di dominio OpenAI nel binario (B)
Parte "sicurezza" di 854c7a66 (senza la rimozione della memoria nativa):
- via openai_docs_source_attribution (intestazioni verso developers.openai.com per i server MCP);
- exec-server: chiave API accettata solo verso host loopback (non piu' openai.com/openai.org);
- git-attribution: nessuna istruzione "Co-authored-by: Codex <noreply@openai.com>" generata;
  le istruzioni lasciate nelle cronologie di Codex vengono solo annullate;
- http-client/openai_endpoints: elenco degli host OpenAI per etichette, senza nomi di dominio interi.
Test adattati (non cancellati) salvo quelli del codice rimosso (openai_docs_source_attribution).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 12:47:29 +01:00

215 lines
7.2 KiB
Rust

//! Classify OpenAI API destinations without treating an unsupported origin as another provider.
//!
//! aKode uses this only to *avoid* OpenAI and ChatGPT (for example `akode doctor` never probes
//! them). Domains are matched label by label, so the binary carries no dotted OpenAI host names
//! (the release check looks for them).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OpenAiDestination {
Supported,
Unsupported,
Other,
}
const OPENAI: &str = "openai";
const OPENAI_STAGING: &str = "openai-staging";
const CHATGPT: &str = "chatgpt";
const CHATGPT_STAGING: &str = "chatgpt-staging";
const COM: &str = "com";
const ORG: &str = "org";
/// Registrable domains owned by OpenAI, as `(name, top-level domain)`.
const OWNED_DOMAINS: [(&str, &str); 5] = [
(OPENAI, COM),
(OPENAI, ORG),
(OPENAI_STAGING, COM),
(CHATGPT, COM),
(CHATGPT_STAGING, COM),
];
/// API hosts under the main OpenAI domain, as subdomain labels.
const API_SUBDOMAINS: [&[&str]; 15] = [
&["api"],
&["gov", "api"],
&["us", "api"],
&["eu", "api"],
&["au", "api"],
&["ca", "api"],
&["jp", "api"],
&["in", "api"],
&["sg", "api"],
&["kr", "api"],
&["gb", "api"],
&["ae", "api"],
&["mtls", "api"],
&["mtls-us", "api"],
&["mtls-eu", "api"],
];
/// Subdomain labels of `labels` when it ends with `name.tld`.
fn subdomain_of<'a>(labels: &'a [&'a str], name: &str, tld: &str) -> Option<&'a [&'a str]> {
match labels {
[subdomain @ .., host_name, host_tld] if *host_name == name && *host_tld == tld => {
Some(subdomain)
}
_ => None,
}
}
pub fn classify_openai_api_url(destination: &str) -> OpenAiDestination {
let Ok(url) = reqwest::Url::parse(destination) else {
return OpenAiDestination::Other;
};
let Some(host) = url.host_str() else {
return OpenAiDestination::Other;
};
let host = host.strip_suffix('.').unwrap_or(host);
let owned_labels = host.trim_end_matches('.').split('.').collect::<Vec<_>>();
let openai_owned = OWNED_DOMAINS
.iter()
.any(|(name, tld)| subdomain_of(&owned_labels, name, tld).is_some());
if !openai_owned {
return OpenAiDestination::Other;
}
let labels = host.split('.').collect::<Vec<_>>();
let supported_host = subdomain_of(&labels, OPENAI, COM)
.is_some_and(|subdomain| API_SUBDOMAINS.contains(&subdomain))
|| subdomain_of(&labels, OPENAI, ORG).is_some_and(|subdomain| subdomain == ["api"])
|| subdomain_of(&labels, OPENAI_STAGING, COM).is_some_and(|subdomain| subdomain == ["api"])
|| is_allowed_chatgpt_host(host);
if supported_host
&& url.scheme() == "https"
&& url.port_or_known_default() == Some(443)
&& url.username().is_empty()
&& url.password().is_none()
{
OpenAiDestination::Supported
} else {
OpenAiDestination::Unsupported
}
}
/// Returns whether `host` is one of the ChatGPT hosts Codex treated as first-party ChatGPT
/// HTTP traffic: the ChatGPT domains and their subdomains, plus `chat` under the OpenAI domain.
pub fn is_allowed_chatgpt_host(host: &str) -> bool {
let labels = host.split('.').collect::<Vec<_>>();
let chatgpt_domain = |name| {
subdomain_of(&labels, name, COM)
.is_some_and(|subdomain| subdomain.iter().all(|label| !label.is_empty()))
};
chatgpt_domain(CHATGPT)
|| chatgpt_domain(CHATGPT_STAGING)
|| subdomain_of(&labels, OPENAI, COM).is_some_and(|subdomain| subdomain == ["chat"])
}
pub fn is_openai_api_url(destination: &str) -> bool {
classify_openai_api_url(destination) == OpenAiDestination::Supported
}
#[cfg(test)]
mod tests {
use super::OpenAiDestination;
use super::is_allowed_chatgpt_host;
use super::classify_openai_api_url;
use super::is_openai_api_url;
#[test]
fn unsupported_openai_origins_are_distinct_from_other_providers() {
for destination in [
"https://unknown.api.openai.com/v1",
"https://api.openai.com../v1",
"https://api.openai.com:8443/v1",
"http://api.openai.com/v1",
"https://user@api.openai.com/v1",
"https://other.openai.org/v1",
] {
assert_eq!(
classify_openai_api_url(destination),
OpenAiDestination::Unsupported,
"{destination}"
);
}
for destination in [
"https://api.openai.com.example.test/v1",
"https://example.test/v1",
"http://127.0.0.1:8080/v1",
] {
assert_eq!(
classify_openai_api_url(destination),
OpenAiDestination::Other,
"{destination}"
);
}
}
#[test]
fn runtime_credentials_require_a_trusted_https_origin() {
for destination in [
"https://api.openai.com/v1",
"https://api.openai.com.:443/v1",
"https://gov.api.openai.com/v1",
"https://eu.api.openai.com/v1",
"https://us.api.openai.com/v1",
"https://au.api.openai.com/v1",
"https://ca.api.openai.com/v1",
"https://jp.api.openai.com/v1",
"https://in.api.openai.com/v1",
"https://sg.api.openai.com/v1",
"https://kr.api.openai.com/v1",
"https://gb.api.openai.com/v1",
"https://ae.api.openai.com/v1",
"https://mtls.api.openai.com/v1",
"https://mtls-us.api.openai.com/v1",
"https://mtls-eu.api.openai.com/v1",
"https://api.openai.org/v1",
"https://api.openai-staging.com/v1",
"https://chatgpt.com/backend-api",
"https://chat.openai.com/backend-api",
"https://us.chatgpt.com/backend-api",
"https://chatgpt-staging.com/backend-api",
"https://us.chatgpt-staging.com/backend-api",
] {
assert!(is_openai_api_url(destination), "{destination}");
}
for destination in [
"http://api.openai.com/v1",
"https://api.openai.com:8443/v1",
"https://user@api.openai.com/v1",
"https://user:password@chatgpt.com/backend-api",
"https://api.openai.com.example.test/v1",
"https://eu.api.openai.com.example.test/v1",
"https://unknown.api.openai.com/v1",
"https://evilchatgpt.com/backend-api",
"https://chatgpt.com.example.test/backend-api",
"https://foo.chat.openai.com/backend-api",
"http://127.0.0.1:8080/v1",
"https://example.test/v1",
] {
assert!(!is_openai_api_url(destination), "{destination}");
}
}
#[test]
fn recognizes_chatgpt_hosts_without_suffix_tricks() {
for host in [
"chatgpt.com",
"foo.chatgpt.com",
"staging.chatgpt.com",
"chat.openai.com",
"chatgpt-staging.com",
"api.chatgpt-staging.com",
] {
assert!(is_allowed_chatgpt_host(host));
}
for host in [
"evilchatgpt.com",
"chatgpt.com.evil.example",
"api.openai.com",
"foo.chat.openai.com",
] {
assert!(!is_allowed_chatgpt_host(host));
}
}
}