Security hardening and OpenID Connect single sign-on

- SSO via OmniAuth OpenID Connect (Authentik, Keycloak, …): link by stable
  subject, by verified email, or from the profile; optional auto-create,
  admin-group sync, and turning password sign-in off
- Current password required to change email or password; password changes
  and resets sign out other sessions; idle sessions expire after 30 days;
  "sign out everywhere else"; session reset on sign-in
- Rate-limit password resets; scope squawk components to the aircraft
- Strict Content Security Policy with nonces (inline handlers removed),
  Permissions-Policy and COOP headers; file links expire after an hour

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-07 08:38:25 -05:00
1 parent 2bfcb845ca
commit ac4da93aaf
27 files changed
+708 -47

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
require "test_helper"
class SecurityHardeningTest < ActionDispatch::IntegrationTest
setup do
@user = create_user("jane@example.com")
end
test "changing email or password needs the current password" do
sign_in @user
patch profile_path, params: { user: { name: "Jane", email_address: "evil@example.com" } }
assert_response :unprocessable_entity
assert_includes response.body, "Enter your current password"
assert_equal "jane@example.com", @user.reload.email_address
patch profile_path, params: { user: { password: "newpassword1", password_confirmation: "newpassword1", current_password: "wrong" } }
assert_response :unprocessable_entity
assert @user.reload.authenticate("password123")
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" } }
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" }
sign_in @user
assert_equal 2, @user.sessions.count
patch profile_path, params: { user: { password: "newpassword1", password_confirmation: "newpassword1", current_password: "password123" } }
assert_redirected_to root_path
assert_equal 1, @user.sessions.count
other.get root_path
other.assert_redirected_to new_session_path
other.post session_path, params: { email_address: @user.email_address, password: "newpassword1" }
get edit_profile_path
assert_select "button", text: "Sign out everywhere else"
delete other_sessions_path
assert_equal "Signed out 1 other session.", flash[:notice]
assert_equal 1, @user.sessions.count
end
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" }
assert_redirected_to new_session_path
assert_equal 0, @user.sessions.count
put password_path(@user.reload.password_reset_token), params: { password: "", password_confirmation: "" }
assert_redirected_to %r{/passwords/.+/edit}
end
test "idle sessions expire" do
sign_in @user
@user.sessions.update_all(updated_at: 31.days.ago)
get root_path
assert_redirected_to new_session_path
assert_equal 0, @user.sessions.count
sign_in @user
@user.sessions.update_all(updated_at: 2.hours.ago)
get root_path
assert_response :success
assert_in_delta Time.current, @user.sessions.sole.updated_at, 5
end
test "a squawk can't point at another aircraft's component" do
a = create_aircraft(owner: @user)
mine = create_component(a)
elsewhere = create_component(create_aircraft)
sign_in @user
post aircraft_squawks_path(a), params: { squawk: { priority: 1, issue_reported: "Leak", component_id: elsewhere.id } }
assert_nil a.squawks.last.component_id
post aircraft_squawks_path(a), params: { squawk: { priority: 1, issue_reported: "Leak", component_id: mine.id } }
assert_equal mine, a.squawks.last.component
end
test "pages send a strict CSP with a nonce on inline scripts" do
sign_in @user
get root_path
csp = response.headers["Content-Security-Policy"]
assert_match "script-src 'self' 'nonce-", csp
assert_match "object-src 'none'", csp
assert_match "frame-ancestors 'self'", csp
nonce = csp[/'nonce-([^']+)'/, 1]
assert_select "script:not([src])" do |scripts|
scripts.each { assert_equal nonce, _1["nonce"], "inline script without the nonce: #{_1.to_html.truncate(80)}" }
end
assert_match "camera=()", response.headers["Permissions-Policy"]
assert_equal "same-origin", response.headers["Cross-Origin-Opener-Policy"]
end
test "no inline event handlers left in views" do
offenders = Dir[Rails.root.join("app/views/**/*.erb")].select { File.read(_1).match?(/\son(click|change|input|submit|load)\s*[=:]/) }
assert_empty offenders.map { _1.delete_prefix("#{Rails.root}/") }
end
test "file links expire" do
a = create_aircraft(owner: @user)
doc = a.documents.create!(name: "W&B", doc_type: "OTHER", pages: [ { io: StringIO.new("%PDF"), filename: "wb.pdf", content_type: "application/pdf" } ])
path = Rails.application.routes.url_helpers.rails_blob_path(doc.pages.first, only_path: true)
get path
assert_response :redirect
travel 2.hours do
get path
assert_response :not_found
end
end
end
+133
View File
@@ -0,0 +1,133 @@
require "test_helper"
class SsoTest < ActionDispatch::IntegrationTest
include ActionMailer::TestHelper
SSO_ENV = { "OIDC_ISSUER" => "https://auth.example.com/application/o/sam/", "OIDC_CLIENT_ID" => "sam", "OIDC_PROVIDER_NAME" => "Authentik" }.freeze
KEYS = SSO_ENV.keys + %w[OIDC_AUTO_CREATE_USERS OIDC_TRUST_EMAIL OIDC_ADMIN_GROUP PASSWORD_LOGIN]
setup do
@saved = KEYS.to_h { [ _1, ENV[_1] ] }
SSO_ENV.each { ENV[_1] = _2 }
OmniAuth.config.test_mode = true
@user = create_user("jane@example.com")
end
teardown do
@saved.each { ENV[_1] = _2 }
OmniAuth.config.mock_auth[:oidc] = nil
OmniAuth.config.test_mode = false
end
def mock(uid: "sub-1", email: "jane@example.com", verified: true, groups: nil, name: "Jane Pilot")
raw = { "email_verified" => verified }
raw["groups"] = groups if groups
OmniAuth.config.mock_auth[:oidc] = OmniAuth::AuthHash.new(provider: "oidc", uid: uid, info: { email: email, name: name }, extra: { raw_info: raw })
end
def sso_sign_in
post "/auth/oidc"
follow_redirect! # to the callback
end
test "sign-in page offers SSO; a verified email links the existing account" do
get new_session_path
assert_select "form[action='/auth/oidc'] button", text: "Sign in with Authentik"
assert_select "input[name=password]" # password sign-in still on
mock
sso_sign_in
assert_redirected_to root_url
assert_equal "sub-1", @user.reload.sso_uid
follow_redirect!
assert_response :success
delete session_path
mock(email: "changed@example.com") # matched by subject id from now on
sso_sign_in
assert_redirected_to root_url
end
test "an unverified email doesn't link (unless trusted) and nobody is created by default" do
mock(verified: false)
sso_sign_in
assert_redirected_to new_session_path
assert_match "There's no account for jane@example.com", flash[:alert]
assert_nil @user.reload.sso_uid
ENV["OIDC_TRUST_EMAIL"] = "true"
sso_sign_in
assert_redirected_to root_url
assert_equal "sub-1", @user.reload.sso_uid
end
test "auto-created users start with no aircraft; admin group is synced only when groups are sent" do
ENV["OIDC_AUTO_CREATE_USERS"] = "true"
ENV["OIDC_ADMIN_GROUP"] = "sam-admins"
mock(uid: "sub-2", email: "new@example.com", groups: [ "sam-admins" ])
assert_difference -> { User.count } do
sso_sign_in
end
created = User.find_by!(email_address: "new@example.com")
assert_equal [ "sub-2", "Jane Pilot", true ], [ created.sso_uid, created.name, created.admin? ]
assert_empty created.aircraft_roles
delete session_path
mock(uid: "sub-2", email: "new@example.com") # no groups claim: leave admin alone
sso_sign_in
assert created.reload.admin?
delete session_path
mock(uid: "sub-2", email: "new@example.com", groups: [ "pilots" ])
sso_sign_in
refute created.reload.admin?
end
test "linking and unlinking from the profile" do
sign_in @user
get edit_profile_path
assert_select "form[action='/auth/oidc'] button", text: "Link Authentik"
mock(uid: "sub-9", email: "someone-else@example.com", verified: false)
sso_sign_in
assert_redirected_to edit_profile_path
assert_equal "sub-9", @user.reload.sso_uid
other = create_user("other@example.com")
sign_in other
sso_sign_in
assert_match "already linked to another user", flash[:alert]
assert_nil other.reload.sso_uid
sign_in @user
delete sso_link_path
assert_nil @user.reload.sso_uid
end
test "password sign-in can be turned off" do
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" }
assert_redirected_to new_session_path
assert_match "Sign in with Authentik", flash[:alert]
post passwords_path, params: { email_address: "jane@example.com" }
assert_redirected_to new_session_path
assert_enqueued_emails 0
sign_in_attempt = -> { mock; sso_sign_in }
sign_in_attempt.()
assert_redirected_to root_url
delete sso_link_path
assert_match "would lock you out", flash[:alert]
end
test "failures and a disabled provider" do
get "/auth/failure", params: { message: "invalid_credentials" }
assert_redirected_to new_session_path
assert_equal "Authentik sign-in failed (invalid credentials).", flash[:alert]
ENV["OIDC_ISSUER"] = nil
get new_session_path
assert_select "form[action='/auth/oidc']", 0
get "/auth/oidc/callback"
assert_response :not_found
end
end