oxedyne/fe2o3/fe2o3_steel/tests/console_ai_key.rs
6.7 KiB, 6 runs
created by r939070283:78541, 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 | //! The AI page keeps only a key its provider could accept, says when the stored one will not work, and |
| 2 | //! offers no field a password manager would fill. |
| 3 | //! |
| 4 | //! The fault this guards (15 Nov 2026): the AI page's key was a password field that any save replaced |
| 5 | //! when it was not blank, so a browser filling it put something other than the OpenRouter key into the |
| 6 | //! store, and the test call came back "Missing Authentication header". |
| 7 | |
| 8 | use oxedyne_fe2o3_steel::srv::{ |
| 9 | admin::{ |
| 10 | guard, |
| 11 | host_sampler::HostSampler, |
| 12 | session::now_secs, |
| 13 | state::AdminState, |
| 14 | traffic::TrafficRecorder, |
| 15 | }, |
| 16 | console::{ |
| 17 | self, |
| 18 | csrf_token, |
| 19 | publish::{ |
| 20 | AI_TEST_SCRIPT, |
| 21 | PATH_AI_SAVE, |
| 22 | PATH_AI_TEST, |
| 23 | ai_form, |
| 24 | }, |
| 25 | session::{ |
| 26 | self, |
| 27 | MANAGE_COOKIE_NAME, |
| 28 | ManageSession, |
| 29 | }, |
| 30 | }, |
| 31 | id, |
| 32 | publish::{ |
| 33 | PublishConfig, |
| 34 | ai::{ |
| 35 | self, |
| 36 | AiSettings, |
| 37 | }, |
| 38 | send::MailSender, |
| 39 | }, |
| 40 | }; |
| 41 | |
| 42 | use oxedyne_fe2o3_core::prelude::*; |
| 43 | use oxedyne_fe2o3_crypto::{ |
| 44 | enc::EncryptionScheme, |
| 45 | keystore::Wallet, |
| 46 | }; |
| 47 | use oxedyne_fe2o3_hash::hash::HashScheme; |
| 48 | use oxedyne_fe2o3_net::http::{ |
| 49 | fields::{ |
| 50 | Cookie, |
| 51 | HeaderFieldValue, |
| 52 | HeaderFields, |
| 53 | HeaderName, |
| 54 | }, |
| 55 | msg::HttpMessage, |
| 56 | }; |
| 57 | |
| 58 | use tokio_rustls::rustls::ClientConfig; |
| 59 | |
| 60 | use std::{ |
| 61 | net::SocketAddr, |
| 62 | path::PathBuf, |
| 63 | sync::{ |
| 64 | Arc, |
| 65 | RwLock, |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | mod common; |
| 70 | |
| 71 | use common::TestDb; |
| 72 | |
| 73 | /// A signed-in admin: the state, the request headers, and the token the page would carry. |
| 74 | fn signed_in() -> Outcome<(AdminState, Arc<HeaderFields>, String)> { |
| 75 | let st = res!(AdminState::new( |
| 76 | Arc::new(RwLock::new(Wallet::default())), |
| 77 | PathBuf::from("./wallet.jdat"), |
| 78 | Some([0u8; 32].to_vec()), |
| 79 | 1, |
| 80 | None, |
| 81 | TrafficRecorder::new_shared(0), |
| 82 | HostSampler::new_shared(), |
| 83 | res!(guard::new_shared()), |
| 84 | res!(guard::new_auth_shared()), |
| 85 | Vec::new(), |
| 86 | None, |
| 87 | )); |
| 88 | let s = ManageSession::new("jason", now_secs()); |
| 89 | let value = res!(session::seal(&st, &s)); |
| 90 | let mut h = HeaderFields::default(); |
| 91 | h.insert(HeaderName::Accept, HeaderFieldValue::Generic("application/json".to_string()), None); |
| 92 | h.insert( |
| 93 | HeaderName::Cookie, |
| 94 | HeaderFieldValue::Cookie(vec![Cookie { |
| 95 | key: MANAGE_COOKIE_NAME.to_string(), |
| 96 | val: value, |
| 97 | attrs: None, |
| 98 | }]), |
| 99 | None, |
| 100 | ); |
| 101 | Ok((st, Arc::new(h), csrf_token(&s.csrf_seed()))) |
| 102 | } |
| 103 | |
| 104 | async fn post( |
| 105 | st: &AdminState, |
| 106 | h: &Arc<HeaderFields>, |
| 107 | db: &(Arc<RwLock<TestDb>>, id::Uid), |
| 108 | path: &str, |
| 109 | body: &str, |
| 110 | ) |
| 111 | -> Outcome<HttpMessage> |
| 112 | { |
| 113 | let cfg = PublishConfig::default(); |
| 114 | let tls: Option<Arc<ClientConfig>> = None; |
| 115 | let mail: Option<Arc<MailSender>> = None; |
| 116 | let resp = res!(console::handle_post::<{ id::UID_LEN }, id::Uid, EncryptionScheme, HashScheme, TestDb>( |
| 117 | &[], Some(st), Some(&cfg), Some(db), &tls, &mail, path, h, body.as_bytes(), |
| 118 | SocketAddr::from(([127, 0, 0, 1], 0)), "t", |
| 119 | ).await); |
| 120 | resp.ok_or_else(|| err!("The console did not answer a POST to '{}'.", path; Missing)) |
| 121 | } |
| 122 | |
| 123 | /// A passphrase in the key field is refused and nothing is stored; an OpenRouter key is kept, and a |
| 124 | /// later save with the field blank keeps it. |
| 125 | #[tokio::test] |
| 126 | async fn test_only_a_key_the_provider_takes_is_saved_00() -> Outcome<()> { |
| 127 | let (st, h, csrf) = res!(signed_in()); |
| 128 | let (db, uid, _tmp) = res!(common::test_db()); |
| 129 | let db = (db, uid); |
| 130 | let form = |key: &str| fmt!( |
| 131 | "csrf={}&provider=openrouter&model=m&api_key={}&fix_prompt=&comment_prompt=&alert_emails=", |
| 132 | csrf, key); |
| 133 | |
| 134 | let resp = res!(post(&st, &h, &db, PATH_AI_SAVE, &form("correct+horse+battery+staple")).await); |
| 135 | let body = resp.body_as_string().into_owned(); |
| 136 | assert!(body.contains("the key was not saved"), "{}", body); |
| 137 | assert!(!body.contains("horse"), "the refusal quoted the key: {}", body); |
| 138 | assert!(res!(ai::get_settings(&db)).api_key.is_empty()); |
| 139 | |
| 140 | let resp = res!(post(&st, &h, &db, PATH_AI_SAVE, &form("sk-or-v1-0123abcd")).await); |
| 141 | assert!(resp.body_as_string().contains("\"ok\":true"), "{}", resp.body_as_string()); |
| 142 | assert_eq!(res!(ai::get_settings(&db)).api_key, "sk-or-v1-0123abcd"); |
| 143 | |
| 144 | let resp = res!(post(&st, &h, &db, PATH_AI_SAVE, &form("")).await); |
| 145 | assert!(resp.body_as_string().contains("\"ok\":true")); |
| 146 | assert_eq!(res!(ai::get_settings(&db)).api_key, "sk-or-v1-0123abcd"); |
| 147 | Ok(()) |
| 148 | } |
| 149 | |
| 150 | /// A stored key the provider will refuse is named as such by Test, before any call goes out. |
| 151 | #[tokio::test] |
| 152 | async fn test_a_stored_key_that_will_not_work_is_named_01() -> Outcome<()> { |
| 153 | let (st, h, csrf) = res!(signed_in()); |
| 154 | let (db, uid, _tmp) = res!(common::test_db()); |
| 155 | let db = (db, uid); |
| 156 | let s = AiSettings { |
| 157 | provider: fmt!("openrouter"), |
| 158 | model: fmt!("m"), |
| 159 | api_key: fmt!("fw_0123abcd"), |
| 160 | ..Default::default() |
| 161 | }; |
| 162 | res!(ai::put_settings(&db, &s)); |
| 163 | let resp = res!(post(&st, &h, &db, PATH_AI_TEST, &fmt!("csrf={}", csrf)).await); |
| 164 | let body = resp.body_as_string().into_owned(); |
| 165 | assert!(body.contains("The stored key will not work"), "{}", body); |
| 166 | assert!(body.contains("sk-or-"), "{}", body); |
| 167 | assert!(!body.contains("fw_0123abcd"), "the answer quoted the key: {}", body); |
| 168 | Ok(()) |
| 169 | } |
| 170 | |
| 171 | /// The page says the stored key will not work, and its key field is no password field. |
| 172 | #[test] |
| 173 | fn test_the_key_field_is_no_password_field_02() { |
| 174 | let mut s = AiSettings { |
| 175 | provider: fmt!("openrouter"), |
| 176 | model: fmt!("m"), |
| 177 | api_key: fmt!("not a key"), |
| 178 | ..Default::default() |
| 179 | }; |
| 180 | let html = ai_form(&s, "t"); |
| 181 | assert!(!html.contains("type=\"password\""), "a password manager would fill the key field"); |
| 182 | assert!(html.contains("id=\"api_key\" name=\"api_key\" autocomplete=\"off\"")); |
| 183 | assert!(html.contains("it will not work")); |
| 184 | s.api_key = fmt!("sk-or-v1-0123abcd"); |
| 185 | assert!(ai_form(&s, "t").contains("A key is set.")); |
| 186 | } |
| 187 | |
| 188 | /// Test saves a changed form before it tests, so a key just pasted is the key tested: the page posts |
| 189 | /// the save first, and the test that follows no longer finds the old key. |
| 190 | #[tokio::test] |
| 191 | async fn test_a_changed_key_is_saved_before_the_test_03() -> Outcome<()> { |
| 192 | let save_at = AI_TEST_SCRIPT.find("mcPost(form.action"); |
| 193 | let test_at = AI_TEST_SCRIPT.find(&fmt!("mcPost('{}'", PATH_AI_TEST)); |
| 194 | assert!(save_at.is_some() && test_at.is_some(), "the script posts neither a save nor a test"); |
| 195 | |
| 196 | let (st, h, csrf) = res!(signed_in()); |
| 197 | let (db, uid, _tmp) = res!(common::test_db()); |
| 198 | let db = (db, uid); |
| 199 | res!(ai::put_settings(&db, &AiSettings { |
| 200 | provider: fmt!("openrouter"), |
| 201 | model: fmt!("m"), |
| 202 | api_key: fmt!("fw_old"), |
| 203 | ..Default::default() |
| 204 | })); |
| 205 | let save = fmt!( |
| 206 | "csrf={}&provider=openrouter&model=m&api_key=sk-or-v1-new&fix_prompt=&comment_prompt=&alert_emails=", |
| 207 | csrf); |
| 208 | let resp = res!(post(&st, &h, &db, PATH_AI_SAVE, &save).await); |
| 209 | assert!(resp.body_as_string().contains("\"ok\":true")); |
| 210 | let resp = res!(post(&st, &h, &db, PATH_AI_TEST, &fmt!("csrf={}", csrf)).await); |
| 211 | let body = resp.body_as_string().into_owned(); |
| 212 | assert!(!body.contains("will not work"), "the test used the old key: {}", body); |
| 213 | assert!(body.contains("no outbound connection"), "{}", body); |
| 214 | Ok(()) |
| 215 | } |