standard_audit 0.10.0 → 0.11.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 +4 -4
- data/CHANGELOG.md +22 -0
- data/README.md +36 -0
- data/lib/standard_audit/configuration.rb +20 -0
- data/lib/standard_audit/record_reference.rb +141 -0
- data/lib/standard_audit/subscriber.rb +10 -0
- data/lib/standard_audit/version.rb +1 -1
- data/lib/standard_audit.rb +20 -12
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b89dc74bd119d2e0f1ff5080cef706439e68b61c79d2a6b653fcd5a74e265ae1
|
|
4
|
+
data.tar.gz: 86686ea3bb611a13e8c8947ef3e0b63aad63eb47bfa1e48b16abba377a426f7f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b06e7ee9a3b5d79191cb47a532cf81efb95e3bb722015540be79ce4bb9412ab13b8e69985a66d368c8dcdfb099f34f1ca1d3f0d894bfc90cc409615782dde921
|
|
7
|
+
data.tar.gz: bb1e9316eba648a8d238becb75d33f705b82f509310e8c3e0d562656accac2b44339e49551e026ae0357bf57dda8fe8251f6f1d6c7df655777d5b157c3d17b0a
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.11.0] - 2026-07-31
|
|
11
|
+
|
|
12
|
+
### Security
|
|
13
|
+
|
|
14
|
+
- **ActiveRecord objects in audit metadata are no longer written with all of their attributes.** Any record found in metadata — at any depth, and inside Arrays, Hashes and `ActiveRecord::Relation`s — is now replaced with a reference: `{ "gid" => "gid://app/Account/1", "type" => "Account", "id" => "1" }`. Applies to both write paths (`ActiveSupport::Notifications` and `StandardAudit.record`).
|
|
15
|
+
|
|
16
|
+
`standard_id` publishes live records under payload keys like `account:`, `current_account:`, `session:` and `code_challenge:`, and `Subscriber#extract_metadata` excluded only `actor`/`target`/`scope`/`request_id`/`ip_address`/`user_agent`/`session_id` — every other key was written whole. Confirmed present in real `audit_logs` rows: `account.password_digest`, `account.password_reset_token_digest`, `session.token_digest`, `session.lookup_hash`, `code_challenge.code`.
|
|
17
|
+
|
|
18
|
+
All three existing defences missed, each for a different reason: `sensitive_keys` matches key names exactly and the secrets are attributes *underneath* `account:`; `filter_nested_metadata` is off by default so the filter never descended to them; and `account:` does not look sensitive at the top level. This is not a redaction bug — key-based redaction is the wrong tool for a value that is an entire database row — so the value is replaced by type, before any key filtering runs.
|
|
19
|
+
|
|
20
|
+
**This fix stops the bleeding; it does not clean up.** `audit_logs` is append-only by design (`before_update` and `before_destroy` raise `ReadOnlyRecord`), so every row already written keeps its digests, for the whole of a retention window that is intentionally long. Upgrading changes only what is written from now on. Assessing and remediating existing rows is a separate, host-side exercise.
|
|
21
|
+
|
|
22
|
+
`sensitive_keys` semantics are untouched: still exact-match, still no substring mode. See rarebit-one/rarebit-ops#296.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- **`config.dereference_record_metadata`** (default `true`) — the escape hatch for the above. It defaults to the SAFE behaviour, unlike `filter_nested_metadata`, because the values it catches are ones no host asked to record: they arrive as a side effect of a payload carrying `account:` or `session:`, and an append-only row cannot be walked back. Set it to `false` only if an app genuinely depends on record attributes in metadata and has satisfied itself no secret-bearing column can reach a row.
|
|
27
|
+
|
|
28
|
+
### Changed
|
|
29
|
+
|
|
30
|
+
- **Consumer-visible beyond the secrets disappearing:** any metadata key that used to hold a record's attribute hash now holds a three-key reference, so dashboards, reports or queries reading e.g. `metadata->'account'->>'email'` will read `NULL` on new rows (old rows are unchanged, which makes the change look like a data gap rather than a schema change). Recover the specific fields you need with `metadata_builder`, which runs BEFORE dereferencing and still receives the record: `->(metadata) { metadata.merge(account_email: metadata[:account]&.email) }`. `actor`/`target`/`scope` are unaffected — they were already stored as GlobalIDs.
|
|
31
|
+
|
|
10
32
|
## [0.10.0] - 2026-07-31
|
|
11
33
|
|
|
12
34
|
### Added
|
data/README.md
CHANGED
|
@@ -332,6 +332,10 @@ StandardAudit.configure(baseline: true) do |config|
|
|
|
332
332
|
# `metadata: { stripe: { client_secret: ... } }` is written intact.
|
|
333
333
|
config.filter_nested_metadata = true
|
|
334
334
|
|
|
335
|
+
# Replace ActiveRecord objects in metadata with a reference instead of
|
|
336
|
+
# serialising every attribute. ON by default; see "Records in metadata".
|
|
337
|
+
config.dereference_record_metadata = true
|
|
338
|
+
|
|
335
339
|
# -- Write-time hooks --
|
|
336
340
|
# Run between the UUID assignment and the checksum computation, so a hook MAY
|
|
337
341
|
# set a checksummed column and the row still passes `verify_chain`. No
|
|
@@ -695,6 +699,38 @@ Nested metadata is **not** redacted unless `filter_nested_metadata` is enabled
|
|
|
695
699
|
— `metadata: { stripe: { client_secret: ... } }` passes both write paths under
|
|
696
700
|
exact matching by default.
|
|
697
701
|
|
|
702
|
+
### Records in metadata
|
|
703
|
+
|
|
704
|
+
An ActiveRecord object appearing anywhere in metadata is replaced, by default,
|
|
705
|
+
with a reference rather than a snapshot of the row:
|
|
706
|
+
|
|
707
|
+
```ruby
|
|
708
|
+
{ account: account }
|
|
709
|
+
# => { account: { "gid" => "gid://myapp/Account/1", "type" => "Account", "id" => "1" } }
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
This applies at any depth, inside Arrays, Hashes and `ActiveRecord::Relation`s,
|
|
713
|
+
on both write paths. It is on by default because key-based redaction cannot
|
|
714
|
+
reach the problem: a payload key like `account:` looks like exactly the kind of
|
|
715
|
+
key you want on an audit row, while the value serialises with
|
|
716
|
+
`password_digest`, `token_digest`, `lookup_hash` and anything else the table
|
|
717
|
+
happens to hold. Audit rows are append-only, so an unsafe default cannot be
|
|
718
|
+
walked back.
|
|
719
|
+
|
|
720
|
+
On the notifications path, records are dereferenced **after**
|
|
721
|
+
`metadata_builder` runs, so a builder that needs real attributes still gets the
|
|
722
|
+
record (`metadata_builder` has never applied to a direct `StandardAudit.record`
|
|
723
|
+
call — pass the attributes you want in `metadata` there):
|
|
724
|
+
|
|
725
|
+
```ruby
|
|
726
|
+
config.metadata_builder = ->(metadata) {
|
|
727
|
+
metadata.merge(account_email: metadata[:account]&.email)
|
|
728
|
+
}
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
`config.dereference_record_metadata = false` restores the pre-0.11.0 behaviour
|
|
732
|
+
of writing full attributes. Prefer `metadata_builder` over turning it off.
|
|
733
|
+
|
|
698
734
|
**Performance**: For high-volume applications, enable async processing and ensure your `audit_logs` table has appropriate indexes (the install generator adds them by default). Consider partitioning by `occurred_at` for very large tables.
|
|
699
735
|
|
|
700
736
|
**Retention**: Set `retention_days` in your configuration and run `rake standard_audit:cleanup` via a scheduled job (e.g., cron or SolidQueue recurring). Archive before deleting if you need long-term storage.
|
|
@@ -7,6 +7,7 @@ module StandardAudit
|
|
|
7
7
|
:current_session_id_resolver,
|
|
8
8
|
:sensitive_keys, :sensitive_key_patterns,
|
|
9
9
|
:sensitive_key_exceptions, :filter_nested_metadata,
|
|
10
|
+
:dereference_record_metadata,
|
|
10
11
|
:metadata_builder, :before_checksum_hooks,
|
|
11
12
|
:anonymizable_metadata_keys, :retention_days,
|
|
12
13
|
:audit_catalogue, :verify_audit_declarations,
|
|
@@ -72,6 +73,25 @@ module StandardAudit
|
|
|
72
73
|
# append-only. Reserved keys are never descended into.
|
|
73
74
|
@filter_nested_metadata = false
|
|
74
75
|
|
|
76
|
+
# When true (the default), an ActiveRecord object appearing anywhere in
|
|
77
|
+
# audit metadata is replaced by a reference —
|
|
78
|
+
# `{ "gid" => …, "type" => …, "id" => … }` — instead of being serialised
|
|
79
|
+
# with all of its attributes. See StandardAudit::RecordReference.
|
|
80
|
+
#
|
|
81
|
+
# Defaults to the SAFE behaviour, unlike `filter_nested_metadata`,
|
|
82
|
+
# because the values this catches are ones no host asked to record:
|
|
83
|
+
# `password_digest`, `token_digest`, `lookup_hash` and PKCE verifiers
|
|
84
|
+
# arriving as a side effect of a payload carrying `account:` or
|
|
85
|
+
# `session:`. Audit rows are append-only, so an unsafe default cannot be
|
|
86
|
+
# walked back — the rows already written keep whatever was in them.
|
|
87
|
+
#
|
|
88
|
+
# Set to false ONLY if an app genuinely depends on record attributes in
|
|
89
|
+
# metadata and has satisfied itself that no secret-bearing column can
|
|
90
|
+
# reach a row. Preferred alternative: keep this on and use
|
|
91
|
+
# `metadata_builder` to pull the specific attributes you want, which runs
|
|
92
|
+
# BEFORE dereferencing and still sees the record.
|
|
93
|
+
@dereference_record_metadata = true
|
|
94
|
+
|
|
75
95
|
@metadata_builder = nil
|
|
76
96
|
|
|
77
97
|
# Callables (or Symbols naming an AuditLog instance method) run on
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
module StandardAudit
|
|
2
|
+
# Replaces ActiveRecord objects found in audit metadata with a stable
|
|
3
|
+
# *reference* instead of a snapshot of the record's whole attribute set.
|
|
4
|
+
#
|
|
5
|
+
# == Why this exists
|
|
6
|
+
#
|
|
7
|
+
# `ActiveSupport::Notifications` payloads routinely carry live records —
|
|
8
|
+
# `standard_id` publishes `account:`, `current_account:`, `session:` and
|
|
9
|
+
# `code_challenge:` — and an AR object serialises with every attribute it
|
|
10
|
+
# has. That put `account.password_digest`,
|
|
11
|
+
# `account.password_reset_token_digest`, `session.token_digest`,
|
|
12
|
+
# `session.lookup_hash` and `code_challenge.code` into `audit_logs` rows
|
|
13
|
+
# across the estate (rarebit-one/rarebit-ops#296).
|
|
14
|
+
#
|
|
15
|
+
# None of the three existing defences fired: `sensitive_keys` matches keys
|
|
16
|
+
# exactly and the secrets are attributes *underneath* `account:`;
|
|
17
|
+
# `filter_nested_metadata` is off by default so the filter never descended
|
|
18
|
+
# to them; and `account:` does not look sensitive at the top level.
|
|
19
|
+
#
|
|
20
|
+
# Key-based redaction is the wrong tool here — the leak is not "this key is
|
|
21
|
+
# sensitive", it is "this value is an entire database row". So the value is
|
|
22
|
+
# replaced wholesale, by type, before any key filtering happens.
|
|
23
|
+
#
|
|
24
|
+
# == What replaces a record
|
|
25
|
+
#
|
|
26
|
+
# { "gid" => "gid://dummy/Account/1", "type" => "Account", "id" => "1" }
|
|
27
|
+
#
|
|
28
|
+
# A GlobalID string is the identifier this gem already uses for `actor`,
|
|
29
|
+
# `target` and `scope` (`actor_gid`), so an audit row stays resolvable with
|
|
30
|
+
# `GlobalID::Locator`. `type` and `id` ride along because they survive a
|
|
31
|
+
# record being deleted, an app whose GlobalID app name changes, and records
|
|
32
|
+
# that have no GlobalID at all (unpersisted ones): for those, `gid` is
|
|
33
|
+
# simply absent and the reference is still meaningful.
|
|
34
|
+
module RecordReference
|
|
35
|
+
# Written in place of a container that contains itself. Depth alone is NOT
|
|
36
|
+
# used as the guard: a plain depth cap would rewrite deeply-but-finitely
|
|
37
|
+
# nested metadata that holds no records at all, and permanently lose audit
|
|
38
|
+
# content in an append-only row to protect against a structure that is
|
|
39
|
+
# merely large. Only a genuine cycle — the one thing that cannot be
|
|
40
|
+
# serialised anyway — is replaced.
|
|
41
|
+
CIRCULAR = "[standard_audit: circular reference]".freeze
|
|
42
|
+
|
|
43
|
+
class << self
|
|
44
|
+
# Returns a copy of `value` with every ActiveRecord object — at any
|
|
45
|
+
# depth, inside Arrays, Hashes and Relations — replaced by a reference
|
|
46
|
+
# Hash. Structures containing no records are returned untouched (same
|
|
47
|
+
# object), so this is a no-op for ordinary metadata.
|
|
48
|
+
#
|
|
49
|
+
# Never mutates the input: notification payloads are shared with every
|
|
50
|
+
# other subscriber.
|
|
51
|
+
def call(value, ancestors = nil)
|
|
52
|
+
return reference_for(value) if record?(value)
|
|
53
|
+
|
|
54
|
+
container = relation?(value) || value.is_a?(Array) || hash_like?(value)
|
|
55
|
+
return value unless container
|
|
56
|
+
|
|
57
|
+
ancestors ||= {}.compare_by_identity
|
|
58
|
+
return CIRCULAR if ancestors.key?(value)
|
|
59
|
+
|
|
60
|
+
ancestors[value] = true
|
|
61
|
+
begin
|
|
62
|
+
if relation?(value)
|
|
63
|
+
map_collection(value.to_a, ancestors)
|
|
64
|
+
elsif value.is_a?(Array)
|
|
65
|
+
map_collection(value, ancestors)
|
|
66
|
+
else
|
|
67
|
+
map_hash(value, ancestors)
|
|
68
|
+
end
|
|
69
|
+
ensure
|
|
70
|
+
ancestors.delete(value)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The reference written in place of a record.
|
|
75
|
+
def reference_for(record)
|
|
76
|
+
{
|
|
77
|
+
"gid" => global_id_for(record),
|
|
78
|
+
"type" => record.class.name,
|
|
79
|
+
"id" => record.id&.to_s
|
|
80
|
+
}.compact
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def record?(value)
|
|
86
|
+
defined?(ActiveRecord::Base) && value.is_a?(ActiveRecord::Base)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def relation?(value)
|
|
90
|
+
defined?(ActiveRecord::Relation) && value.is_a?(ActiveRecord::Relation)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def hash_like?(value)
|
|
94
|
+
value.respond_to?(:each_pair) && value.respond_to?(:key?)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def map_collection(array, ancestors)
|
|
98
|
+
changed = false
|
|
99
|
+
mapped = array.map do |element|
|
|
100
|
+
replacement = call(element, ancestors)
|
|
101
|
+
changed ||= !replacement.equal?(element)
|
|
102
|
+
replacement
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
changed ? mapped : array
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def map_hash(hash, ancestors)
|
|
109
|
+
changed = false
|
|
110
|
+
mapped = {}
|
|
111
|
+
|
|
112
|
+
hash.each_pair do |key, value|
|
|
113
|
+
# Reserved keys are gem-owned (`_tags`, `_source`) and are left
|
|
114
|
+
# exactly alone here, as they are in MetadataFilter.
|
|
115
|
+
if StandardAudit::RESERVED_METADATA_KEYS.include?(key.to_s)
|
|
116
|
+
mapped[key] = value
|
|
117
|
+
next
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
replacement = call(value, ancestors)
|
|
121
|
+
changed ||= !replacement.equal?(value)
|
|
122
|
+
mapped[key] = replacement
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
changed ? mapped : hash
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# `to_global_id` raises for an unpersisted record and for a model that
|
|
129
|
+
# does not include GlobalID::Identification. Neither is a reason to fail
|
|
130
|
+
# an audit write, and neither is a reason to fall back to writing the
|
|
131
|
+
# attributes.
|
|
132
|
+
def global_id_for(record)
|
|
133
|
+
return nil unless record.respond_to?(:to_global_id)
|
|
134
|
+
|
|
135
|
+
record.to_global_id.to_s
|
|
136
|
+
rescue StandardError
|
|
137
|
+
nil
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -79,6 +79,16 @@ module StandardAudit
|
|
|
79
79
|
raw_metadata = config.metadata_builder.call(raw_metadata)
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
+
# ActiveRecord objects in the payload are replaced by a reference BEFORE
|
|
83
|
+
# any key-based redaction, because the leak they cause is not a
|
|
84
|
+
# sensitive *key* — it is a value that serialises as an entire database
|
|
85
|
+
# row (rarebit-one/rarebit-ops#296). Runs after `metadata_builder` so a
|
|
86
|
+
# host that derives fields from a record (`payload[:account].email`)
|
|
87
|
+
# still sees the record.
|
|
88
|
+
if config.dereference_record_metadata
|
|
89
|
+
raw_metadata = StandardAudit::RecordReference.call(raw_metadata)
|
|
90
|
+
end
|
|
91
|
+
|
|
82
92
|
# Redaction lives in MetadataFilter, shared with StandardAudit.record.
|
|
83
93
|
# This path previously carried its own copy that did *not* subtract
|
|
84
94
|
# RESERVED_METADATA_KEYS, so `_tags`/`_source` were strippable here and
|
data/lib/standard_audit.rb
CHANGED
|
@@ -2,6 +2,7 @@ require "standard_audit/version"
|
|
|
2
2
|
require "standard_audit/engine"
|
|
3
3
|
require "standard_audit/configuration"
|
|
4
4
|
require "standard_audit/metadata_filter"
|
|
5
|
+
require "standard_audit/record_reference"
|
|
5
6
|
require "standard_audit/sensitive_keys_dry_run"
|
|
6
7
|
require "standard_audit/subscriber"
|
|
7
8
|
require "standard_audit/event_subscriber"
|
|
@@ -52,9 +53,26 @@ module StandardAudit
|
|
|
52
53
|
|
|
53
54
|
actor ||= config.current_actor_resolver.call
|
|
54
55
|
|
|
56
|
+
if block_given?
|
|
57
|
+
# Block form: instrument via ActiveSupport::Notifications and let the
|
|
58
|
+
# Subscriber write the row, which it does with its own dereferencing and
|
|
59
|
+
# filtering. Nothing built below would be used, so it is not built —
|
|
60
|
+
# dereferencing a Relation here would load it eagerly, before the block
|
|
61
|
+
# has run, purely to discard the result.
|
|
62
|
+
ActiveSupport::Notifications.instrument(event_type, metadata.merge(
|
|
63
|
+
actor: actor, target: target, scope: scope
|
|
64
|
+
)) do
|
|
65
|
+
yield
|
|
66
|
+
end
|
|
67
|
+
return
|
|
68
|
+
end
|
|
69
|
+
|
|
55
70
|
# Redaction lives in MetadataFilter, shared with Subscriber, so the two
|
|
56
|
-
# write paths cannot drift apart.
|
|
57
|
-
|
|
71
|
+
# write paths cannot drift apart. Record dereferencing is applied on both
|
|
72
|
+
# paths for the same reason: a snapshot of a whole row is as unrecoverable
|
|
73
|
+
# here as it is on the notifications path.
|
|
74
|
+
dereferenced = config.dereference_record_metadata ? RecordReference.call(metadata) : metadata
|
|
75
|
+
filtered_metadata = MetadataFilter.call(dereferenced, config: config)
|
|
58
76
|
|
|
59
77
|
attrs = {
|
|
60
78
|
event_type: event_type,
|
|
@@ -66,16 +84,6 @@ module StandardAudit
|
|
|
66
84
|
metadata: filtered_metadata
|
|
67
85
|
}
|
|
68
86
|
|
|
69
|
-
if block_given?
|
|
70
|
-
# Block form: instrument via ActiveSupport::Notifications
|
|
71
|
-
ActiveSupport::Notifications.instrument(event_type, metadata.merge(
|
|
72
|
-
actor: actor, target: target, scope: scope
|
|
73
|
-
)) do
|
|
74
|
-
yield
|
|
75
|
-
end
|
|
76
|
-
return
|
|
77
|
-
end
|
|
78
|
-
|
|
79
87
|
gid_attrs = {
|
|
80
88
|
actor_gid: actor&.to_global_id&.to_s,
|
|
81
89
|
actor_type: actor&.class&.name,
|
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.
|
|
4
|
+
version: 0.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jaryl Sim
|
|
@@ -114,6 +114,7 @@ files:
|
|
|
114
114
|
- lib/standard_audit/metadata_filter.rb
|
|
115
115
|
- lib/standard_audit/operation.rb
|
|
116
116
|
- lib/standard_audit/operation/audit.rb
|
|
117
|
+
- lib/standard_audit/record_reference.rb
|
|
117
118
|
- lib/standard_audit/reference_preloading.rb
|
|
118
119
|
- lib/standard_audit/rspec.rb
|
|
119
120
|
- lib/standard_audit/rspec/operation.rb
|