redact-secret 0.1.0-beta.15

Deterministic secret detection and redaction for runtime data and AI context: scans text locally before it reaches logs, storage, telemetry, tools, or model context. Side-effect free: no runtime network, filesystem, environment, telemetry, secret storage, or UI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Non-enforcing shadow comparison of the evidence scorer with the legacy
//! decision (issue #771,
//! `decision-freeze-the-shadow-evidence-score-and-confidence-contract`
//! sections 2, 5 and 8).
//!
//! The pipeline records one [`ShadowComparison`] per candidate that overlap
//! resolution selects, but only when a caller asks for it
//! (`crate::pipeline::detect`). Every public entry point asks for nothing, so
//! the scorer never runs on the public hot path and nothing here can reach a
//! `Finding`, an error, a placeholder, a policy input or a log. The legacy
//! `Confidence`, action, range and overlap weight are read, never written.
//!
//! [`shadow_evaluation_jsonl`] is the maintainer-local evaluation path: it
//! renders the comparisons of one input as JSON Lines for
//! `crates/secret-scan-core/examples/shadow_evaluation.rs`, which compiles
//! this source as its own crate (`docs/specs/engine.md`, "Maintainer-local
//! shadow evaluation"). A record holds identifiers from closed static sets,
//! integers, the detector id and candidate type, and the caller's record id.
//! It holds no part of the matched value, no substring, class run or hash of
//! it (ADR section 8).

use std::fmt::Write as _;

use super::aggregate::{
    EvidenceGroup, SHADOW_MODEL, ShadowAuthority, ShadowBand, ShadowEvidence, shadow_evidence,
};
use super::features::FEATURE_SCHEMA_VERSION;
use crate::error::SecretScanError;
use crate::limits::WholeInputLimits;
use crate::policy::default_action_for;
use crate::registry::DetectorRegistry;
use crate::types::{Action, ByteRange, Candidate, Confidence, Specificity};

/// Identity of the JSON Lines format [`shadow_evaluation_jsonl`] and
/// [`shadow_evaluation_header`] write. A change to a field, its meaning or a
/// reason code is a new identity.
pub(crate) const SHADOW_EVALUATION_FORMAT: &str = "redact-secret/shadow-evaluation/1";

/// Every reason code a comparison may carry, in the order a record lists
/// them. A closed static set (ADR section 8).
#[cfg(test)]
pub(crate) const REASON_CODES: [&str; 9] = [
    "deterministic-authority",
    "credential-context",
    "no-credential-context",
    "randomness-capped",
    "randomness-partial",
    "randomness-none",
    "strict-exclusion",
    "no-positive-evidence",
    "band-none",
];

/// What a hypothetical future promotion of the shadow band would do to the
/// legacy `Confidence`. Beta.9 applies none of them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum PromotionOutcome {
    /// The band is above the legacy `Confidence`.
    Promote,
    /// The band is below the legacy `Confidence` (`none` included).
    Demote,
    /// The band equals the legacy `Confidence`, or the scorer was not
    /// consulted.
    Preserve,
}

impl PromotionOutcome {
    /// The record's name for the outcome.
    #[must_use]
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Promote => "promote",
            Self::Demote => "demote",
            Self::Preserve => "preserve",
        }
    }

    /// The outcome of promoting `band` over `legacy`.
    #[must_use]
    pub(crate) fn of(band: ShadowBand, legacy: Confidence) -> Self {
        match band.cmp(&ShadowBand::of_confidence(legacy)) {
            std::cmp::Ordering::Greater => Self::Promote,
            std::cmp::Ordering::Less => Self::Demote,
            std::cmp::Ordering::Equal => Self::Preserve,
        }
    }
}

/// The name a record uses for a band.
#[must_use]
pub(crate) const fn band_name(band: ShadowBand) -> &'static str {
    match band {
        ShadowBand::None => "none",
        ShadowBand::Low => "low",
        ShadowBand::Medium => "medium",
        ShadowBand::High => "high",
    }
}

/// The name a record uses for an authority.
#[must_use]
const fn authority_name(authority: ShadowAuthority) -> &'static str {
    match authority {
        ShadowAuthority::Deterministic => "deterministic",
        ShadowAuthority::Statistical => "statistical",
    }
}

/// The legacy decision of one selected candidate next to its shadow evidence
/// result. Nothing here is applied to anything.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ShadowComparison {
    /// Zero-based index of the finding this candidate became, in the order
    /// the caller numbers findings (`finding-{index + 1}`).
    pub(crate) finding_index: usize,
    /// The finding's range, in UTF-8 bytes of the caller's input.
    pub(crate) range: ByteRange,
    /// The emitting detector's registered id.
    pub(crate) detector: String,
    /// The candidate's type.
    pub(crate) type_name: String,
    /// The candidate's effective specificity.
    pub(crate) specificity: Specificity,
    /// The legacy `Confidence`, as the finding carries it.
    pub(crate) legacy_confidence: Confidence,
    /// The default policy's action for the legacy finding.
    pub(crate) legacy_action: Action,
    /// The shadow evidence result.
    pub(crate) shadow: ShadowEvidence,
    /// What promoting `shadow.band` over the legacy `Confidence` would do.
    pub(crate) promotion: PromotionOutcome,
}

