mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
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:
1 parent
2bfcb845ca
commit
ac4da93aaf
27 files changed
+708
-47
No files matched your search
@@ -16,6 +16,15 @@ module SimpleAircraftManager
|
||||
# Common ones are `templates`, `generators`, or `middleware`, for example.
|
||||
config.autoload_lib(ignore: %w[active_storage assets tasks])
|
||||
|
||||
# File links (rails_blob_path) stop working after this, so a link copied
|
||||
# from a page, or seen before access was removed, doesn't work forever.
|
||||
config.active_storage.urls_expire_in = 1.hour
|
||||
|
||||
config.action_dispatch.default_headers.merge!(
|
||||
"Permissions-Policy" => "camera=(), microphone=(), geolocation=(), payment=(), usb=(), interest-cohort=()",
|
||||
"Cross-Origin-Opener-Policy" => "same-origin"
|
||||
)
|
||||
|
||||
# Configuration for the application, engines, and railties goes here.
|
||||
#
|
||||
# These settings can be overridden in specific environments using the files
|
||||
|
||||
@@ -1,25 +1,33 @@
|
||||
# Be sure to restart your server when you modify this file.
|
||||
# Content Security Policy: scripts only from this site, plus inline scripts
|
||||
# carrying the per-session nonce (the theme snippet, the importmap, Chartkick).
|
||||
# Inline style attributes stay allowed (Chartkick sizes and a few views use them).
|
||||
Rails.application.configure do
|
||||
config.content_security_policy do |policy|
|
||||
policy.default_src :self
|
||||
policy.script_src :self
|
||||
policy.style_src :self, :unsafe_inline
|
||||
policy.img_src :self, :data, :blob
|
||||
policy.font_src :self, :data
|
||||
policy.connect_src :self
|
||||
policy.media_src :self, :blob
|
||||
policy.frame_src :self # PDFs in the document viewer
|
||||
policy.worker_src :self
|
||||
policy.manifest_src :self
|
||||
policy.object_src :none
|
||||
policy.base_uri :self
|
||||
policy.frame_ancestors :self
|
||||
# Signing in with SSO posts here, then redirects to the provider.
|
||||
policy.form_action(*[ :self, (ENV["OIDC_ISSUER"].present? ? ENV["OIDC_ISSUER"][%r{\Ahttps?://[^/]+}i] : nil) ].compact)
|
||||
end
|
||||
|
||||
# Define an application-wide content security policy.
|
||||
# See the Securing Rails Applications Guide for more information:
|
||||
# https://guides.rubyonrails.org/security.html#content-security-policy-header
|
||||
# Stable for a session, so pages Turbo swaps in keep matching the policy
|
||||
# the browser got with the first page load.
|
||||
config.content_security_policy_nonce_generator = ->(request) { request.session[:csp_nonce] ||= SecureRandom.base64(16) }
|
||||
config.content_security_policy_nonce_directives = %w[script-src]
|
||||
end
|
||||
|
||||
# Rails.application.configure do
|
||||
# config.content_security_policy do |policy|
|
||||
# policy.default_src :self, :https
|
||||
# policy.font_src :self, :https, :data
|
||||
# policy.img_src :self, :https, :data
|
||||
# policy.object_src :none
|
||||
# policy.script_src :self, :https
|
||||
# policy.style_src :self, :https
|
||||
# # Specify URI for violation reports
|
||||
# # policy.report_uri "/csp-violation-report-endpoint"
|
||||
# end
|
||||
#
|
||||
# # Generate session nonces for permitted importmap, inline scripts, and inline styles.
|
||||
# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
|
||||
# config.content_security_policy_nonce_directives = %w(script-src style-src)
|
||||
#
|
||||
# # Report violations without enforcing the policy.
|
||||
# # config.content_security_policy_report_only = true
|
||||
# end
|
||||
# Uploaded files are served as they are (PDFs need the browser's viewer);
|
||||
# dangerous types are already sent as downloads, with nosniff.
|
||||
Rails.application.config.to_prepare do
|
||||
ActiveStorage::BaseController.content_security_policy false
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
# OpenID Connect single sign-on; see app/models/sso.rb for the settings.
|
||||
# The provider is registered at boot, so changing OIDC_* needs a restart.
|
||||
Rails.application.config.middleware.use OmniAuth::Builder do
|
||||
if (ENV["OIDC_ISSUER"].present? && ENV["OIDC_CLIENT_ID"].present?) || Rails.env.test?
|
||||
app_url = ENV["APP_URL"].presence&.chomp("/") ||
|
||||
"#{ENV.fetch('FORCE_SSL', 'true') == 'true' ? 'https' : 'http'}://#{ENV.fetch('APP_HOST', 'localhost:3000')}"
|
||||
provider :openid_connect,
|
||||
name: :oidc,
|
||||
issuer: ENV["OIDC_ISSUER"],
|
||||
discovery: true,
|
||||
scope: %i[openid email profile],
|
||||
response_type: :code,
|
||||
pkce: true,
|
||||
client_options: {
|
||||
identifier: ENV["OIDC_CLIENT_ID"],
|
||||
secret: ENV["OIDC_CLIENT_SECRET"],
|
||||
redirect_uri: "#{app_url}/auth/oidc/callback"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
OmniAuth.config.allowed_request_methods = [ :post ] # sign-in starts from a CSRF-protected form
|
||||
# Build callback URLs from the configured address, never the request's Host header.
|
||||
# (Tests run on www.example.com, so they keep the default.)
|
||||
unless Rails.env.test?
|
||||
OmniAuth.config.full_host = ENV["APP_URL"].presence&.chomp("/") ||
|
||||
"#{ENV.fetch('FORCE_SSL', 'true') == 'true' ? 'https' : 'http'}://#{ENV.fetch('APP_HOST', 'localhost:3000')}"
|
||||
end
|
||||
OmniAuth.config.logger = Rails.logger
|
||||
@@ -4,6 +4,11 @@ Rails.application.routes.draw do
|
||||
post "rails/active_storage/direct_uploads", to: "direct_uploads#create"
|
||||
|
||||
resource :session
|
||||
# Single sign-on: POST /auth/oidc (handled by OmniAuth) comes back here.
|
||||
match "auth/:provider/callback", to: "sso#callback", via: %i[get post], as: :sso_callback
|
||||
get "auth/failure", to: "sso#failure"
|
||||
delete "profile/sso", to: "sso#destroy", as: :sso_link
|
||||
delete "profile/sessions", to: "profiles#sign_out_others", as: :other_sessions
|
||||
resources :passwords, param: :token
|
||||
resource :setup, only: %i[new create], controller: "setup"
|
||||
resource :profile, only: %i[edit update]
|
||||
|
||||
Reference in new issue
Block a user