From ad18efa84b369a71aba2bbd346d5b6eb2474923c Mon Sep 17 00:00:00 2001 From: yujonglee Date: Mon, 16 Nov 2026 18:15:47 +0900 Subject: [PATCH] feat(fnmatch): add a crate for Python's fnmatchcase glob dialect (#51530) Operator config already holds glob patterns that Python LiteLLM matches with fnmatch.fnmatchcase, such as JWT role maps and JWT routing selectors. Rust code that reads those patterns needs the same dialect, since another glob crate would change what an existing config matches. litellm-fnmatch parses a pattern into typed segments and matches in time linear in the name, with no dependencies and no regex. Every expected value in its tests comes from running fnmatchcase on CPython, apart from one pinned divergence on a negating "!" after a reversed range. --- litellm/rust/Cargo.lock | 7 + litellm/rust/Cargo.toml | 1 + litellm/rust/crates/fnmatch/AGENTS.md | 8 + litellm/rust/crates/fnmatch/Cargo.toml | 9 + litellm/rust/crates/fnmatch/src/lib.rs | 6 + litellm/rust/crates/fnmatch/src/parse.rs | 103 +++++ litellm/rust/crates/fnmatch/src/pattern.rs | 487 +++++++++++++++++++++ 7 files changed, 621 insertions(+) create mode 100644 litellm/rust/crates/fnmatch/AGENTS.md create mode 100644 litellm/rust/crates/fnmatch/Cargo.toml create mode 100644 litellm/rust/crates/fnmatch/src/lib.rs create mode 100644 litellm/rust/crates/fnmatch/src/parse.rs create mode 100644 litellm/rust/crates/fnmatch/src/pattern.rs diff --git a/litellm/rust/Cargo.lock b/litellm/rust/Cargo.lock index dc09a8c06a0..c72f58338c0 100644 --- a/litellm/rust/Cargo.lock +++ b/litellm/rust/Cargo.lock @@ -3946,6 +3946,13 @@ dependencies = [ "thiserror 2.0.19", ] +[[package]] +name = "litellm-fnmatch" +version = "0.1.0" +dependencies = [ + "rstest", +] + [[package]] name = "litellm-framer" version = "0.1.0" diff --git a/litellm/rust/Cargo.toml b/litellm/rust/Cargo.toml index 1585150e725..9a3c996c86e 100644 --- a/litellm/rust/Cargo.toml +++ b/litellm/rust/Cargo.toml @@ -41,6 +41,7 @@ litellm-callbacks-python = { path = "crates/callbacks-python" } litellm-callbacks-budgets = { path = "crates/callbacks-budgets" } litellm-callbacks-s3-log = { path = "crates/callbacks-s3-log" } litellm-framer = { path = "crates/framer" } +litellm-fnmatch = { path = "crates/fnmatch" } litellm-auth = { path = "crates/auth" } litellm-auth-types = { path = "crates/auth-types" } litellm-secret-value = { path = "crates/secret-value" } diff --git a/litellm/rust/crates/fnmatch/AGENTS.md b/litellm/rust/crates/fnmatch/AGENTS.md new file mode 100644 index 00000000000..aef2cd44a76 --- /dev/null +++ b/litellm/rust/crates/fnmatch/AGENTS.md @@ -0,0 +1,8 @@ +# fnmatch + +The glob dialect of Python's `fnmatch.fnmatchcase`, for config patterns Python LiteLLM already matches with it, such as JWT role maps, JWT routing selectors and cost discount model patterns. A crate that reads one of those patterns depends on this crate and never on another glob crate, whose dialect would change what an existing config matches + +- No dependencies and no regex. A pattern parses into typed segments, and matching stays linear in the name for any number of `*` +- Every case outside `bang_after_a_reversed_range_is_a_member` takes its expected value from running `fnmatch.fnmatchcase(name, pattern)` on CPython, never from reasoning. Run it before adding or changing a case. `matches_like_cpython` holds the dialect's syntax, and the tests after it hold the patterns each config surface really sees +- There is one divergence, pinned by its own test. CPython looks for the negating `!` after it has dropped reversed ranges, so `[z-a!b]` means "not b" there. Here `!` negates only as the first character of a class +- `fnmatch.fnmatch` also runs `os.path.normcase` on both sides, which does nothing on POSIX. A caller that needs it folds case itself diff --git a/litellm/rust/crates/fnmatch/Cargo.toml b/litellm/rust/crates/fnmatch/Cargo.toml new file mode 100644 index 00000000000..637c104208b --- /dev/null +++ b/litellm/rust/crates/fnmatch/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "litellm-fnmatch" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true + +[dev-dependencies] +rstest.workspace = true diff --git a/litellm/rust/crates/fnmatch/src/lib.rs b/litellm/rust/crates/fnmatch/src/lib.rs new file mode 100644 index 00000000000..d2a0f8a7479 --- /dev/null +++ b/litellm/rust/crates/fnmatch/src/lib.rs @@ -0,0 +1,6 @@ +#![forbid(unsafe_code)] + +mod parse; +mod pattern; + +pub use pattern::{Pattern, fnmatchcase}; diff --git a/litellm/rust/crates/fnmatch/src/parse.rs b/litellm/rust/crates/fnmatch/src/parse.rs new file mode 100644 index 00000000000..ab4ef567648 --- /dev/null +++ b/litellm/rust/crates/fnmatch/src/parse.rs @@ -0,0 +1,103 @@ +use std::ops::RangeInclusive; + +#[derive(Clone, Debug)] +pub(crate) enum Single { + Any, + Char(char), + Class { + negated: bool, + ranges: Box<[RangeInclusive]>, + }, +} + +impl Single { + pub(crate) fn matches(&self, c: char) -> bool { + match self { + Self::Any => true, + Self::Char(expected) => c == *expected, + Self::Class { negated, ranges } => { + ranges.iter().any(|range| range.contains(&c)) != *negated + } + } + } +} + +pub(crate) enum Token { + Star, + Single(Single), +} + +impl Token { + pub(crate) fn is_star(&self) -> bool { + matches!(self, Self::Star) + } + + pub(crate) fn single(&self) -> Option<&Single> { + match self { + Self::Star => None, + Self::Single(single) => Some(single), + } + } +} + +pub(crate) fn tokens(pattern: &[char]) -> impl Iterator { + let mut rest = pattern; + std::iter::from_fn(move || { + let (token, after) = next_token(rest)?; + rest = after; + Some(token) + }) +} + +fn next_token(pattern: &[char]) -> Option<(Token, &[char])> { + let (first, rest) = pattern.split_first()?; + Some(match first { + '*' => (Token::Star, rest), + '?' => (Token::Single(Single::Any), rest), + '[' => match class_end(rest) { + Some(end) => (Token::Single(class(&rest[..end])), &rest[end + 1..]), + None => (Token::Single(Single::Char('[')), rest), + }, + other => (Token::Single(Single::Char(*other)), rest), + }) +} + +/// Where the `]` closing a class sits in the text after its `[`. A `]` that comes first, or first +/// after a negating `!`, is a member. +fn class_end(after_bracket: &[char]) -> Option { + let skip = match after_bracket { + ['!', ']', ..] => 2, + ['!' | ']', ..] => 1, + _ => 0, + }; + after_bracket[skip..] + .iter() + .position(|c| *c == ']') + .map(|at| skip + at) +} + +fn class(body: &[char]) -> Single { + let (negated, members) = match body { + ['!', members @ ..] => (true, members), + members => (false, members), + }; + Single::Class { + negated, + ranges: ranges(members), + } +} + +/// `lo-hi` is a range and any other character stands for itself. A reversed range holds nothing. +fn ranges(members: &[char]) -> Box<[RangeInclusive]> { + let mut rest = members; + std::iter::from_fn(|| { + let (range, after) = match rest { + [lo, '-', hi, after @ ..] => (*lo..=*hi, after), + [c, after @ ..] => (*c..=*c, after), + [] => return None, + }; + rest = after; + Some(range) + }) + .collect() +} diff --git a/litellm/rust/crates/fnmatch/src/pattern.rs b/litellm/rust/crates/fnmatch/src/pattern.rs new file mode 100644 index 00000000000..418be676073 --- /dev/null +++ b/litellm/rust/crates/fnmatch/src/pattern.rs @@ -0,0 +1,487 @@ +use crate::parse::{Single, Token, tokens}; + +#[derive(Clone, Debug)] +struct Segment(Box<[Single]>); + +impl Segment { + fn new(run: &[Token]) -> Self { + Self(run.iter().filter_map(Token::single).cloned().collect()) + } + + fn len(&self) -> usize { + self.0.len() + } + + fn matches(&self, window: &[char]) -> bool { + self.len() == window.len() + && self + .0 + .iter() + .zip(window) + .all(|(single, c)| single.matches(*c)) + } + + /// What is left of `haystack` after the leftmost place this segment matches. + fn skip_first_match<'a>(&self, haystack: &'a [char]) -> Option<&'a [char]> { + let last_start = haystack.len().checked_sub(self.len())?; + (0..=last_start) + .find(|start| self.matches(&haystack[*start..start + self.len()])) + .map(|start| &haystack[start + self.len()..]) + } +} + +#[derive(Clone, Debug)] +enum Matcher { + Exact(Segment), + Wild { + prefix: Segment, + middles: Box<[Segment]>, + suffix: Segment, + }, +} + +/// A pattern in the dialect of Python's `fnmatch.fnmatchcase`, compiled once. +/// +/// `*` matches any run of characters, `?` any one character, `[seq]` one character of a set that +/// may hold `lo-hi` ranges, and `[!seq]` one character outside it. Nothing escapes, so a class is +/// the only way to match a literal `*`, `?` or `[`. Every string is a pattern, and a `[` that no +/// `]` closes is a literal. +#[derive(Clone, Debug)] +pub struct Pattern(Matcher); + +impl Pattern { + pub fn new(pattern: &str) -> Self { + let chars: Vec = pattern.chars().collect(); + let tokens: Vec = tokens(&chars).collect(); + let runs: Vec<&[Token]> = tokens.split(Token::is_star).collect(); + Self(match runs.as_slice() { + [prefix, middles @ .., suffix] => Matcher::Wild { + prefix: Segment::new(prefix), + middles: middles.iter().map(|run| Segment::new(run)).collect(), + suffix: Segment::new(suffix), + }, + _ => Matcher::Exact(Segment::new(&tokens)), + }) + } + + pub fn matches(&self, name: &str) -> bool { + let name: Vec = name.chars().collect(); + match &self.0 { + Matcher::Exact(segment) => segment.matches(&name), + Matcher::Wild { + prefix, + middles, + suffix, + } => { + let Some(free) = name.len().checked_sub(prefix.len() + suffix.len()) else { + return false; + }; + let (head, rest) = name.split_at(prefix.len()); + let (between, tail) = rest.split_at(free); + prefix.matches(head) + && suffix.matches(tail) + && middles + .iter() + .try_fold(between, |rest, middle| middle.skip_first_match(rest)) + .is_some() + } + } + } +} + +/// Python's `fnmatch.fnmatchcase(name, pattern)`. +pub fn fnmatchcase(name: &str, pattern: &str) -> bool { + Pattern::new(pattern).matches(name) +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + /// Every expected value here and in the use-case tests below is what + /// `fnmatch.fnmatchcase(name, pattern)` returned on CPython 3.12.13 and 3.14.7 on 2026-11-12. + #[rstest] + #[case::literal("admin", "admin", true)] + #[case::literal_is_case_sensitive("Admin", "admin", false)] + #[case::literal_needs_whole_name("admin-eu", "admin", false)] + #[case::literal_needs_whole_pattern("adm", "admin", false)] + #[case::empty_pattern_matches_empty_name("", "", true)] + #[case::empty_pattern_rejects_a_name("a", "", false)] + #[case::trailing_newline_is_not_ignored("a\n", "a", false)] + #[case::dot_is_literal("axb", "a.b", false)] + #[case::dot_matches_itself("a.b", "a.b", true)] + #[case::plus_is_literal("aa", "a+", false)] + #[case::parens_and_dollar_are_literal("(a)$", "(a)$", true)] + #[case::braces_are_literal("{a,b}", "{a,b}", true)] + #[case::braces_are_not_alternation("a", "{a,b}", false)] + #[case::backslash_is_not_an_escape("a*", "a\\*", false)] + #[case::backslash_matches_itself("a\\b", "a\\*", true)] + #[case::question("ab", "a?", true)] + #[case::question_needs_one("a", "a?", false)] + #[case::question_takes_only_one("abc", "a?", false)] + #[case::question_matches_newline("a\nb", "a?b", true)] + #[case::question_matches_slash("a/b", "a?b", true)] + #[case::question_counts_chars_not_bytes("aéb", "a?b", true)] + #[case::star_alone_matches_empty("", "*", true)] + #[case::star_alone_matches_anything("any thing/at\nall", "*", true)] + #[case::star_suffix("admin-eu", "admin-*", true)] + #[case::star_suffix_matches_empty("admin-", "admin-*", true)] + #[case::star_suffix_needs_prefix("user-eu", "admin-*", false)] + #[case::star_prefix("eu-admin", "*-admin", true)] + #[case::star_prefix_needs_suffix("admin", "*x", false)] + #[case::star_middle("a/b/c", "a*c", true)] + #[case::star_middle_matches_empty("ac", "a*c", true)] + #[case::star_spans_newline("a\nc", "a*c", true)] + #[case::star_spans_space("a b c", "a*c", true)] + #[case::double_star_is_one_star("abc", "a**c", true)] + #[case::prefix_and_suffix_cannot_overlap("aba", "ab*ba", false)] + #[case::prefix_and_suffix_may_touch("abba", "ab*ba", true)] + #[case::star_reaches_the_last_occurrence("abcabc", "*abc", true)] + #[case::two_stars_find_middle("mississippi", "*sip*", true)] + #[case::three_stars_in_order("mississippi", "m*ss*ss*pi", true)] + #[case::three_stars_out_of_order("mississippi", "m*pp*ss*i", false)] + #[case::middle_must_fit_between_ends("mississippi", "mi*ssi*ssi*ssippi", false)] + #[case::middle_with_question("a-xyz-b", "a*x?z*b", true)] + #[case::star_then_class("team-7", "*-[0-9]", true)] + #[case::url_wildcards("https://login.example.com/tenant", "https://*.example.com/*", true)] + #[case::url_wildcards_need_host_suffix( + "https://login.example.org/tenant", + "https://*.example.com/*", + false + )] + #[case::scope_wildcard("read:models", "read:*", true)] + #[case::class("b1", "[abc]1", true)] + #[case::class_rejects_non_member("d1", "[abc]1", false)] + #[case::class_takes_one_char("ab", "[abc]", false)] + #[case::class_needs_one_char("", "[abc]", false)] + #[case::range("m", "[a-z]", true)] + #[case::range_is_inclusive_low("a", "[a-z]", true)] + #[case::range_is_inclusive_high("z", "[a-z]", true)] + #[case::range_is_case_sensitive("M", "[a-z]", false)] + #[case::two_ranges("7", "[a-z0-9]", true)] + #[case::range_then_member("_", "[a-z_]", true)] + #[case::unicode_range("é", "[à-ÿ]", true)] + #[case::negated("x", "[!xyz]", false)] + #[case::negated_other("q", "[!xyz]", true)] + #[case::negated_range("m", "[!a-z]", false)] + #[case::negated_range_other("5", "[!a-z]", true)] + #[case::negated_needs_one_char("", "[!a]", false)] + #[case::bang_after_first_is_literal("!", "[a!]", true)] + #[case::double_bang_negates_bang("!", "[!!]", false)] + #[case::double_bang_allows_others("a", "[!!]", true)] + #[case::caret_is_literal("^", "[^a]", true)] + #[case::caret_does_not_negate("b", "[^a]", false)] + #[case::negated_caret("^", "[!^]", false)] + #[case::leading_close_bracket("]", "[]]", true)] + #[case::leading_close_bracket_then_member("a", "[]a]", true)] + #[case::negated_leading_close_bracket("]", "[!]]", false)] + #[case::negated_leading_close_bracket_other("a", "[!]]", true)] + #[case::class_ends_at_first_close_bracket("a]", "[a]]", true)] + #[case::trailing_hyphen_literal("-", "[a-]", true)] + #[case::trailing_hyphen_keeps_member("a", "[a-]", true)] + #[case::leading_hyphen_literal("-", "[-a]", true)] + #[case::negated_leading_hyphen_literal("-", "[!-a]", false)] + #[case::negated_leading_hyphen_is_no_range("#", "[!-a]", true)] + #[case::range_from_hyphen("A", "[--a]", true)] + #[case::range_to_hyphen("+", "[+--]", true)] + #[case::hyphen_after_range_is_literal("-", "[a-c-e]", true)] + #[case::hyphen_after_range_makes_no_second_range("d", "[a-c-e]", false)] + #[case::member_after_range_and_hyphen("e", "[a-c-e]", true)] + #[case::reversed_range_never_matches("m", "[z-a]", false)] + #[case::reversed_range_rejects_its_ends("z", "[z-a]", false)] + #[case::reversed_range_keeps_other_members("b", "[z-ab]", true)] + #[case::negated_reversed_range_matches_any("m", "[!z-a]", true)] + #[case::backslash_in_class_is_literal("\\", "[\\]", true)] + #[case::backslash_is_a_range_bound("m", "[a\\-z]", true)] + #[case::backslash_does_not_escape_hyphen("-", "[a\\-z]", false)] + #[case::open_bracket_in_class("[", "[[]", true)] + #[case::open_bracket_in_class_with_member("a", "[[a]", true)] + #[case::star_in_class_is_literal("*", "[*]", true)] + #[case::star_in_class_matches_no_other("a", "[*]", false)] + #[case::question_in_class_is_literal("a", "[?]", false)] + #[case::set_operators_are_literal("&", "[a&&b]", true)] + #[case::tilde_and_pipe_are_literal("|", "[~|]", true)] + #[case::posix_class_is_not_supported("x", "[[:alpha:]]", false)] + #[case::posix_class_reads_as_members("a]", "[[:alpha:]]", true)] + #[case::unclosed_bracket_literal("[a", "[a", true)] + #[case::unclosed_bracket_matches_no_member("a", "[a", false)] + #[case::empty_brackets_literal("[]", "[]", true)] + #[case::unclosed_negation_literal("[!]", "[!]", true)] + #[case::unclosed_bracket_then_star("[abc", "[a*", true)] + fn matches_like_cpython(#[case] name: &str, #[case] pattern: &str, #[case] expected: bool) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// `jwt_litellm_role_map[].jwt_role` against one role from the token's roles claim. + #[rstest] + #[case::prefix_glob("AI_ADMIN_GLOBAL", "AI_ADMIN_*", true)] + #[case::prefix_glob_matches_empty_tail("AI_ADMIN_", "AI_ADMIN_*", true)] + #[case::prefix_glob_needs_the_separator("AI_ADMIN", "AI_ADMIN_*", false)] + #[case::prefix_glob_is_anchored_at_start("XAI_ADMIN_EU", "AI_ADMIN_*", false)] + #[case::prefix_glob_is_case_sensitive("ai_admin_global", "AI_ADMIN_*", false)] + #[case::exact_role("AI_USER", "AI_USER", true)] + #[case::exact_role_rejects_longer_role("AI_USER_EXTERNAL", "AI_USER", false)] + #[case::catch_all("AI_VIEWER", "*", true)] + #[case::catch_all_matches_empty_role("", "*", true)] + #[case::prefix_glob_rejects_empty_role("", "AI_*", false)] + #[case::role_with_spaces("AI Admin Team", "AI Admin*", true)] + #[case::group_path("/litellm/admins/eu", "/litellm/admins/*", true)] + #[case::group_path_other_group("/litellm/users/eu", "/litellm/admins/*", false)] + #[case::group_path_star_spans_slashes("/litellm/admins/eu/west", "/litellm/*/west", true)] + #[case::urn_role("urn:litellm:role:admin", "urn:litellm:role:*", true)] + #[case::guid_role( + "0f8fad5b-d9cb-469f-a165-70867728950e", + "0f8fad5b-d9cb-469f-a165-70867728950e", + true + )] + #[case::email_domain("admin@example.com", "*@example.com", true)] + #[case::email_other_domain("admin@example.org", "*@example.com", false)] + #[case::dotted_role("litellm.admin", "litellm.*", true)] + #[case::dot_is_not_any_char("litellmXadmin", "litellm.admin", false)] + #[case::one_char_suffix("AI_ADMIN_1", "AI_ADMIN_?", true)] + #[case::one_char_suffix_rejects_two("AI_ADMIN_10", "AI_ADMIN_?", false)] + #[case::class_of_teams("team-a-admin", "team-[ab]-admin", true)] + #[case::class_of_teams_rejects_other("team-c-admin", "team-[ab]-admin", false)] + #[case::bracketed_role_is_a_class_not_itself("[Admin]", "[Admin]", false)] + #[case::bracketed_role_matches_one_member("A", "[Admin]", true)] + #[case::glob_between_literals("AI_eu_ADMIN", "AI_*_ADMIN", true)] + fn jwt_role_map_patterns(#[case] name: &str, #[case] pattern: &str, #[case] expected: bool) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// `routing_overrides` `iss`, `client_id`, `aud` and `scope` selectors against one claim value. The + /// caller sends a selector here only when it holds `*` or `?`, and rejects whitespace in a + /// non-scope claim before it does. + #[rstest] + #[case::client_id_suffix("svc-LITELLM", "*LITELLM", true)] + #[case::client_id_suffix_alone("LITELLM", "*LITELLM", true)] + #[case::client_id_suffix_is_case_sensitive("svc-litellm", "*LITELLM", false)] + #[case::client_id_suffix_is_anchored_at_end("LITELLM-svc", "*LITELLM", false)] + #[case::client_id_guid_prefix("0f8fad5b-d9cb-469f-a165-70867728950e", "0f8fad5b-*", true)] + #[case::issuer_tenant_segment( + "https://login.example.com/9188040d-6c67/v2.0", + "https://login.example.com/*/v2.0", + true + )] + #[case::issuer_tenant_other_version( + "https://login.example.com/9188040d-6c67/v1.0", + "https://login.example.com/*/v2.0", + false + )] + #[case::issuer_dot_is_literal( + "https://login.example.com/9188040d-6c67/v2x0", + "https://login.example.com/*/v2.0", + false + )] + #[case::issuer_subdomain("https://eu.idp.example.com", "https://*.idp.example.com", true)] + #[case::issuer_subdomain_needs_a_label_dot( + "https://idp.example.com", + "https://*.idp.example.com", + false + )] + #[case::issuer_subdomain_star_spans_slash( + "https://evil.test/.idp.example.com", + "https://*.idp.example.com", + true + )] + #[case::issuer_trailing_star_admits_longer_host( + "https://idp.example.com.evil.test", + "https://idp.example.com*", + true + )] + #[case::issuer_path_star( + "https://idp.example.com/realms/prod", + "https://idp.example.com/*", + true + )] + #[case::issuer_path_star_matches_bare_slash( + "https://idp.example.com/", + "https://idp.example.com/*", + true + )] + #[case::issuer_path_star_needs_the_slash( + "https://idp.example.com", + "https://idp.example.com/*", + false + )] + #[case::issuer_port_and_realm( + "https://idp.example.com:8443/realms/prod", + "https://idp.example.com:*/realms/*", + true + )] + #[case::issuer_scheme_is_literal( + "http://idp.example.com/realms/prod", + "https://idp.example.com/*", + false + )] + #[case::issuer_one_char("https://idp-1.example.com", "https://idp-?.example.com", true)] + #[case::issuer_one_char_rejects_two( + "https://idp-10.example.com", + "https://idp-?.example.com", + false + )] + #[case::query_mark_matches_itself( + "https://idp.example.com/auth?tenant=a", + "https://idp.example.com/auth?tenant=a", + true + )] + #[case::query_mark_matches_any_char( + "https://idp.example.com/auth/tenant=a", + "https://idp.example.com/auth?tenant=a", + true + )] + #[case::ipv6_host_is_a_class("https://[::1]:8443/realms/prod", "https://[::1]:8443/*", false)] + #[case::ipv6_host_class_takes_one_member( + "https://::8443/realms/prod", + "https://[::1]:8443/*", + true + )] + #[case::star_spans_whitespace("trusted.example.com evil.test", "trusted.*", true)] + #[case::scope_prefix("App:LiteLLM", "App:*", true)] + #[case::scope_prefix_other_app("Api:LiteLLM", "App:*", false)] + #[case::scope_suffix("read:models", "*:models", true)] + #[case::scope_dotted_permission("App:LiteLLM.Read", "App:LiteLLM.*", true)] + #[case::scope_dotted_permission_needs_dot("App:LiteLLM", "App:LiteLLM.*", false)] + #[case::scope_uri_default("api://litellm/.default", "api://litellm/*", true)] + #[case::audience_scheme("api://litellm", "api://*", true)] + #[case::audience_env_suffix("api://litellm-prod", "api://litellm-*", true)] + #[case::audience_env_suffix_other_app("api://other-prod", "api://litellm-*", false)] + fn routing_override_selectors( + #[case] name: &str, + #[case] pattern: &str, + #[case] expected: bool, + ) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// `cost_discount_config` model patterns and wildcard `model_name` routes against a model name. + #[rstest] + #[case::provider_wildcard("openai/gpt-5", "openai/*", true)] + #[case::provider_wildcard_matches_empty_model("openai/", "openai/*", true)] + #[case::provider_wildcard_needs_the_slash("openai", "openai/*", false)] + #[case::provider_wildcard_other_provider("anthropic/claude-sonnet-4-5", "openai/*", false)] + #[case::provider_wildcard_is_case_sensitive("OpenAI/gpt-5", "openai/*", false)] + #[case::provider_wildcard_spans_nested_slashes( + "or-team/openai/gpt-5.4-mini", + "or-team/*", + true + )] + #[case::catch_all("bedrock/us-east-1/anthropic.claude-sonnet-4-5", "*", true)] + #[case::family_prefix("claude-sonnet-4-5@20250929", "claude-*", true)] + #[case::family_prefix_other_family("gemini-2.5-pro", "claude-*", false)] + #[case::family_prefix_without_hyphen("claude-opus-4-1", "claude-opus*", true)] + #[case::narrower_family_prefix("claude-sonnet-4-5", "claude-sonnet-*", true)] + #[case::narrower_family_prefix_other_tier("claude-opus-4-1", "claude-sonnet-*", false)] + #[case::partial_name_prefix( + "openai/gpt-partial-route-9-mini", + "openai/gpt-partial-route-9*", + true + )] + #[case::partial_name_prefix_other_number( + "openai/gpt-partial-route-8", + "openai/gpt-partial-route-9*", + false + )] + #[case::bedrock_vendor_prefix("anthropic.claude-3-5-sonnet-20241022-v2:0", "anthropic.*", true)] + #[case::bedrock_vendor_prefix_rejects_region_profile( + "us.anthropic.claude-sonnet-4-5-20250929-v1:0", + "anthropic.*", + false + )] + #[case::bedrock_any_region_profile( + "us.anthropic.claude-sonnet-4-5-20250929-v1:0", + "*anthropic.claude-*", + true + )] + #[case::bedrock_region_path( + "us-east-1/anthropic.claude-3-haiku-20240307-v1:0", + "us-east-1/anthropic.claude-*", + true + )] + #[case::bedrock_other_region_path( + "eu-west-1/anthropic.claude-3-haiku-20240307-v1:0", + "us-east-1/anthropic.claude-*", + false + )] + #[case::bedrock_dotted_family("meta.llama3-70b-instruct-v1:0", "meta.llama3-*", true)] + #[case::vertex_version_suffix("claude-3-5-haiku@20241022", "claude-3-5-haiku@*", true)] + #[case::any_vendor_fixed_model("openai/auto", "*/auto", true)] + #[case::any_vendor_fixed_model_needs_vendor("auto", "*/auto", false)] + #[case::fine_tune_id("ft:gpt-4o-mini:my-org::abc123", "ft:gpt-4o-mini:*", true)] + #[case::double_colon_separators("openai/fo::a::static::b", "openai/fo::*::static::*", true)] + #[case::double_colon_separators_need_both_colons( + "openai/fo::a:static::b", + "openai/fo::*::static::*", + false + )] + #[case::at_least_one_char_after_prefix("gemini-2.5-flash", "gemini-?*", true)] + #[case::at_least_one_char_after_prefix_rejects_none("gemini-", "gemini-?*", false)] + #[case::minor_version_one_char("gemini-2.5-pro", "gemini-2.?-pro", true)] + #[case::minor_version_one_char_rejects_two("gemini-2.10-pro", "gemini-2.?-pro", false)] + #[case::major_version_class("gpt-4o-mini", "gpt-[45]*", true)] + #[case::major_version_class_rejects_other("gpt-3.5-turbo", "gpt-[45]*", false)] + #[case::letter_class_then_star("claude-sonnet", "claude-[abcdefghijklmnopqrstuvwxyz]*", true)] + #[case::letter_class_rejects_digit( + "claude-3-opus", + "claude-[abcdefghijklmnopqrstuvwxyz]*", + false + )] + #[case::digit_range_then_star("o3-mini", "o[0-9]*", true)] + #[case::digit_range_rejects_letter("omni-moderation", "o[0-9]*", false)] + #[case::unclosed_bracket_is_literal("weird[", "weird[", true)] + #[case::unclosed_negation_is_literal("gemini-[!]-pro", "gemini-[!]*", true)] + #[case::unclosed_negation_rejects_plain_name("gemini-pro", "gemini-[!]*", false)] + #[case::stray_close_bracket_is_literal("a]x", "a]*", true)] + #[case::class_holding_close_bracket("a]pro", "a[]x]*", true)] + #[case::class_holding_close_bracket_other_member("axpro", "a[]x]*", true)] + #[case::class_holding_close_bracket_non_member("abpro", "a[]x]*", false)] + #[case::regex_group_is_literal("gpt-5", "(.*)", false)] + #[case::regex_group_matches_only_its_own_shape("(.x)", "(.*)", true)] + #[case::regex_route_is_literal("openai/gpt-5", "openai/(.*)", false)] + fn model_name_patterns(#[case] name: &str, #[case] pattern: &str, #[case] expected: bool) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// Header allowlist globs against a header name the caller has lowercased. + #[rstest] + #[case::vendor_header("x-stainless-arch", "x-stainless-*", true)] + #[case::vendor_header_bare_prefix("x-stainless-", "x-stainless-*", true)] + #[case::vendor_header_needs_hyphen("x-stainless", "x-stainless-*", false)] + #[case::litellm_header("x-litellm-trace-id", "x-litellm-*", true)] + #[case::prefix_is_anchored_at_start("my-x-litellm-trace-id", "x-litellm-*", false)] + #[case::other_header("authorization", "x-litellm-*", false)] + #[case::caller_must_lowercase_the_name("X-Stainless-Arch", "x-stainless-*", false)] + fn header_allowlist_patterns( + #[case] name: &str, + #[case] pattern: &str, + #[case] expected: bool, + ) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// The one divergence. CPython looks for the negating `!` after it has dropped reversed ranges, + /// so it answers the opposite for each of these. + #[rstest] + #[case::does_not_become_match_any("a", "[z-a!]", false)] + #[case::does_not_negate_what_follows("b", "[z-a!b]", true)] + #[case::does_not_admit_non_members("a", "[z-a!b]", false)] + fn bang_after_a_reversed_range_is_a_member( + #[case] name: &str, + #[case] pattern: &str, + #[case] expected: bool, + ) { + assert_eq!(fnmatchcase(name, pattern), expected); + } + + /// Retrying every start per `*` would need about 2000^20 steps here. + #[rstest] + #[case::no_match("", false)] + #[case::match_at_the_end("b", true)] + fn many_stars_on_a_long_name_finish(#[case] ending: &str, #[case] expected: bool) { + let name = format!("{}{ending}", "a".repeat(2000)); + let pattern = format!("{}b", "*a".repeat(20)); + assert_eq!(fnmatchcase(&name, &pattern), expected); + } +}