data_redactor 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b29290519836ca25d5188a5ef4da2585bd7f11faa0c072927863c637fb618eeb
4
- data.tar.gz: 465091099d2fcf4b990d4e4259c3c4ad549588839d918d831c9747236f84e864
3
+ metadata.gz: dfda0b2a543fc9b415c0816dd08a3fb727c1abcfb082c4c0b6d04362c83ee4d2
4
+ data.tar.gz: 6e1b528c5ce1759ebbefff1f3f2e72bdfc2b2fa2959e5c523945752bebb999f2
5
5
  SHA512:
6
- metadata.gz: fbc51cb331674163af43d4e952bce6ec936db4e3235ca356082a83211ae552d84409bebcdffcb364c09ed8099504ac8418d2fffed3d273d4392d762d99098d59
7
- data.tar.gz: e57d9545b5acec4ca25c1c5a30b1987d3d9769f027725e6613f396e7b2bedbe352620278a6cb8c613d5e1a1c1ecabb0e446bf2a4b6ae0339bedf8a4563a33b01
6
+ metadata.gz: a6a3e64351089a1b69b94a9ed33ee50eeb7339a0d359d357ce0ae6ca57c722d3b9fc5c92a4cb3b5fe8d7cb0fa40d0f3e50529e5b27226465294df78a30872714
7
+ data.tar.gz: 9f4082c693c639a8f4ab211bbbf92d4c9e6d79422f1696971937d95539670171cac662d64f2a3dd05185d5946b9c79f3b6a35d4ec1c4c4376d8853ecaaea719c
data/CHANGELOG.md CHANGED
@@ -7,7 +7,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ## [0.14.0] - 2026-06-17
10
+ ## [0.15.0] - 2026-06-17
11
+
12
+ ### Changed
13
+ - **Overlap resolution is now longest-match-wins** (was earlier-index-wins). When
14
+ two patterns match overlapping spans, the engine keeps the **longer** span;
15
+ equal-length ties go to the lower pattern index (preserving prior behaviour for
16
+ same-length matches). The previous "earliest pattern by index wins any region it
17
+ can match" semantic was an accidental by-product of sequential per-pattern
18
+ rewriting, and it could leave a secret **partly unredacted** — e.g.
19
+ `AKIA…EXAMPLE` followed by 20 more alphanumeric bytes used to redact only the
20
+ 20-char access-key prefix and leak the trailing 20 bytes; it now redacts the full
21
+ 40-char secret. The public API (`redact`, `scan`) is unchanged; `scan` may report
22
+ one longer match where it previously reported several shorter overlapping ones.
23
+ Aligns with Onigmo/PCRE/RE2/Hyperscan semantics. Resolver only — no measurable
24
+ throughput change (still ~2.4× over pure-Ruby on the 1 MB log).
25
+
26
+ ### Added
27
+ - **CI throughput regression gate** (`throughput-gate` job). Runs
28
+ `benchmark/ci_throughput_gate.rb`, which gates on the ratio of the C engine to
29
+ a pure-Ruby gsub loop over the same patterns (the ratio cancels CI-runner
30
+ speed variance, unlike absolute MB/s). Loose floor (1.5×; known result
31
+ ~2.25×), informational throughput output, plus a correctness guard so an
32
+ engine that redacts less cannot pass as "faster". Repo/CI only — not packaged.
33
+
34
+ ## [0.14.1] - 2026-06-17
35
+
36
+ ### Changed
37
+ - **Bounded the greedy tails of seven built-in token patterns** (`jwt`,
38
+ `grafana_api_token`, `ssh_public_key`, `bearer_token`, `anthropic_api_key`,
39
+ `openai_project_api_key`, `sendgrid_api_key`). Open-ended quantifiers (`+` and
40
+ `{n,}`) are capped at the POSIX `RE_DUP_MAX` of 255 (`{n,255}`), matching the
41
+ existing `hashicorp_vault_batch_token` precedent. A token is unusable once its
42
+ front is redacted, so a bounded prefix is sufficient to neutralize it. This
43
+ restores a finite `max_len` for these patterns (re-enabling the engine's
44
+ literal back-up skip) and removes a theoretical O(N²) worst case where a
45
+ crafted prefix plus a megabyte of matching characters forces a long greedy
46
+ scan. Tokens longer than 255 characters are still neutralized — only a
47
+ cryptographically-dead tail may remain.
11
48
 
