Harden installs reachable from the internet without a proxy

- Refuse to start with a placeholder or short SECRET_KEY_BASE (under 32
  characters); warn and fail sam:doctor under 64. .env.example no longer
  ships a placeholder.
- First-run setup needs a one-time code printed only in the server log, so
  only whoever runs the server can create the admin account.
- AirTrail: the server resolves the address and connects to exactly that;
  never the server itself or link-local addresses (cloud metadata);
  private-network addresses only for admins or hosts in
  AIRTRAIL_ALLOWED_HOSTS.
- Host authorization when APP_HOST is set: APP_HOST, the APP_URL host,
  ALLOWED_HOSTS, localhost and IP addresses; /up exempt. A blocked name
  gets a short explanation instead of a blank 403.
- docker-compose.direct.yml: HTTPS without a proxy via Thruster's built-in
  Let's Encrypt (TLS_DOMAIN).
- Notes' Markdown no longer allows the unused class attribute.
- README, .env.example and the user guide updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-14 12:42:53 -05:00
1 parent faae719b12
commit 5876c07a1a
20 files changed
+319 -29

No files matched your search

@@ -0,0 +1,84 @@
require "test_helper"
# Protections for an installation reachable from the internet with nothing in
# front of it: secret key strength, the setup code, the AirTrail address check
# and the allowed host names.
class InternetExposureTest < ActionDispatch::IntegrationTest
test "a placeholder or short secret key is refused; one shorter than recommended is weak" do
assert_equal :unsafe, SecretKeyCheck.strength("change-me")
assert_equal :unsafe, SecretKeyCheck.strength("x")
assert_equal :unsafe, SecretKeyCheck.strength("a" * 80)
assert_equal :weak, SecretKeyCheck.strength(SecureRandom.hex(20))
assert_equal :ok, SecretKeyCheck.strength(SecureRandom.hex(64))
end
test "the setup code is required, shown only in the log, and used up" do
SetupCode.clear
logged = capture_log { get new_setup_path }
assert_match(/Setup code: #{Regexp.escape(SetupCode.current)}/, logged)
assert_no_match SetupCode.current, response.body
end
test "AirTrail can't be pointed at the server itself or link-local addresses, nor the private network by non-admins" do
{ "http://127.0.0.1:8080" => :never, "http://169.254.169.254" => :never, "http://[::1]" => :never,
"http://10.0.0.5" => :private, "http://192.168.1.20:3000" => :private, "http://100.100.1.1" => :private }.each do |url, reason|
error = assert_raises(AirTrailClient::BlockedAddress, url) { AirTrailClient.new(base_url: url, api_key: "k").me }
assert_match(reason == :never ? "link-local" : "private network", error.message)
end
end
test "admins and listed hosts may use private addresses, but never the server itself" do
stub_request(:get, "http://192.168.1.20:3000/api/v1/me").to_return(status: 200, body: { data: { username: "u" } }.to_json)
assert_equal "u", AirTrailClient.new(base_url: "http://192.168.1.20:3000", api_key: "k", allow_private: true).me["username"]
assert_raises(AirTrailClient::BlockedAddress) { AirTrailClient.new(base_url: "http://127.0.0.1", api_key: "k", allow_private: true).me }
with_env("AIRTRAIL_ALLOWED_HOSTS" => "airtrail.lan") do
resolving_to("192.168.1.30") do
stub_request(:get, "http://airtrail.lan/api/v1/me").to_return(status: 200, body: { data: { username: "v" } }.to_json)
assert_equal "v", AirTrailClient.new(base_url: "http://airtrail.lan", api_key: "k").me["username"]
end
end
end
test "a name that resolves to a private address is checked by its address" do
resolving_to("203.0.113.5", "10.1.2.3") do
assert_raises(AirTrailClient::BlockedAddress) { AirTrailClient.new(base_url: "https://rebind.example.com", api_key: "k").me }
end
end
test "a non-admin's connection to a private address is refused with a reason" do
pilot = create_user
sign_in pilot
post air_trail_connection_path, params: { air_trail_connection: { base_url: "http://192.168.1.20", api_key: "k" } }
follow_redirect! if response.redirect?
assert_match "private network", flash[:alert].to_s + response.body
end
private
def resolving_to(*ips)
old = AirTrailClient.resolver
AirTrailClient.resolver = ->(_) { ips }
yield
ensure
AirTrailClient.resolver = old
end
def with_env(vars)
old = vars.keys.to_h { [ _1, ENV[_1] ] }
vars.each { ENV[_1] = _2 }
yield
ensure
old.each { ENV[_1] = _2 }
end
def capture_log
io = StringIO.new
logger = ActiveSupport::Logger.new(io)
old, Rails.logger = Rails.logger, logger
yield
io.string
ensure
Rails.logger = old
end
end
+11 -2
View File
@@ -1,11 +1,20 @@
require "test_helper"
class SetupTest < ActionDispatch::IntegrationTest
test "first run creates admin, then setup is closed" do
test "first run creates admin with the code from the server log, then setup is closed" do
get root_path
assert_redirected_to new_setup_path
post setup_path, params: { user: { name: "Jane", email_address: "a@b.com", password: "test-password-123", password_confirmation: "test-password-123" } }
get new_setup_path
assert_select "input[name=setup_code]"
admin = { name: "Jane", email_address: "a@b.com", password: "test-password-123", password_confirmation: "test-password-123" }
post setup_path, params: { user: admin, setup_code: "AAAA-AAAA" }
assert_response :unprocessable_entity
assert_not User.exists?
post setup_path, params: { user: admin, setup_code: SetupCode.current.downcase.delete("-") } # forgiving about case and the dash
assert_redirected_to root_path
assert_not SetupCode.file.exist?, "the code is used up"
assert User.last.admin?
get new_setup_path
assert_redirected_to root_path