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
408
409
410
411
//! Postman API key detection.
//!
//! Postman's own documentation (`learning.postman.com/docs/reference/postman-api/authentication`,
//! observed 2026-09-22) explains only how to generate and send an API key
//! (the `X-API-Key` header or a `postman-api-key` variable); it publishes no
//! prefix, length, or alphabet for the key value itself. With the provider
//! silent, the reviewed grammar rests on tool agreement alone, the same
//! evidence bar `super::linear`'s `lin_oauth_` guard and
//! `super::additional_providers`'s support-policy shapes already use:
//!
//! - gitleaks 8.30.1's `postman-api-token` rule:
//!   `\b(PMAK-(?i)[a-f0-9]{24}\-[a-f0-9]{34})(?:[\x60'"\s;]|\\[nr]|$)` — a
//!   literal, case-sensitive `PMAK-` prefix, then a case-insensitive
//!   24-hex-byte segment, a literal `-`, and a case-insensitive 34-hex-byte
//!   segment.
//! - trufflehog 3.97.4's `postman` detector: `\b(PMAK-[a-zA-Z-0-9]{59})\b` —
//!   the same literal prefix, then 59 bytes of `[A-Za-z0-9-]` with no
//!   internal structure asserted.
//!
//! Both tools agree on the prefix and on a 59-byte body
//! (`24 + 1 + 34 == 59`); gitleaks additionally corroborates an internal
//! hex-hex structure with the dash fixed at byte 24 of the body.
//! Trufflehog's looser `[A-Za-z0-9-]{59}` is a strict superset of that
//! structured shape (it also accepts non-hex letters and a dash anywhere),
//! so it does not contradict gitleaks' narrower rule — the same
//! "adopt the more specific, evidence-backed shape" reasoning
//! `super::cloudflare`'s checksum-tail [`pattern::PostCheck`] already
//! applies. The reviewed grammar is therefore gitleaks' structured shape: a
//! body missing the internal dash, or one with the dash at any other
//! position, is an intentional false negative rather than a fuzzy match
//! against the looser union.
//!
//! ## Collection access keys (issue #700)
//!
//! Postman documents a second credential, the collection access key
//! (`learning.postman.com/docs/collaborating-in-postman/sharing/` and
//! `.../developer/postman-api/authentication/`, observed 2026-09-24). It
//! grants read-only access to one collection's JSON, expires after 60 days
//! of inactivity, and Postman tells users to remove sensitive data before
//! sharing one. The docs show only a masked `PMAT-` key and state no
//! length. GitLab's secret-detection rules match `PMAT-` + 26 `[A-Z0-9]`,
//! and GitHub secret scanning lists partner pattern `postman_collection_key`.
//! It is a separate family, [`POSTMAN_COLLECTION_ACCESS_KEY`], reported as
//! `postman_collection_access_key`: `PMAT-` followed by exactly 26
//! [`pattern::is_alnum`] bytes. The case is read loosely because only one
//! tool states it. Tier T2.
//!
//! No other Postman credential form (a collection or workspace id, a
//! Postman Vault variable reference, a redacted export placeholder) is
//! documented or tool-corroborated with its own grammar, so none is given
//! one here — the issue's stated exclusion boundary.
//!
//! The 59-byte run mixes two alphabets at fixed offsets (hex, then a single
//! literal dash, then hex again) that no single [`Alphabet`] predicate can
//! express, so — mirroring [`super::cloudflare`]'s checksum-tail shape —
//! this stays one [`PrefixShape::exact`] over the combined
//! [`pattern::is_hex_or_dash`] alphabet, narrowed by a
//! [`pattern::PostCheck`] that pins the dash to its documented offset and
//! requires hex on both sides.

use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, Alphabet, PrefixShape};

const PREFIX: &str = "PMAK-";
/// The gitleaks-corroborated first hex segment's length.
const HEX_SEGMENT_1_LEN: usize = 24;
/// The gitleaks-corroborated second hex segment's length.
const HEX_SEGMENT_2_LEN: usize = 34;
/// The literal separator's own single-byte width.
const SEPARATOR_LEN: usize = 1;
/// The whole body's length: both tools agree `24 + 1 + 34 == 59`.
const BODY_LEN: usize = HEX_SEGMENT_1_LEN + SEPARATOR_LEN + HEX_SEGMENT_2_LEN;
/// The dash's fixed offset within the body, counted from the body's own
/// start (i.e. immediately after [`PREFIX`]).
const DASH_OFFSET: usize = HEX_SEGMENT_1_LEN;