12
49
  ### Added
13
50
  - **Key-name-anchored secret redaction** (`:credentials`). A new pattern tier
@@ -275,7 +312,9 @@ features as 0.7.1 plus the pipeline fix.
275
312
  - `DataRedactor.redact(text)` module function returning the input with every match replaced by `[REDACTED]`.
276
313
  - RSpec suite with one example per pattern.
277
314
 
278
- [Unreleased]: https://github.com/danielefrisanco/data_redactor/compare/v0.14.0...HEAD
315
+ [Unreleased]: https://github.com/danielefrisanco/data_redactor/compare/v0.15.0...HEAD
316
+ [0.15.0]: https://github.com/danielefrisanco/data_redactor/compare/v0.14.1...v0.15.0
317
+ [0.14.1]: https://github.com/danielefrisanco/data_redactor/compare/v0.14.0...v0.14.1
279
318
  [0.14.0]: https://github.com/danielefrisanco/data_redactor/compare/v0.13.0...v0.14.0
280
319
  [0.13.0]: https://github.com/danielefrisanco/data_redactor/compare/v0.11.0...v0.13.0
281
320
  [0.11.0]: https://github.com/danielefrisanco/data_redactor/compare/v0.10.1...v0.11.0
data/README.md CHANGED
@@ -12,10 +12,10 @@ DataRedactor scans text for sensitive data — API keys and cloud secrets, IBANs
12
12
  credit cards, national IDs, emails, phone numbers, IPs, and more — and replaces
13
13
  each match with a placeholder. The scanning runs in a C extension backed by a
14
14
  zero-dependency Thompson NFA → lazy-DFA multi-pattern engine (v19) that scans
15
- all 88 built-in patterns in a single pass — 2–2.5× faster than pure-Ruby `gsub`
15
+ every built-in pattern in a single pass — 2–2.5× faster than pure-Ruby `gsub`
16
16
  on large payloads, with no external library dependencies.
17
17
 
18
- It ships **88 built-in patterns** across 15+ countries, grouped into tags
18
+ It ships **89 built-in patterns** across 15+ countries, grouped into tags
19
19
  (`:credentials`, `:financial`, `:contact`, ...) so you can redact only what you
20
20
  care about. Beyond plain strings it can walk nested Hashes, Arrays, and JSON,
21
21
  audit a payload without mutating it (`scan`), and plug into Logger, Rails, and
@@ -309,7 +309,7 @@ safe_response = DataRedactor::Integrations::OpenAI.redact_response(response)
309
309
 
310
310
  `content` may be a plain String or an array of content blocks/parts (`{ type: "text", text: "..." }`) — only the `text` of `text` blocks is redacted; image and other block types pass through untouched. For Claude, a top-level `system:` String is also redacted; for OpenAI, a `{ role: "system" }` message in the array is redacted like any other. Pass a bare `messages` array or the whole request Hash (with a `messages` key) — either works.
311
311
 
312
- ## Detected patterns (88 total)
312
+ ## Detected patterns (89 total)
313
313
 
314
314
  The table below is a representative sample. Use `DataRedactor.pattern_names` for the canonical, machine-readable list — it stays in sync with the C extension automatically.
315
315
 
@@ -435,7 +435,7 @@ redactor/
435
435
  │ ├── README.md # How to run, what each script measures
436
436
  │ ├── support/corpus.rb # Shared payload builders + pure-Ruby baseline redactor
437
437
  │ ├── throughput.rb # MB/s on representative payloads
