litellm/litellm-rust/crates/traces/tests/query_access.rs
yujonglee 677205b3f5
fix(proxy): share ownership permissions for spend logs and traces (#44239)
* refactor(proxy): extract shared spend log read policy

* test(proxy): use named bindings for spend scope regression

* test(proxy): reuse existing spend log query harness

* test(proxy): cover spend log permission lookup adoption

* chore(proxy): relocate existing spend query baseline

* refactor(proxy): make scope query returns explicit

* refactor(proxy): inject deferred log permission lookup

* test(proxy): cover teamless management compatibility lookup

* refactor(proxy): compose user and team log grants

* refactor(proxy): share generic authorization composition

* refactor(proxy): compose trace read permissions

* refactor(proxy): centralize spend and trace authorization

* refactor(proxy): strengthen spend and trace scope types

* refactor(proxy): flatten log read scope into owned logs

Replace the AnyOf grant tree with a flat OwnedLogs(user_id, team_ids) scope,
and OwnedTraces(logs, api_key_hash) for traces, since every consumer flattened
the tree back into that shape.

A caller with no user id now gets an empty scope instead of matching ownerless
rows through Prisma's IS NULL. The dead request_id guard in ui_view_spend_logs
is removed, and the management facets inject the log team lookup and reuse
read_scope_sql instead of the list shim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(proxy): run spend scope tests through one SQLite emulator

Replace the string-matching payload emulator and the hand-rolled Prisma where
interpreter with one SQLite helper that runs the real scope SQL. Session scope
tests now go through the endpoint, including the no-user caller that must not
match ownerless rows. Drop duplicated lookup-failure and trace mapping cases.

load_permitted_log_team_ids returns no teams without a database instead of
relying on the resolver's broad except.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(proxy): unify log and trace ownership permissions

* test(tracing): align fixtures with ownership read scopes

* refactor(tracing): align query scopes with row ownership

* refactor(spend): make ownership SQL predicates explicit

* test(spend): validate ownership SQL against PostgreSQL

* docs(traces): drop key-row visibility from query help guide

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(spend): reach the empty-memberships branch in team lookup test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* chore(ui): regenerate dashboard API types

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-03 01:38:54 +00:00

43 lines
2 KiB
Rust

use litellm_traces::{InvalidScope, QueryScope};
use rstest::rstest;
use serde_json::{Value, json};
#[rstest]
#[case::all(json!({"kind": "all"}), true)]
#[case::own_user(json!({"kind": "owned", "user_id": "user", "team_ids": []}), true)]
#[case::permitted_teams(json!({"kind": "owned", "user_id": "", "team_ids": ["team"]}), true)]
#[case::own_user_and_permitted_teams(json!({"kind": "owned", "user_id": "user", "team_ids": ["team"]}), true)]
#[case::no_identity(json!({"kind": "owned", "user_id": "", "team_ids": []}), false)]
#[case::empty_permitted_team(json!({"kind": "owned", "user_id": "user", "team_ids": [""]}), false)]
fn scope_validation_preserves_authorization_and_wire_shape(
#[case] wire: Value,
#[case] valid: bool,
) {
let scope: QueryScope = serde_json::from_value(wire.clone()).unwrap();
assert_eq!(serde_json::to_value(&scope).unwrap(), wire);
match (scope.validate(), valid) {
(Ok(()), true) | (Err(InvalidScope), false) => (),
(result, _) => panic!("unexpected validation: {result:?}"),
}
}
#[rstest]
#[case::unknown_kind(json!({"kind": "unknown"}))]
#[case::unknown_field(json!({"kind": "owned", "user_id": "user", "team_ids": [], "extra": true}))]
#[case::legacy_admin(json!({"kind": "admin"}))]
#[case::legacy_logs(json!({"kind": "logs", "user_id": "user", "team_ids": []}))]
#[case::legacy_team(json!({"kind": "team", "team_id": "team"}))]
#[case::key_scope(json!({"kind": "key", "team_id": "team", "api_key_hash": "key"}))]
#[case::key_grant(json!({"kind": "owned", "user_id": "user", "team_ids": [], "api_key_hash": "key"}))]
fn scope_rejects_invalid_wire_shape(#[case] wire: Value) {
assert!(serde_json::from_value::<QueryScope>(wire).is_err());
}
#[rstest]
fn all_preserves_existing_extra_field_handling() {
let scope: QueryScope =
serde_json::from_value(json!({"kind": "all", "team_id": "ignored"})).unwrap();
assert!(matches!(scope, QueryScope::All));
assert!(scope.validate().is_ok());
assert_eq!(serde_json::to_value(scope).unwrap(), json!({"kind": "all"}));
}