const SIGNALS: [&str; 2] = ["postman-documented-prefix", "hex-hex-structured-body"];

/// A value is never a slice of a wider `[A-Za-z0-9_-]` identifier, the same
/// boundary rule every other prefixed provider in this crate shares.
const BOUNDARY: Alphabet = pattern::is_alnum_dash;

/// `true` when the matched run's dash sits at exactly [`DASH_OFFSET`] and
/// every other byte on both sides is hex — the internal structure
/// [`BODY_LEN`]'s combined [`pattern::is_hex_or_dash`] alphabet alone cannot
/// express, because that alphabet accepts a dash (or a run of them) at any
/// offset.
fn body_is_hex_dash_hex(bytes: &[u8], _start: usize, end: usize) -> bool {
    let body_start = end - BODY_LEN;
    let dash_index = body_start + DASH_OFFSET;
    bytes[dash_index] == b'-'
        && bytes[body_start..dash_index]
            .iter()
            .copied()
            .all(pattern::is_hex)
        && bytes[dash_index + 1..end]
            .iter()
            .copied()
            .all(pattern::is_hex)
}

/// Requires the exact `PMAK-<24 hex>-<34 hex>` shape (case-insensitive hex).
/// A body missing the internal dash, one with the dash at any other offset,
/// a non-hex byte in either hex segment, or a run embedded in a wider
/// identifier is an intentional false negative rather than a fuzzy match.
pub(super) const POSTMAN: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
    "postman-api-key",
    "postman_api_key",
    &[
        PrefixShape::exact(PREFIX, BODY_LEN, pattern::is_hex_or_dash, &SIGNALS)
            .with_post_check(body_is_hex_dash_hex),
    ],
    BOUNDARY,
);

const COLLECTION_ACCESS_KEY_PREFIX: &str = "PMAT-";
/// GitLab's rule body length for a collection access key.
const COLLECTION_ACCESS_KEY_BODY_LEN: usize = 26;
const COLLECTION_ACCESS_KEY_SIGNALS: [&str; 2] = [
    "postman-collection-access-key-prefix",
    "tool-corroborated-length",
];

/// Rejects a body that is one repeated character, a masked placeholder.
fn collection_access_key_is_not_filler(bytes: &[u8], start: usize, end: usize) -> bool {
    let body = &bytes[start + COLLECTION_ACCESS_KEY_PREFIX.len()..end];
    body.iter().any(|&byte| byte != body[0])
}

/// Requires `PMAT-` followed by exactly 26 `[A-Za-z0-9]` bytes that are not
/// one repeated character (issue #700; see the module doc).
pub(super) const POSTMAN_COLLECTION_ACCESS_KEY: KnownFormatProviderDetector =
    KnownFormatProviderDetector::new(
        "postman-collection-access-key",
        "postman_collection_access_key",
        &[PrefixShape::exact(
            COLLECTION_ACCESS_KEY_PREFIX,
            COLLECTION_ACCESS_KEY_BODY_LEN,
            pattern::is_alnum,
            &COLLECTION_ACCESS_KEY_SIGNALS,
        )
        .with_post_check(collection_access_key_is_not_filler)],
        BOUNDARY,
    );

