use std::fmt;
use zeroize::{Zeroize, ZeroizeOnDrop};
pub(crate) const REDACTED: &str = "[REDACTED]";
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct ApiKey(String);
impl ApiKey {
pub fn new(mut key: String) -> Result<Self, InvalidApiKey> {
let trimmed = key.trim().to_owned();
key.zeroize();
let mut candidate = Self(trimmed);
if candidate.0.is_empty() {
return Err(InvalidApiKey::Empty);
}
if !candidate.0.bytes().all(|byte| byte.is_ascii_graphic()) {
candidate.zeroize();
return Err(InvalidApiKey::InvalidCharacters);
}
Ok(candidate)
}
#[must_use]
pub fn expose(&self) -> &str {
&self.0
}
pub(crate) fn scrub(&self, text: &str) -> String {
text.replace(self.0.as_str(), REDACTED)
}
pub(crate) fn scrub_bytes(&self, bytes: Vec<u8>) -> Vec<u8> {
let key = self.0.as_bytes();
if !bytes.windows(key.len()).any(|window| window == key) {
return bytes;
}
let mut scrubbed = Vec::with_capacity(bytes.len());
let mut rest = bytes.as_slice();
while !rest.is_empty() {
if rest.starts_with(key) {
scrubbed.extend_from_slice(REDACTED.as_bytes());
rest = rest.get(key.len()..).unwrap_or_default();
} else if let Some((first, tail)) = rest.split_first() {
scrubbed.push(*first);
rest = tail;
}
}
scrubbed
}
}
impl fmt::Debug for ApiKey {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "ApiKey({REDACTED})")
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum InvalidApiKey {
#[error("the API key is empty")]
Empty,
#[error("the API key contains characters that cannot be sent in an HTTP header")]
InvalidCharacters,
}
#[cfg(test)]
mod tests {
use super::{ApiKey, InvalidApiKey};
const SENTINEL: &str = "sentinel-key-0123456789";
#[test]
fn trims_the_whitespace_a_file_or_pipe_leaves_behind() {
let key = ApiKey::new(format!(" {SENTINEL}\r\n")).unwrap();
assert_eq!(key.expose(), SENTINEL);
}
#[test]
fn debug_never_shows_the_key() {
let key = ApiKey::new(SENTINEL.to_owned()).unwrap();
for rendered in [
format!("{key:?}"),
format!("{key:#?}"),
format!("{:?}", Some(&key)),
] {
assert!(!rendered.contains(SENTINEL), "{rendered}");
assert!(rendered.contains("[REDACTED]"), "{rendered}");
}
}
#[test]
fn rejects_keys_that_cannot_be_sent_without_echoing_them() {
let cases = [
("", InvalidApiKey::Empty),
(" \n\t", InvalidApiKey::Empty),
("sentinel key", InvalidApiKey::InvalidCharacters),
("sentinel\nkey", InvalidApiKey::InvalidCharacters),
("sentinel-ключ", InvalidApiKey::InvalidCharacters),
];
for (input, expected) in cases {
let error = ApiKey::new(input.to_owned()).unwrap_err();
assert_eq!(error, expected, "{input:?}");
assert!(!error.to_string().contains("sentinel"), "{error}");
assert!(!format!("{error:?}").contains("sentinel"), "{error:?}");
}
}
#[test]
fn scrubs_the_key_out_of_text_that_echoes_it() {
let key = ApiKey::new(SENTINEL.to_owned()).unwrap();
let scrubbed = key.scrub(&format!("bad header: Bearer {SENTINEL} ({SENTINEL})"));
assert_eq!(scrubbed, "bad header: Bearer [REDACTED] ([REDACTED])");
}
#[test]
fn scrubs_the_key_out_of_bytes_and_leaves_other_bytes_alone() {
let key = ApiKey::new(SENTINEL.to_owned()).unwrap();
let mut body = format!("{{\"echo\":\"Bearer {SENTINEL}\",\"k\":\"{SENTINEL}").into_bytes();
body.extend_from_slice(b"\xff\"}");
let scrubbed = key.scrub_bytes(body);
assert_eq!(
scrubbed,
b"{\"echo\":\"Bearer [REDACTED]\",\"k\":\"[REDACTED]\xff\"}".to_vec()
);
let untouched = b"no key here \xfe".to_vec();
assert_eq!(key.scrub_bytes(untouched.clone()), untouched);
}
}