Skip to content

Commit 2f6b500

Browse files
fix(pricing): tighten cached body validation
Reject cache entries whose body exceeds the decompressed pricing limit after the ETag framing is removed. Keep endpoint cache checks structural and endpoint-specific so the refresh path performs the full pricing-map load once, while preserving invalid-cache retry behavior. Co-authored-by: wishworldbetter <[email protected]>
1 parent b6f6633 commit 2f6b500

2 files changed

Lines changed: 101 additions & 6 deletions

File tree

‎rust/crates/ccusage-core/src/pricing.rs‎

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,87 @@ impl PricingEndpoint {
7373
}
7474
}
7575

76-
/// Returns whether a body contains usable pricing data for this endpoint.
76+
/// Returns whether a body has a non-empty pricing shape for this endpoint.
7777
pub fn validates(self, json: &str) -> bool {
78-
let mut map = PricingMap::default();
78+
let Ok(Value::Object(entries)) = serde_json::from_str::<Value>(json) else {
79+
return false;
80+
};
81+
7982
match self {
80-
Self::LiteLlm => map.load_json(json) > 0,
81-
Self::ModelsDev => map
82-
.load_models_dev_json_missing(json)
83-
.is_some_and(|loaded_count| loaded_count > 0),
83+
Self::LiteLlm => entries.values().any(litellm_entry_has_required_cost),
84+
Self::ModelsDev => models_dev_object_has_required_shape(&entries),
8485
}
8586
}
8687
}
8788

89+
fn litellm_entry_has_required_cost(value: &Value) -> bool {
90+
let Some(entry) = value.as_object() else {
91+
return false;
92+
};
93+
let full = entry
94+
.get("input_cost_per_token")
95+
.is_some_and(Value::is_number)
96+
&& entry
97+
.get("output_cost_per_token")
98+
.is_some_and(Value::is_number);
99+
let compact = entry.get("i").is_some_and(Value::is_number)
100+
&& entry.get("o").is_some_and(Value::is_number);
101+
full || compact
102+
}
103+
104+
fn models_dev_object_has_required_shape(entries: &serde_json::Map<String, Value>) -> bool {
105+
if entries.values().any(models_dev_entry_has_models_field) {
106+
return entries.values().all(models_dev_provider_has_required_shape)
107+
&& entries.values().any(models_dev_provider_has_required_cost);
108+
}
109+
entries.values().all(models_dev_entry_has_required_cost)
110+
&& entries.values().any(models_dev_entry_has_nonzero_cost)
111+
}
112+
113+
fn models_dev_provider_has_required_shape(value: &Value) -> bool {
114+
let Some(provider) = value.as_object() else {
115+
return false;
116+
};
117+
let Some(models) = provider.get("models").and_then(Value::as_object) else {
118+
return false;
119+
};
120+
models.values().all(Value::is_object)
121+
}
122+
123+
fn models_dev_provider_has_required_cost(value: &Value) -> bool {
124+
value
125+
.as_object()
126+
.and_then(|provider| provider.get("models"))
127+
.and_then(Value::as_object)
128+
.is_some_and(|models| models.values().any(models_dev_model_has_required_cost))
129+
}
130+
131+
fn models_dev_model_has_required_cost(value: &Value) -> bool {
132+
value
133+
.as_object()
134+
.and_then(|model| model.get("cost"))
135+
.and_then(Value::as_object)
136+
.is_some_and(models_dev_cost_has_required_rates)
137+
}
138+
139+
fn models_dev_entry_has_nonzero_cost(value: &Value) -> bool {
140+
value
141+
.as_object()
142+
.and_then(|entry| entry.get("cost"))
143+
.and_then(Value::as_object)
144+
.is_some_and(models_dev_cost_has_required_rates)
145+
}
146+
147+
fn models_dev_cost_has_required_rates(cost: &serde_json::Map<String, Value>) -> bool {
148+
let Some(input) = cost.get("input").and_then(Value::as_f64) else {
149+
return false;
150+
};
151+
let Some(output) = cost.get("output").and_then(Value::as_f64) else {
152+
return false;
153+
};
154+
input != 0.0 || output != 0.0
155+
}
156+
88157
#[derive(Debug, Clone, Copy)]
89158
pub struct Pricing {
90159
pub input: f64,
@@ -2234,10 +2303,15 @@ mod tests {
22342303
r#"{"openai":{"models":{"gpt-test":{"cost":{"input":1.0,"output":2.0}}}}}"#;
22352304

22362305
assert!(PricingEndpoint::LiteLlm.validates(litellm));
2306+
assert!(PricingEndpoint::LiteLlm.validates(r#"{"gpt-test":{"i":1.0,"o":2.0}}"#));
22372307
assert!(!PricingEndpoint::LiteLlm.validates(models_dev));
22382308
assert!(PricingEndpoint::ModelsDev.validates(models_dev));
22392309
assert!(!PricingEndpoint::ModelsDev.validates(litellm));
22402310
assert!(!PricingEndpoint::ModelsDev.validates("{}"));
2311+
assert!(
2312+
!PricingEndpoint::ModelsDev
2313+
.validates(r#"{"openai":{"models":{"free":{"cost":{"input":0.0,"output":0.0}}}}}"#)
2314+
);
22412315
}
22422316

22432317
#[test]

‎rust/crates/ccusage/src/http.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ impl CacheEntry {
179179
return None;
180180
}
181181
let (etag, body) = raw.split_once('\n')?;
182+
if body.len() as u64 > PRICING_FETCH_MAX_BYTES {
183+
return None;
184+
}
182185
let etag = etag.trim();
183186
if etag.is_empty()
184187
|| etag.len() as u64 > CACHE_ETAG_MAX_BYTES
@@ -401,6 +404,24 @@ mod tests {
401404
let _ = fs::remove_dir_all(&dir);
402405
}
403406

407+
#[test]
408+
fn cache_read_rejects_body_over_the_pricing_limit_after_the_etag() {
409+
let dir = unique_test_dir("oversized-body");
410+
let _ = fs::remove_dir_all(&dir);
411+
let entry = CacheEntry::for_url(&dir, "https://example.com/pricing.json");
412+
413+
entry.write("etag", "small body");
414+
let body = "x".repeat((PRICING_FETCH_MAX_BYTES + 1) as usize);
415+
fs::write(&entry.path, format!("etag\n{body}")).expect("overwrite cache");
416+
417+
assert!(
418+
entry.read().is_none(),
419+
"the body limit must not include the ETag allowance"
420+
);
421+
422+
let _ = fs::remove_dir_all(&dir);
423+
}
424+
404425
#[test]
405426
fn cache_writes_replace_the_pair_atomically_and_leave_no_temporary_files() {
406427
let fixture = Fixture::new();

0 commit comments

Comments
 (0)