thecore_auth_commons 3.7.1 → 3.8.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/app/controllers/users/sessions_controller.rb +2 -1
- data/app/models/concerns/rails_admin/ldap_server.rb +3 -0
- data/app/models/ldap_server.rb +24 -0
- data/db/migrate/20261125150000_remove_admin_password_index_from_ldap_servers.rb +11 -0
- data/lib/thecore_auth_commons/engine.rb +5 -0
- data/lib/thecore_auth_commons/login_failure_throttle.rb +134 -0
- data/lib/thecore_auth_commons/version.rb +1 -1
- data/lib/thecore_auth_commons.rb +1 -0
- metadata +5 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d4ab06cb52922ac27e548bf8f57a997390428561052a96b87329926c733c7603
|
|
4
|
+
data.tar.gz: 1abb889f8ddea305a754c40598420d0d80a15662da7957d5efaf4d80fc8cc9de
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4559c5d32ef7d910c36c02933c8bd1217d1c3085f50bfc142a4e9bddeeb607ff7242e7e0ea8f25e26f81cdcebf606c42884276344e16ab696cca2e17d3e62061
|
|
7
|
+
data.tar.gz: ae374e58c232268b170483f2c40857660cbe062d18d55a4fc6e12f5d2f6a2a93aaa7f2c5256c8225045a34572f6c6cfbbf4defca9ec4095e6301b4b96ef8e072
|
|
@@ -27,7 +27,8 @@ class Users::SessionsController < Devise::SessionsController
|
|
|
27
27
|
# the wrong-password answer, never the failure app's 302, which would tell that the password was
|
|
28
28
|
# right. Devise refuses by throw :warden from its after_set_user hook (after logging the user
|
|
29
29
|
# out) wherever the user is first authenticated in this request: in create, in sign_in after
|
|
30
|
-
# LDAP, or already in a before_action reading current_user (thecore_ui_commons
|
|
30
|
+
# LDAP, or already in a host's before_action reading current_user (thecore_ui_commons <= 3.4.0
|
|
31
|
+
# did, in reject_locked!),
|
|
31
32
|
# since Devise lets the password strategy run on the form params for the whole sign-in request.
|
|
32
33
|
# So the catch wraps the whole action, callbacks included.
|
|
33
34
|
def process_action(*)
|
|
@@ -8,6 +8,9 @@ module RailsAdmin::LdapServer
|
|
|
8
8
|
rails_admin do
|
|
9
9
|
navigation_label I18n.t('admin.settings.label')
|
|
10
10
|
navigation_icon 'fa fa-passport'
|
|
11
|
+
# Write-only (host ADR 0046 point 10): RailsAdmin's password type shows it only in the edit
|
|
12
|
+
# form, empty, and a blank submit leaves the stored value as it is.
|
|
13
|
+
configure :admin_password, :password
|
|
11
14
|
end
|
|
12
15
|
end
|
|
13
16
|
end
|
data/app/models/ldap_server.rb
CHANGED
|
@@ -14,6 +14,30 @@ class LdapServer < ApplicationRecord
|
|
|
14
14
|
# and need a local password set by hand to log in again.
|
|
15
15
|
after_destroy :turn_users_into_local_users
|
|
16
16
|
|
|
17
|
+
# admin_password is write-only (host ADR 0046 point 10): the authenticator and test_connection
|
|
18
|
+
# read it, nothing serializes it. `methods:` is dropped too, since attributes,
|
|
19
|
+
# attributes_before_type_cast and friends would carry it, and LdapServer serializes no method.
|
|
20
|
+
WRITE_ONLY_ATTRIBUTES = %w[admin_password].freeze
|
|
21
|
+
|
|
22
|
+
# model_driven_api >= 3.17.0 leaves these out of v3's JSON:API serializers, which read the
|
|
23
|
+
# columns directly instead of going through serializable_hash.
|
|
24
|
+
def self.write_only_attributes
|
|
25
|
+
WRITE_ONLY_ATTRIBUTES
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def serializable_hash(options = nil)
|
|
29
|
+
options = (options || {}).to_h.symbolize_keys.except(:methods)
|
|
30
|
+
options[:except] = Array(options[:except]) + WRITE_ONLY_ATTRIBUTES.map(&:to_sym)
|
|
31
|
+
options[:only] = Array(options[:only]).map(&:to_s) - WRITE_ONLY_ATTRIBUTES if options[:only]
|
|
32
|
+
super(options).except(*WRITE_ONLY_ATTRIBUTES)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Not searchable nor sortable through Ransack (the API's q[...] and s): a filter on it would
|
|
36
|
+
# tell the password character by character.
|
|
37
|
+
def self.ransackable_attributes(auth_object = nil)
|
|
38
|
+
(defined?(super) ? super : column_names) - WRITE_ONLY_ATTRIBUTES
|
|
39
|
+
end
|
|
40
|
+
|
|
17
41
|
# The outcome of #test_connection: success? and, when it failed, the reason in message.
|
|
18
42
|
ConnectionResult = Data.define(:success, :message) do
|
|
19
43
|
def success? = success
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# The LDAP bind password is a secret: an index on it is a second copy of it, sorted (host ADR 0046
|
|
2
|
+
# point 10, host Odoo #6084). Down restores the index of CreateLdapServers.
|
|
3
|
+
class RemoveAdminPasswordIndexFromLdapServers < ActiveRecord::Migration[7.2]
|
|
4
|
+
def up
|
|
5
|
+
remove_index :ldap_servers, :admin_password, if_exists: true
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def down
|
|
9
|
+
add_index :ldap_servers, :admin_password, if_not_exists: true
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
module ThecoreAuthCommons
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
|
+
# Right after RemoteIp, so the throttle sees the client's address as Rails resolves it
|
|
4
|
+
# (X-Forwarded-For only from trusted proxies), and wraps every login route (host Odoo #6052).
|
|
5
|
+
initializer "thecore_auth_commons.login_failure_throttle" do |app|
|
|
6
|
+
app.middleware.insert_after ActionDispatch::RemoteIp, ThecoreAuthCommons::LoginFailureThrottle
|
|
7
|
+
end
|
|
3
8
|
end
|
|
4
9
|
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
require "ipaddr"
|
|
2
|
+
|
|
3
|
+
module ThecoreAuthCommons
|
|
4
|
+
# Per-IP limit on failed logins (host Odoo #6052, R6 of host
|
|
5
|
+
# docs/research/admin-access-strong-security.md; release/3, so backward compatible).
|
|
6
|
+
#
|
|
7
|
+
# Only failures count: users behind one NAT, or an integration that signs in often, never meet
|
|
8
|
+
# the limit with the right password. After THECORE_LOGIN_FAILURES_PER_MINUTE (default 20, 0 turns
|
|
9
|
+
# it off) failed attempts within the current minute from one IP, every login attempt from that IP
|
|
10
|
+
# answers 429 until the minute is over. Nothing else changes: no per-account suspension and no
|
|
11
|
+
# uniform error messages, which are release/4 (host ADR 0040).
|
|
12
|
+
#
|
|
13
|
+
# The IP is the connection's peer, unless that peer is a trusted proxy
|
|
14
|
+
# (config.action_dispatch.trusted_proxies, private networks by default): only then is it
|
|
15
|
+
# ActionDispatch's remote_ip, read from X-Forwarded-For. remote_ip alone would believe an
|
|
16
|
+
# X-Forwarded-For sent straight to Puma by anyone, so each forged address would get its own count. A failure is only known from the
|
|
17
|
+
# response, which is why this is a middleware and not a rack-attack rule.
|
|
18
|
+
#
|
|
19
|
+
# Counters live in a Redis store of their own (THECORE_LOGIN_THROTTLE_REDIS_URL, else REDIS_URL),
|
|
20
|
+
# shared by every process; without Redis, in memory per process; in the test environment nowhere. If Redis is down the limit
|
|
21
|
+
# fails open: logins go on as before.
|
|
22
|
+
class LoginFailureThrottle
|
|
23
|
+
DEFAULT_LIMIT = 20
|
|
24
|
+
PERIOD = 60 # seconds
|
|
25
|
+
|
|
26
|
+
# POST /users/sign_in and /api/vN/authenticate, any method on the OAuth callbacks.
|
|
27
|
+
LOGIN_POST_PATH = %r{\A/(users/sign_in|api/v\d+/authenticate)(\.json)?/?\z}
|
|
28
|
+
OAUTH_CALLBACK_PATH = %r{\A/users/auth/[^/]+/callback/?\z}
|
|
29
|
+
SIGN_IN_REDIRECT_PATH = %r{/users/sign_in/?\z}
|
|
30
|
+
|
|
31
|
+
class << self
|
|
32
|
+
attr_writer :store
|
|
33
|
+
|
|
34
|
+
def store
|
|
35
|
+
@store ||= build_store
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def limit
|
|
39
|
+
value = Integer(ENV.fetch("THECORE_LOGIN_FAILURES_PER_MINUTE", DEFAULT_LIMIT.to_s), exception: false)
|
|
40
|
+
value.nil? || value.negative? ? DEFAULT_LIMIT : value
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def build_store
|
|
46
|
+
# Test suites of host apps and ATOMs sign in and fail many times a minute from 127.0.0.1:
|
|
47
|
+
# there nothing is counted unless a test sets a store (as this gem's own tests do).
|
|
48
|
+
return ActiveSupport::Cache::NullStore.new if Rails.env.test?
|
|
49
|
+
|
|
50
|
+
url = ENV["THECORE_LOGIN_THROTTLE_REDIS_URL"].presence || ENV["REDIS_URL"].presence
|
|
51
|
+
return ActiveSupport::Cache::MemoryStore.new if url.nil?
|
|
52
|
+
|
|
53
|
+
require "redis"
|
|
54
|
+
ActiveSupport::Cache::RedisCacheStore.new(
|
|
55
|
+
url: url, namespace: "thecore:login_failures",
|
|
56
|
+
connect_timeout: 0.5, read_timeout: 0.5, write_timeout: 0.5, reconnect_attempts: 0,
|
|
57
|
+
error_handler: ->(method:, returning:, exception:) {
|
|
58
|
+
Rails.logger.warn("[LoginFailureThrottle] Redis #{method} failed, not limiting: #{exception.class}: #{exception.message}")
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
rescue LoadError
|
|
62
|
+
Rails.logger.warn("[LoginFailureThrottle] the redis gem is missing: counting in memory, per process")
|
|
63
|
+
ActiveSupport::Cache::MemoryStore.new
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def initialize(app)
|
|
68
|
+
@app = app
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def call(env)
|
|
72
|
+
limit = self.class.limit
|
|
73
|
+
return @app.call(env) unless limit.positive? && login_attempt?(env)
|
|
74
|
+
|
|
75
|
+
request = ActionDispatch::Request.new(env)
|
|
76
|
+
key = "#{client_ip(request)}:#{Time.now.to_i / PERIOD}"
|
|
77
|
+
return too_many(request) if store.read(key, raw: true).to_i >= limit
|
|
78
|
+
|
|
79
|
+
# Read before the call: Warden's failure app rewrites PATH_INFO to /unauthenticated.
|
|
80
|
+
oauth_callback = OAUTH_CALLBACK_PATH.match?(env["PATH_INFO"].to_s)
|
|
81
|
+
status, headers, body = @app.call(env)
|
|
82
|
+
store.increment(key, 1, expires_in: PERIOD * 2) if failed?(env, status, headers, oauth_callback)
|
|
83
|
+
[status, headers, body]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def store
|
|
89
|
+
self.class.store
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def client_ip(request)
|
|
93
|
+
peer = request.remote_addr.to_s
|
|
94
|
+
trusted_proxy?(peer) ? request.remote_ip : peer
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def trusted_proxy?(address)
|
|
98
|
+
ip = IPAddr.new(address)
|
|
99
|
+
proxies = Rails.application.config.action_dispatch.trusted_proxies || ActionDispatch::RemoteIp::TRUSTED_PROXIES
|
|
100
|
+
Array(proxies).any? { |proxy| proxy.is_a?(IPAddr) ? proxy.include?(ip) : proxy.to_s == address }
|
|
101
|
+
rescue IPAddr::InvalidAddressError
|
|
102
|
+
false
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def login_attempt?(env)
|
|
106
|
+
path = env["PATH_INFO"].to_s
|
|
107
|
+
(env["REQUEST_METHOD"] == "POST" && LOGIN_POST_PATH.match?(path)) || OAUTH_CALLBACK_PATH.match?(path)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# 401: a wrong password or a refused (locked) user, on the web form and on the API. An OAuth
|
|
111
|
+
# callback fails through OmniAuth (omniauth.error) or by sending the user back to the sign-in form.
|
|
112
|
+
def failed?(env, status, headers, oauth_callback)
|
|
113
|
+
return true if status.to_i == 401
|
|
114
|
+
return false unless oauth_callback
|
|
115
|
+
|
|
116
|
+
location = headers["location"] || headers["Location"]
|
|
117
|
+
env["omniauth.error"].present? || env["omniauth.error.type"].present? ||
|
|
118
|
+
(status.to_i.between?(300, 399) && SIGN_IN_REDIRECT_PATH.match?(URI.parse(location.to_s).path.to_s))
|
|
119
|
+
rescue URI::InvalidURIError
|
|
120
|
+
false
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def too_many(request)
|
|
124
|
+
retry_after = (PERIOD - (Time.now.to_i % PERIOD)).to_s
|
|
125
|
+
Rails.logger.warn("[LoginFailureThrottle] #{client_ip(request)} over #{self.class.limit} failed logins a minute: 429 on #{request.path}")
|
|
126
|
+
message = "Too many failed login attempts: retry in #{retry_after} seconds."
|
|
127
|
+
if request.path.start_with?("/api/")
|
|
128
|
+
[429, { "content-type" => "application/json", "retry-after" => retry_after }, [{ error: message }.to_json]]
|
|
129
|
+
else
|
|
130
|
+
[429, { "content-type" => "text/plain; charset=utf-8", "retry-after" => retry_after }, [message]]
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
data/lib/thecore_auth_commons.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: thecore_auth_commons
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
@@ -134,20 +134,14 @@ dependencies:
|
|
|
134
134
|
requirements:
|
|
135
135
|
- - "~>"
|
|
136
136
|
- !ruby/object:Gem::Version
|
|
137
|
-
version: '3.
|
|
138
|
-
- - ">"
|
|
139
|
-
- !ruby/object:Gem::Version
|
|
140
|
-
version: 3.0.5
|
|
137
|
+
version: '3.1'
|
|
141
138
|
type: :runtime
|
|
142
139
|
prerelease: false
|
|
143
140
|
version_requirements: !ruby/object:Gem::Requirement
|
|
144
141
|
requirements:
|
|
145
142
|
- - "~>"
|
|
146
143
|
- !ruby/object:Gem::Version
|
|
147
|
-
version: '3.
|
|
148
|
-
- - ">"
|
|
149
|
-
- !ruby/object:Gem::Version
|
|
150
|
-
version: 3.0.5
|
|
144
|
+
version: '3.1'
|
|
151
145
|
- !ruby/object:Gem::Dependency
|
|
152
146
|
name: simplecov
|
|
153
147
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -306,12 +300,14 @@ files:
|
|
|
306
300
|
- db/migrate/20250516075204_add_auth_source_to_user.rb
|
|
307
301
|
- db/migrate/20251216110301_add_ldap_match_fields_to_ldap_server.rb
|
|
308
302
|
- db/migrate/20251216111217_add_code_to_ldap_server.rb
|
|
303
|
+
- db/migrate/20261125150000_remove_admin_password_index_from_ldap_servers.rb
|
|
309
304
|
- db/seeds.rb
|
|
310
305
|
- lib/tasks/ldap.rake
|
|
311
306
|
- lib/tasks/thecore_auth_commons_tasks.rake
|
|
312
307
|
- lib/thecore/seed.rb
|
|
313
308
|
- lib/thecore_auth_commons.rb
|
|
314
309
|
- lib/thecore_auth_commons/engine.rb
|
|
310
|
+
- lib/thecore_auth_commons/login_failure_throttle.rb
|
|
315
311
|
- lib/thecore_auth_commons/version.rb
|
|
316
312
|
homepage: https://github.com/gabrieletassoni/thecore_auth_commons
|
|
317
313
|
licenses: []
|