438
- │ ├── vs_pure_ruby.rb # C extension vs pure-Ruby gsub (same 88 patterns)
438
+ │ ├── vs_pure_ruby.rb # C extension vs pure-Ruby gsub (same patterns)
439
439
  │ ├── scaling.rb # Runtime vs input size 1KB → 50MB
440
440
  │ └── per_pattern.rb # Per-pattern scan cost
441
441
  └── docs/ # Design and execution docs for future work
@@ -523,7 +523,7 @@ different angles. They are **not** packaged with the gem.
523
523
  ```bash
524
524
  bundle install # pulls benchmark-ips, benchmark-memory (dev deps)
525
525
  bundle exec rake compile
526
- bundle exec ruby benchmark/vs_pure_ruby.rb # head-to-head vs pure-Ruby gsub, same 88 patterns
526
+ bundle exec ruby benchmark/vs_pure_ruby.rb # head-to-head vs pure-Ruby gsub, same patterns
527
527
  bundle exec ruby benchmark/throughput.rb # MB/s on a log line, JSON, 1MB and 10MB log files
528
528
  bundle exec ruby benchmark/scaling.rb # runtime vs input size (1KB → 50MB), confirms linear scaling
529
529
  bundle exec ruby benchmark/per_pattern.rb # per-pattern scan cost over a 1MB payload
@@ -535,11 +535,8 @@ C engine uses, via `DataRedactor::BUILTIN_PATTERN_SOURCES`).
535
535
 
536
536
  ### Performance (0.10.0 — v19 multi-pattern engine)
537
537
 
538
- As of 0.10.0 the C extension runs a **Thompson NFA lazy-DFA multi-pattern
539
- engine** (v19) that scans the input once across all 88 built-in patterns,
540
- with two selective-merge passes (pure-digit group + IBAN union) that further
541
- reduce work for the most common pattern classes. Custom patterns (`add_pattern`)
542
- still use the glibc path (required for correct UTF-8 diacritic matching).
538
+ Measured on the v19 engine ([How it works](#how-it-works)) vs a pure-Ruby `gsub`
539
+ loop over the same patterns:
543
540
 
544
541
  | Payload | v19 engine (0.10.0) | Pure-Ruby `gsub` | Ratio |
545
542
  |-----------------------|---------------------|------------------|-----------------|
@@ -576,14 +573,14 @@ machine-dependent, but the flat curve is not.
576
573
 
577
574
  ## How it works
578
575
 
579
- 1. At load time, `Init_data_redactor` compiles all 85 regex patterns once using `regcomp` (POSIX ERE) and stores them as static `regex_t` structs. Patterns marked as boundary-wrapped are expanded with `wrap_boundary()` before compilation.
580
- 2. `DataRedactor.redact(text)` receives a Ruby `String`, converts it to a C `char*` via `StringValueCStr`, and runs each compiled pattern in sequence on a working buffer.
581
- 3. For each pattern, `replace_all_matches` iterates using `regexec`, copies non-matching segments to a fresh output buffer, and inserts `[REDACTED]` in place of each match. For boundary-wrapped patterns, `regexec` is called with `nmatch=4` and sub-match groups `[1]`/`[3]` identify the boundary characters so they are preserved verbatim.
582
- 4. The output buffer is grown with `realloc` as needed. After all patterns are applied the result is returned as a Ruby `String` via `rb_str_new_cstr`. All intermediate `malloc`/`strdup` allocations are explicitly `free`d.
576
+ 1. At load time, `mm_init()` compiles every built-in pattern from a Thompson NFA into bytecode, lazily building each pattern's DFA on first use (interned and cached). Boundary-wrapped patterns are expanded with the word-boundary group before compilation.
577
+ 2. `DataRedactor.redact(text)` / `scan(text)` hand the input to the v19 engine, which scans it **once** and emits `(pattern_id, start, length)` events for every enabled pattern. Two selective-merge passes (a pure-digit group and an IBAN union) collapse the most common pattern classes into shared scans. The single pass over the original buffer is what makes the engine O(N).
578
+ 3. The raw events are resolved by `mm_resolve` under the **longest-match-wins** policy: overlapping spans are reduced to a non-overlapping set keeping the longest match at each position, with the lower pattern index breaking equal-length ties.
579
+ 4. `redact` rewrites the surviving spans to placeholders in one buffer build (preserving the boundary characters of boundary-wrapped matches); `scan` returns the event list with byte offsets into the original string. Custom patterns (`add_pattern`) run on the glibc `regexec` path afterward required for correct UTF-8 diacritic matching.
583
580
 
584
581
  ## Memory management
585
582
 
586
- All C-side buffers are heap-allocated with `malloc`/`strdup` and freed before the function returns. The only Ruby-managed allocation is the final return value from `rb_str_new_cstr`. No Ruby objects are created mid-processing, so GC cannot collect anything out from under the C code.
583
+ All C-side working buffers are heap-allocated and freed before the call returns; the only Ruby-managed allocation is the final result `String`. No Ruby objects are created mid-scan, so GC cannot collect anything out from under the C code. Per-thread engine scratch (NFA state, lazy-DFA cache) is freed automatically when the thread exits — see [Thread safety](#thread-safety).
587
584
 
588
585
  ## Thread safety
589
586
 
@@ -601,7 +598,6 @@ Released under the [MIT License](LICENSE).
601
598
 
602
599
  ## Known limitations
603
600
 
604
- - **Pattern ordering matters** — patterns run sequentially. An early broad pattern (e.g. the 9-digit passport) may consume digits that a later pattern (e.g. credit card) depends on. Boundary wrapping mitigates this for pure-digit patterns.
605
601
  - **AWS Secret Key (pattern 1)** — 40 consecutive base64 characters is a broad match. It can produce false positives in base64-encoded content such as embedded images or binary blobs.
606
602
  - **Duplicate digit patterns** — several national ID formats share the same digit-length (11 digits: PESEL, Norwegian Fødselsnummer, Belgian National Number). They are kept as separate slots for clarity but the practical effect is that any 11-digit boundary-delimited number will be redacted.
607
- - **Single-pass overlap semantics** — built-in patterns are resolved by an index-order greedy claim: the lower-index pattern wins any region it matches. When two secrets abut with no separator, a rewrite-created word boundary can cause the second to be missed. This is rare in real text (secrets are almost always separator-delimited) and will be fixed by the upcoming longest-match-wins resolver in 1.0.
603
+ - **Overlap resolution is longest-match-wins** — when two patterns match overlapping spans the engine keeps the longer span; equal-length ties go to the lower pattern index. This favours redacting *more* when uncertain (a 40-char secret is redacted whole rather than leaking the bytes past a shorter prefix match). When two secrets abut with **no separator** between them, a boundary-wrapped pattern can fail to match because the original buffer has no word boundary where one token meets the next, leaving the abutting token unredacted. This is rare in real text (secrets are almost always separator-delimited).
@@ -16,10 +16,10 @@
16
16
  *
17
17
  * 2. Output contract. mm_scan() takes an enable_bits gate and emits ORIGINAL-
18
18
  * frame (pattern_id, start, span) events for ALL enabled patterns in one
19
- * pass; it does NOT model the gem's cross-pattern sequential rewrite. The
20
- * caller applies mm_resolve() (index-order greedy claim) to reproduce
21
- * today's "earlier-index pattern wins" semantics byte-for-byte. See
22
- * TODO.md §1d Gap 5 and the AKIA specs in spec/data_redactor_spec.rb.
19
+ * pass. The caller applies mm_resolve() (longest-match-wins greedy claim:
20
+ * longest span at each position wins, equal lengths broken by lower
21
+ * pattern_id) to pick the final non-overlapping set. See TODO.md §1d Gap 5
22
+ * and the overlap-resolution specs in spec/data_redactor_spec.rb.
23
23
  *
24
24
  * The infix-literal classification and the BM_INFIX hint table below are ported
25
25
  * from prototypes/multi_pattern_matcher/gen_patterns.rb (which derived them from the
@@ -1260,14 +1260,14 @@ size_t mm_scan(const char *input, size_t len,
1260
1260
  return count;
1261
1261
  }
1262
1262
 
1263
- /* Order events for the index-order greedy claim: ascending pattern_id, then
1264
- * ascending start (so a lower-index pattern always gets first claim on a region;
1265
- * within a pattern, earlier matches are seen first). */
1263
+ /* Order events for the longest-match-wins greedy claim: ascending start, then
1264
+ * descending length (so the longest span at a given start is seen first), then
1265
+ * ascending pattern_id (lower index wins a tie of equal length). */
1266
1266
  static int ev_cmp_resolve(const void *a, const void *b) {
1267
1267
  const mm_match_t *x = a, *y = b;
1268
- if (x->pattern_id != y->pattern_id) return x->pattern_id - y->pattern_id;
1269
1268
  if (x->start != y->start) return x->start < y->start ? -1 : 1;
1270
- return 0;
1269
+ if (x->length != y->length) return x->length > y->length ? -1 : 1;
1270
+ return x->pattern_id - y->pattern_id;
1271
1271
  }
1272
1272
 
1273
1273
  /* Order kept events for emission: ascending start. */
@@ -1281,12 +1281,12 @@ size_t mm_resolve(mm_match_t *ev, size_t n) {
1281
1281
  if (n == 0) return 0;
1282
1282
  qsort(ev, n, sizeof(mm_match_t), ev_cmp_resolve);
1283
1283
 
1284
- /* Greedy claim in (pattern_id, start) order. An event is kept iff its span
1285
- * [start, start+length) does not overlap any already-kept span. Kept spans
1286
- * are accumulated in `kept`; we check membership against them. n is small
1287
- * for typical inputs, but to stay linear-ish we keep `kept` sorted by start
1288
- * and binary-search the neighbourhood. For simplicity and because match
1289
- * counts are modest, a linear overlap check against the kept set is used. */
1284
+ /* Greedy claim in (start, -length, pattern_id) order: the longest span at
1285
+ * each position is offered first and claims its region; any later (shorter,
1286
+ * or equal-length higher-id) event overlapping an already-kept span is
1287
+ * dropped. An event is kept iff its span [start, start+length) does not
1288
+ * overlap any already-kept span. Match counts are modest, so a linear
1289
+ * overlap check against the kept set is used. */
1290
1290
  mm_match_t *kept = mm_xmalloc(n * sizeof(mm_match_t));
1291
1291
  size_t nk = 0;
1292
1292
  for (size_t i = 0; i < n; i++) {
@@ -53,19 +53,20 @@ void mm_clear_custom(void);
53
53
  * array disables out-of-range patterns. Events carry ORIGINAL-frame offsets.
54
54
  *
55
55
  * Events are NOT pre-resolved for cross-pattern overlap — the caller applies
56
- * the index-order greedy claim (mm_resolve) to reproduce the gem's sequential
57
- * per-pattern rewrite semantics.
56
+ * the longest-match-wins greedy claim (mm_resolve) to pick the final
57
+ * non-overlapping set.
58
58
  */
59
59
  size_t mm_scan(const char *input, size_t len,
60
60
  const int *enable_bits, size_t n_bits,
61
61
  mm_match_t *out, size_t max);
62
62
 
63
63
  /*
64
- * Resolve raw scan events into the non-overlapping set the gem's sequential
65
- * per-pattern rewrite would produce: in (pattern_id, start) order, keep an
66
- * event iff its CORE span does not overlap an already-kept span. Sorts `ev`
67
- * in place and returns the kept count (compacted to the front of `ev`), in
68
- * ascending start order. n_total is the pattern-id upper bound for ordering.
64
+ * Resolve raw scan events into the final non-overlapping set under the
65
+ * longest-match-wins policy: process events in (start asc, length desc,
66
+ * pattern_id asc) order and keep an event iff its CORE span does not overlap an
67
+ * already-kept span. The longest match at each position wins; equal-length ties
68
+ * go to the lower pattern_id. Sorts `ev` in place and returns the kept count
69
+ * (compacted to the front of `ev`), in ascending start order.
69
70
  */
70
71
  size_t mm_resolve(mm_match_t *ev, size_t n);
71
72
 
@@ -425,18 +425,21 @@ const char *pattern_strings[NUM_PATTERNS] = {
425
425
  /* ---- Tier 2: Long prefixed tokens ---- */
426
426
  /* 6: GitHub PAT fine-grained (github_pat_ + 82 chars) */
427
427
  "github_pat_[0-9a-zA-Z_]{82}",
428
- /* 7: JWT (three base64url segments) */
429
- "eyJ[A-Za-z0-9_-]{10,}\\.eyJ[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]+",
428
+ /* 7: JWT (three base64url segments). Tails bounded at RE_DUP_MAX (255):
429
+ * a JWT is unusable once its front is gone, so a bounded prefix is enough to
430
+ * neutralize it. Bounding restores a finite max_len (re-enables the engine's
431
+ * literal back-up skip) and removes the O(N^2) greedy-tail worst case. */
432
+ "eyJ[A-Za-z0-9_-]{10,255}\\.eyJ[A-Za-z0-9_-]{10,255}\\.[A-Za-z0-9_-]{1,255}",
430
433
  /* 8: Grafana API Token (base64 of {\"k\":\") */
431
- "eyJrIjoi[A-Za-z0-9_=-]{42,}",
434
+ "eyJrIjoi[A-Za-z0-9_=-]{42,255}",
432
435
  /* 9: SSH Public Key */
433
- "ssh-(rsa|ed25519|ecdsa) [a-zA-Z0-9/+=]{20,}",
436
+ "ssh-(rsa|ed25519|ecdsa) [a-zA-Z0-9/+=]{20,255}",
434
437
  /* 10: Bearer Token */
435
- "[Bb]earer [a-zA-Z0-9_.=/+:-]{12,}",
438
+ "[Bb]earer [a-zA-Z0-9_.=/+:-]{12,255}",
436
439
  /* 11: Anthropic API Key (sk-ant-apiNN-... ~ 95+ chars) */
437
- "sk-ant-api[0-9]{2}-[A-Za-z0-9_-]{90,}",
440
+ "sk-ant-api[0-9]{2}-[A-Za-z0-9_-]{90,255}",
438
441
  /* 12: OpenAI Project API Key (sk-proj-...) */
439
- "sk-proj-[A-Za-z0-9_-]{20,}",
442
+ "sk-proj-[A-Za-z0-9_-]{20,255}",
440
443
  /* 13: Google API Key (AIza + 35 chars) */
441
444
  "AIza[0-9A-Za-z_-]{35}",
442
445
  /* 14: AWS Access Key ID (all prefixes + 16 chars) */
@@ -444,7 +447,7 @@ const char *pattern_strings[NUM_PATTERNS] = {
444
447
  /* 15: AWS Secret Access Key (40 base64 chars) */
445
448
  "[A-Za-z0-9/+=]{40}",
446
449
  /* 16: SendGrid API Key */
447
- "SG\\.[a-zA-Z0-9_-]{5,}\\.[a-zA-Z0-9_-]{5,}",
450
+ "SG\\.[a-zA-Z0-9_-]{5,255}\\.[a-zA-Z0-9_-]{5,255}",
448
451
  /* 17: Amazon MWS Auth Token */
449
452
  "amzn\\.mws\\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
450
453
  /* 18: LaunchDarkly API Key (api-UUID or sdk-UUID) */
@@ -1,4 +1,4 @@
1
1
  module DataRedactor
2
2
  # Current gem version. Follows {https://semver.org Semantic Versioning 2.0.0}.
3
- VERSION = "0.14.0"
3
+ VERSION = "0.15.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_redactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniele Frisanco