use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum VerifiedResult {
VerifiedTrue,
#[default]
Unverified,
VerifiedFalse,
}
impl VerifiedResult {
pub fn is_verified(&self) -> bool {
matches!(self, VerifiedResult::VerifiedTrue)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Span {
pub line: u32,
pub col_start: usize,
pub col_end: usize,
pub line_text: String,
}
impl Span {
pub fn new(line: u32, col_start: usize, col_end: usize, line_text: String) -> Self {
Self {
line,
col_start,
col_end,
line_text,
}
}
pub fn snippet(&self) -> Option<&str> {
self.line_text.get(self.col_start..self.col_end)
}
pub fn len(&self) -> usize {
self.col_end.saturating_sub(self.col_start)
}
pub fn is_empty(&self) -> bool {
self.col_end <= self.col_start
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Location {
pub file: PathBuf,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Finding {
pub detector: String,
pub secret_type: String,
#[serde(skip)]
pub secret: String,
pub hashed_secret: String,
pub location: Location,
pub verified: VerifiedResult,
}
impl Finding {
pub fn redacted(&self) -> Self {
let mut out = self.clone();
out.secret = String::new();
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snippet_slices_exact_bytes() {
let span = Span::new(1, 7, 11, "key = \"abcd\"".to_string());
assert_eq!(span.snippet(), Some("abcd"));
}
#[test]
fn secret_is_skipped_in_json() {
let f = Finding {
detector: "Stripe".to_string(),
secret_type: "Stripe Access Key".to_string(),
secret: "sk_live_aaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
hashed_secret: "deadbeef".to_string(),
location: Location {
file: PathBuf::from("a.py"),
span: Span::new(1, 0, 5, "hello".to_string()),
},
verified: VerifiedResult::Unverified,
};
let v: serde_json::Value = serde_json::to_value(&f).unwrap();
assert!(v.get("secret").is_none(), "plaintext must not serialize");
assert_eq!(v["hashed_secret"], "deadbeef");
}
}