| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | | use crate::protocol::RemoteOptions; |
| 25 | | use std::collections::{BTreeMap, HashMap}; |
| 26 | | use std::io::Write; |
| 27 | | use std::path::{Path, PathBuf}; |
| 28 | | use std::process::{Command, Stdio}; |
| 29 | | use std::sync::{Mutex, OnceLock}; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | | const HELPER_USER: &str = "oxy-token"; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 2290× | pub fn hub_of(url: &str) -> Option<String> { |
| 38 | 2290× | let (scheme, rest) = url.split_once("://")?; |
| 39 | 431× | let scheme = scheme.to_ascii_lowercase(); |
| 40 | 431× | if scheme != "http" && scheme != "https" { |
| 41 | 102× | return None; |
| 42 | 329× | } |
| 43 | 329× | let host = rest.split(['/', '?', '#']).next()?; |
| 44 | | |
| 45 | 329× | let host = host.rsplit('@').next()?; |
| 46 | 329× | if host.is_empty() { |
| 47 | 0× | return None; |
| 48 | 329× | } |
| 49 | 329× | Some(format!("{scheme}://{}", host.to_ascii_lowercase())) |
| 50 | 2290× | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 15× | pub fn token_allowed(url: &str) -> bool { |
| 55 | 15× | let Some(hub) = hub_of(url) else { |
| 56 | 0× | return true; |
| 57 | | }; |
| 58 | 15× | match hub.split_once("://") { |
| 59 | 15× | Some(("https", _)) => true, |
| 60 | 14× | Some((_, host)) => { |
| 61 | 14× | let name = match host.strip_prefix('[') { |
| 62 | 1× | Some(v6) => v6.split(']').next().unwrap_or(""), |
| 63 | 13× | None => host.rsplit_once(':').map_or(host, |(h, _)| h), |
| 64 | | }; |
| 65 | 14× | name == "localhost" |
| 66 | 13× | || name == "::1" |
| 67 | 12× | || name |
| 68 | 12× | .parse::<std::net::Ipv4Addr>() |
| 69 | 12× | .is_ok_and(|a| a.is_loopback()) |
| 70 | | } |
| 71 | 0× | None => false, |
| 72 | | } |
| 73 | 15× | } |
| 74 | |
|
| 75 | |
|
| 76 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 77 | | pub struct HubEntry { |
| 78 | | |
| 79 | | pub hub: String, |
| 80 | | |
| 81 | | pub account: Option<String>, |
| 82 | | |
| 83 | | pub token: Option<String>, |
| 84 | | |
| 85 | | pub ca: Option<PathBuf>, |
| 86 | | } |
| 87 | |
|
| 88 | |
|
| 89 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 90 | | pub enum Stored { |
| 91 | | |
| 92 | | File(PathBuf), |
| 93 | | |
| 94 | | Helper(String), |
| 95 | | } |
| 96 | |
|
| 97 | |
|
| 98 | | #[derive(Clone, Debug)] |
| 99 | | pub struct Credentials { |
| 100 | | home: PathBuf, |
| 101 | | } |
| 102 | |
|
| 103 | | impl Credentials { |
| 104 | | |
| 105 | 1252× | pub fn at(home: impl Into<PathBuf>) -> Credentials { |
| 106 | 1252× | Credentials { home: home.into() } |
| 107 | 1252× | } |
| 108 | |
|
| 109 | | |
| 110 | 238× | pub fn user() -> Credentials { |
| 111 | 238× | Credentials::at(crate::repo::default_home()) |
| 112 | 238× | } |
| 113 | |
|
| 114 | | |
| 115 | 99× | pub fn path(&self) -> PathBuf { |
| 116 | 99× | self.home.join("credentials.toml") |
| 117 | 99× | } |
| 118 | |
|
| 119 | | |
| 120 | 46× | pub fn helper(&self) -> Option<String> { |
| 121 | 46× | crate::repo::read_config_file(&self.home.join("config.toml")) |
| 122 | 46× | .ok()? |
| 123 | 46× | .remove("auth.helper") |
| 124 | 46× | .filter(|h| !h.trim().is_empty()) |
| 125 | 46× | } |
| 126 | |
|
| 127 | | |
| 128 | 1× | pub fn entries(&self) -> Result<Vec<HubEntry>, String> { |
| 129 | 1× | Ok(self.read()?.into_values().collect()) |
| 130 | 1× | } |
| 131 | |
|
| 132 | | |
| 133 | | |
| 134 | 35× | pub fn too_open(&self) -> bool { |
| 135 | | #[cfg(unix)] |
| 136 | | { |
| 137 | | use std::os::unix::fs::PermissionsExt; |
| 138 | 35× | if let Ok(m) = std::fs::metadata(self.path()) { |
| 139 | 10× | return m.permissions().mode() & 0o077 != 0; |
| 140 | 25× | } |
| 141 | | } |
| 142 | 25× | false |
| 143 | 35× | } |
| 144 | |
|
| 145 | | |
| 146 | 37× | pub fn get(&self, hub: &str) -> Result<Option<HubEntry>, String> { |
| 147 | 37× | let entry = self.read()?.remove(hub); |
| 148 | 37× | let helper = self.helper(); |
| 149 | 37× | let mut entry = match (entry, &helper) { |
| 150 | 7× | (Some(e), _) => e, |
| 151 | | |
| 152 | 0× | (None, Some(_)) => HubEntry { |
| 153 | 0× | hub: hub.to_string(), |
| 154 | 0× | ..HubEntry::default() |
| 155 | 0× | }, |
| 156 | 30× | (None, None) => return Ok(None), |
| 157 | | }; |
| 158 | 7× | if entry.token.is_none() { |
| 159 | 2× | if let Some(h) = &helper { |
| 160 | 2× | entry.token = run_helper(h, "get", hub, None)?; |
| 161 | 0× | } |
| 162 | 5× | } |
| 163 | 7× | if entry.token.is_none() && entry.account.is_none() && entry.ca.is_none() { |
| 164 | 0× | return Ok(None); |
| 165 | 7× | } |
| 166 | 7× | Ok(Some(entry)) |
| 167 | 37× | } |
| 168 | |
|
| 169 | | |
| 170 | 6× | pub fn save( |
| 171 | 6× | &self, |
| 172 | 6× | hub: &str, |
| 173 | 6× | token: &str, |
| 174 | 6× | account: Option<&str>, |
| 175 | 6× | ca: Option<&Path>, |
| 176 | 6× | ) -> Result<Stored, String> { |
| 177 | 6× | let mut all = self.read()?; |
| 178 | 6× | let helper = self.helper(); |
| 179 | 6× | let entry = all.entry(hub.to_string()).or_insert_with(|| HubEntry { |
| 180 | 6× | hub: hub.to_string(), |
| 181 | 6× | ..HubEntry::default() |
| 182 | 6× | }); |
| 183 | 6× | entry.account = account.map(String::from).or(entry.account.take()); |
| 184 | 6× | if let Some(ca) = ca { |
| 185 | 1× | entry.ca = Some(ca.to_path_buf()); |
| 186 | 5× | } |
| 187 | 6× | let stored = match &helper { |
| 188 | 1× | Some(h) => { |
| 189 | 1× | run_helper(h, "store", hub, Some(token))?; |
| 190 | 1× | entry.token = None; |
| 191 | 1× | Stored::Helper(h.clone()) |
| 192 | | } |
| 193 | | None => { |
| 194 | 5× | entry.token = Some(token.to_string()); |
| 195 | 5× | Stored::File(self.path()) |
| 196 | | } |
| 197 | | }; |
| 198 | 6× | self.write(&all)?; |
| 199 | 6× | forget_cached(); |
| 200 | 6× | Ok(stored) |
| 201 | 6× | } |
| 202 | |
|
| 203 | | |
| 204 | | |
| 205 | 2× | pub fn remove(&self, hub: &str) -> Result<Option<HubEntry>, String> { |
| 206 | 2× | let found = self.get(hub)?; |
| 207 | 2× | let mut all = self.read()?; |
| 208 | 2× | if all.remove(hub).is_some() { |
| 209 | 2× | self.write(&all)?; |
| 210 | 0× | } |
| 211 | 2× | if let Some(h) = self.helper() { |
| 212 | 1× | run_helper(&h, "erase", hub, None)?; |
| 213 | 1× | } |
| 214 | 2× | forget_cached(); |
| 215 | 2× | Ok(found) |
| 216 | 2× | } |
| 217 | |
|
| 218 | 46× | fn read(&self) -> Result<BTreeMap<String, HubEntry>, String> { |
| 219 | 46× | let path = self.path(); |
| 220 | 46× | let text = match std::fs::read_to_string(&path) { |
| 221 | 15× | Ok(t) => t, |
| 222 | 31× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()), |
| 223 | 0× | Err(e) => return Err(format!("reading {}: {e}", path.display())), |
| 224 | | }; |
| 225 | 15× | let mut out = BTreeMap::new(); |
| 226 | 15× | let mut current: Option<HubEntry> = None; |
| 227 | 100× | for (n, line) in text.lines().enumerate() { |
| 228 | 100× | let line = line.trim(); |
| 229 | 100× | if line.is_empty() || line.starts_with('#') { |
| 230 | 37× | continue; |
| 231 | 63× | } |
| 232 | 63× | if let Some(hub) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) { |
| 233 | 22× | if let Some(e) = current.take() { |
| 234 | 7× | out.insert(e.hub.clone(), e); |
| 235 | 15× | } |
| 236 | 22× | current = Some(HubEntry { |
| 237 | 22× | hub: hub.trim().to_string(), |
| 238 | 22× | ..HubEntry::default() |
| 239 | 22× | }); |
| 240 | 22× | continue; |
| 241 | 41× | } |
| 242 | 41× | let bad = || { |
| 243 | 0× | format!( |
| 244 | | "{} line {}: expected key = \"value\"", |
| 245 | 0× | path.display(), |
| 246 | 0× | n + 1 |
| 247 | | ) |
| 248 | 0× | }; |
| 249 | 41× | let (k, v) = line.split_once('=').ok_or_else(bad)?; |
| 250 | 41× | let v = unquote(v.trim()).ok_or_else(bad)?; |
| 251 | 41× | let Some(e) = current.as_mut() else { |
| 252 | 0× | return Err(format!( |
| 253 | 0× | "{} line {}: a key before any [hub] section", |
| 254 | 0× | path.display(), |
| 255 | 0× | n + 1 |
| 256 | 0× | )); |
| 257 | | }; |
| 258 | 41× | match k.trim() { |
| 259 | 41× | "account" => e.account = Some(v), |
| 260 | 28× | "token" => e.token = Some(v), |
| 261 | 9× | "ca" => e.ca = Some(PathBuf::from(v)), |
| 262 | | |
| 263 | 0× | _ => {} |
| 264 | | } |
| 265 | | } |
| 266 | 15× | if let Some(e) = current { |
| 267 | 15× | out.insert(e.hub.clone(), e); |
| 268 | 15× | } |
| 269 | 15× | Ok(out) |
| 270 | 46× | } |
| 271 | |
|
| 272 | 8× | fn write(&self, all: &BTreeMap<String, HubEntry>) -> Result<(), String> { |
| 273 | 8× | let mut text = |
| 274 | 8× | String::from("# oxy credentials: a token for each hub. Keep this file private.\n"); |
| 275 | 8× | for e in all.values() { |
| 276 | 8× | text.push_str(&format!("\n[{}]\n", e.hub)); |
| 277 | 8× | if let Some(a) = &e.account { |
| 278 | 5× | text.push_str(&format!("account = {}\n", quote(a))); |
| 279 | 5× | } |
| 280 | 8× | if let Some(t) = &e.token { |
| 281 | 7× | text.push_str(&format!("token = {}\n", quote(t))); |
| 282 | 7× | } |
| 283 | 8× | if let Some(c) = &e.ca { |
| 284 | 2× | text.push_str(&format!("ca = {}\n", quote(&c.to_string_lossy()))); |
| 285 | 6× | } |
| 286 | | } |
| 287 | 8× | let path = self.path(); |
| 288 | 8× | std::fs::create_dir_all(&self.home) |
| 289 | 8× | .map_err(|e| format!("creating {}: {e}", self.home.display()))?; |
| 290 | 8× | crate::durable::write_atomic_private(&path, text.as_bytes()) |
| 291 | 8× | .map_err(|e| format!("writing {}: {e}", path.display())) |
| 292 | 8× | } |
| 293 | |
|
| 294 | | |
| 295 | | |
| 296 | | |
| 297 | 1253× | pub fn lookup(&self, url: &str) -> (Option<String>, Option<PathBuf>) { |
| 298 | 1253× | let Some(hub) = hub_of(url) else { |
| 299 | 1086× | return (None, None); |
| 300 | | }; |
| 301 | 167× | let key = (self.home.clone(), hub.clone()); |
| 302 | 167× | if let Some(hit) = cache().lock().unwrap().get(&key) { |
| 303 | 134× | return hit.clone(); |
| 304 | 33× | } |
| 305 | 33× | let found = if self.too_open() { |
| 306 | 1× | warn_once(&format!( |
| 307 | 1× | "warning: {} can be read by other users, so oxy ignores it; run chmod 600 on it", |
| 308 | 1× | self.path().display() |
| 309 | 1× | )); |
| 310 | 1× | (None, None) |
| 311 | | } else { |
| 312 | 32× | match self.get(&hub) { |
| 313 | 3× | Ok(Some(e)) => (e.token, e.ca), |
| 314 | 29× | Ok(None) => (None, None), |
| 315 | 0× | Err(e) => { |
| 316 | 0× | warn_once(&format!("warning: credentials for {hub}: {e}")); |
| 317 | 0× | (None, None) |
| 318 | | } |
| 319 | | } |
| 320 | | }; |
| 321 | 33× | cache().lock().unwrap().insert(key, found.clone()); |
| 322 | 33× | found |
| 323 | 1253× | } |
| 324 | |
|
| 325 | | |
| 326 | | |
| 327 | 1247× | pub fn options_for(&self, url: &str) -> RemoteOptions { |
| 328 | 1247× | let env_token = std::env::var("OXY_TOKEN").ok().filter(|t| !t.is_empty()); |
| 329 | 1247× | let env_ca = std::env::var("OXY_CA").ok().map(PathBuf::from); |
| 330 | 1247× | let (token, ca) = match (&env_token, &env_ca) { |
| 331 | 0× | (Some(_), Some(_)) => (None, None), |
| 332 | 1247× | _ => self.lookup(url), |
| 333 | | }; |
| 334 | 1247× | RemoteOptions { |
| 335 | 1247× | token: env_token.or(token), |
| 336 | 1247× | ca: env_ca.or(ca), |
| 337 | 1247× | ..RemoteOptions::default() |
| 338 | 1247× | } |
| 339 | 1247× | } |
| 340 | | } |
| 341 | |
|
| 342 | | type Cache = Mutex<HashMap<(PathBuf, String), (Option<String>, Option<PathBuf>)>>; |
| 343 | |
|
| 344 | 208× | fn cache() -> &'static Cache { |
| 345 | | static CACHE: OnceLock<Cache> = OnceLock::new(); |
| 346 | 208× | CACHE.get_or_init(Default::default) |
| 347 | 208× | } |
| 348 | |
|
| 349 | 8× | fn forget_cached() { |
| 350 | 8× | cache().lock().unwrap().clear(); |
| 351 | 8× | } |
| 352 | |
|
| 353 | 2× | pub(crate) fn warn_once(message: &str) { |
| 354 | | static SEEN: OnceLock<Mutex<std::collections::HashSet<String>>> = OnceLock::new(); |
| 355 | 2× | if SEEN |
| 356 | 2× | .get_or_init(Default::default) |
| 357 | 2× | .lock() |
| 358 | 2× | .unwrap() |
| 359 | 2× | .insert(message.to_string()) |
| 360 | 2× | { |
| 361 | 2× | eprintln!("{message}"); |
| 362 | 2× | } |
| 363 | 2× | } |
| 364 | |
|
| 365 | 14× | fn quote(v: &str) -> String { |
| 366 | 14× | let mut out = String::from('"'); |
| 367 | 68× | for c in v.chars() { |
| 368 | 68× | match c { |
| 369 | 2× | '"' => out.push_str("\\\""), |
| 370 | 0× | '\\' => out.push_str("\\\\"), |
| 371 | 0× | '\n' => out.push_str("\\n"), |
| 372 | 66× | c => out.push(c), |
| 373 | | } |
| 374 | | } |
| 375 | 14× | out.push('"'); |
| 376 | 14× | out |
| 377 | 14× | } |
| 378 | |
|
| 379 | 41× | fn unquote(v: &str) -> Option<String> { |
| 380 | 41× | let inner = v.strip_prefix('"')?.strip_suffix('"')?; |
| 381 | 41× | let mut out = String::new(); |
| 382 | 41× | let mut chars = inner.chars(); |
| 383 | 260× | while let Some(c) = chars.next() { |
| 384 | 219× | if c == '\\' { |
| 385 | 8× | match chars.next()? { |
| 386 | 0× | 'n' => out.push('\n'), |
| 387 | 8× | c => out.push(c), |
| 388 | | } |
| 389 | 211× | } else { |
| 390 | 211× | out.push(c); |
| 391 | 211× | } |
| 392 | | } |
| 393 | 41× | Some(out) |
| 394 | 41× | } |
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | 4× | fn run_helper( |
| 402 | 4× | helper: &str, |
| 403 | 4× | action: &str, |
| 404 | 4× | hub: &str, |
| 405 | 4× | token: Option<&str>, |
| 406 | 4× | ) -> Result<Option<String>, String> { |
| 407 | 4× | let (protocol, host) = hub |
| 408 | 4× | .split_once("://") |
| 409 | 4× | .ok_or_else(|| format!("{hub} is not a hub URL"))?; |
| 410 | 4× | let mut cmd = if let Some(shell) = helper.strip_prefix('!') { |
| 411 | 4× | let mut c = Command::new("sh"); |
| 412 | 4× | c.arg("-c").arg(format!("{shell} {action}")); |
| 413 | 4× | c |
| 414 | 0× | } else if Path::new(helper).is_absolute() { |
| 415 | 0× | let mut c = Command::new(helper); |
| 416 | 0× | c.arg(action); |
| 417 | 0× | c |
| 418 | | } else { |
| 419 | 0× | let mut c = Command::new("git"); |
| 420 | 0× | c.arg(format!("credential-{helper}")).arg(action); |
| 421 | 0× | c |
| 422 | | }; |
| 423 | 4× | let mut input = format!("protocol={protocol}\nhost={host}\nusername={HELPER_USER}\n"); |
| 424 | 4× | if let Some(t) = token { |
| 425 | 1× | input.push_str(&format!("password={t}\n")); |
| 426 | 3× | } |
| 427 | 4× | input.push('\n'); |
| 428 | 4× | let mut child = cmd |
| 429 | 4× | .stdin(Stdio::piped()) |
| 430 | 4× | .stdout(Stdio::piped()) |
| 431 | 4× | .stderr(Stdio::inherit()) |
| 432 | 4× | .spawn() |
| 433 | 4× | .map_err(|e| format!("running credential helper {helper:?}: {e}"))?; |
| 434 | 4× | child |
| 435 | 4× | .stdin |
| 436 | 4× | .take() |
| 437 | 4× | .expect("piped") |
| 438 | 4× | .write_all(input.as_bytes()) |
| 439 | 4× | .map_err(|e| format!("credential helper {helper:?}: {e}"))?; |
| 440 | 4× | let out = child |
| 441 | 4× | .wait_with_output() |
| 442 | 4× | .map_err(|e| format!("credential helper {helper:?}: {e}"))?; |
| 443 | 4× | if !out.status.success() { |
| 444 | 0× | return Err(format!("credential helper {helper:?} {action} failed")); |
| 445 | 4× | } |
| 446 | 4× | if action != "get" { |
| 447 | 2× | return Ok(None); |
| 448 | 2× | } |
| 449 | 2× | Ok(String::from_utf8_lossy(&out.stdout) |
| 450 | 2× | .lines() |
| 451 | 2× | .find_map(|l| l.strip_prefix("password=")) |
| 452 | 2× | .map(String::from) |
| 453 | 2× | .filter(|p| !p.is_empty())) |
| 454 | 4× | } |
| 455 | |
|
| 456 | | #[cfg(test)] |
| 457 | | mod tests { |
| 458 | | use super::*; |
| 459 | |
|
| 460 | | #[test] |
| 461 | 1× | fn hubs_are_scheme_host_and_port() { |
| 462 | 1× | assert_eq!( |
| 463 | 1× | hub_of("https://OxygenHub.io/doomsdayone/metalgear").as_deref(), |
| 464 | | Some("https://oxygenhub.io") |
| 465 | | ); |
| 466 | 1× | assert_eq!( |
| 467 | 1× | hub_of("http://127.0.0.1:7300/a/b").as_deref(), |
| 468 | | Some("http://127.0.0.1:7300") |
| 469 | | ); |
| 470 | 1× | assert_eq!( |
| 471 | 1× | hub_of("https://user:[email protected]/x").as_deref(), |
| 472 | | Some("https://hub.example") |
| 473 | | ); |
| 474 | 1× | assert_eq!(hub_of("/srv/repo"), None); |
| 475 | 1× | assert_eq!(hub_of("file:///srv/repo"), None); |
| 476 | 1× | } |
| 477 | |
|
| 478 | | #[test] |
| 479 | 1× | fn tokens_go_over_https_or_to_this_machine() { |
| 480 | 1× | assert!(token_allowed("https://oxygenhub.io/a/b")); |
| 481 | 1× | assert!(token_allowed("http://127.0.0.1:7300/a/b")); |
| 482 | 1× | assert!(token_allowed("http://localhost/a")); |
| 483 | 1× | assert!(token_allowed("http://[::1]:8080/a")); |
| 484 | 1× | assert!(!token_allowed("http://oxygenhub.io/a/b")); |
| 485 | 1× | assert!(!token_allowed("http://10.0.0.1/a")); |
| 486 | 1× | assert!(!token_allowed("http://127.0.0.1.evil.example/a")); |
| 487 | 1× | } |
| 488 | |
|
| 489 | | #[test] |
| 490 | 1× | fn save_get_and_remove_round_trip_privately() { |
| 491 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 492 | 1× | let store = Credentials::at(dir.path()); |
| 493 | 1× | assert_eq!(store.get("https://hub.example").unwrap(), None); |
| 494 | 1× | let stored = store |
| 495 | 1× | .save("https://hub.example", "tok\"en", Some("andy"), None) |
| 496 | 1× | .unwrap(); |
| 497 | 1× | assert_eq!(stored, Stored::File(store.path())); |
| 498 | 1× | store |
| 499 | 1× | .save( |
| 500 | 1× | "http://127.0.0.1:7300", |
| 501 | 1× | "local", |
| 502 | 1× | None, |
| 503 | 1× | Some(Path::new("/ca.pem")), |
| 504 | | ) |
| 505 | 1× | .unwrap(); |
| 506 | 1× | let e = store.get("https://hub.example").unwrap().unwrap(); |
| 507 | 1× | assert_eq!(e.token.as_deref(), Some("tok\"en")); |
| 508 | 1× | assert_eq!(e.account.as_deref(), Some("andy")); |
| 509 | 1× | assert!(!store.too_open()); |
| 510 | 1× | assert_eq!( |
| 511 | 1× | store.lookup("https://hub.example/o/r"), |
| 512 | 1× | (Some("tok\"en".into()), None) |
| 513 | | ); |
| 514 | | |
| 515 | 1× | assert_eq!(store.lookup("https://other.example/o/r"), (None, None)); |
| 516 | 1× | assert_eq!(store.lookup("http://hub.example/o/r"), (None, None)); |
| 517 | 1× | assert_eq!(store.lookup("https://hub.example:8443/o/r"), (None, None)); |
| 518 | 1× | let gone = store.remove("https://hub.example").unwrap().unwrap(); |
| 519 | 1× | assert_eq!(gone.token.as_deref(), Some("tok\"en")); |
| 520 | 1× | assert_eq!(store.lookup("https://hub.example/o/r"), (None, None)); |
| 521 | 1× | assert_eq!(store.entries().unwrap().len(), 1); |
| 522 | 1× | } |
| 523 | |
|
| 524 | | #[cfg(unix)] |
| 525 | | #[test] |
| 526 | 1× | fn a_file_others_can_read_is_ignored() { |
| 527 | | use std::os::unix::fs::PermissionsExt; |
| 528 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 529 | 1× | let store = Credentials::at(dir.path()); |
| 530 | 1× | store.save("https://open.example", "t", None, None).unwrap(); |
| 531 | 1× | let mode = std::fs::metadata(store.path()) |
| 532 | 1× | .unwrap() |
| 533 | 1× | .permissions() |
| 534 | 1× | .mode(); |
| 535 | 1× | assert_eq!(mode & 0o777, 0o600); |
| 536 | 1× | std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o644)).unwrap(); |
| 537 | 1× | assert!(store.too_open()); |
| 538 | 1× | assert_eq!(store.lookup("https://open.example/o/r"), (None, None)); |
| 539 | 1× | } |
| 540 | |
|
| 541 | | #[cfg(unix)] |
| 542 | | #[test] |
| 543 | 1× | fn a_helper_keeps_the_token_out_of_the_file() { |
| 544 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 545 | 1× | let vault = dir.path().join("vault"); |
| 546 | | |
| 547 | 1× | let helper = format!( |
| 548 | | "!f() {{ case $1 in get) [ -f {v} ] && echo password=$(cat {v});; store) sed -n 's/^password=//p' > {v};; erase) rm -f {v};; esac; }}; f", |
| 549 | 1× | v = vault.display() |
| 550 | | ); |
| 551 | 1× | std::fs::write( |
| 552 | 1× | dir.path().join("config.toml"), |
| 553 | 1× | format!("auth.helper = \"{helper}\"\n"), |
| 554 | | ) |
| 555 | 1× | .unwrap(); |
| 556 | 1× | let store = Credentials::at(dir.path()); |
| 557 | 1× | assert!(store.helper().is_some()); |
| 558 | 1× | let stored = store |
| 559 | 1× | .save("https://hub.example", "secret", Some("andy"), None) |
| 560 | 1× | .unwrap(); |
| 561 | 1× | assert!(matches!(stored, Stored::Helper(_))); |
| 562 | 1× | let text = std::fs::read_to_string(store.path()).unwrap(); |
| 563 | 1× | assert!(!text.contains("secret"), "{text}"); |
| 564 | 1× | assert_eq!( |
| 565 | 1× | store |
| 566 | 1× | .get("https://hub.example") |
| 567 | 1× | .unwrap() |
| 568 | 1× | .unwrap() |
| 569 | 1× | .token |
| 570 | 1× | .as_deref(), |
| 571 | | Some("secret") |
| 572 | | ); |
| 573 | 1× | store.remove("https://hub.example").unwrap(); |
| 574 | 1× | assert!(!vault.exists()); |
| 575 | 1× | } |
| 576 | | } |