use std::borrow::Cow;
use std::sync::OnceLock;
use crate::error::{
DetectorFailure, FormatterFailure, PolicyFailure, SecretScanError, SecretScanErrorCode,
};
pub const MAX_IDENTIFIER_LENGTH: usize = 64;
#[must_use]
pub fn is_identifier(value: &str) -> bool {
if value.is_empty() || value.len() > MAX_IDENTIFIER_LENGTH {
return false;
}
let bytes = value.as_bytes();
if !bytes[0].is_ascii_lowercase() {
return false;
}
let mut previous_was_separator = false;
for &byte in &bytes[1..] {
match byte {
b'a'..=b'z' | b'0'..=b'9' => previous_was_separator = false,
b'.' | b'_' | b'-' if !previous_was_separator => previous_was_separator = true,
_ => return false,
}
}
!previous_was_separator
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Confidence {
Low,
Medium,
High,
}
impl Confidence {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::High => "high",
Self::Medium => "medium",
Self::Low => "low",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"high" => Some(Self::High),
"medium" => Some(Self::Medium),
"low" => Some(Self::Low),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Specificity {
Entropy,
Contextual,
Structural,
Provider,
PrivateKey,
}
impl Specificity {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::PrivateKey => "private-key",
Self::Provider => "provider",
Self::Structural => "structural",
Self::Contextual => "contextual",
Self::Entropy => "entropy",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"private-key" => Some(Self::PrivateKey),
"provider" => Some(Self::Provider),
"structural" => Some(Self::Structural),
"contextual" => Some(Self::Contextual),
"entropy" => Some(Self::Entropy),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Action {
Redact,
Block,
Warn,
Allow,
}
impl Action {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Redact => "redact",
Self::Block => "block",
Self::Warn => "warn",
Self::Allow => "allow",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"redact" => Some(Self::Redact),
"block" => Some(Self::Block),
"warn" => Some(Self::Warn),
"allow" => Some(Self::Allow),
_ => None,
}
}
#[must_use]
pub const fn replaces_text(self) -> bool {
matches!(self, Self::Redact | Self::Block)
}
#[must_use]
pub(crate) const fn overlap_resolution_severity(self) -> u8 {
match self {
Self::Block => 3,
Self::Redact => 2,
Self::Warn => 1,
Self::Allow => 0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Obfuscation {
None,
InvisibleCharacters,
}
impl Obfuscation {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::None => "none",
Self::InvisibleCharacters => "invisible-characters",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"none" => Some(Self::None),
"invisible-characters" => Some(Self::InvisibleCharacters),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ByteRange {
start: usize,
end: usize,
}
#[allow(clippy::len_without_is_empty)]
impl ByteRange {
#[must_use]
pub const fn new(start: usize, end: usize) -> Option<Self> {
if start < end {
Some(Self { start, end })
} else {
None
}
}
#[must_use]
pub const fn start(self) -> usize {
self.start
}
#[must_use]
pub const fn end(self) -> usize {
self.end
}
#[must_use]
pub const fn len(self) -> usize {
self.end - self.start
}
#[must_use]
pub const fn overlaps(self, other: Self) -> bool {
self.start < other.end && other.start < self.end
}
#[must_use]
pub fn is_char_aligned_in(self, input: &str) -> bool {
self.end <= input.len()
&& input.is_char_boundary(self.start)
&& input.is_char_boundary(self.end)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DetectorContext {
input_len: usize,
}
impl DetectorContext {
#[must_use]
pub const fn new(input_len: usize) -> Self {
Self { input_len }
}
#[must_use]
pub const fn input_len(self) -> usize {
self.input_len
}
}
pub(crate) struct SignalPack {
names: &'static [&'static str],
strings: OnceLock<Box<[String]>>,
}
impl SignalPack {
pub(crate) const fn new(names: &'static [&'static str]) -> Self {
Self {
names,
strings: OnceLock::new(),
}
}
fn strings(&'static self) -> &'static [String] {
self.strings
.get_or_init(|| self.names.iter().map(|name| (*name).to_owned()).collect())
}
}
macro_rules! signal_pack {
($($name:literal),+ $(,)?) => {{
static PACK: $crate::types::SignalPack = $crate::types::SignalPack::new(&[$($name),+]);
&PACK
}};
}
pub(crate) use signal_pack;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Candidate {
type_name: Cow<'static, str>,
confidence: Confidence,
specificity: Option<Specificity>,
range: ByteRange,
signals: Cow<'static, [String]>,
obfuscation: Obfuscation,
reject_invisible_normalization: bool,
}
impl Candidate {
#[must_use]
pub fn new(type_name: impl Into<String>, confidence: Confidence, range: ByteRange) -> Self {
Self {
type_name: Cow::Owned(type_name.into()),
confidence,
specificity: None,
range,
signals: Cow::Borrowed(&[]),
obfuscation: Obfuscation::None,
reject_invisible_normalization: false,
}
}
#[must_use]
pub(crate) const fn built_in(
type_name: &'static str,
confidence: Confidence,
range: ByteRange,
) -> Self {
Self {
type_name: Cow::Borrowed(type_name),
confidence,
specificity: None,
range,
signals: Cow::Borrowed(&[]),
obfuscation: Obfuscation::None,
reject_invisible_normalization: false,
}
}
#[must_use]
pub const fn with_specificity(mut self, specificity: Specificity) -> Self {
self.specificity = Some(specificity);
self
}
#[must_use]
pub const fn with_obfuscation(mut self, obfuscation: Obfuscation) -> Self {
self.obfuscation = obfuscation;
self
}
#[must_use]
pub(crate) const fn reject_invisible_normalization(mut self) -> Self {
self.reject_invisible_normalization = true;
self
}
#[must_use]
pub fn with_signals<I, S>(mut self, signals: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.signals = Cow::Owned(signals.into_iter().map(Into::into).collect());
self
}
#[must_use]
pub(crate) fn with_signal_pack(mut self, pack: &'static SignalPack) -> Self {
self.signals = Cow::Borrowed(pack.strings());
self
}
#[must_use]
pub fn type_name(&self) -> &str {
&self.type_name
}
pub(crate) const fn type_name_cow(&self) -> &Cow<'static, str> {
&self.type_name
}
#[must_use]
pub const fn confidence(&self) -> Confidence {
self.confidence
}
#[must_use]
pub const fn specificity(&self) -> Option<Specificity> {
self.specificity
}
#[must_use]
pub fn effective_specificity(&self) -> Specificity {
self.specificity.unwrap_or(Specificity::Entropy)
}
#[must_use]
pub const fn range(&self) -> ByteRange {
self.range
}
#[must_use]
pub fn signals(&self) -> &[String] {
&self.signals
}
#[must_use]
pub const fn obfuscation(&self) -> Obfuscation {
self.obfuscation
}
pub(crate) const fn rejects_invisible_normalization(&self) -> bool {
self.reject_invisible_normalization
}
pub(crate) fn shifted(mut self, by: usize) -> Option<Self> {
self.range = ByteRange::new(
self.range.start().checked_add(by)?,
self.range.end().checked_add(by)?,
)?;
Some(self)
}
}
pub trait Detector {
fn id(&self) -> &str;
fn detect(
&self,
input: &str,
context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure>;
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct DetectedFinding {
id: String,
type_name: Cow<'static, str>,
detector: Cow<'static, str>,
confidence: Confidence,
range: ByteRange,
obfuscation: Obfuscation,
}
impl DetectedFinding {
pub fn new(
id: impl Into<String>,
type_name: impl Into<String>,
detector: impl Into<String>,
confidence: Confidence,
range: ByteRange,
) -> Result<Self, SecretScanError> {
let id = id.into();
let type_name = type_name.into();
let detector = detector.into();
if !is_identifier(&id) || !is_identifier(&type_name) || !is_identifier(&detector) {
return Err(SecretScanErrorCode::InvalidCandidate.into());
}
Ok(Self {
id,
type_name: Cow::Owned(type_name),
detector: Cow::Owned(detector),
confidence,
range,
obfuscation: Obfuscation::None,
})
}
pub(crate) fn from_validated(
id: String,
type_name: Cow<'static, str>,
detector: Cow<'static, str>,
confidence: Confidence,
range: ByteRange,
obfuscation: Obfuscation,
) -> Self {
debug_assert!(is_identifier(&id) && is_identifier(&type_name) && is_identifier(&detector));
Self {
id,
type_name,
detector,
confidence,
range,
obfuscation,
}
}
#[must_use]
pub const fn with_obfuscation(mut self, obfuscation: Obfuscation) -> Self {
self.obfuscation = obfuscation;
self
}
#[must_use]
pub(crate) const fn with_range(mut self, range: ByteRange) -> Self {
self.range = range;
self
}
#[must_use]
pub(crate) fn with_id(mut self, id: String) -> Self {
debug_assert!(is_identifier(&id));
self.id = id;
self
}
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
#[must_use]
pub fn type_name(&self) -> &str {
&self.type_name
}
#[must_use]
pub fn detector(&self) -> &str {
&self.detector
}
#[must_use]
pub const fn confidence(&self) -> Confidence {
self.confidence
}
#[must_use]
pub const fn range(&self) -> ByteRange {
self.range
}
#[must_use]
pub const fn obfuscation(&self) -> Obfuscation {
self.obfuscation
}
#[must_use]
pub fn with_action(self, action: Action) -> Finding {
Finding {
detected: self,
action,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Finding {
detected: DetectedFinding,
action: Action,
}
impl Finding {
pub fn new(
id: impl Into<String>,
type_name: impl Into<String>,
detector: impl Into<String>,
confidence: Confidence,
action: Action,
range: ByteRange,
) -> Result<Self, SecretScanError> {
Ok(DetectedFinding::new(id, type_name, detector, confidence, range)?.with_action(action))
}
#[must_use]
pub fn id(&self) -> &str {
self.detected.id()
}
#[must_use]
pub fn type_name(&self) -> &str {
self.detected.type_name()
}
#[must_use]
pub fn detector(&self) -> &str {
self.detected.detector()
}
#[must_use]
pub const fn confidence(&self) -> Confidence {
self.detected.confidence()
}
#[must_use]
pub const fn action(&self) -> Action {
self.action
}
#[must_use]
pub const fn range(&self) -> ByteRange {
self.detected.range()
}
#[must_use]
pub const fn obfuscation(&self) -> Obfuscation {
self.detected.obfuscation()
}
#[must_use]
pub const fn detected(&self) -> &DetectedFinding {
&self.detected
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScanResult {
text: String,
findings: Vec<Finding>,
}
impl ScanResult {
#[must_use]
pub(crate) const fn new(text: String, findings: Vec<Finding>) -> Self {
Self { text, findings }
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn findings(&self) -> &[Finding] {
&self.findings
}
#[must_use]
pub fn into_parts(self) -> (String, Vec<Finding>) {
(self.text, self.findings)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PolicyContext {
finding_index: usize,
finding_count: usize,
}
impl PolicyContext {
#[must_use]
pub const fn new(finding_index: usize, finding_count: usize) -> Self {
Self {
finding_index,
finding_count,
}
}
#[must_use]
pub const fn finding_index(self) -> usize {
self.finding_index
}
#[must_use]
pub const fn finding_count(self) -> usize {
self.finding_count
}
}
pub trait Policy {
fn evaluate(
&self,
finding: &DetectedFinding,
context: &PolicyContext,
) -> Result<Action, PolicyFailure>;
}
impl<F> Policy for F
where
F: Fn(&DetectedFinding, &PolicyContext) -> Result<Action, PolicyFailure>,
{
fn evaluate(
&self,
finding: &DetectedFinding,
context: &PolicyContext,
) -> Result<Action, PolicyFailure> {
self(finding, context)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PlaceholderContext {
placeholder_index: usize,
}
impl PlaceholderContext {
#[must_use]
pub const fn new(placeholder_index: usize) -> Self {
Self { placeholder_index }
}
#[must_use]
pub const fn placeholder_index(self) -> usize {
self.placeholder_index
}
}
pub trait PlaceholderFormatter {
fn format(
&self,
finding: &Finding,
context: &PlaceholderContext,
) -> Result<String, FormatterFailure>;
}
impl<F> PlaceholderFormatter for F
where
F: Fn(&Finding, &PlaceholderContext) -> Result<String, FormatterFailure>,
{
fn format(
&self,
finding: &Finding,
context: &PlaceholderContext,
) -> Result<String, FormatterFailure> {
self(finding, context)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identifier_grammar() {
for valid in [
"a",
"a1",
"aws.access-key_id",
"x-y",
"github_token",
"a.b.c",
] {
assert!(is_identifier(valid), "{valid}");
}
for invalid in [
"", "A", "1a", "a-", "-a", "a--b", "a.-b", "a b", "a_", "a/b", "ä", "a\n",
] {
assert!(!is_identifier(invalid), "{invalid:?}");
}
let longest = "a".repeat(MAX_IDENTIFIER_LENGTH);
assert!(is_identifier(&longest));
assert!(!is_identifier(&format!("{longest}a")));
}
#[test]
fn enum_names_round_trip() {
for confidence in [Confidence::High, Confidence::Medium, Confidence::Low] {
assert_eq!(Confidence::from_name(confidence.as_str()), Some(confidence));
}
for specificity in [
Specificity::PrivateKey,
Specificity::Provider,
Specificity::Structural,
Specificity::Contextual,
Specificity::Entropy,
] {
assert_eq!(
Specificity::from_name(specificity.as_str()),
Some(specificity)
);
}
for action in [Action::Redact, Action::Block, Action::Warn, Action::Allow] {
assert_eq!(Action::from_name(action.as_str()), Some(action));
}
for obfuscation in [Obfuscation::None, Obfuscation::InvisibleCharacters] {
assert_eq!(
Obfuscation::from_name(obfuscation.as_str()),
Some(obfuscation)
);
}
assert_eq!(Confidence::from_name("HIGH"), None);
assert_eq!(Specificity::from_name("private_key"), None);
assert_eq!(Action::from_name(""), None);
assert_eq!(Obfuscation::from_name("None"), None);
}
#[test]
fn precedence_orderings() {
assert!(Confidence::High > Confidence::Medium && Confidence::Medium > Confidence::Low);
assert!(Specificity::PrivateKey > Specificity::Provider);
assert!(Specificity::Provider > Specificity::Structural);
assert!(Specificity::Structural > Specificity::Contextual);
assert!(Specificity::Contextual > Specificity::Entropy);
assert!(Action::Redact.replaces_text() && Action::Block.replaces_text());
assert!(!Action::Warn.replaces_text() && !Action::Allow.replaces_text());
}
#[test]
fn byte_range_rejects_empty_and_reversed() {
assert_eq!(ByteRange::new(3, 3), None);
assert_eq!(ByteRange::new(4, 3), None);
let range = ByteRange::new(2, 5).unwrap();
assert_eq!((range.start(), range.end(), range.len()), (2, 5, 3));
assert!(range.overlaps(ByteRange::new(4, 9).unwrap()));
assert!(range.overlaps(ByteRange::new(0, 3).unwrap()));
assert!(!range.overlaps(ByteRange::new(5, 6).unwrap()));
assert!(!range.overlaps(ByteRange::new(0, 2).unwrap()));
}
#[test]
fn unicode_conversion_corpus_byte_offsets_are_char_aligned() {
let cases = [
(
"🔑 TOKEN_SYNTHETIC_REVOKED_VALUE",
5,
34,
"TOKEN_SYNTHETIC_REVOKED_VALUE",
),
(
"TOKEN_🔑_SYNTHETIC_REVOKED",
0,
28,
"TOKEN_🔑_SYNTHETIC_REVOKED",
),
(
"TOKEN_SYNTHETIC_REVOKED_VALUE 🔑",
0,
29,
"TOKEN_SYNTHETIC_REVOKED_VALUE",
),
(
"e\u{0301} TOKEN_SYNTHETIC_REVOKED_VALUE",
4,
33,
"TOKEN_SYNTHETIC_REVOKED_VALUE",
),
(
"é”® TOKEN_SYNTHETIC_REVOKED_VALUE",
4,
33,
"TOKEN_SYNTHETIC_REVOKED_VALUE",
),
(
"\u{201c}TOKEN_SYNTHETIC_REVOKED_VALUE\u{201d}",
3,
32,
"TOKEN_SYNTHETIC_REVOKED_VALUE",
),
(
"TOKEN_\u{200B}_SYNTHETIC_REVOKED_VALUE",
0,
33,
"TOKEN_\u{200B}_SYNTHETIC_REVOKED_VALUE",
),
(
"TOKEN_\u{00AD}\u{00AD}\u{2060}_SYNTHETIC_REVOKED_VALUE",
0,
37,
"TOKEN_\u{00AD}\u{00AD}\u{2060}_SYNTHETIC_REVOKED_VALUE",
),
(
"TOKEN_\u{E0041}_SYNTHETIC_REVOKED",
0,
28,
"TOKEN_\u{E0041}_SYNTHETIC_REVOKED",
),
];
for (input, start, end, expected_slice) in cases {
let range = ByteRange::new(start, end).unwrap();
assert!(range.is_char_aligned_in(input), "{input:?}");
assert_eq!(&input[start..end], expected_slice, "{input:?}");
}
}
#[test]
fn byte_range_char_alignment() {
let input = "a😀b";
assert!(ByteRange::new(1, 5).unwrap().is_char_aligned_in(input));
assert!(!ByteRange::new(2, 5).unwrap().is_char_aligned_in(input));
assert!(!ByteRange::new(1, 3).unwrap().is_char_aligned_in(input));
assert!(!ByteRange::new(5, 7).unwrap().is_char_aligned_in(input));
assert!(ByteRange::new(5, 6).unwrap().is_char_aligned_in(input));
}
#[test]
fn candidate_defaults_to_entropy_specificity() {
let range = ByteRange::new(0, 4).unwrap();
let candidate = Candidate::new("token", Confidence::Low, range);
assert_eq!(candidate.specificity(), None);
assert_eq!(candidate.effective_specificity(), Specificity::Entropy);
assert!(candidate.signals().is_empty());
assert_eq!(candidate.obfuscation(), Obfuscation::None);
let candidate = candidate
.with_specificity(Specificity::Provider)
.with_signals(["prefix"])
.with_obfuscation(Obfuscation::InvisibleCharacters);
assert_eq!(candidate.effective_specificity(), Specificity::Provider);
assert_eq!(candidate.signals(), ["prefix"]);
assert_eq!(candidate.type_name(), "token");
assert_eq!(candidate.range(), range);
assert_eq!(candidate.obfuscation(), Obfuscation::InvisibleCharacters);
}
fn signal_candidate() -> Candidate {
Candidate::new("token", Confidence::Low, ByteRange::new(0, 4).unwrap())
}
#[test]
fn a_signal_pack_equals_with_signals_for_the_same_labels() {
use std::hash::{BuildHasher, BuildHasherDefault, DefaultHasher};
let packed = signal_candidate().with_signal_pack(signal_pack!("alpha-label", "beta-label"));
let owned = signal_candidate().with_signals(["alpha-label", "beta-label"]);
assert_eq!(packed.signals(), ["alpha-label", "beta-label"]);
assert_eq!(packed.signals(), owned.signals());
assert_eq!(packed, owned);
let hasher = BuildHasherDefault::<DefaultHasher>::default();
assert_eq!(hasher.hash_one(&packed), hasher.hash_one(&owned));
assert_eq!(format!("{packed:?}"), format!("{owned:?}"));
let _: &[String] = packed.signals();
}
#[test]
fn candidates_from_one_pack_share_a_single_slice_and_clone_keeps_it() {
fn built() -> Candidate {
signal_candidate().with_signal_pack(signal_pack!("shared-one", "shared-two"))
}
let first = built();
let second = built();
assert!(std::ptr::eq(first.signals(), second.signals()));
assert!(std::ptr::eq(first.signals(), first.clone().signals()));
let other = signal_candidate().with_signal_pack(signal_pack!("shared-one", "shared-two"));
assert!(!std::ptr::eq(first.signals(), other.signals()));
let owned = signal_candidate().with_signals(["shared-one", "shared-two"]);
assert!(!std::ptr::eq(first.signals(), owned.signals()));
assert!(!std::ptr::eq(owned.signals(), owned.clone().signals()));
assert_eq!(owned.clone().signals(), first.signals());
}
#[test]
fn with_signals_and_with_signal_pack_each_replace_the_previous_list() {
let packed_then_owned = signal_candidate()
.with_signal_pack(signal_pack!("first-pack"))
.with_signals(["custom"]);
assert_eq!(packed_then_owned.signals(), ["custom"]);
let owned_then_packed = signal_candidate()
.with_signals(["custom"])
.with_signal_pack(signal_pack!("second-pack"));
assert_eq!(owned_then_packed.signals(), ["second-pack"]);
let dynamic = signal_candidate().with_signals([format!("dyn-{}", 7)]);
assert_eq!(dynamic.signals(), ["dyn-7"]);
}
#[test]
fn finding_constructors_validate_identifiers() {
let range = ByteRange::new(0, 1).unwrap();
let finding = Finding::new(
"finding-1",
"jwt",
"jwt",
Confidence::High,
Action::Redact,
range,
)
.unwrap();
assert_eq!(finding.id(), "finding-1");
assert_eq!(finding.detected().detector(), "jwt");
assert_eq!(finding.action(), Action::Redact);
assert_eq!(finding.obfuscation(), Obfuscation::None);
assert_eq!(finding.detected().obfuscation(), Obfuscation::None);
let obfuscated = DetectedFinding::new("finding-2", "jwt", "jwt", Confidence::High, range)
.unwrap()
.with_obfuscation(Obfuscation::InvisibleCharacters)
.with_action(Action::Redact);
assert_eq!(obfuscated.obfuscation(), Obfuscation::InvisibleCharacters);
for (id, type_name, detector) in [
("Finding-1", "jwt", "jwt"),
("finding-1", "JWT", "jwt"),
("finding-1", "jwt", "jwt "),
] {
let error = Finding::new(
id,
type_name,
detector,
Confidence::High,
Action::Redact,
range,
)
.unwrap_err();
assert_eq!(error.code(), SecretScanErrorCode::InvalidCandidate);
}
}
#[test]
fn closures_implement_policy_and_formatter() {
let policy = |_: &DetectedFinding, context: &PolicyContext| {
if context.finding_index() < context.finding_count() {
Ok(Action::Warn)
} else {
Err(PolicyFailure)
}
};
let finding = DetectedFinding::new(
"finding-1",
"t",
"d",
Confidence::Low,
ByteRange::new(0, 1).unwrap(),
)
.unwrap();
assert_eq!(
policy.evaluate(&finding, &PolicyContext::new(0, 1)),
Ok(Action::Warn)
);
assert_eq!(
policy.evaluate(&finding, &PolicyContext::new(1, 1)),
Err(PolicyFailure)
);
let formatter = |_: &Finding, context: &PlaceholderContext| {
Ok(format!("<SECRET_{}>", context.placeholder_index()))
};
let finding = finding.with_action(Action::Redact);
assert_eq!(
formatter
.format(&finding, &PlaceholderContext::new(3))
.as_deref(),
Ok("<SECRET_3>")
);
}
}