admin: allow superuser login only on admin.asxp.io
Superuser accounts can do everything, so their login should not be exposed on the public site. With SUPERUSER_HOST set, a superuser's correct password on any other host fails exactly like a wrong one, an existing superuser session there counts as none, a password reset sends nothing, and the env recovery login only works on that host. admin.asxp.io is a second Traefik router to the same container, allowed only from WireGuard and the VPC. The host comes from HTTP_HOST, since Rack's request.host would trust a client-sent X-Forwarded-Host. Invite and reset links for regular users use SITE_URL, so an invite created on admin.asxp.io still opens for someone off WireGuard. Assisted-by: Claude:opus-5-5
This commit is contained in:
parent
26fa3cc98a
commit
19cd0fb675
2 changed files with 119 additions and 6 deletions
|
|
@ -133,6 +133,68 @@ class AdminAuthTest < Minitest::Test
|
|||
assert_equal '/admin', URI(last_response.location).path
|
||||
end
|
||||
|
||||
def with_superuser_host(host = 'admin.example.org')
|
||||
old = ENV['SUPERUSER_HOST']
|
||||
ENV['SUPERUSER_HOST'] = host
|
||||
yield
|
||||
ensure
|
||||
ENV['SUPERUSER_HOST'] = old
|
||||
end
|
||||
|
||||
def login_at(base, username, password, extra = {})
|
||||
get "#{base}/admin/login"
|
||||
token = last_response.body[/name="authenticity_token" value="([^"]+)"/, 1]
|
||||
post "#{base}/admin/login", { authenticity_token: token, username: username, password: password }, extra
|
||||
end
|
||||
|
||||
def test_superuser_login_only_on_superuser_host
|
||||
User.create(username: 'root', superuser: true, password: 'root-password-1')
|
||||
User.create(username: 'staff', password: 'staff-password-1')
|
||||
with_superuser_host do
|
||||
login_at('http://example.org', 'root', 'root-password-1')
|
||||
assert_equal 401, last_response.status
|
||||
assert_includes last_response.body, 'Wrong username or password.'
|
||||
|
||||
# A forged X-Forwarded-Host does not count
|
||||
login_at('http://example.org', 'root', 'root-password-1', 'HTTP_X_FORWARDED_HOST' => 'admin.example.org')
|
||||
assert_equal 401, last_response.status
|
||||
|
||||
login_at('http://example.org', 'staff', 'staff-password-1')
|
||||
assert_equal 302, last_response.status
|
||||
|
||||
login_at('http://admin.example.org', 'root', 'root-password-1')
|
||||
assert_equal 302, last_response.status
|
||||
get 'http://admin.example.org/admin'
|
||||
assert last_response.ok?
|
||||
end
|
||||
end
|
||||
|
||||
def test_superuser_session_ignored_off_host
|
||||
User.create(username: 'root', superuser: true, password: 'root-password-1')
|
||||
login('root', 'root-password-1') # no restriction yet
|
||||
get '/admin'
|
||||
assert last_response.ok?
|
||||
with_superuser_host do
|
||||
get '/admin'
|
||||
assert_equal 302, last_response.status
|
||||
assert_match %r{/admin/login}, last_response.location
|
||||
end
|
||||
end
|
||||
|
||||
def test_emergency_login_only_on_superuser_host
|
||||
old = [ENV['ADMIN_USER'], ENV['ADMIN_PASSWORD']]
|
||||
ENV['ADMIN_USER'], ENV['ADMIN_PASSWORD'] = 'rescue', 'rescue-password-1'
|
||||
with_superuser_host do
|
||||
login_at('http://example.org', 'rescue', 'rescue-password-1')
|
||||
assert_equal 401, last_response.status
|
||||
assert_nil User[username: 'rescue']
|
||||
login_at('http://admin.example.org', 'rescue', 'rescue-password-1')
|
||||
assert_equal 302, last_response.status
|
||||
end
|
||||
ensure
|
||||
ENV['ADMIN_USER'], ENV['ADMIN_PASSWORD'] = old
|
||||
end
|
||||
|
||||
def test_login_rate_limited
|
||||
AdminAuth.login_limit = RateLimit.new(limit: 2, window: 900)
|
||||
3.times { login('x', 'y') }
|
||||
|
|
|
|||
|
|
@ -124,6 +124,24 @@ class UsersAdminTest < Minitest::Test
|
|||
assert last_response.ok?, "#{url.inspect} -> #{last_response.status} #{last_response.body[0, 200]}"
|
||||
end
|
||||
|
||||
def test_superuser_reset_only_on_superuser_host
|
||||
User.create(username: 'root', email: 'root@example.com', superuser: true, password: 'root-password-1')
|
||||
old = ENV['SUPERUSER_HOST']
|
||||
ENV['SUPERUSER_HOST'] = 'admin.example.org'
|
||||
get '/admin/reset'
|
||||
tok = last_response.body[/name="authenticity_token" value="([^"]+)"/, 1]
|
||||
post '/admin/reset', { authenticity_token: tok, login: 'root' }
|
||||
assert last_response.ok?
|
||||
assert_empty Mail::TestMailer.deliveries
|
||||
get 'http://admin.example.org/admin/reset'
|
||||
tok = last_response.body[/name="authenticity_token" value="([^"]+)"/, 1]
|
||||
post 'http://admin.example.org/admin/reset', { authenticity_token: tok, login: 'root' }
|
||||
assert_equal 1, Mail::TestMailer.deliveries.size
|
||||
assert_match %r{http://admin\.example\.org/admin/reset/}, Mail::TestMailer.deliveries.last.body.to_s
|
||||
ensure
|
||||
ENV['SUPERUSER_HOST'] = old
|
||||
end
|
||||
|
||||
def test_no_escalation_by_user_manager
|
||||
boss = User.create(username: 'manager', password: 'manager-pass-1')
|
||||
UserPermission.create(user_id: boss.id, area: 'users', can_read: true, can_write: true)
|
||||
|
|
|
|||
Loading…
Reference in a new issue