open-loam 0.2.0 → 0.4.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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +178 -0
  3. data/README.md +4 -2
  4. data/app/jobs/open_loam/event_log_prune_job.rb +15 -0
  5. data/app/jobs/open_loam/webhook_delivery_job.rb +22 -35
  6. data/app/jobs/open_loam/webhook_sweep_job.rb +22 -0
  7. data/app/models/open_loam/api_token.rb +27 -4
  8. data/app/models/open_loam/event_record.rb +42 -0
  9. data/app/models/open_loam/pending_action.rb +47 -3
  10. data/app/models/open_loam/sso_provider.rb +31 -0
  11. data/app/models/open_loam/webhook_delivery.rb +40 -0
  12. data/app/models/open_loam/webhook_endpoint.rb +130 -11
  13. data/app/views/open_loam/custom_fields/_fields.html.erb +4 -0
  14. data/lib/generators/open_loam/entity/templates/api_controller.rb +1 -0
  15. data/lib/generators/open_loam/entity/templates/controller.rb +11 -1
  16. data/lib/generators/open_loam/entity/templates/views/_form.html.erb +4 -1
  17. data/lib/generators/open_loam/entity/templates/views/index.html.erb +6 -2
  18. data/lib/generators/open_loam/entity/templates/views/show.html.erb +2 -0
  19. data/lib/generators/open_loam/install/install_generator.rb +13 -0
  20. data/lib/generators/open_loam/install/templates/AGENTS.md +6 -2
  21. data/lib/generators/open_loam/install/templates/admin/api_tokens_controller.rb +25 -7
  22. data/lib/generators/open_loam/install/templates/admin/api_tokens_index.html.erb +23 -0
  23. data/lib/generators/open_loam/install/templates/admin/base_controller.rb +63 -0
  24. data/lib/generators/open_loam/install/templates/admin/dashboard_controller.rb +2 -0
  25. data/lib/generators/open_loam/install/templates/admin/events_controller.rb +2 -0
  26. data/lib/generators/open_loam/install/templates/admin/history_controller.rb +26 -0
  27. data/lib/generators/open_loam/install/templates/admin/history_index.html.erb +1 -1
  28. data/lib/generators/open_loam/install/templates/admin/imports_controller.rb +13 -1
  29. data/lib/generators/open_loam/install/templates/admin/inbound_webhook_sources_controller.rb +1 -1
  30. data/lib/generators/open_loam/install/templates/admin/inbound_webhook_sources_new.html.erb +0 -2
  31. data/lib/generators/open_loam/install/templates/admin/mfa_controller.rb +5 -0
  32. data/lib/generators/open_loam/install/templates/admin/notifications_controller.rb +2 -0
  33. data/lib/generators/open_loam/install/templates/admin/perspectives_controller.rb +6 -0
  34. data/lib/generators/open_loam/install/templates/admin/progress_jobs_controller.rb +4 -0
  35. data/lib/generators/open_loam/install/templates/admin/search_controller.rb +2 -0
  36. data/lib/generators/open_loam/install/templates/admin/sessions_controller.rb +3 -4
  37. data/lib/generators/open_loam/install/templates/admin/sso_providers_index.html.erb +8 -1
  38. data/lib/generators/open_loam/install/templates/admin/sudo_controller.rb +3 -1
  39. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_controller.rb +4 -1
  40. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_created.html.erb +9 -0
  41. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_index.html.erb +8 -6
  42. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_new.html.erb +2 -2
  43. data/lib/generators/open_loam/install/templates/api_base_controller.rb +43 -6
  44. data/lib/generators/open_loam/install/templates/import_job.rb +14 -1
  45. data/lib/generators/open_loam/install/templates/initializer.rb +14 -2
  46. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_api_tokens.rb +2 -2
  47. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_event_records.rb +13 -0
  48. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_inbound_webhooks.rb +0 -1
  49. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_sso_providers.rb +3 -0
  50. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_deliveries.rb +29 -0
  51. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_endpoints.rb +12 -2
  52. data/lib/open_loam/auth_throttle.rb +11 -3
  53. data/lib/open_loam/custom_field_index.rb +11 -8
  54. data/lib/open_loam/encryptable.rb +8 -3
  55. data/lib/open_loam/encryption/key_provider.rb +17 -1
  56. data/lib/open_loam/encryption.rb +47 -4
  57. data/lib/open_loam/engine.rb +14 -0
  58. data/lib/open_loam/errors.rb +10 -0
  59. data/lib/open_loam/event_log.rb +77 -0
  60. data/lib/open_loam/export.rb +6 -6
  61. data/lib/open_loam/import.rb +1 -4
  62. data/lib/open_loam/inbound_webhooks.rb +12 -11
  63. data/lib/open_loam/lifecycle.rb +29 -0
  64. data/lib/open_loam/mcp.rb +10 -1
  65. data/lib/open_loam/outbound_url.rb +120 -0
  66. data/lib/open_loam/policy.rb +16 -3
  67. data/lib/open_loam/search/token_driver.rb +1 -1
  68. data/lib/open_loam/sso/http_client.rb +15 -6
  69. data/lib/open_loam/sso.rb +23 -2
  70. data/lib/open_loam/version.rb +1 -1
  71. data/lib/open_loam/webhooks/http_transport.rb +76 -0
  72. data/lib/open_loam/webhooks.rb +271 -17
  73. data/lib/open_loam.rb +3 -0
  74. data/lib/tasks/open_loam.rake +32 -0
  75. metadata +12 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fda058767d913097dee8faac85c9957d680233405c3d57004cf786a9ccc155d