#[cfg(test)]
mod collection_access_key_tests {
    use super::*;
    use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};

    /// Exactly 26 bytes. Locally constructed synthetic value; never issued
    /// by Postman.
    const BODY: &str = "SynthRevoked0Postman0Pmat1";
    const _: () = assert!(BODY.len() == COLLECTION_ACCESS_KEY_BODY_LEN);

    fn key() -> String {
        format!("{COLLECTION_ACCESS_KEY_PREFIX}{BODY}")
    }

    fn detect(input: &str) -> Vec<Candidate> {
        POSTMAN_COLLECTION_ACCESS_KEY
            .detect(input, &DetectorContext::new(input.len()))
            .unwrap()
    }

    #[test]
    fn detects_a_key_bare_and_in_a_share_url() {
        let key = key();
        for input in [
            key.clone(),
            format!(
                "https://api.getpostman.com/collections/12345678-0000-0000-0000-000000000000?access_key={key}"
            ),
            format!("POSTMAN_COLLECTION_ACCESS_KEY={key}"),
        ] {
            let candidates = detect(&input);
            assert_eq!(candidates.len(), 1, "{input}");
            assert_eq!(candidates[0].type_name(), "postman_collection_access_key");
            assert_eq!(candidates[0].confidence(), Confidence::High);
            assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
            let start = input.find(&key).unwrap();
            assert_eq!(
                candidates[0].range(),
                ByteRange::new(start, start + key.len()).unwrap()
            );
        }
    }

    #[test]
    fn accepts_a_lowercase_body() {
        assert_eq!(
            detect(&format!("PMAT-{}", BODY.to_ascii_lowercase())).len(),
            1
        );
    }

    #[test]
    fn rejects_the_wrong_length_filler_and_embedding() {
        for input in [
            format!("PMAT-{}", &BODY[..25]),
            format!("PMAT-{BODY}X"),
            format!("PMAT-{}", "*".repeat(26)),
            format!("PMAT-{}", "X".repeat(26)),
            format!("xPMAT-{BODY}"),
            format!("PMAT-{BODY}-x"),
            "PMAT-<your-collection-access-key>".to_owned(),
        ] {
            assert!(detect(&input).is_empty(), "{input}");
        }
    }

    #[test]
    fn the_api_key_prefix_is_not_a_collection_access_key() {
        assert!(detect(&format!("PMAK-{BODY}")).is_empty());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};

    /// Exactly [`HEX_SEGMENT_1_LEN`] lowercase-hex bytes. Locally
    /// constructed synthetic value; never provider-issued.
    const HEX_SEGMENT_1: &str = "0123456789abcdef01234567";
    const _: () = assert!(HEX_SEGMENT_1.len() == HEX_SEGMENT_1_LEN);
    /// Exactly [`HEX_SEGMENT_2_LEN`] lowercase-hex bytes.
    const HEX_SEGMENT_2: &str = "0123456789abcdef0123456789abcdef01";
    const _: () = assert!(HEX_SEGMENT_2.len() == HEX_SEGMENT_2_LEN);

    fn detect(input: &str) -> Vec<Candidate> {
        POSTMAN
            .detect(input, &DetectorContext::new(input.len()))
            .unwrap()
    }

    fn token() -> String {
        format!("{PREFIX}{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")
    }

    #[test]
    fn detects_the_api_key_with_provider_specificity() {
        let value = token();
        let candidates = detect(&value);
        assert_eq!(candidates.len(), 1);
        assert_eq!(candidates[0].type_name(), "postman_api_key");
        assert_eq!(candidates[0].confidence(), Confidence::High);
        assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(0, value.len()).unwrap()
        );
    }

    #[test]
    fn accepts_uppercase_hex_segments() {
        let value = format!(
            "{PREFIX}{}-{}",
            HEX_SEGMENT_1.to_ascii_uppercase(),
            HEX_SEGMENT_2.to_ascii_uppercase()
        );
        let candidates = detect(&value);
        assert_eq!(candidates.len(), 1);
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(0, value.len()).unwrap()
        );
    }

    #[test]
    fn accepts_mixed_case_hex_segments() {
        let value = format!("{PREFIX}0123456789ABCdef01234567-{HEX_SEGMENT_2}");
        assert_eq!(detect(&value).len(), 1);
    }

    /// gitleaks' rule -- the reviewed contract -- pins the dash to byte 24;
    /// a bare 59-hex-byte run with no separator is trufflehog's looser
    /// shape only, not this family's contract.
    #[test]
    fn rejects_a_body_with_no_internal_dash() {
        let bare = format!("{HEX_SEGMENT_1}0{HEX_SEGMENT_2}");
        assert_eq!(bare.len(), BODY_LEN);
        assert!(detect(&format!("{PREFIX}{bare}")).is_empty());
    }

    #[test]
    fn rejects_a_dash_at_the_wrong_offset() {
        // The dash lands one byte early; the run is still the documented
        // total length, but the structural post-check must still reject it.
        let value = format!(
            "{PREFIX}{}-{}0",
            &HEX_SEGMENT_1[..HEX_SEGMENT_1_LEN - 1],
            HEX_SEGMENT_2
        );
        assert!(detect(&value).is_empty());
    }

    #[test]
    fn rejects_a_second_dash_in_place_of_a_hex_byte() {
        let mut segment_2 = HEX_SEGMENT_2.to_string();
        segment_2.replace_range(5..6, "-");
        assert!(detect(&format!("{PREFIX}{HEX_SEGMENT_1}-{segment_2}")).is_empty());
    }

    #[test]
    fn rejects_a_body_one_byte_short_of_the_documented_length() {
        let short = &HEX_SEGMENT_2[..HEX_SEGMENT_2_LEN - 1];
        assert!(detect(&format!("{PREFIX}{HEX_SEGMENT_1}-{short}")).is_empty());
    }

    #[test]
    fn rejects_a_body_one_byte_longer_than_the_documented_length() {
        assert!(detect(&format!("{}0", token())).is_empty());
    }

    /// Both tools agree the hex segments exclude `g`-`z`; a non-hex letter
    /// breaks the alphabet run before the documented length is reached.
    #[test]
    fn rejects_a_non_hex_letter_in_a_hex_segment() {
        let mut segment_1 = HEX_SEGMENT_1.to_string();
        segment_1.replace_range(0..1, "g");
        assert!(detect(&format!("{PREFIX}{segment_1}-{HEX_SEGMENT_2}")).is_empty());
    }

    #[test]
    fn rejects_a_lowercase_prefix() {
        assert!(detect(&format!("pmak-{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")).is_empty());
    }

    #[test]
    fn rejects_the_prefix_embedded_in_a_wider_identifier() {
        assert!(detect(&format!("legacy{}", token())).is_empty());
    }

    #[test]
    fn rejects_the_value_embedded_in_a_wider_identifier_leading_trailing_or_dash_joined() {
        let value = token();
        assert!(detect(&format!("legacy{value}")).is_empty());
        assert!(detect(&format!("{value}_backup")).is_empty());
        assert!(detect(&format!("{value}-1")).is_empty());
    }

    #[test]
    fn rejects_a_percent_encoded_delimiter_lookalike() {
        assert!(detect(&format!("PMAK%2D{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")).is_empty());
    }

    #[test]
    fn accepts_punctuation_boundaries() {
        let value = token();
        let input = format!("({value}).");
        let candidates = detect(&input);
        assert_eq!(candidates.len(), 1);
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(1, value.len() + 1).unwrap()
        );
    }

    #[test]
    fn reports_a_repeated_identical_value_once_per_occurrence() {
        let value = token();
        let input = format!("{value} {value}");
        assert_eq!(detect(&input).len(), 2);
    }

    #[test]
    fn finds_a_match_across_crlf_and_a_unicode_prefix() {
        let value = token();
        let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
        let candidates = detect(&input);
        assert_eq!(candidates.len(), 1);
        let start = input.find(&value).unwrap();
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(start, start + value.len()).unwrap()
        );
    }

    #[test]
    fn rejects_a_masked_value() {
        assert!(detect(&format!("{PREFIX}{}", "*".repeat(BODY_LEN))).is_empty());
        assert!(detect(&format!("{PREFIX}${{POSTMAN_API_KEY}}")).is_empty());
    }

    #[test]
    fn rejects_a_doc_style_x_filled_placeholder() {
        let placeholder = format!(
            "{}-{}",
            "x".repeat(HEX_SEGMENT_1_LEN),
            "x".repeat(HEX_SEGMENT_2_LEN)
        );
        assert!(detect(&format!("{PREFIX}{placeholder}")).is_empty());
    }

    #[test]
    fn finds_deterministic_findings_across_repeated_calls() {
        let value = token();
        assert_eq!(detect(&value), detect(&value));
    }

    /// A pathological run of near-miss prefixes must not make the scan
    /// quadratic: every occurrence is one byte short of the documented
    /// length, so none matches.
    #[test]
    fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
        let short = &HEX_SEGMENT_2[..HEX_SEGMENT_2_LEN - 1];
        let input = format!("{PREFIX}{HEX_SEGMENT_1}-{short} ").repeat(10_000);
        assert_eq!(detect(&input).len(), 0);
    }
}