oxedyne/fe2o3/fe2o3_net/tests/llm_key.rs
2.2 KiB, 1 run
created by r939070283:78539, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A key the provider could never accept is caught before it is sent, and a call names its host once. |
| 2 | //! |
| 3 | //! The fault this guards (15 Nov 2026): a site's AI key that was not an OpenRouter key went out as the |
| 4 | //! bearer token, and OpenRouter answered "Missing Authentication header", which named neither. |
| 5 | |
| 6 | use oxedyne_fe2o3_net::{ |
| 7 | http::{ |
| 8 | client::format_request, |
| 9 | header::HttpMethod, |
| 10 | }, |
| 11 | llm::{ |
| 12 | OPENROUTER_KEY_PREFIX, |
| 13 | Provider, |
| 14 | chat_headers, |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | #[test] |
| 19 | fn an_openrouter_key_passes_00() { |
| 20 | assert_eq!(Provider::OpenRouter.key_fault("sk-or-v1-0123456789abcdef"), None); |
| 21 | } |
| 22 | |
| 23 | /// What a browser fills into a key field, and what would split the request's headers, is refused. |
| 24 | #[test] |
| 25 | fn a_passphrase_or_a_broken_token_is_refused_01() { |
| 26 | for k in ["correct horse battery staple", "", " sk-or-v1-abc", "sk-or-v1-a\r\nX-Evil: y", "sk-or-v1-\u{e9}"] { |
| 27 | assert!(Provider::OpenRouter.key_fault(k).is_some(), "accepted {:?}", k); |
| 28 | assert!(Provider::Mistral.key_fault(k).is_some(), "Mistral accepted {:?}", k); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Another provider's key, or a bare word, is no OpenRouter key, and the reason says what one is. |
| 33 | #[test] |
| 34 | fn another_providers_key_is_no_openrouter_key_02() { |
| 35 | for k in ["fw_0123abcd", "0123456789abcdef0123456789abcdef", "sk-proj-abc", "sk-or"] { |
| 36 | let why = Provider::OpenRouter.key_fault(k); |
| 37 | assert!(why.is_some(), "accepted {:?}", k); |
| 38 | assert!(why.unwrap_or_default().contains(OPENROUTER_KEY_PREFIX)); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// The other providers publish no prefix, so any plain token is theirs to judge. |
| 43 | #[test] |
| 44 | fn other_providers_take_any_plain_token_03() { |
| 45 | assert_eq!(Provider::Mistral.key_fault("0123456789abcdef0123456789abcdef"), None); |
| 46 | assert_eq!(Provider::Fireworks.key_fault("fw_0123abcd"), None); |
| 47 | } |
| 48 | |
| 49 | /// The send seam writes `Host`, so the call's own headers do not, and the request carries one. |
| 50 | #[test] |
| 51 | fn a_call_names_its_host_once_04() { |
| 52 | let h = chat_headers("Bearer sk-or-v1-abc"); |
| 53 | assert!(!h.iter().any(|(n, _)| n.eq_ignore_ascii_case("host"))); |
| 54 | assert!(h.contains(&("Authorization", "Bearer sk-or-v1-abc"))); |
| 55 | let req = format_request(HttpMethod::POST, "openrouter.ai", "/api/v1/chat/completions", &h, b"{}"); |
| 56 | let text = String::from_utf8_lossy(&req).to_ascii_lowercase(); |
| 57 | assert_eq!(text.matches("\r\nhost:").count(), 1, "{}", text); |
| 58 | } |