4
- data.tar.gz: 87763cfa7c0fa23c24111d8dceda31d73c491b6b8ead4cd1559aaeb742c7df2e
3
+ metadata.gz: 2617b3b1e2ebbdb4b37164f3f0544cdd8ae2130c40d87bda37f535ae08aa05c7
4
+ data.tar.gz: b04798cd19ac5aa56d73c362d848e65220ac61857abd878889b8f0d9aae1def4
5
5
  SHA512:
6
- metadata.gz: 1d40e81813088308a110726cea561b9a2fa4496658e95b7292e64af28473d2363a6b629546892989fae52abd05cd13bddc76578fdd08257fed60f8736ca37efc
7
- data.tar.gz: 97caae5c7991ccadff19905a0224959aa9c40b1c446dfc39dcae4a51bb575ce685c579d24836e92069c7b8b4788c20002d519e3e613556967c4e43e63bd90f87
6
+ metadata.gz: d0663a2e2b2145fdc208e614d0b2c468a910125bcee3dcc9709553f85d1eb56c377829dfba630d2c3cd01545e52bac5c3d1568e074d71c3e461fd2a7568d6844
7
+ data.tar.gz: 998ad613e6df75073714b95d6c237d007176861dd47b493e7b79568bd46354fdba9ba4250516eb06516277e6db473f6d1122f8047a012be6cbe126f2dece8de1
data/CHANGELOG.md CHANGED
@@ -5,6 +5,184 @@ with the caveat that 0.x releases may break the public surface; the
5
5
  [backward-compatibility contract](BACKWARD_COMPATIBILITY.md) names what is
6
6
  frozen and what is not.
7
7
 
