standard_audit 0.15.0 → 0.15.2

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: 9372349d710bae063f9384029caef1467f308d63766ddd3216558e7442068c28
4
- data.tar.gz: 57956388488eb0c875c0a67dfdc101a72ea0588369bef06ef3e18009784f8d6a
3
+ metadata.gz: 0f28d1e0742cc3754f99b25e1242c69c70f72cbf781d2fc696b3be326fccd74c
4
+ data.tar.gz: 06f1dfe7a30578b6f8df0d3a672996802963dcdf9c316af967042b5b8f02648b
5
5
  SHA512:
6
- metadata.gz: 6baf203963e2df6713d6f16037ba93c00d26aa182315b3e6ad3b2c8731e1bf4c078ba15ff2a25437e9f6eb3c7e61b41ee5657d1db31c83c0c543be0cd360cec2
7
- data.tar.gz: bd922770bb429ac240b7f365ee0471eb93e155cebb454858d12cea6b884d6bc86319eefb7feaee79dd511070e2721669cdcb3d1765200c1b6e3b017dc1cc5c54
6
+ metadata.gz: 32f665b055842c67a00bb099f4919ae230b0f304f7c493a2ad4b783fa83540099fc7cf2b2259e0c7d08d71024a87af6707548110813062ee795e137b123fa73f
7
+ data.tar.gz: 3a8a1dc2fc568ee1668eb1dcd06fdc00620cb33f202c6b535d955e2b826ca2df0cd08e5ce3f9a214dfdbe741dc1f0bbf6def9add32531d96dabf92b6db2212fe
data/CHANGELOG.md CHANGED
@@ -7,6 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.15.2] - 2026-10-30
11
+
12
+ ### Fixed
13
+ - `verify_chain` and `relink_checksums!` now find a parent that sorts **after**
14
+ its child: two concurrent writers, the parent committed first (so the child
15
+ read its checksum as the tip) but stamped `created_at` a few milliseconds
16
+ later. The backward recovery window never held it, so a sound row was
17
+ reported as `:digest_mismatch` and never relinked (3 rows on one production
18
+ host, 2026-10-30). A row that reproduces no earlier digest is now held for
19
+ the next `lookahead` rows (default 64, `lookahead: 0` disables) and counted
20
+ as `:recovered` / relinked when one of them is its parent.
21
+
22
+ ## [0.15.1] - 2026-10-28
23
+
24
+ ### Fixed
25
+
26
+ - **`anonymize_actor!` also strips anonymizable keys from nested metadata.**
27
+ It used to drop `anonymizable_metadata_keys` (`email`, `name`, `ip_address`)
28
+ from the top level only, so a publisher that nests the subject's details
29
+ (standard_id's `social.auth.completed` carries `social_info: { email: ... }`)
30
+ kept them after erasure. With `filter_nested_metadata` on (the default since
31
+ 0.14.0) it now walks nested hashes and arrays, as the write-side filter does.
32
+
10
33
  ## [0.15.0] - 2026-10-28
11
34
 
12
35
  ### Security
data/README.md CHANGED
@@ -1128,6 +1128,10 @@ They have no `previous_checksum`. Verification falls back to the preceding row
1128
1128
  in the walk and, failing that, searches back through the last `recovery_window`
1129
1129
  (default 256) digests for the one that reproduces the row's checksum — which
1130
1130
  recovers the true parent of a row that forked, **without re-signing anything**.
1131
+ A parent can also sort *after* its child (two concurrent writers: the parent
1132
+ committed first, so the child read its checksum as the tip, but was stamped a
1133
+ few milliseconds later), so a row that reproduces no earlier digest is held for
1134
+ the next `lookahead` rows (default 64) before it is reported.
1131
1135
  This does not weaken tamper detection: a row whose fields were altered
1132
1136
  reproduces no candidate's digest. Pass `strict: true` to skip the search and see
1133
1137
  every fork as a failure.
@@ -124,8 +124,7 @@ module StandardAudit
124
124
  attrs[:target_type] = "[anonymized]" if log.target_gid == gid
125
125
 
126
126
  if log.metadata.present? && anonymizable_keys.any?
127
- cleaned_metadata = log.metadata.reject { |k, _| anonymizable_keys.include?(k.to_s) }
128
- attrs[:metadata] = cleaned_metadata
127
+ attrs[:metadata] = strip_anonymizable_keys(log.metadata, anonymizable_keys)
129
128
  end
130
129
 
131
130
  attrs[:anonymized_at] = log.anonymized_at || stamp if stamp
@@ -136,6 +135,31 @@ module StandardAudit
136
135
  count
137
136
  end
138
137
 
138
+ # Drops the anonymizable keys from +metadata+. Nested hashes (and hashes
139
+ # inside arrays) are walked too when `filter_nested_metadata` is on, as the
140
+ # write-side MetadataFilter does: a publisher that nests the subject's
141
+ # email (standard_id's social `social_info: { email: ... }`) would
142
+ # otherwise keep it after erasure.
143
+ def self.strip_anonymizable_keys(metadata, keys)
144
+ cleaned = metadata.reject { |k, _| keys.include?(k.to_s) }
145
+ return cleaned unless StandardAudit.config.filter_nested_metadata
146
+
147
+ cleaned.to_h { |k, v| [k, strip_anonymizable_descendant(v, keys)] }
148
+ end
149
+
150
+ def self.strip_anonymizable_descendant(value, keys)
151
+ case value
152
+ when Hash
153
+ value.reject { |k, _| keys.include?(k.to_s) }
154
+ .to_h { |k, v| [k, strip_anonymizable_descendant(v, keys)] }
155
+ when Array
156
+ value.map { |element| strip_anonymizable_descendant(element, keys) }
157
+ else
158
+ value
159
+ end
160
+ end
161
+ private_class_method :strip_anonymizable_keys, :strip_anonymizable_descendant
162
+
139
163
  # True when the table carries `anonymized_at` (the
140
164
  # `standard_audit:add_anonymized_at` migration, or a 0.12+ install).
141
165
  def self.anonymization_column?
@@ -336,6 +360,13 @@ module StandardAudit
336
360
  # this window search, so a legacy row that was both forked and
337
361
  # reordered stays unverifiable.)
338
362
  #
363
+ # A parent can also sort AFTER its child: two concurrent writers, the
364
+ # parent committed first (so the child read its checksum as the tip) but
365
+ # stamped `created_at` a few milliseconds later. The backward window never
366
+ # holds it, so a row that reproduces no earlier digest is held for the next
367
+ # `lookahead` rows and counted as :recovered when one of them is its
368
+ # parent; only then is it a :digest_mismatch. (`lookahead: 0` disables it.)
369
+ #
339
370
  # The chain links across the cutover like anywhere else: the first
340
371
  # canonical row's parent is the last legacy row's stored checksum.
341
372
  #
@@ -355,7 +386,7 @@ module StandardAudit
355
386
  # `created_at`. Rows whose two timestamps disagree (a backdated
356
387
  # `occurred_at`) can leave a hole rather than a prefix, and a hole is
357
388
  # reported — truthfully, since rows really are missing.
358
- def self.verify_chain(scope: nil, batch_size: 1000, recovery_window: 256, strict: false,
389
+ def self.verify_chain(scope: nil, batch_size: 1000, recovery_window: 256, lookahead: 64, strict: false,
359
390
  key_order_search_limit: StandardAudit::Checksum::KeyOrderSearch::DEFAULT_LIMIT,
360
391
  fail_on_legacy_unverifiable: false)
361
392
  relation = scope ? where(scope_gid: scope.to_global_id.to_s) : all
@@ -371,6 +402,10 @@ module StandardAudit
371
402
  unverifiable = []
372
403
  failures = []
373
404
  window = []
405
+ # Rows whose parent may still be AHEAD in the walk (see `lookahead`):
406
+ # [{ record:, algorithm:, expected:, seen: }], resolved against each
407
+ # later row's checksum, given up after `lookahead` rows.
408
+ pending = []
374
409
  first_row = true
375
410
  pruned_parents = []
376
411
 
@@ -402,6 +437,17 @@ module StandardAudit
402
437
  next
403
438
  end
404
439
 
440
+ pending.delete_if do |p|
441
+ if walk_digester(p[:record], p[:algorithm]).call(record.checksum) == p[:record].checksum
442
+ recovered += 1
443
+ elsif (p[:seen] += 1) >= lookahead
444
+ failures << chain_failure(p[:record], expected: p[:expected], reason: :digest_mismatch)
445
+ else
446
+ next false
447
+ end
448
+ true
449
+ end
450
+
405
451
  declared = declared_parents ? record.previous_checksum : nil
406
452
 
407
453
  if record.anonymized?
@@ -441,6 +487,10 @@ module StandardAudit
441
487
  unverifiable << entry
442
488
  failures << entry if fail_on_legacy_unverifiable
443
489
  end
490
+ elsif declared.blank? && !strict && lookahead.positive?
491
+ # Its parent may sort AFTER it (committed first, stamped later by a
492
+ # concurrent writer); decided when the next `lookahead` rows pass.
493
+ pending << { record: record, algorithm: algorithm, expected: expected, seen: 0 }
444
494
  else
445
495
  failures << chain_failure(record, expected: expected, reason: :digest_mismatch)
446
496
  end
@@ -452,6 +502,8 @@ module StandardAudit
452
502
  window.shift if window.size > recovery_window
453
503
  end
454
504
 
505
+ pending.each { |p| failures << chain_failure(p[:record], expected: p[:expected], reason: :digest_mismatch) }
506
+
455
507
  {
456
508
  valid: failures.empty?,
457
509
  verified: verified,
@@ -475,7 +527,7 @@ module StandardAudit
475
527
  # the row has held since it was written. Rows whose parent cannot be
476
528
  # reproduced are left untouched and counted in :unresolved — they are the
477
529
  # rows verification should keep reporting.
478
- def self.relink_checksums!(batch_size: 1000, recovery_window: 256)
530
+ def self.relink_checksums!(batch_size: 1000, recovery_window: 256, lookahead: 64)
479
531
  return { relinked: 0, unresolved: 0, skipped: 0 } unless chain_parent_column?
480
532
 
481
533
  previous_checksum = nil
@@ -483,6 +535,7 @@ module StandardAudit
483
535
  unresolved = 0
484
536
  skipped = 0
485
537
  window = []
538
+ pending = [] # rows whose parent may still be ahead (see verify_chain's `lookahead`)
486
539
 
487
540
  each_in_chain_order(all, batch_size: batch_size) do |record|
488
541
  if record.checksum.blank?
@@ -491,12 +544,28 @@ module StandardAudit
491
544
  next
492
545
  end
493
546
 
547
+ pending.delete_if do |p|
548
+ if walk_digester(p[:record], p[:algorithm]).call(record.checksum) == p[:record].checksum
549
+ p[:record].update_columns(previous_checksum: record.checksum)
550
+ relinked += 1
551
+ elsif (p[:seen] += 1) >= lookahead
552
+ unresolved += 1
553
+ else
554
+ next false
555
+ end
556
+ true
557
+ end
558
+
494
559
  found = resolve_parent(record, previous_checksum, window) if record.previous_checksum.blank?
495
560
 
496
561
  if record.previous_checksum.present?
497
562
  skipped += 1
498
563
  elsif found.nil?
499
- unresolved += 1
564
+ if lookahead.positive?
565
+ pending << { record: record, algorithm: StandardAudit::Checksum.algorithm_for(record.created_at), seen: 0 }
566
+ else
567
+ unresolved += 1
568
+ end
500
569
  elsif found.first.present?
501
570
  record.update_columns(previous_checksum: found.first)
502
571
  relinked += 1
@@ -510,6 +579,7 @@ module StandardAudit
510
579
  window.shift if window.size > recovery_window
511
580
  end
512
581
 
582
+ unresolved += pending.size
513
583
  { relinked: relinked, unresolved: unresolved, skipped: skipped }
514
584
  end
515
585
 
@@ -1,3 +1,3 @@
1
1
  module StandardAudit
2
- VERSION = "0.15.0"
2
+ VERSION = "0.15.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.15.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim