mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-18 00:07:02 +00:00
fix(openai): replace stale passive rate-limit snapshots
Treat each passive response-header observation as a complete rate-limit bucket snapshot, preventing stale secondary windows from surviving into a newer primary-only update.\n\nExtract duplicate OAuth callback parameter rejection to keep the release quality gate warning-free. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -635,16 +635,8 @@ fn callback_parts(value: &str) -> Result<(SecretString, SecretString), CodexOAut
|
||||
let mut state = None;
|
||||
for (key, value) in url.query_pairs() {
|
||||
match key.as_ref() {
|
||||
"code" => {
|
||||
if code.replace(value.into_owned()).is_some() {
|
||||
return Err(CodexOAuthAdminError::UpstreamRejected);
|
||||
}
|
||||
}
|
||||
"state" => {
|
||||
if state.replace(value.into_owned()).is_some() {
|
||||
return Err(CodexOAuthAdminError::UpstreamRejected);
|
||||
}
|
||||
}
|
||||
"code" => set_unique_callback_parameter(&mut code, value.into_owned())?,
|
||||
"state" => set_unique_callback_parameter(&mut state, value.into_owned())?,
|
||||
// OAuth 服务可能附加 `scope`、`iss` 等标准参数。
|
||||
// 固定回调地址与唯一的 `code`/`state` 才是安全边界,无关参数不参与校验。
|
||||
_ => {}
|
||||
@@ -658,6 +650,16 @@ fn callback_parts(value: &str) -> Result<(SecretString, SecretString), CodexOAut
|
||||
}
|
||||
}
|
||||
|
||||
fn set_unique_callback_parameter(
|
||||
target: &mut Option<String>,
|
||||
value: String,
|
||||
) -> Result<(), CodexOAuthAdminError> {
|
||||
if target.replace(value).is_some() {
|
||||
return Err(CodexOAuthAdminError::UpstreamRejected);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn random_secret() -> Result<String, CodexOAuthAdminError> {
|
||||
let mut bytes = [0_u8; 32];
|
||||
getrandom::fill(&mut bytes).map_err(|_| CodexOAuthAdminError::StorageUnavailable)?;
|
||||
|
||||
@@ -973,13 +973,11 @@ fn merge_passive_quota(
|
||||
.and_then(|value| value.as_array().cloned())
|
||||
.unwrap_or_default();
|
||||
for (limit_id, details) in &rate_limits.limits {
|
||||
let Some(rate_limit) = passive_rate_limit_snapshot(details) else {
|
||||
continue;
|
||||
};
|
||||
if Some(limit_id.as_str()) == active_limit {
|
||||
let mut value = quota
|
||||
.remove("rate_limit")
|
||||
.and_then(|value| value.as_object().cloned())
|
||||
.unwrap_or_default();
|
||||
merge_rate_limit_details(&mut value, details);
|
||||
quota.insert("rate_limit".to_owned(), Value::Object(value));
|
||||
quota.insert("rate_limit".to_owned(), Value::Object(rate_limit));
|
||||
continue;
|
||||
}
|
||||
let index = additional.iter().position(|item| {
|
||||
@@ -1000,12 +998,7 @@ fn merge_passive_quota(
|
||||
if let Some(name) = details.limit_name.as_ref() {
|
||||
item.insert("limit_name".to_owned(), Value::String(name.clone()));
|
||||
}
|
||||
let mut value = item
|
||||
.remove("rate_limit")
|
||||
.and_then(|value| value.as_object().cloned())
|
||||
.unwrap_or_default();
|
||||
merge_rate_limit_details(&mut value, details);
|
||||
item.insert("rate_limit".to_owned(), Value::Object(value));
|
||||
item.insert("rate_limit".to_owned(), Value::Object(rate_limit));
|
||||
match index {
|
||||
Some(index) => additional[index] = Value::Object(item),
|
||||
None => additional.push(Value::Object(item)),
|
||||
@@ -1018,12 +1011,14 @@ fn merge_passive_quota(
|
||||
quota
|
||||
}
|
||||
|
||||
fn merge_rate_limit_details(target: &mut Map<String, Value>, details: &RateLimitDetails) {
|
||||
fn passive_rate_limit_snapshot(details: &RateLimitDetails) -> Option<Map<String, Value>> {
|
||||
// 响应头的窗口属于同一次上游观测;跨响应合并会制造不存在的额度窗口。
|
||||
let mut snapshot = Map::new();
|
||||
if let Some(allowed) = details.allowed {
|
||||
target.insert("allowed".to_owned(), Value::Bool(allowed));
|
||||
snapshot.insert("allowed".to_owned(), Value::Bool(allowed));
|
||||
}
|
||||
if let Some(limit_reached) = details.limit_reached {
|
||||
target.insert("limit_reached".to_owned(), Value::Bool(limit_reached));
|
||||
snapshot.insert("limit_reached".to_owned(), Value::Bool(limit_reached));
|
||||
}
|
||||
for (field, window) in [
|
||||
("primary_window", details.primary),
|
||||
@@ -1032,28 +1027,29 @@ fn merge_rate_limit_details(target: &mut Map<String, Value>, details: &RateLimit
|
||||
let Some(window) = window else {
|
||||
continue;
|
||||
};
|
||||
let mut value = target
|
||||
.remove(field)
|
||||
.and_then(|value| value.as_object().cloned())
|
||||
.unwrap_or_default();
|
||||
merge_rate_limit_window(&mut value, window);
|
||||
target.insert(field.to_owned(), Value::Object(value));
|
||||
snapshot.insert(
|
||||
field.to_owned(),
|
||||
Value::Object(passive_rate_limit_window(window)),
|
||||
);
|
||||
}
|
||||
(!snapshot.is_empty()).then_some(snapshot)
|
||||
}
|
||||
|
||||
fn merge_rate_limit_window(target: &mut Map<String, Value>, window: RateLimitWindow) {
|
||||
fn passive_rate_limit_window(window: RateLimitWindow) -> Map<String, Value> {
|
||||
let mut snapshot = Map::new();
|
||||
if let Some(number) = serde_json::Number::from_f64(window.used_percent) {
|
||||
target.insert("used_percent".to_owned(), Value::Number(number));
|
||||
snapshot.insert("used_percent".to_owned(), Value::Number(number));
|
||||
}
|
||||
if let Some(seconds) = window
|
||||
.window_minutes
|
||||
.and_then(|minutes| minutes.checked_mul(60))
|
||||
{
|
||||
target.insert("limit_window_seconds".to_owned(), Value::from(seconds));
|
||||
snapshot.insert("limit_window_seconds".to_owned(), Value::from(seconds));
|
||||
}
|
||||
if let Some(reset_at) = window.reset_at {
|
||||
target.insert("reset_at".to_owned(), Value::from(reset_at));
|
||||
snapshot.insert("reset_at".to_owned(), Value::from(reset_at));
|
||||
}
|
||||
snapshot
|
||||
}
|
||||
|
||||
fn quota_success_availability(
|
||||
|
||||
@@ -361,6 +361,73 @@ async fn passive_rate_limit_headers_update_quota_and_account_state_with_revision
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn passive_rate_limit_headers_replace_stale_secondary_window() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_passive_quota_snapshot").await;
|
||||
let account = store
|
||||
.account("acct_passive_quota_snapshot")
|
||||
.expect("account");
|
||||
let baseline = json!({
|
||||
"rate_limit": {
|
||||
"primary_window": {
|
||||
"used_percent": 91,
|
||||
"reset_at": 1_900_000_000,
|
||||
"limit_window_seconds": 2_592_000
|
||||
},
|
||||
"secondary_window": {
|
||||
"used_percent": 43,
|
||||
"reset_at": 1_900_000_000,
|
||||
"limit_window_seconds": 2_592_000
|
||||
}
|
||||
}
|
||||
});
|
||||
store
|
||||
.compare_and_swap_quota(QuotaObservation {
|
||||
account_id: account.id().clone(),
|
||||
expected_revision: account.revision(),
|
||||
quota: Some(OpaqueProviderData::new(
|
||||
baseline.as_object().expect("quota object").clone(),
|
||||
)),
|
||||
observed_at: Some(SystemTime::now()),
|
||||
})
|
||||
.await
|
||||
.expect("persist baseline quota");
|
||||
let service = quota_service(&store);
|
||||
let headers = vec![
|
||||
("x-codex-active-limit".to_owned(), "codex".to_owned()),
|
||||
("x-codex-primary-used-percent".to_owned(), "92".to_owned()),
|
||||
(
|
||||
"x-codex-primary-window-minutes".to_owned(),
|
||||
"43200".to_owned(),
|
||||
),
|
||||
(
|
||||
"x-codex-primary-reset-at".to_owned(),
|
||||
"1900000000".to_owned(),
|
||||
),
|
||||
];
|
||||
|
||||
assert!(
|
||||
service
|
||||
.synchronize_passive_headers(&account, &headers)
|
||||
.await
|
||||
.expect("passive quota")
|
||||
);
|
||||
let snapshot = service
|
||||
.read_account(account.id())
|
||||
.await
|
||||
.expect("read quota")
|
||||
.expect("quota snapshot");
|
||||
let monthly = snapshot
|
||||
.windows()
|
||||
.iter()
|
||||
.filter(|window| window.kind() == CodexQuotaWindowKind::Monthly)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(monthly.len(), 1);
|
||||
assert_eq!(monthly[0].used_percent(), Some(92.0));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn synchronize_without_accounts_is_a_noop_before_network_io() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
Reference in New Issue
Block a user