impl ShadowComparison {
    /// Evaluates `candidate`, whose matched text in the scan copy is `value`,
    /// under [`SHADOW_MODEL`]. `range` is where the finding lies in the
    /// caller's input.
    #[must_use]
    pub(crate) fn of(
        finding_index: usize,
        range: ByteRange,
        detector: &str,
        candidate: &Candidate,
        value: &str,
    ) -> Self {
        let shadow = shadow_evidence(&SHADOW_MODEL, candidate, value);
        let legacy_confidence = candidate.confidence();
        let promotion = match shadow.authority {
            ShadowAuthority::Deterministic => PromotionOutcome::Preserve,
            ShadowAuthority::Statistical => PromotionOutcome::of(shadow.band, legacy_confidence),
        };
        Self {
            finding_index,
            range,
            detector: detector.to_owned(),
            type_name: candidate.type_name().to_owned(),
            specificity: candidate.effective_specificity(),
            legacy_confidence,
            legacy_action: default_action_for(candidate.type_name(), legacy_confidence),
            shadow,
            promotion,
        }
    }

    /// Moves the comparison by `findings` findings and `bytes` bytes, for an
    /// incremental unit that starts after them.
    #[must_use]
    pub(crate) fn shifted(mut self, findings: usize, bytes: usize) -> Option<Self> {
        self.finding_index = self.finding_index.checked_add(findings)?;
        self.range = ByteRange::new(
            self.range.start().checked_add(bytes)?,
            self.range.end().checked_add(bytes)?,
        )?;
        Some(self)
    }

    /// The reason codes of this comparison: a subset of the closed set
    /// `REASON_CODES`, in that set's order.
    #[must_use]
    pub(crate) fn reasons(&self) -> Vec<&'static str> {
        let Some(explanation) = self.shadow.explanation else {
            return vec!["deterministic-authority"];
        };
        let group = |group: EvidenceGroup| explanation.groups[group as usize];
        let mut reasons = Vec::with_capacity(4);
        reasons.push(if group(EvidenceGroup::Contextual).contribution > 0 {
            "credential-context"
        } else {
            "no-credential-context"
        });
        let randomness = group(EvidenceGroup::Randomness);
        let randomness_cap = SHADOW_MODEL.groups[EvidenceGroup::Randomness as usize].cap;
        reasons.push(if randomness.contribution == 0 {
            "randomness-none"
        } else if randomness.contribution >= randomness_cap || randomness.is_capped() {
            "randomness-capped"
        } else {
            "randomness-partial"
        });
        if explanation.exclusion.is_some() {
            reasons.push("strict-exclusion");
        }
        if explanation.positive == 0 {
            reasons.push("no-positive-evidence");
        }
        if explanation.band == ShadowBand::None {
            reasons.push("band-none");
        }
        reasons
    }

    /// Appends this comparison as one JSON object and a newline, for the
    /// input the caller identifies as `input_id`. Fields are written in a
    /// fixed order, so equal comparisons render to equal bytes.
    pub(crate) fn write_jsonl(&self, input_id: &str, out: &mut String) {
        out.push_str("{\"record\":\"shadow-comparison\",\"input\":");
        push_json_string(out, input_id);
        let _ = write!(
            out,
            ",\"finding\":\"finding-{}\",\"start\":{},\"end\":{},\"byteLength\":{}",
            self.finding_index.saturating_add(1),
            self.range.start(),
            self.range.end(),
            self.range.len(),
        );
        out.push_str(",\"detector\":");
        push_json_string(out, &self.detector);
        out.push_str(",\"type\":");
        push_json_string(out, &self.type_name);
        let _ = write!(
            out,
            ",\"specificity\":\"{}\",\"legacyConfidence\":\"{}\",\"legacyAction\":\"{}\",\"authority\":\"{}\",\"model\":\"{}\",\"featureSchema\":\"{}\"",
            self.specificity.as_str(),
            self.legacy_confidence.as_str(),
            self.legacy_action.as_str(),
            authority_name(self.shadow.authority),
            SHADOW_MODEL.id,
            FEATURE_SCHEMA_VERSION,
        );
        match self.shadow.explanation {
            None => out.push_str(
                ",\"contextClass\":null,\"exclusion\":null,\"groups\":null,\"signals\":[],\"score\":null",
            ),
            Some(explanation) => {
                let _ = write!(
                    out,
                    ",\"contextClass\":\"{}\"",
                    explanation.context.as_str()
                );
                match explanation.exclusion {
                    Some(grammar) => {
                        let _ = write!(out, ",\"exclusion\":\"{}\"", grammar.as_str());
                    }
                    None => out.push_str(",\"exclusion\":null"),
                }
                out.push_str(",\"groups\":{");
                for (index, group) in explanation.groups.iter().enumerate() {
                    let separator = if index == 0 { "" } else { "," };
                    let _ = write!(
                        out,
                        "{separator}\"{}\":{}",
                        group.group.as_str(),
                        group.contribution
                    );
                }
                out.push_str("},\"signals\":[");
                let mut first = true;
                for group in &explanation.groups {
                    for signal in group.signals() {
                        let separator = if first { "" } else { "," };
                        first = false;
                        let _ = write!(
                            out,
                            "{separator}{{\"group\":\"{}\",\"signal\":\"{}\",\"points\":{}}}",
                            group.group.as_str(),
                            signal.signal,
                            signal.points
                        );
                    }
                }
                let _ = write!(
                    out,
                    "],\"positive\":{},\"negative\":{},\"score\":{}",
                    explanation.positive, explanation.negative, explanation.score
                );
            }
        }
        let _ = write!(
            out,
            ",\"band\":\"{}\",\"promotion\":\"{}\",\"reasons\":[",
            band_name(self.shadow.band),
            self.promotion.as_str()
        );
        for (index, reason) in self.reasons().iter().enumerate() {
            let separator = if index == 0 { "" } else { "," };
            let _ = write!(out, "{separator}\"{reason}\"");
        }
        out.push_str("]}\n");
    }
}

/// Appends `value` as a JSON string literal.
fn push_json_string(out: &mut String, value: &str) {
    out.push('"');
    for character in value.chars() {
        match character {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            control if u32::from(control) < 0x20 => {
                let _ = write!(out, "\\u{:04x}", u32::from(control));
            }
            other => out.push(other),
        }
    }
    out.push('"');
}

/// The first line of a maintainer-local evaluation: the format, product
/// version and scorer identities, the scoring artifact's `revision` and
/// `modelFingerprint` as the caller read them, and the detector profile.
#[cfg_attr(
    not(test),
    allow(
        dead_code,
        reason = "called only by examples/shadow_evaluation.rs, which compiles the core source as its own crate"
    )
)]
#[must_use]
pub(crate) fn shadow_evaluation_header(
    artifact_revision: u64,
    model_fingerprint: &str,
    profile: &str,
) -> String {
    let mut out = String::new();
    let _ = write!(
        out,
        "{{\"record\":\"shadow-evaluation\",\"format\":\"{SHADOW_EVALUATION_FORMAT}\",\"productVersion\":\"{}\",\"model\":\"{}\",\"featureSchema\":\"{FEATURE_SCHEMA_VERSION}\",\"artifactRevision\":{artifact_revision},\"modelFingerprint\":",
        crate::VERSION,
        SHADOW_MODEL.id,
    );
    push_json_string(&mut out, model_fingerprint);
    out.push_str(",\"profile\":");
    push_json_string(&mut out, profile);
    out.push_str(",\"path\":\"whole-input\"}\n");
    out
}

/// The maintainer-local evaluation of one input (`docs/specs/engine.md`,
/// "Maintainer-local shadow evaluation").
///
/// Applies the default [`WholeInputLimits`] exactly as `crate::scan` does,
/// runs the same detection and overlap resolution, and returns one
/// `shadow-comparison` line per finding, ordered by input offset, or one
/// `shadow-error` line carrying only the sanitized error code. The legacy
/// findings are the ones `crate::scan` with `crate::DefaultPolicy` returns
/// for the same input and registry.
///
/// No public item reaches this function. Its only non-test caller is the
/// evaluation example, which compiles this source as its own crate, so the
/// core library's own build sees no caller.
#[cfg_attr(
    not(test),
    allow(
        dead_code,
        reason = "called only by examples/shadow_evaluation.rs, which compiles the core source as its own crate"
    )
)]
#[must_use]
pub(crate) fn shadow_evaluation_jsonl(
    input_id: &str,
    input: &str,
    registry: &DetectorRegistry,
) -> String {
    let mut out = String::new();
    match evaluate(input, registry) {
        Ok(comparisons) => {
            for comparison in &comparisons {
                comparison.write_jsonl(input_id, &mut out);
            }
        }
        Err(error) => {
            out.push_str("{\"record\":\"shadow-error\",\"input\":");
            push_json_string(&mut out, input_id);
            let _ = writeln!(out, ",\"code\":\"{}\"}}", error.code().as_str());
        }
    }
    out
}

/// The comparisons of `input` under the default whole-input limits.
fn evaluate(
    input: &str,
    registry: &DetectorRegistry,
) -> Result<Vec<ShadowComparison>, SecretScanError> {
    let limits = WholeInputLimits::default();
    limits.check_input(input)?;
    let mut comparisons = Vec::new();
    let detected = crate::pipeline::detect(input, registry, Some(&mut comparisons))?;
    limits.check_findings(detected.len())?;
    Ok(comparisons)
}

#[cfg(test)]
mod tests;