8
+ ## 0.4.0 — 2026-11-20
9
+
10
+ ### Fixed
11
+
12
+ - **Outbound webhooks dropped data on any receiver error.** The response code
13
+ was never read, so a 500 or a timeout lost the event. Deliveries are now rows
14
+ (`open_loam_webhook_deliveries`) retried by response code.
15
+ - **SSRF through IPv4-mapped IPv6 literals** (`http://[::ffff:127.0.0.1]/`,
16
+ `[::ffff:169.254.169.254]`). `OutboundUrl` judges mapped and compatible
17
+ addresses by the IPv4 ranges and blocks NAT64 (`64:ff9b::/96`,
18
+ `64:ff9b:1::/48`).
19
+
20
+ ### Added
21
+
22
+ - Webhook delivery ledger, retry schedule, `Retry-After`, auto-disable,
23
+ `open_loam.webhook.endpoint_disabled` event, sweep job, retention prune,
24
+ `send_test!`, `redeliver!` (finished deliveries only; resets `attempts`).
25
+ - Auto-disable needs BOTH `disable_after_failures` (default 50) failures in a row
26
+ AND a streak older than `disable_after_duration` (default 24h; new
27
+ `failing_since` column). 410 and a blocked URL still disable at once. Errors
28
+ raised by a host `body_builder` (other than `SkipDelivery`) are retried without
29
+ counting against the endpoint.
30
+ - The transport bounds the whole request at 3 x `timeout`, ignores `http_proxy`,
31
+ and stores a UTF-8-safe, NUL-free excerpt. A stuck row past the retry schedule
32
+ is failed (`last_error: "stuck"`) by the sweep instead of resent.
33
+ - The admin shows a new endpoint's signing secret once, on the page rendered
34
+ after create (`webhook_endpoints/created.html.erb`, `Cache-Control: no-store`).
35
+ - `<prefix>-Signature-V2` (timestamped) plus `-Event-Id`, `-Delivery`,
36
+ `-Timestamp` headers; configurable prefix and user agent. The transport sends
37
+ `Accept-Encoding: identity` and reads at most 512 bytes of the response.
38
+ - Secret rotation with a grace period; optional sealing of secrets
39
+ (`encrypt_secrets`).
40
+ - `event_patterns` (several per endpoint), `metadata` and `description` on
41
+ endpoints; host `body_builder` and `SkipDelivery`.
42
+ - `OutboundUrl` `ports:` allowlist; `require_https`/`allowed_ports` for webhooks.
43
+ - `OpenLoam::OutboundUrl::UnresolvableError` (a `BlockedError` subclass) for a
44
+ host that resolves to nothing. Webhook deliveries retry it like a network
45
+ error; only a URL that resolves to a non-public address fails and disables the
46
+ endpoint (`blocked_url`).
47
+
48
+ #### Migration
49
+
50
+ Copy `demo/db/migrate/20261120090000_add_open_loam_webhook_reliability.rb`.
51
+ Existing endpoints keep working unchanged (single pattern, plaintext secret).
52
+ Jobs enqueued by 0.3.0 are still accepted. Apps that do not tick
53
+ `OpenLoam::Scheduler` must schedule `OpenLoam::WebhookSweepJob` every minute.
54
+ An error raised by a subscriber to `open_loam.webhook.endpoint_disabled`
55
+ propagates out of the delivery job rather than being retried.
56
+ The admin endpoints index no longer prints the secret; re-copy it if you
57
+ generated it. Generated apps get a new `admin/webhook_endpoints/created.html.erb`
58
+ and an updated controller (create renders it with status 201).
59
+
60
+ The migration adds `open_loam_webhook_endpoints.failing_since` and indexes on
61
+ `open_loam_webhook_deliveries` `(status, next_attempt_at)` and
62
+ `(status, updated_at)` for the per-minute cross-tenant sweep lookup. It is tagged
63
+ `Migration[8.2]` and uses bigint keys: adjust the version for your Rails, and the
64
+ `references ... type:` for uuid or string primary keys.
65
+
66
+ With `require_https` or `allowed_ports` switched on, an existing `http://` or
67
+ non-allowed-port endpoint fails its first delivery and is disabled as
68
+ `blocked_url`. Review existing endpoints before turning these on.
69
+
70
+ ## 0.3.0 — 2026-09-05
71
+
72
+ ### Security
73
+
74
+ A full-repo audit found 16 issues. Fifteen are fixed here, each with a regression
75
+ test verified by reverting the fix and confirming the test fails; the sixteenth
76
+ is the demo app's committed `master.key`, which is accepted — the demo holds no
77
+ real data and must never be deployed. **Anyone running 0.2.0 or earlier should
78
+ upgrade**, and the migration below is not optional: three steps change stored
79
+ data.
80
+
81
+ Worst first:
82
+
83
+ - **Cross-tenant account takeover through SSO.** A tenant could register a domain
84
+ it did not own and be handed the matching global account, then select any
85
+ tenant that account belonged to. A provider now does nothing until an operator
86
+ confirms ownership out of band (`rake open_loam:sso:verify_domain`), and
87
+ editing `domain` revokes the confirmation.
88
+ - **The MFA lockout was bypassable** — a password success cleared the TOTP
89
+ counter, so a 6-digit code could be brute-forced without limit. Each factor now
90
+ clears only its own attempts.
91
+ - **`readable:` field rules were ignored on most read paths** — the JSON API, the
92
+ generated admin index/show/form, the custom-fields partial, the CSV export,
93
+ the history screen and the edit-conflict diff all served restricted values.
94
+ Sorting by an unreadable column is refused too: ordering leaks a ranking even
95
+ when the values are hidden. A model with no policy class no longer falls back
96
+ to the base policy, which answered "any member" to everything.
97
+ - **API tokens were stored in plaintext.** A SHA-256 digest is persisted now and
98
+ the value is shown once. Offboarding a user ends their machine access, and
99
+ managers can revoke another member's token — previously nobody could.
100
+ - **SSRF through tenant-supplied URLs** — webhook endpoints and the SSO issuer
101
+ were both fetched unvalidated. `OpenLoam::OutboundUrl` checks shape at save and
102
+ pins the resolved address at fetch, so DNS rebinding cannot swap the target
103
+ after the check. The issuer must now be `https`.
104
+ - **Inbound webhook replay** — the dedupe key came from an unsigned header, so
105
+ one captured delivery could re-publish its event without limit. `external_id`
106
+ is now the body hash, the only signed material.
107
+ - **A staged change was applied without checks.** The target must now be a
108
+ `TenantRecord`, and approval runs the approver's own policy — both the action
109
+ (`create?`/`update?`/`destroy?`) and the fields the changeset writes.
110
+ - **`security.mfa_required_roles` was advisory** — enrollment was redirected to
111
+ at login and never checked again. Now enforced on every request.
112
+ - **Master-key rotation was impossible**, despite being the documented answer to
113
+ key compromise. Set `OPEN_LOAM_PREVIOUS_MASTER_KEY` to the outgoing key and
114
+ `open_loam:encryption:rotate` works.
115
+ - **The blind-index key ignored table and column**, so one value hashed alike
116
+ across every searchable encrypted column. It now binds both, like the AAD.
117
+ - **The uploaded import CSV** sat in the clear in the queue backend and the
118
+ request log. It goes to blob storage now, and the job purges it.
119
+ - **Brakeman never scanned the gem**, only `demo/`. CI now scans both, which
120
+ surfaced four warnings, all fixed. CI's `GITHUB_TOKEN` is read-only, and the
121
+ repository has secret scanning, push protection and private vulnerability
122
+ reporting enabled.
123
+
124
+ Report vulnerabilities through [private reporting](https://github.com/DeliveristsIO/open-loam/security/advisories/new),
125
+ not a public issue.
126
+
127
+ #### Migration
128
+
129
+ - `open_loam_api_tokens`: add `token_digest`, backfill `sha256(token)` per row,
130
+ drop `token`. Tokens in use keep working. See the demo's
131
+ `hash_open_loam_api_tokens`.
132
+ - `open_loam_sso_providers`: add `domain_verified_at`, then run
133
+ `rake open_loam:sso:verify_domain[<id>]` for each provider you actually own.
134
+ Until you do, SSO will not resolve for it.
135
+ - Searchable encrypted fields: re-index by running
136
+ `rake open_loam:encryption:rotate[Model,tenant_id]` for each model that has
137
+ one. The blind-index derivation changed, so existing `<field>_hash` values no
138
+ longer match.
139
+ - `delivery_id_header` on inbound webhook sources is gone; the column can be
140
+ dropped.
141
+
142
+ ### Added
143
+
144
+ - **The event log — `OpenLoam::EventLog` + `OpenLoam::EventRecord`.** Every
145
+ published event is captured as an append-only, tenant-scoped row, so a
146
+ tenant's history is queryable and replayable rather than only observable live.
147
+ `EventLog.read(name_or_prefix, since:, limit:)` and `EventLog.replay(...)` take
148
+ the same patterns as `Events.subscribe`. `OpenLoam::DurableEvents` had made
149
+ *delivery* durable; this closes *capture*, which its contract explicitly did
150
+ not cover.
151
+
152
+ Capture is on by default and captures everything except
153
+ `OpenLoam.uncaptured_events` (default: `["open_loam.progress."]`). It runs
154
+ inline, so a failed insert propagates into the publishing operation. Retention
155
+ is `OpenLoam.event_log_retention` (90 days), swept per tenant by
156
+ `OpenLoam::EventLogPruneJob`. New apps get the table from
157
+ `rails g open_loam:install`; existing apps need the
158
+ `create_open_loam_event_records` migration.
159
+
160
+ - **An authorization-called guard in the generated base controllers** (L-202).
161
+ `verify_authorized!` fails any admin or API action that finished without
162
+ calling `authorize!`, `require_role!` or `require_permission!`. Forgetting the
163
+ check was previously silent — the screen just rendered. Screens authorized
164
+ structurally declare `skip_authorization! "<reason>"`, and the reason is
165
+ required, so every exemption is a documented claim.
166
+
167
+ It runs *after* the action, so it is a development and test guard, not a
168
+ runtime access-control layer. It raises `OpenLoam::AuthorizationNotPerformedError`,
169
+ deliberately not a `NotAuthorizedError` — a developer bug must not render as a
170
+ polite 403.
171
+
172
+ Turning it on found two generated actions that never checked `read?` while
173
+ their `show`/`deleted` siblings did: the admin and JSON `index`. Both now
174
+ authorize. Existing apps will see the guard fire on any action of their own
175
+ that never authorized.
176
+
177
+ ### Changed
178
+
179
+ - **The four "wrap a proven gem" roadmap items are resolved**, recorded in
180
+ [ADR 0007](docs/_adr/0007-proven-gem-swaps-resolved.md). Tenancy (L-201) and
181
+ audit (L-203) stay in-gem for good; the Pundit swap (L-202) is declined, but
182
+ the gap it exposed — no `verify_authorized`-equivalent guard — is tracked
183
+ separately. L-204 is the event log above. No public `OpenLoam::` contract
184
+ changed.
185
+
8
186
  ## 0.2.0 — 2026-09-05
9
187
 
10
188
  ### BREAKING
data/README.md CHANGED
@@ -136,8 +136,10 @@ whole thesis in miniature.
136
136
  **How honest the "prototype" label is** — deliberately, each pillar is a
137
137
  *minimal in-gem implementation* rather than a wrapper around
138
138
  `acts_as_tenant`/`pundit`/`paper_trail`/Rails Event Store: the smallest surface
139
- that proves the conventions and the agent flow. Swapping the proven gems back in
140
- *behind the same `OpenLoam::` conventions* is the roadmap, not a reversal. Custom
139
+ that proves the conventions and the agent flow. Those swaps have since been
140
+ evaluated one at a time and settled — the in-gem versions stay, and the one real
141
+ gap they exposed (event capture) was closed in-gem too, in
142
+ [ADR 0007](docs/_adr/0007-proven-gem-swaps-resolved.md). Custom
141
143
  fields use the portable Rails `json` column (not Postgres `jsonb`/GIN) because
142
144
  the demo runs on SQLite. See [How OpenLoam works](docs/_foundation/overview.md) for
143
145
  the pillar-by-pillar breakdown and the decisions behind them.
@@ -0,0 +1,15 @@
1
+ module OpenLoam
2
+ # Enforces the event log's retention window (OpenLoam::EventLog.prune).
3
+ # Registered per-tenant in OpenLoam::Engine, so it runs inside one tenant and
4
+ # never deletes across the boundary.
5
+ class EventLogPruneJob < ActiveJob::Base
6
+ queue_as :default
7
+
8
+ def perform(tenant_id: nil)
9
+ tenant = OpenLoam::Tenant.find_by(id: tenant_id)
10
+ return if tenant.nil?
11
+
12
+ OpenLoam.as_tenant(tenant) { OpenLoam::EventLog.prune }
13
+ end
14
+ end
15
+ end
@@ -2,19 +2,16 @@ require "net/http"
2
2
  require "openssl"
3
3
 
4
4
  module OpenLoam
5
- # Delivers one event to one endpoint. Inherits ActiveJob::Base rather than
6
- # the host app's ApplicationJob: the gem must not depend on a class the app
7
- # owns and may have configured for its own retries.
5
+ # Runs one attempt for one ledger row (OpenLoam::WebhookDelivery). Inherits
6
+ # ActiveJob::Base rather than the host's ApplicationJob, and keeps no retry
7
+ # state: OpenLoam::Webhooks.deliver moves the row and schedules the next try.
8
8
  #
9
- # Signing: the receiver recomputes HMAC-SHA256 of the exact body with the
10
- # endpoint's secret and compares it to X-OpenLoam-Signature. Body building and
11
- # signing are class methods so both sides — and the tests — can call them
12
- # without a network.
9
+ # The 4-argument form is what 0.3.0 enqueued (endpoint id, event, payload); a
10
+ # job still in the queue at upgrade time becomes a ledger row and is delivered.
13
11
  class WebhookDeliveryJob < ActiveJob::Base
14
12
  queue_as :default
15
13
 
16
- TIMEOUT_SECONDS = 5
17
-
14
+ # Frozen (BACKWARD_COMPATIBILITY.md §6): the default body and the v1 signature.
18
15
  def self.body_for(event_name, payload, tenant_id)
19
16
  JSON.generate(event: event_name, payload: payload, tenant_id: tenant_id)
20
17
  end
@@ -23,37 +20,27 @@ module OpenLoam
23
20
  "sha256=#{OpenSSL::HMAC.hexdigest('SHA256', secret.to_s, body)}"
24
21
  end
25
22
 
26
- def perform(tenant_id, endpoint_id, event_name, payload)
23
+ def perform(tenant_id, delivery_id, legacy_event_name = nil, legacy_payload = nil)
27
24
  tenant = OpenLoam::Tenant.find_by(id: tenant_id)
28
25
  return if tenant.nil?
29
26
 
30
27
  OpenLoam.as_tenant(tenant) do
31
- endpoint = OpenLoam::WebhookEndpoint.find_by(id: endpoint_id)
32
- # The endpoint may have been deleted or switched off between enqueue
33
- # and delivery — that is an answer, not an error.
34
- next if endpoint.nil? || !endpoint.active?
35
-
36
- deliver(endpoint, self.class.body_for(event_name, payload, tenant_id), event_name)
28
+ delivery =
29
+ if legacy_event_name
30
+ endpoint = OpenLoam::WebhookEndpoint.find_by(id: delivery_id)
31
+ next if endpoint.nil? || !endpoint.active?
32
+
33
+ endpoint.deliveries.create!(event_id: SecureRandom.uuid, event_name: legacy_event_name.to_s,
34
+ payload: legacy_payload || {})
35
+ else
36
+ OpenLoam::WebhookDelivery.find_by(id: delivery_id)
37
+ end
38
+ # Not visible yet (the creating transaction has not committed) or gone:
39
+ # the sweep covers the first case.
40
+ next if delivery.nil?
41
+
42
+ OpenLoam::Webhooks.deliver(delivery)
37
43
  end
38
44
  end
39
-
40
- private
41
-
42
- def deliver(endpoint, body, event_name)
43
- uri = URI.parse(endpoint.url)
44
-
45
- http = Net::HTTP.new(uri.host, uri.port)
46
- http.use_ssl = uri.scheme == "https"
47
- http.open_timeout = TIMEOUT_SECONDS
48
- http.read_timeout = TIMEOUT_SECONDS
49
-
50
- request = Net::HTTP::Post.new(uri.request_uri)
51
- request["Content-Type"] = "application/json"
52
- request["X-OpenLoam-Event"] = event_name
53
- request["X-OpenLoam-Signature"] = self.class.signature(endpoint.secret, body)
54
- request.body = body
55
-
56
- http.request(request)
57
- end
58
45
  end
59
46
  end
@@ -0,0 +1,22 @@
1
+ module OpenLoam
2
+ # Finishes what a lost job left behind and prunes the ledger.
3
+ #
4
+ # With a tenant (how OpenLoam::Scheduler runs it): sweeps that tenant.
5
+ # Without one (how a host's own cron runs it, e.g. Solid Queue recurring):
6
+ # enqueues one tenant-scoped sweep per tenant that has work.
7
+ class WebhookSweepJob < ActiveJob::Base
8
+ queue_as :default
9
+
10
+ def perform(tenant_id: nil)
11
+ if tenant_id.nil?
12
+ OpenLoam::Webhooks.tenant_ids_needing_sweep.each { |id| self.class.perform_later(tenant_id: id) }
13
+ return
14
+ end
15
+
16
+ tenant = OpenLoam::Tenant.find_by(id: tenant_id)
17
+ return if tenant.nil?
18
+
19
+ OpenLoam.as_tenant(tenant) { OpenLoam::Webhooks.sweep }
20
+ end
21
+ end
22
+ end
@@ -1,16 +1,31 @@
1
+ require "openssl"
2
+
1
3
  module OpenLoam
2
4
  # A bearer token that lets a machine act as one user in one tenant. Same
3
5
  # rules as a human session: whatever the token's user may do in that tenant,
4
6
  # no more. Plumbing, so not audited and not evented.
7
+ #
8
+ # Only the SHA-256 digest is stored. The plaintext is returned once, from the
9
+ # instance that generated it, and is unrecoverable afterwards — a dump of this
10
+ # table is not a set of working credentials. A 24-byte random token needs no
11
+ # slow KDF; it is not guessable the way a password is.
5
12
  class ApiToken < OpenLoam::TenantRecord
6
13
  self.table_name = "open_loam_api_tokens"
7
14
 
8
15
  belongs_to :user
9
16
 
10
- validates :token, presence: true, uniqueness: true
17
+ validates :token_digest, presence: true, uniqueness: true
18
+
19
+ # Present only on the instance that just created it — never after a reload.
20
+ attr_reader :token
11
21
 
12
22
  before_validation on: :create do
13
- self.token ||= SecureRandom.hex(24)
23
+ @token ||= SecureRandom.hex(24)
24
+ self.token_digest = self.class.digest(@token)
25
+ end
26
+
27
+ def self.digest(raw_token)
28
+ OpenSSL::Digest::SHA256.hexdigest(raw_token.to_s)
14
29
  end
15
30
 
16
31
  # THE blessed cross-tenant lookup, and the reason it lives in the gem.
@@ -27,13 +42,21 @@ module OpenLoam
27
42
  def self.authenticate(raw_token)
28
43
  return nil if raw_token.blank?
29
44
 
30
- api_token = unscoped.find_by(token: raw_token)
45
+ api_token = unscoped.find_by(token_digest: digest(raw_token))
31
46
  return nil unless api_token
32
47
 
33
48
  OpenLoam::Current.tenant = api_token.tenant
34
49
  OpenLoam::Current.actor = api_token.user
35
- api_token.update_column(:last_used_at, Time.current)
36
50
 
51
+ # A token outlives the membership that justified it, so offboarding a user
52
+ # from a tenant would otherwise leave their machine access intact. Checked
53
+ # here rather than at revoke time: the membership is the authority.
54
+ unless OpenLoam::Membership.exists?(user_id: api_token.user_id)
55
+ OpenLoam::Current.reset
56
+ return nil
57
+ end
58
+
59
+ api_token.update_column(:last_used_at, Time.current)
37
60
  api_token
38
61
  end
39
62
  end
@@ -0,0 +1,42 @@
1
+ module OpenLoam
2
+ # One captured domain event (see OpenLoam::EventLog). Where
3
+ # OpenLoam::EventDelivery records that a durable subscriber was told, this
4
+ # records that the thing happened at all.
5
+ #
6
+ # Append-only: a persisted row is readonly, so retention must delete_all
7
+ # rather than destroy.
8
+ class EventRecord < OpenLoam::TenantRecord
9
+ self.table_name = "open_loam_event_records"
10
+
11
+ validates :name, :occurred_at, presence: true
12
+
13
+ scope :chronological, -> { order(:occurred_at, :id) }
14
+
15
+ # ESCAPE is load-bearing: sanitize_sql_like backslash-escapes `_`, and SQLite
16
+ # has no default escape character, so "damage_report." would search for a
17
+ # literal backslash and silently match nothing.
18
+ scope :matching, ->(name_or_prefix) {
19
+ pattern = name_or_prefix.to_s
20
+ if pattern.end_with?(".")
21
+ where("name LIKE ? ESCAPE ?", "#{sanitize_sql_like(pattern)}%", "\\")
22
+ else
23
+ where(name: pattern)
24
+ end
25
+ }
26
+
27
+ def readonly?
28
+ persisted?
29
+ end
30
+
31
+ # The stored payload as a Hash regardless of adapter (Postgres jsonb returns
32
+ # a Hash; a text/json column may hand back a String).
33
+ def payload_hash
34
+ value = self[:payload]
35
+ return value if value.is_a?(Hash)
36
+
37
+ JSON.parse(value.to_s.presence || "{}")
38
+ rescue JSON::ParserError
39
+ {}
40
+ end
41
+ end
42
+ end
@@ -150,16 +150,26 @@ module OpenLoam
150
150
  update!(error: error.message)
151
151
  end
152
152
 
153
+ # Never settable through a staged change: soft-delete state has its own
154
+ # audited action (and its own policy question), and the rest is plumbing.
155
+ PROTECTED_FIELDS = %w[id tenant_id deleted_at lock_version].freeze
156
+
153
157
  def execute_change!
154
158
  case action_type
155
159
  when "create"
156
- record = target_class.create!(changeset)
160
+ record = target_class.new
161
+ authorize_action!(record, :create?)
162
+ record.assign_attributes(permitted_changeset(record))
163
+ record.save!
157
164
  "created #{target_type}##{record.id}"
158
165
  when "update"
159
- target_class.find(target_id).update!(changeset)
166
+ record = target_class.find(target_id)
167
+ authorize_action!(record, :update?)
168
+ record.update!(permitted_changeset(record))
160
169
  "updated #{target_type}##{target_id}"
161
170
  when "destroy"
162
171
  target = target_class.find(target_id)
172
+ authorize_action!(target, :destroy?)
163
173
  target.respond_to?(:soft_delete!) ? target.soft_delete! : target.destroy!
164
174
  "deleted #{target_type}##{target_id}"
165
175
  else
@@ -167,8 +177,42 @@ module OpenLoam
167
177
  end
168
178
  end
169
179
 
180
+ # The field rules below are not the whole policy: a host may restrict the
181
+ # ACTION itself (destroy? to an owner, say). Staging must not route around
182
+ # that, so the approver's verdict on the action is asked first.
183
+ def authorize_action!(record, question)
184
+ return if OpenLoam::Policy.for(record).public_send(question)
185
+
186
+ raise OpenLoam::NotAuthorizedError,
187
+ "the approver may not #{question.to_s.chomp('?')} #{target_type}"
188
+ end
189
+
190
+ # target_type is a string off a stored row, so it gets the same treatment as
191
+ # any other type resolution from untrusted input. The TenantRecord check is
192
+ # the load-bearing half: a non-tenant-scoped target (User, say) has no
193
+ # default scope, which would turn approval into a cross-tenant write.
170
194
  def target_class
171
- target_type.constantize
195
+ klass = target_type.to_s.safe_constantize
196
+ unless klass.is_a?(Class) && klass < OpenLoam::TenantRecord
197
+ raise OpenLoam::Error, "#{target_type.inspect} is not a tenant-scoped model — refusing to apply a staged change"
198
+ end
199
+
200
+ klass
201
+ end
202
+
203
+ # Staging is deliberately ungated ("propose" is not "apply"), so approval is
204
+ # where authority is spent and the APPROVER's field rules are what bind —
205
+ # otherwise staging routes around every one of them. Refusals raise rather
206
+ # than drop, so the proposal fails visibly instead of applying a lesser change.
207
+ def permitted_changeset(record)
208
+ blocked = changeset.keys & PROTECTED_FIELDS
209
+ raise OpenLoam::Error, "a staged change may not set #{blocked.join(', ')}" if blocked.any?
210
+
211
+ policy = OpenLoam::Policy.for(record)
212
+ refused = changeset.keys - policy.permitted_fields(changeset.keys).map(&:to_s)
213
+ raise OpenLoam::NotAuthorizedError, "the approver may not write #{refused.join(', ')}" if refused.any?
214
+
215
+ changeset
172
216
  end
173
217
 
174
218
  def load_target
@@ -25,11 +25,42 @@ module OpenLoam
25
25
 
26
26
  normalizes :domain, with: ->(domain) { domain.to_s.strip.downcase.presence }
27
27
 
28
+ # The server fetches the issuer's discovery document and posts the code
29
+ # exchange to it, so a manager-typed issuer is an SSRF vector unless it is
30
+ # provably external. https because the client_secret travels over it.
31
+ validate :issuer_is_reachable_from_outside, if: -> { issuer.present? }
32
+
33
+ # Editing the domain drops the proof — otherwise a verified provider could be
34
+ # repointed at a domain nobody approved.
35
+ before_validation :reset_domain_verification, if: -> { persisted? && domain_changed? }
36
+
28
37
  scope :active, -> { where(active: true) }
38
+ scope :domain_verified, -> { where.not(domain_verified_at: nil) }
39
+
40
+ # Until ownership is proven, HRD skips the provider and OpenLoam::Sso refuses
41
+ # to link an existing account through it.
42
+ def domain_verified? = domain_verified_at.present?
43
+
44
+ # Operator entry point (rake open_loam:sso:verify_domain), off the admin path.
45
+ def verify_domain!(at: Time.current)
46
+ update!(domain_verified_at: at)
47
+ end
29
48
 
30
49
  # IdP group -> OpenLoam role, first match wins; falls back to jit_role.
31
50
  def group_roles
32
51
  group_role_map.is_a?(Hash) ? group_role_map : {}
33
52
  end
53
+
54
+ private
55
+
56
+ def reset_domain_verification
57
+ self.domain_verified_at = nil
58
+ end
59
+
60
+ def issuer_is_reachable_from_outside
61
+ OpenLoam::OutboundUrl.validate!(issuer, require_https: true)
62
+ rescue OpenLoam::OutboundUrl::BlockedError => error
63
+ errors.add(:issuer, error.message)
64
+ end
34
65
  end
35
66
  end
@@ -0,0 +1,40 @@
1
+ module OpenLoam
2
+ # One event on its way to one endpoint. The ROW is the record of intent: it is
3
+ # written in the publishing transaction, the job only accelerates it, and the
4
+ # sweep finishes whatever a lost job left behind.
5
+ #
6
+ # pending -> sending -> succeeded | failed | (pending again, with next_attempt_at)
7
+ # pending -> cancelled (endpoint switched off, or the host's body builder skipped it)
8
+ class WebhookDelivery < OpenLoam::TenantRecord
9
+ self.table_name = "open_loam_webhook_deliveries"
10
+
11
+ STATUSES = %w[pending sending succeeded failed cancelled].freeze
12
+ FINAL_STATUSES = %w[succeeded failed cancelled].freeze
13
+
14
+ belongs_to :endpoint, class_name: "OpenLoam::WebhookEndpoint", inverse_of: :deliveries
15
+
16
+ validates :event_id, :event_name, :delivery_id, presence: true
17
+ validates :status, inclusion: { in: STATUSES }
18
+
19
+ before_validation on: :create do
20
+ self.delivery_id ||= "whd_#{SecureRandom.hex(12)}"
21
+ end
22
+
23
+ scope :due, ->(now = Time.current) { where(status: "pending").where("next_attempt_at IS NULL OR next_attempt_at <= ?", now) }
24
+ # Pending rows the job should already have taken: never attempted for a
25
+ # minute, or past their backoff. Fresh rows are left to their own job.
26
+ scope :overdue, ->(now = Time.current) {
27
+ where(status: "pending").where("(next_attempt_at IS NULL AND created_at <= ?) OR next_attempt_at <= ?", now - 60, now)
28
+ }
29
+ scope :stuck, ->(now = Time.current) { where(status: "sending").where("sending_at < ?", now - OpenLoam::Webhooks.stuck_after) }
30
+ scope :prunable, ->(now, retention) { where(status: FINAL_STATUSES).where("updated_at < ?", now - retention) }
31
+ scope :recent_first, -> { order(created_at: :desc, id: :desc) }
32
+
33
+ def payload_hash
34
+ value = self[:payload]
35
+ value.is_a?(String) ? JSON.parse(value) : (value || {})
36
+ end
37
+
38
+ def final? = FINAL_STATUSES.include?(status)
39
+ end
40
+ end