StandardRB fixes
This commit is contained in:
@@ -13,7 +13,7 @@ module Api
|
||||
|
||||
# Authentication Tests
|
||||
test "should redirect to login when no session cookie" do
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 302
|
||||
assert_match %r{/signin}, response.location
|
||||
@@ -23,7 +23,7 @@ module Api
|
||||
test "should redirect when user is inactive" do
|
||||
sign_in_as(@inactive_user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 302
|
||||
assert_equal "User account is not active", response.headers["x-auth-reason"]
|
||||
@@ -32,7 +32,7 @@ module Api
|
||||
test "should return 200 when user is authenticated" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
end
|
||||
@@ -41,7 +41,7 @@ module Api
|
||||
test "should return 200 when matching rule exists" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
end
|
||||
@@ -49,7 +49,7 @@ module Api
|
||||
test "should return 403 when no rule matches (fail-closed security)" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "unknown.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "unknown.example.com"}
|
||||
|
||||
assert_response 403
|
||||
assert_equal "No authentication rule configured for this domain", response.headers["x-auth-reason"]
|
||||
@@ -58,7 +58,7 @@ module Api
|
||||
test "should return 403 when rule exists but is inactive" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "inactive.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "inactive.example.com"}
|
||||
|
||||
assert_response 403
|
||||
assert_equal "No authentication rule configured for this domain", response.headers["x-auth-reason"]
|
||||
@@ -68,7 +68,7 @@ module Api
|
||||
@rule.allowed_groups << @group
|
||||
sign_in_as(@user) # User not in group
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 403
|
||||
assert_match %r{permission to access this domain}, response.headers["x-auth-reason"]
|
||||
@@ -79,35 +79,35 @@ module Api
|
||||
@user.groups << @group
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
end
|
||||
|
||||
# Domain Pattern Tests
|
||||
test "should match wildcard domains correctly" do
|
||||
wildcard_rule = Application.create!(name: "Wildcard App", slug: "wildcard-app", app_type: "forward_auth", domain_pattern: "*.example.com", active: true)
|
||||
Application.create!(name: "Wildcard App", slug: "wildcard-app", app_type: "forward_auth", domain_pattern: "*.example.com", active: true)
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "app.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "app.example.com"}
|
||||
assert_response 200
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "api.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "api.example.com"}
|
||||
assert_response 200
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "other.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "other.com"}
|
||||
assert_response 403 # No rule configured - fail-closed
|
||||
assert_equal "No authentication rule configured for this domain", response.headers["x-auth-reason"]
|
||||
end
|
||||
|
||||
test "should match exact domains correctly" do
|
||||
exact_rule = Application.create!(name: "Exact App", slug: "exact-app", app_type: "forward_auth", domain_pattern: "api.example.com", active: true)
|
||||
Application.create!(name: "Exact App", slug: "exact-app", app_type: "forward_auth", domain_pattern: "api.example.com", active: true)
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "api.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "api.example.com"}
|
||||
assert_response 200
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "app.api.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "app.api.example.com"}
|
||||
assert_response 403 # No rule configured - fail-closed
|
||||
assert_equal "No authentication rule configured for this domain", response.headers["x-auth-reason"]
|
||||
end
|
||||
@@ -116,7 +116,7 @@ module Api
|
||||
test "should return default headers when rule has no custom config" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
assert_equal @user.email_address, response.headers["x-remote-user"]
|
||||
@@ -126,7 +126,7 @@ module Api
|
||||
end
|
||||
|
||||
test "should return custom headers when configured" do
|
||||
custom_rule = Application.create!(
|
||||
Application.create!(
|
||||
name: "Custom App",
|
||||
slug: "custom-app",
|
||||
app_type: "forward_auth",
|
||||
@@ -140,7 +140,7 @@ module Api
|
||||
)
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "custom.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "custom.example.com"}
|
||||
|
||||
assert_response 200
|
||||
assert_equal @user.email_address, response.headers["x-webauth-user"]
|
||||
@@ -151,17 +151,17 @@ module Api
|
||||
end
|
||||
|
||||
test "should return no headers when all headers disabled" do
|
||||
no_headers_rule = Application.create!(
|
||||
Application.create!(
|
||||
name: "No Headers App",
|
||||
slug: "no-headers-app",
|
||||
app_type: "forward_auth",
|
||||
domain_pattern: "noheaders.example.com",
|
||||
active: true,
|
||||
headers_config: { user: "", email: "", name: "", groups: "", admin: "" }
|
||||
headers_config: {user: "", email: "", name: "", groups: "", admin: ""}
|
||||
)
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "noheaders.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "noheaders.example.com"}
|
||||
|
||||
assert_response 200
|
||||
# Check that auth-specific headers are not present (exclude Rails security headers)
|
||||
@@ -173,7 +173,7 @@ module Api
|
||||
@user.groups << @group
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
groups_header = response.headers["x-remote-groups"]
|
||||
@@ -186,7 +186,7 @@ module Api
|
||||
@user.groups.clear # Remove fixture groups
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
assert_nil response.headers["x-remote-groups"]
|
||||
@@ -195,7 +195,7 @@ module Api
|
||||
test "should include admin header correctly" do
|
||||
sign_in_as(@admin_user) # Assuming users(:two) is admin
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
assert_equal "true", response.headers["x-remote-admin"]
|
||||
@@ -207,7 +207,7 @@ module Api
|
||||
@user.groups << group2
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
groups_header = response.headers["x-remote-groups"]
|
||||
@@ -219,7 +219,7 @@ module Api
|
||||
test "should fall back to Host header when X-Forwarded-Host is missing" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"Host" => "test.example.com"}
|
||||
|
||||
assert_response 200
|
||||
end
|
||||
@@ -239,7 +239,7 @@ module Api
|
||||
long_domain = "a" * 250 + ".example.com"
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => long_domain }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => long_domain}
|
||||
|
||||
assert_response 403 # No rule configured - fail-closed
|
||||
assert_equal "No authentication rule configured for this domain", response.headers["x-auth-reason"]
|
||||
@@ -248,7 +248,7 @@ module Api
|
||||
test "should handle case insensitive domain matching" do
|
||||
sign_in_as(@user)
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "TEST.Example.COM" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "TEST.Example.COM"}
|
||||
|
||||
assert_response 200
|
||||
end
|
||||
@@ -262,7 +262,7 @@ module Api
|
||||
get "/api/verify", headers: {
|
||||
"X-Forwarded-Host" => "test.example.com",
|
||||
"X-Forwarded-Uri" => "/admin"
|
||||
}, params: { rd: evil_url }
|
||||
}, params: {rd: evil_url}
|
||||
|
||||
assert_response 302
|
||||
assert_match %r{/signin}, response.location
|
||||
@@ -292,8 +292,8 @@ module Api
|
||||
# This should be allowed (domain has ForwardAuthRule)
|
||||
allowed_url = "https://test.example.com/dashboard"
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" },
|
||||
params: { rd: allowed_url }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"},
|
||||
params: {rd: allowed_url}
|
||||
|
||||
assert_response 302
|
||||
assert_match allowed_url, response.location
|
||||
@@ -305,8 +305,8 @@ module Api
|
||||
# This should be rejected (no ForwardAuthRule for evil-site.com)
|
||||
evil_url = "https://evil-site.com/steal-credentials"
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" },
|
||||
params: { rd: evil_url }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"},
|
||||
params: {rd: evil_url}
|
||||
|
||||
assert_response 302
|
||||
# Should redirect to login page or default URL, NOT to evil_url
|
||||
@@ -320,8 +320,8 @@ module Api
|
||||
# This should be rejected (HTTP not HTTPS)
|
||||
http_url = "http://test.example.com/dashboard"
|
||||
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" },
|
||||
params: { rd: http_url }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"},
|
||||
params: {rd: http_url}
|
||||
|
||||
assert_response 302
|
||||
# Should redirect to login page or default URL, NOT to HTTP URL
|
||||
@@ -340,8 +340,8 @@ module Api
|
||||
]
|
||||
|
||||
dangerous_schemes.each do |dangerous_url|
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" },
|
||||
params: { rd: dangerous_url }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"},
|
||||
params: {rd: dangerous_url}
|
||||
|
||||
assert_response 302, "Should reject dangerous URL: #{dangerous_url}"
|
||||
# Should redirect to login page or default URL, NOT to dangerous URL
|
||||
@@ -355,7 +355,7 @@ module Api
|
||||
sign_in_as(@user)
|
||||
|
||||
# Authenticated GET requests should return 200
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
assert_response 200
|
||||
end
|
||||
|
||||
@@ -461,11 +461,11 @@ module Api
|
||||
sign_in_as(@user)
|
||||
|
||||
# First request
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
assert_response 200
|
||||
|
||||
# Second request with same session
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "test.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "test.example.com"}
|
||||
assert_response 200
|
||||
|
||||
# Should maintain user identity across requests
|
||||
@@ -481,8 +481,8 @@ module Api
|
||||
|
||||
5.times do |i|
|
||||
threads << Thread.new do
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "app#{i}.example.com" }
|
||||
results << { status: response.status }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "app#{i}.example.com"}
|
||||
results << {status: response.status}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -524,7 +524,7 @@ module Api
|
||||
request_count = 10
|
||||
|
||||
request_count.times do |i|
|
||||
get "/api/verify", headers: { "X-Forwarded-Host" => "app#{i}.example.com" }
|
||||
get "/api/verify", headers: {"X-Forwarded-Host" => "app#{i}.example.com"}
|
||||
assert_response 403 # No rules configured for these domains
|
||||
end
|
||||
|
||||
@@ -535,4 +535,4 @@ module Api
|
||||
assert average_time < 0.1, "Average request time too slow: #{average_time}s"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,10 +10,14 @@ class AuthenticationTest < ActiveSupport::TestCase
|
||||
return nil if host.blank? || host.match?(/^(localhost|127\.0\.0\.1|::1)$/)
|
||||
|
||||
# Strip port number for domain parsing
|
||||
host_without_port = host.split(':').first
|
||||
host_without_port = host.split(":").first
|
||||
|
||||
# Check if it's an IP address (IPv4 or IPv6) - if so, don't set domain cookie
|
||||
return nil if IPAddr.new(host_without_port) rescue false
|
||||
begin
|
||||
return nil if IPAddr.new(host_without_port)
|
||||
rescue
|
||||
false
|
||||
end
|
||||
|
||||
# Use Public Suffix List for accurate domain parsing
|
||||
domain = PublicSuffix.parse(host_without_port)
|
||||
@@ -214,4 +218,4 @@ class AuthenticationTest < ActiveSupport::TestCase
|
||||
assert_equal domain, extract_root_domain("api.example.com")
|
||||
assert_equal domain, extract_root_domain("sub.example.com")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -31,7 +31,7 @@ class InputValidationTest < ActionDispatch::IntegrationTest
|
||||
user = User.create!(email_address: "xss_test@example.com", password: "password123", name: xss_payload)
|
||||
|
||||
# Sign in
|
||||
post signin_path, params: { email_address: "xss_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "xss_test@example.com", password: "password123"}
|
||||
assert_response :redirect
|
||||
|
||||
# Get a page that displays user name
|
||||
@@ -59,7 +59,7 @@ class InputValidationTest < ActionDispatch::IntegrationTest
|
||||
)
|
||||
|
||||
# Sign in
|
||||
post signin_path, params: { email_address: "oauth_tamper_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "oauth_tamper_test@example.com", password: "password123"}
|
||||
assert_response :redirect
|
||||
|
||||
# Try to tamper with OAuth authorization parameters
|
||||
@@ -112,7 +112,7 @@ class InputValidationTest < ActionDispatch::IntegrationTest
|
||||
test "JSON input validation prevents malicious payloads" do
|
||||
# Try to send malformed JSON
|
||||
post "/oauth/token", params: '{"grant_type":"authorization_code",}'.to_json,
|
||||
headers: { "CONTENT_TYPE" => "application/json" }
|
||||
headers: {"CONTENT_TYPE" => "application/json"}
|
||||
|
||||
# Should handle malformed JSON gracefully
|
||||
assert_includes [400, 422], response.status
|
||||
@@ -124,9 +124,9 @@ class InputValidationTest < ActionDispatch::IntegrationTest
|
||||
grant_type: "authorization_code",
|
||||
code: "test_code",
|
||||
redirect_uri: "http://localhost:4000/callback",
|
||||
nested: { __proto__: "tampered", constructor: { prototype: "tampered" } }
|
||||
nested: {__proto__: "tampered", constructor: {prototype: "tampered"}}
|
||||
}.to_json,
|
||||
headers: { "CONTENT_TYPE" => "application/json" }
|
||||
headers: {"CONTENT_TYPE" => "application/json"}
|
||||
|
||||
# Should sanitize or reject prototype pollution attempts
|
||||
# The request should be handled (either accept or reject, not crash)
|
||||
@@ -165,7 +165,7 @@ class InputValidationTest < ActionDispatch::IntegrationTest
|
||||
|
||||
malicious_paths.each do |malicious_path|
|
||||
# Try to access files with path traversal
|
||||
get root_path, params: { file: malicious_path }
|
||||
get root_path, params: {file: malicious_path}
|
||||
|
||||
# Should prevent access to files outside public directory
|
||||
assert_response :redirect, "Should reject path traversal attempt"
|
||||
|
||||
@@ -100,7 +100,7 @@ class InvitationsControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "should destroy existing sessions when accepting invitation" do
|
||||
# Create an existing session for the user
|
||||
existing_session = @user.sessions.create!
|
||||
@user.sessions.create!
|
||||
|
||||
put invitation_path(@token), params: {
|
||||
password: "newpassword123",
|
||||
@@ -145,4 +145,4 @@ class InvitationsControllerTest < ActionDispatch::IntegrationTest
|
||||
get invitation_path(@token)
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -35,7 +35,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "prevents authorization code reuse - sequential attempts" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -81,7 +81,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "revokes existing tokens when authorization code is reused" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -135,7 +135,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects already used authorization code" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -171,7 +171,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects expired authorization code" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -206,7 +206,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects authorization code with mismatched redirect_uri" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -256,7 +256,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects authorization code for different application" do
|
||||
# Create consent for the first application
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -308,7 +308,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects invalid client_id in Basic auth" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -341,7 +341,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects invalid client_secret in Basic auth" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -374,7 +374,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "accepts client credentials in POST body" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -408,7 +408,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "rejects request with no client authentication" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -474,7 +474,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "client authentication uses constant-time comparison" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -546,7 +546,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
)
|
||||
|
||||
# Sign in first
|
||||
post signin_path, params: { email_address: "security_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "security_test@example.com", password: "password123"}
|
||||
|
||||
# Test authorization with state parameter
|
||||
get "/oauth/authorize", params: {
|
||||
@@ -573,7 +573,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
)
|
||||
|
||||
# Sign in first
|
||||
post signin_path, params: { email_address: "security_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "security_test@example.com", password: "password123"}
|
||||
|
||||
# Test authorization without state parameter
|
||||
get "/oauth/authorize", params: {
|
||||
@@ -593,7 +593,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "nonce parameter is included in ID token" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -637,7 +637,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "access tokens are not exposed in referer header" do
|
||||
# Create consent and authorization code
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -664,7 +664,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
assert_response :success
|
||||
response_body = JSON.parse(@response.body)
|
||||
access_token = response_body["access_token"]
|
||||
response_body["access_token"]
|
||||
|
||||
# Verify token is not in response headers (especially Referer)
|
||||
assert_nil response.headers["Referer"], "Access token should not leak in Referer header"
|
||||
@@ -677,7 +677,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "PKCE code_verifier is required when code_challenge was provided" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -716,7 +716,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "PKCE with S256 method validates correctly" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -755,7 +755,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "PKCE rejects invalid code_verifier" do
|
||||
# Create consent
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
@@ -798,7 +798,7 @@ class OidcAuthorizationCodeSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "refresh token rotation is enforced" do
|
||||
# Create consent for the refresh token endpoint
|
||||
consent = OidcUserConsent.create!(
|
||||
OidcUserConsent.create!(
|
||||
user: @user,
|
||||
application: @application,
|
||||
scopes_granted: "openid profile",
|
||||
|
||||
@@ -38,7 +38,6 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
test "authorization endpoint accepts PKCE parameters (S256)" do
|
||||
code_verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
||||
code_challenge = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
|
||||
|
||||
auth_params = {
|
||||
@@ -56,7 +55,7 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
# Should show consent page (user is already authenticated)
|
||||
assert_response :success
|
||||
assert_match /consent/, @response.body.downcase
|
||||
assert_match(/consent/, @response.body.downcase)
|
||||
end
|
||||
|
||||
test "authorization endpoint accepts PKCE parameters (plain)" do
|
||||
@@ -77,7 +76,7 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
# Should show consent page (user is already authenticated)
|
||||
assert_response :success
|
||||
assert_match /consent/, @response.body.downcase
|
||||
assert_match(/consent/, @response.body.downcase)
|
||||
end
|
||||
|
||||
test "authorization endpoint rejects invalid code_challenge_method" do
|
||||
@@ -478,7 +477,7 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :bad_request
|
||||
error = JSON.parse(@response.body)
|
||||
assert_equal "invalid_request", error["error"]
|
||||
assert_match /PKCE is required for public clients/, error["error_description"]
|
||||
assert_match(/PKCE is required for public clients/, error["error_description"])
|
||||
|
||||
# Cleanup
|
||||
OidcRefreshToken.where(application: public_app).delete_all
|
||||
@@ -525,7 +524,7 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :bad_request
|
||||
error = JSON.parse(@response.body)
|
||||
assert_equal "invalid_request", error["error"]
|
||||
assert_match /PKCE is required/, error["error_description"]
|
||||
assert_match(/PKCE is required/, error["error_description"])
|
||||
end
|
||||
|
||||
# ====================
|
||||
@@ -697,4 +696,4 @@ class OidcPkceControllerTest < ActionDispatch::IntegrationTest
|
||||
expected_hash = Base64.urlsafe_encode64(Digest::SHA256.digest(access_token)[0..15], padding: false)
|
||||
assert_equal expected_hash, decoded["at_hash"], "at_hash should match SHA-256 hash of access token"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,8 +9,8 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
test "create" do
|
||||
post passwords_path, params: { email_address: @user.email_address }
|
||||
assert_enqueued_email_with PasswordsMailer, :reset, args: [ @user ]
|
||||
post passwords_path, params: {email_address: @user.email_address}
|
||||
assert_enqueued_email_with PasswordsMailer, :reset, args: [@user]
|
||||
assert_redirected_to signin_path
|
||||
|
||||
follow_redirect!
|
||||
@@ -18,7 +18,7 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
test "create for an unknown user redirects but sends no mail" do
|
||||
post passwords_path, params: { email_address: "missing-user@example.com" }
|
||||
post passwords_path, params: {email_address: "missing-user@example.com"}
|
||||
assert_enqueued_emails 0
|
||||
assert_redirected_to signin_path
|
||||
|
||||
@@ -41,7 +41,7 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
test "update" do
|
||||
assert_changes -> { @user.reload.password_digest } do
|
||||
put password_path(@user.generate_token_for(:password_reset)), params: { password: "newpassword", password_confirmation: "newpassword" }
|
||||
put password_path(@user.generate_token_for(:password_reset)), params: {password: "newpassword", password_confirmation: "newpassword"}
|
||||
assert_redirected_to signin_path
|
||||
end
|
||||
|
||||
@@ -52,7 +52,7 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
||||
test "update with non matching passwords" do
|
||||
token = @user.password_reset_token
|
||||
assert_no_changes -> { @user.reload.password_digest } do
|
||||
put password_path(token), params: { password: "no", password_confirmation: "match" }
|
||||
put password_path(token), params: {password: "no", password_confirmation: "match"}
|
||||
assert_redirected_to edit_password_path(token)
|
||||
end
|
||||
|
||||
@@ -61,7 +61,8 @@ class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
private
|
||||
def assert_notice(text)
|
||||
assert_select "div", /#{text}/
|
||||
end
|
||||
|
||||
def assert_notice(text)
|
||||
assert_select "div", /#{text}/
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,14 +9,14 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
test "create with valid credentials" do
|
||||
post session_path, params: { email_address: @user.email_address, password: "password" }
|
||||
post session_path, params: {email_address: @user.email_address, password: "password"}
|
||||
|
||||
assert_redirected_to root_path
|
||||
assert cookies[:session_id]
|
||||
end
|
||||
|
||||
test "create with invalid credentials" do
|
||||
post session_path, params: { email_address: @user.email_address, password: "wrong" }
|
||||
post session_path, params: {email_address: @user.email_address, password: "wrong"}
|
||||
|
||||
assert_redirected_to signin_path
|
||||
assert_nil cookies[:session_id]
|
||||
|
||||
@@ -14,11 +14,11 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
valid_code = totp.now
|
||||
|
||||
# Set up pending TOTP session
|
||||
post signin_path, params: { email_address: "totp_replay_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_replay_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# First use of the code should succeed
|
||||
post totp_verification_path, params: { code: valid_code }
|
||||
post totp_verification_path, params: {code: valid_code}
|
||||
assert_response :redirect
|
||||
assert_redirected_to root_path
|
||||
|
||||
@@ -50,12 +50,12 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
original_codes = user.reload.backup_codes
|
||||
|
||||
# Set up pending TOTP session
|
||||
post signin_path, params: { email_address: "backup_code_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "backup_code_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Use a backup code
|
||||
backup_code = backup_codes.first
|
||||
post totp_verification_path, params: { code: backup_code }
|
||||
post totp_verification_path, params: {code: backup_code}
|
||||
|
||||
# Should successfully sign in
|
||||
assert_response :redirect
|
||||
@@ -70,11 +70,11 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
assert_response :redirect
|
||||
|
||||
# Sign in again
|
||||
post signin_path, params: { email_address: "backup_code_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "backup_code_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Try the same backup code
|
||||
post totp_verification_path, params: { code: backup_code }
|
||||
post totp_verification_path, params: {code: backup_code}
|
||||
|
||||
# Should fail - backup code already used
|
||||
assert_response :redirect
|
||||
@@ -91,13 +91,13 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
# Generate backup codes
|
||||
user.totp_secret = ROTP::Base32.random
|
||||
backup_codes = user.send(:generate_backup_codes) # Call private method
|
||||
user.send(:generate_backup_codes) # Call private method
|
||||
user.save!
|
||||
|
||||
# Check that stored codes are BCrypt hashes (start with $2a$)
|
||||
# backup_codes is already an Array (JSON column), no need to parse
|
||||
user.backup_codes.each do |code|
|
||||
assert_match /^\$2[aby]\$/, code, "Backup codes should be BCrypt hashed"
|
||||
assert_match(/^\$2[aby]\$/, code, "Backup codes should be BCrypt hashed")
|
||||
end
|
||||
|
||||
user.destroy
|
||||
@@ -116,7 +116,7 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
user.save!
|
||||
|
||||
# Set up pending TOTP session
|
||||
post signin_path, params: { email_address: "totp_time_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_time_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Generate a TOTP code for a time far in the future (outside valid window)
|
||||
@@ -124,7 +124,7 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
future_code = totp.at(Time.now.to_i + 300) # 5 minutes in the future
|
||||
|
||||
# Try to use the future code
|
||||
post totp_verification_path, params: { code: future_code }
|
||||
post totp_verification_path, params: {code: future_code}
|
||||
|
||||
# Should fail - code is outside valid time window
|
||||
assert_response :redirect
|
||||
@@ -145,16 +145,16 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
|
||||
# Verify the TOTP secret exists (sanity check)
|
||||
assert user.totp_secret.present?
|
||||
totp_secret = user.totp_secret
|
||||
user.totp_secret
|
||||
|
||||
# Sign in with TOTP
|
||||
post signin_path, params: { email_address: "totp_secret_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_secret_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Complete TOTP verification
|
||||
totp = ROTP::TOTP.new(user.totp_secret)
|
||||
valid_code = totp.now
|
||||
post totp_verification_path, params: { code: valid_code }
|
||||
post totp_verification_path, params: {code: valid_code}
|
||||
assert_response :redirect
|
||||
|
||||
# The TOTP secret should never be exposed in the response body or headers
|
||||
@@ -210,7 +210,7 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
user.update!(totp_required: true, totp_secret: nil)
|
||||
|
||||
# Sign in
|
||||
post signin_path, params: { email_address: "totp_setup_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_setup_test@example.com", password: "password123"}
|
||||
|
||||
# Should redirect to TOTP setup, not verification
|
||||
assert_response :redirect
|
||||
@@ -232,7 +232,7 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
user.save!
|
||||
|
||||
# Set up pending TOTP session
|
||||
post signin_path, params: { email_address: "totp_format_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_format_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Try invalid formats
|
||||
@@ -245,7 +245,7 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
]
|
||||
|
||||
invalid_codes.each do |invalid_code|
|
||||
post totp_verification_path, params: { code: invalid_code }
|
||||
post totp_verification_path, params: {code: invalid_code}
|
||||
assert_response :redirect
|
||||
assert_redirected_to totp_verification_path
|
||||
end
|
||||
@@ -266,11 +266,11 @@ class TotpSecurityTest < ActionDispatch::IntegrationTest
|
||||
user.save!
|
||||
|
||||
# Sign in
|
||||
post signin_path, params: { email_address: "totp_recovery_test@example.com", password: "password123" }
|
||||
post signin_path, params: {email_address: "totp_recovery_test@example.com", password: "password123"}
|
||||
assert_redirected_to totp_verification_path
|
||||
|
||||
# Use backup code instead of TOTP
|
||||
post totp_verification_path, params: { code: backup_codes.first }
|
||||
post totp_verification_path, params: {code: backup_codes.first}
|
||||
|
||||
# Should successfully sign in
|
||||
assert_response :redirect
|
||||
|
||||
Reference in New Issue
Block a user