mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Sign-in hardening: lockout, stronger passwords, authenticator codes
- 10 failed sign-ins in a row (password, authenticator code, or password at signing) lock an account for 15 minutes from any address; the user is emailed; admins can unlock. Same message as a wrong password. - Passwords: at least 12 characters (max 72), refused if found in Have I Been Pwned (k-anonymity range API; fails open; PWNED_PASSWORDS_CHECK=false) - Optional TOTP (authenticator app) with 10 recovery codes for password and emailed-link sign-in, and at signing (auth method password_totp); codes accepted once; admins can reset; SSO sign-in unaffected - Demo/test passwords updated; guide, README and procedures document Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a6bfe922be
commit
14316bb60f
41 files changed
+600
-45
No files matched your search
@@ -15,7 +15,7 @@ class ElectronicSignaturesTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
def sign(entry = @entry, **params)
|
||||
post aircraft_logbook_entry_signature_path(@a, entry), params: { certificate_id: @ap.id, password: "password123", confirm: "1" }.merge(params)
|
||||
post aircraft_logbook_entry_signature_path(@a, entry), params: { certificate_id: @ap.id, password: "test-password-123", confirm: "1" }.merge(params)
|
||||
end
|
||||
|
||||
test "signing: review, certificate, password, confirmation; then the record shows who certified what and when" do
|
||||
@@ -114,14 +114,14 @@ class ElectronicSignaturesTest < ActionDispatch::IntegrationTest
|
||||
assert_match "preventive maintenance", @entry.certification_statement("PILOT")
|
||||
|
||||
sign_in @owner # signing with someone else's certificate
|
||||
post aircraft_logbook_entry_signature_path(@a, @entry), params: { certificate_id: @ap.id, password: "password123", confirm: "1" }
|
||||
post aircraft_logbook_entry_signature_path(@a, @entry), params: { certificate_id: @ap.id, password: "test-password-123", confirm: "1" }
|
||||
assert_includes response.body, "That certificate isn't yours"
|
||||
get new_aircraft_logbook_entry_signature_path(@a, @entry)
|
||||
assert_select "input[type=radio][value='#{pilot_cert.id}']:not([disabled])"
|
||||
|
||||
@mech.update!(disabled_at: Time.current)
|
||||
assert_raises(EntrySigner::Error) { EntrySigner.new(@entry, user: @mech, certificate: @ap, auth_method: "password").sign! }
|
||||
post session_path, params: { email_address: "mech@example.com", password: "password123" }
|
||||
post session_path, params: { email_address: "mech@example.com", password: "test-password-123" }
|
||||
assert_equal "This account is disabled.", flash[:alert]
|
||||
end
|
||||
|
||||
|
||||
@@ -14,24 +14,24 @@ class SecurityHardeningTest < ActionDispatch::IntegrationTest
|
||||
|
||||
patch profile_path, params: { user: { password: "newpassword1", password_confirmation: "newpassword1", current_password: "wrong" } }
|
||||
assert_response :unprocessable_entity
|
||||
assert @user.reload.authenticate("password123")
|
||||
assert @user.reload.authenticate("test-password-123")
|
||||
|
||||
patch profile_path, params: { user: { name: "Jane Pilot", email_address: "jane@example.com" } } # no sensitive change
|
||||
assert_redirected_to root_path
|
||||
assert_equal "Jane Pilot", @user.reload.name
|
||||
|
||||
patch profile_path, params: { user: { email_address: "jane2@example.com", current_password: "password123" } }
|
||||
patch profile_path, params: { user: { email_address: "jane2@example.com", current_password: "test-password-123" } }
|
||||
assert_redirected_to root_path
|
||||
assert_equal "jane2@example.com", @user.reload.email_address
|
||||
end
|
||||
|
||||
test "a new password signs out other sessions; so does the button" do
|
||||
other = open_session
|
||||
other.post session_path, params: { email_address: @user.email_address, password: "password123" }
|
||||
other.post session_path, params: { email_address: @user.email_address, password: "test-password-123" }
|
||||
sign_in @user
|
||||
assert_equal 2, @user.sessions.count
|
||||
|
||||
patch profile_path, params: { user: { password: "newpassword1", password_confirmation: "newpassword1", current_password: "password123" } }
|
||||
patch profile_path, params: { user: { password: "newpassword1", password_confirmation: "newpassword1", current_password: "test-password-123" } }
|
||||
assert_redirected_to root_path
|
||||
assert_equal 1, @user.sessions.count
|
||||
other.get root_path
|
||||
@@ -48,7 +48,7 @@ class SecurityHardeningTest < ActionDispatch::IntegrationTest
|
||||
test "a password reset signs out every session; a blank password is refused" do
|
||||
sign_in @user
|
||||
token = @user.password_reset_token
|
||||
put password_path(token), params: { password: "brandnew99", password_confirmation: "brandnew99" }
|
||||
put password_path(token), params: { password: "brandnew9999", password_confirmation: "brandnew9999" }
|
||||
assert_redirected_to new_session_path
|
||||
assert_equal 0, @user.sessions.count
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ class SetupTest < ActionDispatch::IntegrationTest
|
||||
test "first run creates admin, then setup is closed" do
|
||||
get root_path
|
||||
assert_redirected_to new_setup_path
|
||||
post setup_path, params: { user: { name: "Ricky", email_address: "a@b.com", password: "password123", password_confirmation: "password123" } }
|
||||
post setup_path, params: { user: { name: "Ricky", email_address: "a@b.com", password: "test-password-123", password_confirmation: "test-password-123" } }
|
||||
assert_redirected_to root_path
|
||||
assert User.last.admin?
|
||||
get new_setup_path
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
require "test_helper"
|
||||
|
||||
# Account lockout, password rules, and authenticator-app two-factor.
|
||||
class SignInHardeningTest < ActionDispatch::IntegrationTest
|
||||
include ActionMailer::TestHelper
|
||||
|
||||
PASSWORD = "test-password-123"
|
||||
|
||||
setup do
|
||||
@user = create_user("jane@example.com")
|
||||
end
|
||||
|
||||
def attempt(password, email: @user.email_address)
|
||||
post session_path, params: { email_address: email, password: password }
|
||||
end
|
||||
|
||||
def totp(user, at: Time.current) = ROTP::TOTP.new(user.reload.otp_secret).at(at)
|
||||
|
||||
def enable_two_factor(user)
|
||||
user.enable_otp!(User.new_otp_secret)
|
||||
end
|
||||
|
||||
test "ten failed sign-ins lock the account for 15 minutes, even with the right password, and email the user" do
|
||||
assert_enqueued_emails(1) { 10.times { attempt("wrong-password") } }
|
||||
assert @user.reload.locked?
|
||||
attempt(PASSWORD)
|
||||
assert_redirected_to new_session_path(email_address: @user.email_address)
|
||||
assert_includes flash[:alert], "locked for 15 minutes"
|
||||
attempt("whatever", email: "nobody@example.com")
|
||||
assert_includes flash[:alert], "Try another email address or password" # same answer for unknown accounts
|
||||
|
||||
travel 16.minutes do
|
||||
attempt(PASSWORD)
|
||||
assert_redirected_to root_url
|
||||
assert_equal 0, @user.reload.failed_sign_ins
|
||||
end
|
||||
end
|
||||
|
||||
test "a successful sign-in resets the count; admins can unlock" do
|
||||
5.times { attempt("wrong-password") }
|
||||
attempt(PASSWORD)
|
||||
assert_equal 0, @user.reload.failed_sign_ins
|
||||
@user.update_columns(locked_until: 10.minutes.from_now)
|
||||
admin = create_user("admin@example.com", admin: true)
|
||||
sign_in admin
|
||||
get admin_users_path
|
||||
assert_select ".badge-red", "Locked"
|
||||
patch unlock_admin_user_path(@user)
|
||||
refute @user.reload.locked?
|
||||
end
|
||||
|
||||
test "passwords need 12 characters and mustn't be known from a breach" do
|
||||
sign_in @user
|
||||
patch profile_path, params: { user: { password: "short-pw1", password_confirmation: "short-pw1", current_password: PASSWORD } }
|
||||
assert_response :unprocessable_entity
|
||||
assert_includes response.body, "too short (minimum is 12 characters)"
|
||||
patch profile_path, params: { user: { password: "breached-password-1", password_confirmation: "breached-password-1", current_password: PASSWORD } }
|
||||
assert_response :unprocessable_entity
|
||||
assert_includes response.body, "data breach"
|
||||
patch profile_path, params: { user: { password: "a-good-new-password", password_confirmation: "a-good-new-password", current_password: PASSWORD } }
|
||||
assert_redirected_to root_path
|
||||
assert @user.reload.authenticate("a-good-new-password")
|
||||
end
|
||||
|
||||
test "the breach check sends only a hash prefix and allows the password if the service is down" do
|
||||
checker = PwnedPasswords.checker
|
||||
PwnedPasswords.checker = ->(pw) { PwnedPasswords.lookup(pw) }
|
||||
sha1 = Digest::SHA1.hexdigest("correct horse battery").upcase
|
||||
stub_request(:get, "https://api.pwnedpasswords.com/range/#{sha1[0, 5]}").with(headers: { "Add-Padding" => "true" })
|
||||
.to_return(body: "0000000000000000000000000000000000A:0\r\n#{sha1[5..]}:42\r\n")
|
||||
assert PwnedPasswords.breached?("correct horse battery")
|
||||
stub_request(:get, %r{api\.pwnedpasswords\.com}).to_timeout
|
||||
refute PwnedPasswords.breached?("another long password")
|
||||
ensure
|
||||
PwnedPasswords.checker = checker
|
||||
end
|
||||
|
||||
test "setting up an authenticator: scan, confirm with a code, get recovery codes" do
|
||||
sign_in @user
|
||||
get new_two_factor_path
|
||||
assert_select "img[src^='data:image/svg+xml;base64,']"
|
||||
secret = response.body[%r{<code class="break-all">([A-Z2-7 ]+)</code>}, 1].delete(" ")
|
||||
post two_factor_path, params: { code: "000000" }
|
||||
assert_redirected_to new_two_factor_path
|
||||
refute @user.reload.otp_enabled?
|
||||
post two_factor_path, params: { code: ROTP::TOTP.new(secret).now }
|
||||
assert_response :success
|
||||
assert_select "ul li", User::RECOVERY_CODES
|
||||
assert @user.reload.otp_enabled?
|
||||
assert_equal secret, @user.otp_secret
|
||||
assert_not_equal secret, @user.read_attribute_before_type_cast(:otp_secret) # encrypted
|
||||
end
|
||||
|
||||
test "with two-factor on, sign-in asks for a code; codes work once; recovery codes work once" do
|
||||
codes = enable_two_factor(@user)
|
||||
attempt(PASSWORD)
|
||||
assert_redirected_to two_factor_session_path
|
||||
get root_path
|
||||
assert_redirected_to new_session_path # not signed in yet
|
||||
|
||||
post two_factor_session_path, params: { code: "123456" }
|
||||
assert_redirected_to two_factor_session_path
|
||||
assert_equal 1, @user.reload.failed_sign_ins
|
||||
code = totp(@user)
|
||||
post two_factor_session_path, params: { code: code }
|
||||
assert_redirected_to root_url
|
||||
get root_path
|
||||
assert_response :success
|
||||
|
||||
delete session_path
|
||||
attempt(PASSWORD)
|
||||
post two_factor_session_path, params: { code: code } # same code again: refused
|
||||
assert_redirected_to two_factor_session_path
|
||||
post two_factor_session_path, params: { code: codes.first.upcase }
|
||||
assert_redirected_to root_url
|
||||
assert_equal User::RECOVERY_CODES - 1, @user.reload.recovery_codes_left
|
||||
|
||||
delete session_path
|
||||
attempt(PASSWORD)
|
||||
post two_factor_session_path, params: { code: codes.first }
|
||||
assert_redirected_to two_factor_session_path
|
||||
end
|
||||
|
||||
test "the code step expires, and an emailed sign-in link also needs the code" do
|
||||
enable_two_factor(@user)
|
||||
attempt(PASSWORD)
|
||||
travel 6.minutes do
|
||||
post two_factor_session_path, params: { code: totp(@user) }
|
||||
assert_redirected_to new_session_path
|
||||
end
|
||||
|
||||
token = @user.generate_token_for(:email_sign_in)
|
||||
post consume_sign_in_link_path(token)
|
||||
assert_redirected_to two_factor_session_path
|
||||
post two_factor_session_path, params: { code: totp(@user) }
|
||||
assert_redirected_to root_url
|
||||
end
|
||||
|
||||
test "turning it off or making new recovery codes needs a code; admins can reset it" do
|
||||
enable_two_factor(@user)
|
||||
attempt(PASSWORD)
|
||||
post two_factor_session_path, params: { code: totp(@user) }
|
||||
delete two_factor_path, params: { code: "000000" }
|
||||
assert @user.reload.otp_enabled?
|
||||
travel 31.seconds do
|
||||
post recovery_codes_two_factor_path, params: { code: totp(@user) }
|
||||
assert_select "ul li", User::RECOVERY_CODES
|
||||
end
|
||||
travel 61.seconds do
|
||||
delete two_factor_path, params: { code: totp(@user) }
|
||||
refute @user.reload.otp_enabled?
|
||||
end
|
||||
|
||||
enable_two_factor(@user)
|
||||
admin = create_user("admin@example.com", admin: true)
|
||||
sign_in admin
|
||||
patch reset_two_factor_admin_user_path(@user)
|
||||
refute @user.reload.otp_enabled?
|
||||
end
|
||||
|
||||
test "signing an entry also needs the authenticator code, and wrong passwords count toward the lockout" do
|
||||
a = create_aircraft(owner: @user)
|
||||
entry = a.logbook_entries.create!(date: Date.current, entry_type: "MAINTENANCE", text: "Changed oil", aircraft_hours_at_entry: 1000)
|
||||
cert = @user.certificates.create!(kind: "AP", number: "7654321")
|
||||
enable_two_factor(@user)
|
||||
attempt(PASSWORD)
|
||||
post two_factor_session_path, params: { code: totp(@user) }
|
||||
|
||||
post aircraft_logbook_entry_signature_path(a, entry), params: { certificate_id: cert.id, password: PASSWORD, confirm: "1" }
|
||||
assert_response :unprocessable_entity
|
||||
assert_includes response.body, "authenticator code"
|
||||
post aircraft_logbook_entry_signature_path(a, entry), params: { certificate_id: cert.id, password: "wrong-password", confirm: "1" }
|
||||
assert_equal 2, @user.reload.failed_sign_ins
|
||||
travel 31.seconds do
|
||||
post aircraft_logbook_entry_signature_path(a, entry), params: { certificate_id: cert.id, password: PASSWORD, otp_code: totp(@user), confirm: "1" }
|
||||
assert_equal "password_totp", entry.reload.signature.auth_method
|
||||
assert_equal 0, @user.reload.failed_sign_ins
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -105,7 +105,7 @@ class SsoTest < ActionDispatch::IntegrationTest
|
||||
ENV["PASSWORD_LOGIN"] = "false"
|
||||
get new_session_path
|
||||
assert_select "input[name=password]", 0
|
||||
post session_path, params: { email_address: "jane@example.com", password: "password123" }
|
||||
post session_path, params: { email_address: "jane@example.com", password: "test-password-123" }
|
||||
assert_redirected_to new_session_path
|
||||
assert_match "Sign in with Authentik", flash[:alert]
|
||||
post passwords_path, params: { email_address: "jane@example.com" }
|
||||
|
||||
Reference in new issue
Block a user