Skip to content

Commit 6e30e4e

Browse files
authored
Anchor OIDC filter_parameters so host params like code_id and state_eq stay visible (#27)
Match OIDC filter_parameters by exact key, not substring (#26) ParameterFilter treated the engine's symbol keys as substrings, so :code and :state also hid host params like code_id, state_eq and postal_code. Token keys (code_verifier, id_token, access_token, refresh_token) are now filtered at any depth. The generic callback keys (code, state, session_state, nonce) are filtered only at the top level, where the IdP sends them, so host attributes like data.attributes.state stay visible. Hosts that relied on the substring match must add their own filters. Co-Authored-By: Clanker
1 parent 08c3bb3 commit 6e30e4e

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
- Fix: the OIDC `filter_parameters` entries now match exact keys instead of substrings. `code_verifier`, `id_token`, `access_token` and `refresh_token` are filtered at any depth; `code`, `state`, `session_state` and `nonce` only at the top level. Host params such as `code_id`, `state_eq` or `order[state]` are no longer filtered from logs. If you relied on the old substring match to hide params like `invite_code` or `reset_code`, add them to your own `filter_parameters`.

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The gem's Rails engine handles several things so host apps don't have to:
8383
* **Login view override** — the engine prepends an SSO-only login page (no email/password fields) to the sessions controller's view path. If your host app ships its own `app/views/active_admin/devise/sessions/new.html.erb`, the gem detects it and backs off — your view wins.
8484
* **Session routes** — the engine mounts `GET /admin/login` (renders the SSO landing page) and `DELETE /admin/logout` under `devise_scope`, with the scope name derived from `config.admin_user_class`. Devise normally generates session routes as a side effect of `:database_authenticatable`; without that module the route helpers would not exist and ActiveAdmin's login redirect would 404.
8585
* **Path prefix** — the engine registers the strategy with `path_prefix: '/admin/auth'` so the middleware intercepts requests under ActiveAdmin's mount point, and sets `Devise.omniauth_path_prefix` to the prefix Devise declares its routes with. Compatible with Rails 7.2+ and Rails 8's lazy route loading.
86-
* **Parameter filtering** — `code`, `id_token`, `access_token`, `refresh_token`, `state`, and `nonce` are added to `Rails.application.config.filter_parameters`.
86+
* **Parameter filtering** — the OIDC keys are added to `Rails.application.config.filter_parameters` as exact-key matches. `code_verifier`, `id_token`, `access_token` and `refresh_token` are filtered at any depth. The generic `code`, `state`, `session_state` and `nonce` are filtered only as top-level params, where the callback sends them, so host params like `code_id`, `state_eq` or `order[state]` stay visible.
8787

8888
## Configuration
8989

@@ -410,7 +410,7 @@ The gem also adds a unique `(provider, uid)` partial index in its own install mi
410410

411411
### What's filtered from logs
412412

413-
The engine merges `code`, `id_token`, `access_token`, `refresh_token`, `state`, and `nonce` into `Rails.application.config.filter_parameters` so a mid-callback crash can't dump them into production logs. Your own `filter_parameters` entries are preserved.
413+
The engine adds the OIDC keys to `Rails.application.config.filter_parameters`, so a mid-callback crash can't dump them into production logs. Keys are matched exactly. `code_verifier`, `id_token`, `access_token` and `refresh_token` are filtered at any depth; `code`, `state`, `session_state` and `nonce` only as top-level params. So `code_id`, `state_eq` and nested params like `order[state]` are not filtered. Your own `filter_parameters` entries are preserved.
414414

415415
## Logger
416416

‎lib/activeadmin/oidc/engine.rb‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ def controllers
136136
end
137137

138138
initializer 'activeadmin_oidc.filter_parameters' do |app|
139-
app.config.filter_parameters |= %i[code id_token access_token refresh_token state nonce]
139+
# ActiveSupport::ParameterFilter matches a regexp containing "\." against the dotted
140+
# full key (data.attributes.state), so the generic callback names stay visible when nested.
141+
callback_params = %w[code state session_state nonce]
142+
token_params = %w[code_verifier id_token access_token refresh_token]
143+
app.config.filter_parameters |= [
144+
/\A(?!.*\.)(?:#{Regexp.union(callback_params).source})\z/i,
145+
/\A(?:#{Regexp.union(token_params).source})\z/i
146+
]
140147
end
141148

142149
# The gem is OIDC-first: mount our SSO landing page at /admin/login

‎spec/security_spec.rb‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@
1313
# out of the Rails log is a liability — a crash mid-callback will
1414
# dump the whole params hash (including `code`, `id_token`,
1515
# `access_token`, `refresh_token`) straight into production logs.
16-
#
17-
# Note: once Rails has served a request, filter_parameters is
18-
# compiled into a single combined regex (e.g.
19-
# "(?-mix:(?i:code)|(?i:id_token)|(?i:access_token)|...)"
20-
# ), so we stringify the collection and look for each key in it
21-
# rather than asserting on discrete symbol entries.
22-
it "includes the OIDC token/code/state/nonce keys so they never end up in Rails logs" do
23-
filters_str = Rails.application.config.filter_parameters.map(&:to_s).join(" ")
16+
let(:oidc_keys) { %w[code code_verifier state session_state nonce id_token access_token refresh_token] }
17+
let(:host_params) do
18+
{ "code_id" => "1", "state_eq" => "2", "data" => { "attributes" => { "state" => "shipped", "code" => "X" } } }
19+
end
20+
let(:nested_tokens) { %w[code_verifier id_token access_token refresh_token] }
21+
22+
[
23+
["raw", ->(filters) { filters }],
24+
["precompiled", ->(filters) { ActiveSupport::ParameterFilter.precompile_filters(filters) }]
25+
].each do |form, compile|
26+
it "filters OIDC keys and nested tokens, and leaves host params visible (#{form} filters)" do
27+
filter = ActiveSupport::ParameterFilter.new(compile.call(Rails.application.config.filter_parameters))
28+
params = oidc_keys.index_with("secret").merge(host_params, "auth" => nested_tokens.index_with("secret"))
29+
result = filter.filter(params)
2430

25-
%w[code id_token access_token refresh_token state nonce].each do |key|
26-
expect(filters_str).to include(key),
27-
"expected filter_parameters to filter #{key.inspect}, got: #{filters_str}"
31+
expect(result).to eq(oidc_keys.index_with("[FILTERED]")
32+
.merge(host_params, "auth" => nested_tokens.index_with("[FILTERED]")))
2833
end
2934
end
3035
end

0 commit comments

Comments
 (0)