use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fmt;
use std::ops::Range;
use std::sync::{Mutex, PoisonError};
use hmac::{Hmac, KeyInit, Mac};
use sha2::{Digest, Sha256};
use toml_edit::{Item, TableLike, Value};
use crate::error::{CoreError, ErrorCode};
use crate::secrets::classify::{self, Reason};
pub const ALLOWLIST_FILE: &str = ".loadout-allow";
const ALLOW_PREFIX: &str = "sha256:";
const ALLOW_DOMAIN: &[u8] = b"loadout-allow\0";
pub const MIN_TRACKED_LEN: usize = 8;
pub struct SecretMemory {
key: [u8; 32],
by_len: Mutex<BTreeMap<usize, HashSet<[u8; 32]>>>,
}
impl fmt::Debug for SecretMemory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SecretMemory")
.field("tracked", &self.len())
.finish_non_exhaustive()
}
}
impl SecretMemory {
pub fn new() -> Result<Self, CoreError> {
let mut key = [0u8; 32];
getrandom::fill(&mut key).map_err(|err| {
CoreError::new(
ErrorCode::IoError,
format!("cannot read random bytes for the secret memory key: {err}"),
)
})?;
Ok(Self {
key,
by_len: Mutex::new(BTreeMap::new()),
})
}
pub fn record(&self, value: &str) {
self.record_here(value);
crate::diagnostics::redact::remember(value);
}
pub(crate) fn record_here(&self, value: &str) {
let mut by_len = self.by_len.lock().unwrap_or_else(PoisonError::into_inner);
for form in [value, value.trim()] {
if form.len() >= MIN_TRACKED_LEN {
by_len
.entry(form.len())
.or_default()
.insert(self.digest(form.as_bytes()));
}
}
}
pub fn len(&self) -> usize {
self.by_len
.lock()
.unwrap_or_else(PoisonError::into_inner)
.values()
.map(HashSet::len)
.sum()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn find(&self, haystack: &[u8]) -> Option<Range<usize>> {
let by_len = self.by_len.lock().unwrap_or_else(PoisonError::into_inner);
let mut first: Option<Range<usize>> = None;
for (&len, digests) in by_len.iter() {
if len > haystack.len() {
break;
}
let limit = first.as_ref().map_or(haystack.len(), |found| found.start);
let hit = (0..=haystack.len() - len)
.take_while(|start| *start < limit)
.find(|&start| digests.contains(&self.digest(&haystack[start..start + len])));
if let Some(start) = hit {
first = Some(start..start + len);
}
}
first
}
fn digest(&self, bytes: &[u8]) -> [u8; 32] {
let mut mac = <Hmac<Sha256> as KeyInit>::new_from_slice(&self.key)
.expect("HMAC accepts any key length");
mac.update(bytes);
mac.finalize().into_bytes().into()
}
}
pub fn allow_hash(text: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(ALLOW_DOMAIN);
hasher.update(text.as_bytes());
let digest = hasher.finalize();
let mut out = String::with_capacity(ALLOW_PREFIX.len() + 64);
out.push_str(ALLOW_PREFIX);
for byte in digest {
out.push_str(&format!("{byte:02x}"));
}
out
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Allowlist(BTreeSet<String>);
impl Allowlist {
pub fn parse(text: &str) -> Self {
Self(
text.lines()
.map(|line| line.split('#').next().unwrap_or("").trim())
.filter(|line| line.starts_with(ALLOW_PREFIX))
.map(str::to_owned)
.collect(),
)
}
pub fn contains(&self, hash: &str) -> bool {
self.0.contains(hash)
}
pub fn append(existing: Option<&str>, hash: &str) -> String {
let mut text = existing.unwrap_or_default().to_owned();
if Self::parse(&text).contains(hash) {
return text;
}
if text.is_empty() {
text.push_str("# Values marked as not secret (SPEC 11.7); hashes only.\n");
} else if !text.ends_with('\n') {
text.push('\n');
}
text.push_str(hash);
text.push('\n');
text
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LeakKind {
Classified(Reason),
ResolvedValue,
}
impl LeakKind {
pub const fn as_str(self) -> &'static str {
match self {
LeakKind::Classified(reason) => reason.as_str(),
LeakKind::ResolvedValue => "resolved_value",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeakFinding {
pub file: String,
pub line: Option<usize>,
pub key: Option<String>,
pub kind: LeakKind,
}
impl fmt::Display for LeakFinding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.file)?;
if let Some(line) = self.line {
write!(f, " line {line}")?;
}
if let Some(key) = &self.key {
write!(f, " ({key})")?;
}
match self.kind {
LeakKind::Classified(reason) => write!(f, ": {}", reason.describe()),
LeakKind::ResolvedValue => f.write_str(": holds a secret value resolved by Surjection"),
}
}
}
pub fn scan_text(
file: &str,
bytes: &[u8],
toml: bool,
memory: &SecretMemory,
allow: &Allowlist,
) -> Vec<LeakFinding> {
let mut findings = Vec::new();
if let Some(range) = memory.find(bytes) {
findings.push(LeakFinding {
file: file.to_owned(),
line: Some(line_of(bytes, range.start)),
key: None,
kind: LeakKind::ResolvedValue,
});
}
let text = String::from_utf8_lossy(bytes);
for hit in classify::find_in_text(&text) {
if allow.contains(&allow_hash(&text[hit.range.clone()])) {
continue;
}
findings.push(LeakFinding {
file: file.to_owned(),
line: Some(line_of(text.as_bytes(), hit.range.start)),
key: None,
kind: LeakKind::Classified(hit.reason),
});
}
if toml && let Ok(doc) = toml_edit::Document::parse(text.as_ref()) {
let mut keyed = Vec::new();
walk_table(doc.as_table(), "", &mut keyed);
for (key, value, span) in keyed {
if allow.contains(&allow_hash(&value)) {
continue;
}
findings.push(LeakFinding {
file: file.to_owned(),
line: span.map(|span| line_of(text.as_bytes(), span.start)),
key: Some(key),
kind: LeakKind::Classified(Reason::HighEntropy),
});
}
}
findings
}
pub fn leak_error(findings: &[LeakFinding]) -> Option<CoreError> {
if findings.is_empty() {
return None;
}
let listed: Vec<String> = findings.iter().map(ToString::to_string).collect();
Some(CoreError::new(
ErrorCode::LeakDetected,
format!(
"refusing to commit a possible secret: {}. Store files hold references \
(`${{secret:ALIAS}}`), never values; mark text that is not secret in {ALLOWLIST_FILE}",
listed.join("; ")
),
))
}
fn line_of(bytes: &[u8], offset: usize) -> usize {
bytes[..offset.min(bytes.len())]
.iter()
.filter(|b| **b == b'\n')
.count()
+ 1
}
type Keyed = Vec<(String, String, Option<Range<usize>>)>;
fn walk_table(table: &dyn TableLike, prefix: &str, out: &mut Keyed) {
for (key, item) in table.iter() {
let path = if prefix.is_empty() {
key.to_owned()
} else {
format!("{prefix}.{key}")
};
match item {
Item::Value(value) => walk_value(key, value, &path, out),
Item::Table(table) => walk_table(table, &path, out),
Item::ArrayOfTables(array) => {
for (index, table) in array.iter().enumerate() {
walk_table(table, &format!("{path}[{index}]"), out);
}
}
Item::None => {}
}
}
}
fn walk_value(key: &str, value: &Value, path: &str, out: &mut Keyed) {
match value {
Value::String(text) => {
if classify::classify_stored(Some(key), text.value()) == Some(Reason::HighEntropy) {
out.push((path.to_owned(), text.value().clone(), value.span()));
}
}
Value::Array(array) => {
let strings: Vec<(usize, &Value)> = array
.iter()
.enumerate()
.filter(|(_, item)| item.is_str())
.collect();
let args: Vec<String> = strings
.iter()
.filter_map(|(_, item)| item.as_str().map(str::to_owned))
.collect();
for ((index, item), reason) in strings.iter().zip(classify::classify_args_stored(&args))
{
if reason == Some(Reason::HighEntropy) {
let text = item.as_str().unwrap_or_default().to_owned();
out.push((format!("{path}[{index}]"), text, item.span()));
}
}
for (index, item) in array.iter().enumerate() {
if !item.is_str() {
walk_value(key, item, &format!("{path}[{index}]"), out);
}
}
}
Value::InlineTable(table) => walk_table(table, path, out),
Value::Integer(_) | Value::Float(_) | Value::Boolean(_) | Value::Datetime(_) => {}
}
}