Skip to content

Commit 2f8dd90

Browse files
committed
test(auth): harden test DB guard to cover TEST_DATABASE_URL and pooler URLs
The DatabaseCleaner.allow_remote_database_url flag was set to true to support Docker-network Postgres hostnames in local test runs. To compensate, extend the production URL guard to also inspect TEST_DATABASE_URL (the variable database.yml actually uses in test env) and add 'pooler' as a blocked keyword, which catches database pooler endpoints even if 'supabase' is absent from the URL.
1 parent c3f93fe commit 2f8dd90

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

spec/rails_helper.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@
5252
config.include RequestSpecHelper, type: :request
5353

5454
unless RSWAG_GENERATE
55-
# Database cleaner - SECURITY: Never allow remote database truncation
56-
# This prevents accidentally wiping production data when running tests
57-
if ENV['DATABASE_URL']&.include?('supabase') || ENV['DATABASE_URL']&.include?('prod')
58-
abort('CRITICAL: Cannot run tests against production database! Use a local test database.')
55+
# Abort if any known production URL is set — catches the most common cases.
56+
# TEST_DATABASE_URL is what database.yml actually uses in test env;
57+
# DATABASE_URL is checked as a fallback for misconfigured environments.
58+
[ENV['TEST_DATABASE_URL'], ENV['DATABASE_URL']].compact.each do |url|
59+
if url.include?('supabase') || url.include?('prod') || url.include?('pooler')
60+
abort('CRITICAL: Cannot run tests against production database! Use a local test database.')
61+
end
5962
end
6063

61-
# Custom guard above already aborts on 'supabase'/'prod' URLs, so we can
62-
# allow Docker-network hostnames (e.g. docker-postgres-1) here safely.
64+
# DatabaseCleaner's built-in remote URL safeguard only recognises
65+
# localhost/127.0.0.1 as safe. Docker-network hostnames (e.g.
66+
# docker-postgres-1) are flagged as remote even though they are
67+
# test-only containers. The guard above is the authoritative protection;
68+
# allow_remote_database_url avoids the false-positive block.
6369
DatabaseCleaner.allow_remote_database_url = true
6470

6571
config.before(:suite) do

0 commit comments

Comments
 (0)