Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

security: allow-list Mercury and Sophtron base URLs (SSRF) (#1945)

David Gil755ce258cd5b
diff --git a/app/models/concerns/base_url_allowlistable.rb b/app/models/concerns/base_url_allowlistable.rb
new file mode 100644
index 0000000000..b6952f898e
--- /dev/null
+++ b/app/models/concerns/base_url_allowlistable.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+# An allow-list for a provider's outbound base URL.
+#
+# Several providers let an operator override where the app sends their API
+# requests. Left unchecked that is a server-side request forgery: the value is
+# whatever a user typed, and the request carries their credentials, so pointing
+# it at 169.254.169.254 or an address inside the network makes the server fetch
+# it and hands the token over with it.
+#
+# Provider::Brex already worked this way; this is that logic, shared.
+#
+# class Provider::Foo
+# extend BaseUrlAllowlistable
+#
+# DEFAULT_BASE_URL = "https://api.foo.com/v1"
+# ALLOWED_BASE_URLS = [ DEFAULT_BASE_URL, "https://api-sandbox.foo.com/v1" ].freeze
+# end
+module BaseUrlAllowlistable
+ # Returns the canonical form of an allowed URL, or nil when it is not on the
+ # list. A blank value means "unset", which resolves to the default.
+ def normalize_base_url(value)
+ # A blank value means "the default", and the default goes through the same
+ # check as everything else. Returning it raw would let a provider that
+ # declared a cleartext default hand it straight out.
+ stripped = value.to_s.strip
+ stripped = self::DEFAULT_BASE_URL if stripped.blank?
+
+ canonical = BaseUrlAllowlistable.canonicalize(stripped)
+ return nil if canonical.nil?
+
+ allowed = self::ALLOWED_BASE_URLS.filter_map { |url| BaseUrlAllowlistable.canonicalize(url) }
+ allowed.include?(canonical) ? canonical : nil
+ end
+
+ def allowed_base_url?(value)
+ normalize_base_url(value).present?
+ end
+
+ # Reduces a URL to the parts the allow-list compares, and refuses anything
+ # that cannot be compared safely. HTTPS only, no credentials in the URL, no
+ # query or fragment, and no non-default port, so a value cannot smuggle past
+ # the comparison by carrying something the allow-list does not mention.
+ def self.canonicalize(value)
+ uri = URI.parse(value.to_s.strip)
+ return nil unless uri.is_a?(URI::HTTPS)
+ return nil if uri.host.blank?
+ return nil if uri.userinfo.present? || uri.query.present? || uri.fragment.present?
+ return nil unless uri.port == uri.default_port
+
+ "#{uri.scheme.downcase}://#{uri.host.downcase}#{uri.path.to_s.chomp('/')}"
+ rescue URI::InvalidURIError
+ nil
+ end
+end
diff --git a/app/models/mercury_item.rb b/app/models/mercury_item.rb
index 4bb65214c0..90a4907c5f 100644
--- a/app/models/mercury_item.rb
+++ b/app/models/mercury_item.rb
@@ -12,6 +12,7 @@ class MercuryItem < ApplicationRecord
validates :name, presence: true
validates :token, presence: true, on: :create
+ validate :base_url_must_be_an_official_host, if: :will_save_change_to_base_url?
belongs_to :family
has_one_attached :logo, dependent: :purge_later
@@ -159,7 +160,16 @@ class MercuryItem < ApplicationRecord
token.to_s.strip.present?
end
+ # The value an operator typed decides where the app sends their token, so it
+ # is checked against the provider's allow-list rather than used as given.
def effective_base_url
- base_url.presence || "https://api.mercury.com/api/v1"
+ Provider::Mercury.normalize_base_url(base_url) || Provider::Mercury::DEFAULT_BASE_URL
end
+
+ private
+ def base_url_must_be_an_official_host
+ return if base_url.blank? || Provider::Mercury.allowed_base_url?(base_url)
+
+ errors.add(:base_url, :official_hosts_only)
+ end
end
diff --git a/app/models/provider/brex.rb b/app/models/provider/brex.rb
index 969dfee508..74855567cb 100644
--- a/app/models/provider/brex.rb
+++ b/app/models/provider/brex.rb
@@ -3,6 +3,7 @@
class Provider::Brex
include HTTParty
extend SslConfigurable
+ extend BaseUrlAllowlistable
DEFAULT_BASE_URL = "https://api.brex.com"
STAGING_BASE_URL = "https://api-staging.brex.com"
@@ -12,6 +13,11 @@ class Provider::Brex
MAX_PAGES = 25
headers "User-Agent" => "Sure Finance Brex Client"
+ # The credential travels in a custom header, and HTTParty resends custom
+ # headers on a redirect; its host-change protection covers basic_auth only.
+ # A redirect off the allow-listed host would carry the token with it, so
+ # redirects are not followed at all.
+ no_follow true
default_options.merge!({ timeout: 120 }.merge(httparty_ssl_options))
attr_reader :token, :base_url
@@ -22,28 +28,6 @@ class Provider::Brex
raise ArgumentError, "Brex base URL must be blank or one of: #{ALLOWED_BASE_URLS.join(', ')}" unless @base_url.present?
end
- def self.normalize_base_url(value)
- stripped = value.to_s.strip
- return DEFAULT_BASE_URL if stripped.blank?
-
- uri = URI.parse(stripped)
- return nil unless uri.is_a?(URI::HTTPS)
- return nil if uri.userinfo.present?
- return nil if uri.query.present? || uri.fragment.present?
- return nil unless uri.path.blank? || uri.path == "/"
- return nil unless uri.port == 443
-
- # This exact allowlist is the SSRF boundary; arbitrary Brex-like hosts are never accepted.
- normalized = "#{uri.scheme.downcase}://#{uri.host.to_s.downcase}"
- ALLOWED_BASE_URLS.include?(normalized) ? normalized : nil
- rescue URI::InvalidURIError
- nil
- end
-
- def self.allowed_base_url?(value)
- normalize_base_url(value).present?
- end
-
def get_accounts
cash_accounts = get_cash_accounts
card_accounts = get_card_accounts
diff --git a/app/models/provider/mercury.rb b/app/models/provider/mercury.rb
index 566dc0bd79..1d4a21a456 100644
--- a/app/models/provider/mercury.rb
+++ b/app/models/provider/mercury.rb
@@ -1,15 +1,26 @@
class Provider::Mercury
include HTTParty
extend SslConfigurable
+ extend BaseUrlAllowlistable
+
+ DEFAULT_BASE_URL = "https://api.mercury.com/api/v1"
+ SANDBOX_BASE_URL = "https://api-sandbox.mercury.com/api/v1"
+ ALLOWED_BASE_URLS = [ DEFAULT_BASE_URL, SANDBOX_BASE_URL ].freeze
headers "User-Agent" => "Sure Finance Mercury Client"
+ # The credential travels in a custom header, and HTTParty resends custom
+ # headers on a redirect; its host-change protection covers basic_auth only.
+ # A redirect off the allow-listed host would carry the token with it, so
+ # redirects are not followed at all.
+ no_follow true
default_options.merge!({ timeout: 120 }.merge(httparty_ssl_options))
attr_reader :token, :base_url
- def initialize(token, base_url: "https://api.mercury.com/api/v1")
+ def initialize(token, base_url: DEFAULT_BASE_URL)
@token = token
- @base_url = base_url
+ @base_url = self.class.normalize_base_url(base_url)
+ raise ArgumentError, "Mercury base URL must be blank or one of: #{ALLOWED_BASE_URLS.join(', ')}" if @base_url.blank?
end
# Get all accounts
diff --git a/app/models/provider/sophtron.rb b/app/models/provider/sophtron.rb
index 69261133f6..7cd0f66c7d 100644
--- a/app/models/provider/sophtron.rb
+++ b/app/models/provider/sophtron.rb
@@ -5,12 +5,19 @@
# - V1 RPC-style endpoints for institution connection, jobs, MFA, accounts, and transactions.
class Provider::Sophtron < Provider
include HTTParty
+ extend BaseUrlAllowlistable
DEFAULT_BASE_URL = "https://api.sophtron.com/api"
+ ALLOWED_BASE_URLS = [ DEFAULT_BASE_URL ].freeze
USER_AGENT = "Sure Finance Sophtron Client"
FAILURE_JOB_STATUSES = %w[Completed Timeout Failed Failure Error].freeze
headers "User-Agent" => USER_AGENT
+ # The credential travels in a custom header, and HTTParty resends custom
+ # headers on a redirect; its host-change protection covers basic_auth only.
+ # A redirect off the allow-listed host would carry the token with it, so
+ # redirects are not followed at all.
+ no_follow true
default_options.merge!(verify: true, ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER, timeout: 120)
Error = Class.new(Provider::Error) do
@@ -22,12 +29,27 @@ class Provider::Sophtron < Provider
end
end
+ # Accepts the shapes operators have historically pasted (a bare host, a
+ # trailing slash, the /v2 suffix) and reduces them to the API root before
+ # the allow-list decides. The allow-list is what makes the value safe; this
+ # only stops a well-meaning operator from being refused over a slash.
+ def self.normalize_base_url(value)
+ url = value.to_s.strip.chomp("/")
+ return super if url.blank?
+
+ url = url.delete_suffix("/v2")
+ url = "#{url}/api" unless URI.parse(url).path.to_s.chomp("/").end_with?("/api")
+ super(url)
+ rescue URI::InvalidURIError
+ nil
+ end
+
attr_reader :user_id, :access_key, :base_url
def initialize(user_id, access_key, base_url: DEFAULT_BASE_URL)
@user_id = user_id
@access_key = access_key
- @base_url = normalize_base_url(base_url)
+ @base_url = self.class.normalize_base_url(base_url) || DEFAULT_BASE_URL
super()
end
@@ -380,17 +402,6 @@ class Provider::Sophtron < Provider
body
end
- def normalize_base_url(value)
- url = value.presence || DEFAULT_BASE_URL
- url = url.to_s.chomp("/")
- url = url.delete_suffix("/v2") if url.end_with?("/v2")
-
- parsed = URI.parse(url)
- parsed.path.to_s.end_with?("/api") ? url : "#{url}/api"
- rescue URI::InvalidURIError
- DEFAULT_BASE_URL
- end
-
def normalize_account(account, user_institution_id:)
account = account.with_indifferent_access
account_id = first_present(account, :AccountID, :account_id, :id)
diff --git a/app/models/sophtron_item.rb b/app/models/sophtron_item.rb
index fde7404a63..4d5f7b8798 100644
--- a/app/models/sophtron_item.rb
+++ b/app/models/sophtron_item.rb
@@ -32,6 +32,7 @@ class SophtronItem < ApplicationRecord
validates :name, presence: true
validates :user_id, presence: true, on: :create
validates :access_key, presence: true, on: :create
+ validate :base_url_must_be_an_official_host, if: :will_save_change_to_base_url?
belongs_to :family
belongs_to :current_job_sophtron_account, class_name: "SophtronAccount", optional: true
@@ -363,7 +364,7 @@ class SophtronItem < ApplicationRecord
end
def effective_base_url
- base_url.presence || Provider::Sophtron::DEFAULT_BASE_URL
+ Provider::Sophtron.normalize_base_url(base_url) || Provider::Sophtron::DEFAULT_BASE_URL
end
def generated_customer_unique_id
@@ -399,4 +400,12 @@ class SophtronItem < ApplicationRecord
customer_payload = customer_payload.with_indifferent_access
customer_payload[:CustomerName] || customer_payload[:customer_name] || customer_payload[:name]
end
+
+ # The value an operator typed decides where the app sends their key, so it
+ # is checked against the provider's allow-list rather than used as given.
+ def base_url_must_be_an_official_host
+ return if base_url.blank? || Provider::Sophtron.allowed_base_url?(base_url)
+
+ errors.add(:base_url, :official_hosts_only)
+ end
end
diff --git a/config/locales/models/mercury_item/en.yml b/config/locales/models/mercury_item/en.yml
new file mode 100644
index 0000000000..418e98ccf6
--- /dev/null
+++ b/config/locales/models/mercury_item/en.yml
@@ -0,0 +1,9 @@
+---
+en:
+ activerecord:
+ errors:
+ models:
+ mercury_item:
+ attributes:
+ base_url:
+ official_hosts_only: must be blank, https://api.mercury.com/api/v1, or https://api-sandbox.mercury.com/api/v1
diff --git a/config/locales/models/sophtron_item/en.yml b/config/locales/models/sophtron_item/en.yml
new file mode 100644
index 0000000000..a984abc960
--- /dev/null
+++ b/config/locales/models/sophtron_item/en.yml
@@ -0,0 +1,9 @@
+---
+en:
+ activerecord:
+ errors:
+ models:
+ sophtron_item:
+ attributes:
+ base_url:
+ official_hosts_only: must be blank or https://api.sophtron.com/api
diff --git a/test/models/concerns/base_url_allowlistable_test.rb b/test/models/concerns/base_url_allowlistable_test.rb
new file mode 100644
index 0000000000..5a76be8aa3
--- /dev/null
+++ b/test/models/concerns/base_url_allowlistable_test.rb
@@ -0,0 +1,91 @@
+require "test_helper"
+
+class BaseUrlAllowlistableTest < ActiveSupport::TestCase
+ class Subject
+ extend BaseUrlAllowlistable
+
+ DEFAULT_BASE_URL = "https://api.example.com/v1"
+ ALLOWED_BASE_URLS = [ DEFAULT_BASE_URL, "https://api-sandbox.example.com/v1" ].freeze
+ end
+
+ # A provider that declared a cleartext URL by mistake must not thereby permit
+ # cleartext. The entry is dropped rather than honoured, so nothing matches.
+ class MisconfiguredSubject
+ extend BaseUrlAllowlistable
+
+ DEFAULT_BASE_URL = "http://api.example.com/v1"
+ ALLOWED_BASE_URLS = [ DEFAULT_BASE_URL ].freeze
+ end
+
+ test "an http entry in the allow-list is ignored rather than honoured" do
+ assert_nil MisconfiguredSubject.normalize_base_url("http://api.example.com/v1")
+ assert_nil MisconfiguredSubject.normalize_base_url("https://api.example.com/v1")
+ end
+
+ # Blank resolves to the default, and the default is checked too. A cleartext
+ # default must not come back just because nothing was configured.
+ test "a blank value does not hand out a cleartext default" do
+ assert_nil MisconfiguredSubject.normalize_base_url("")
+ assert_nil MisconfiguredSubject.normalize_base_url(nil)
+ end
+
+ test "a blank value resolves to the default" do
+ [ nil, "", " " ].each do |value|
+ assert_equal Subject::DEFAULT_BASE_URL, Subject.normalize_base_url(value)
+ end
+ end
+
+ test "accepts every URL on the list" do
+ Subject::ALLOWED_BASE_URLS.each do |url|
+ assert_equal url, Subject.normalize_base_url(url)
+ end
+ end
+
+ test "accepts a listed URL written with a trailing slash or odd case" do
+ assert_equal Subject::DEFAULT_BASE_URL, Subject.normalize_base_url("HTTPS://API.Example.com/v1/")
+ end
+
+ # The point of the allow-list: an operator cannot redirect the app's outbound
+ # requests, credentials included, anywhere else.
+ test "refuses anything that is not on the list" do
+ [
+ "https://evil.example.com/v1",
+ "https://api.example.com.evil.test/v1",
+ "https://api.example.com/v2",
+ "http://api.example.com/v1",
+ "https://169.254.169.254/v1",
+ "https://localhost/v1",
+ "https://api.example.com./v1",
+ "https://xn--pi-ula.example.com/v1",
+ "https://äpi.example.com/v1",
+ "https://[::ffff:169.254.169.254]/v1",
+ "https:/v1",
+ "not a url",
+ "//api.example.com/v1"
+ ].each do |value|
+ assert_nil Subject.normalize_base_url(value), "#{value.inspect} must be refused"
+ assert_not Subject.allowed_base_url?(value), "#{value.inspect} must be refused"
+ end
+ end
+
+ # Each of these carries something the allow-list does not mention, so
+ # comparing on host and path alone would let it through.
+ test "refuses credentials, a query, a fragment or a non-default port" do
+ [
+ "https://user:[email protected]/v1",
+ "https://api.example.com/v1?x=1",
+ "https://api.example.com/v1#x",
+ "https://api.example.com:8443/v1"
+ ].each do |value|
+ assert_nil Subject.normalize_base_url(value), "#{value.inspect} must be refused"
+ end
+ end
+
+ # canonicalize also reduces the declared entries, so it must refuse what it
+ # cannot name a host for, and give a declared entry the form inputs get.
+ test "canonicalize refuses a URL without a host and normalises a declared entry" do
+ assert_nil BaseUrlAllowlistable.canonicalize("https:///v1")
+ assert_nil BaseUrlAllowlistable.canonicalize("https:/v1")
+ assert_equal "https://api.example.com/v1", BaseUrlAllowlistable.canonicalize("HTTPS://API.example.com/v1/")
+ end
+end
diff --git a/test/models/mercury_item_test.rb b/test/models/mercury_item_test.rb
index 494e95dac8..5e28415e19 100644
--- a/test/models/mercury_item_test.rb
+++ b/test/models/mercury_item_test.rb
@@ -96,4 +96,43 @@ class MercuryItemTest < ActiveSupport::TestCase
assert_includes encrypted, "raw_payload"
assert_includes encrypted, "raw_institution_payload"
end
+
+ # base_url decides where the app sends this connection's credentials, so a
+ # value pointing anywhere else is refused at save time and ignored at read
+ # time. Both layers matter: rows can be written by console or raw SQL.
+ test "refuses a base_url that is not the provider's own host" do
+ [
+ "https://evil.example.com/api/v1",
+ "http://169.254.169.254/",
+ "https://localhost/api/v1"
+ ].each do |value|
+ @mercury_item.base_url = value
+
+ assert_not @mercury_item.valid?, "#{value} must be refused"
+ assert_includes @mercury_item.errors.attribute_names, :base_url
+ end
+ end
+
+ test "a value that slipped past validation is not used" do
+ @mercury_item.update_column(:base_url, "https://evil.example.com/api/v1")
+
+ assert_equal Provider::Mercury::DEFAULT_BASE_URL, @mercury_item.reload.effective_base_url
+ end
+
+ # Rows saved before the allow-list existed may hold a value it now refuses.
+ # The read-time fallback already keeps them safe, so they must not make every
+ # later save of the row fail (status changes during a sync, a rename).
+ test "a stored value the allow-list refuses does not block unrelated saves" do
+ @mercury_item.update_column(:base_url, "https://evil.example.com/api/v1")
+
+ assert @mercury_item.reload.update(name: "Renamed")
+ assert_equal Provider::Mercury::DEFAULT_BASE_URL, @mercury_item.effective_base_url
+ end
+
+ test "an allowed alternative host is kept" do
+ @mercury_item.base_url = "https://api-sandbox.mercury.com/api/v1"
+
+ assert @mercury_item.valid?
+ assert_equal "https://api-sandbox.mercury.com/api/v1", @mercury_item.effective_base_url
+ end
end
diff --git a/test/models/provider/brex_test.rb b/test/models/provider/brex_test.rb
index f84185bff6..15e08e0679 100644
--- a/test/models/provider/brex_test.rb
+++ b/test/models/provider/brex_test.rb
@@ -49,6 +49,21 @@ class Provider::BrexTest < ActiveSupport::TestCase
end
end
+ # HTTParty resends caller-supplied headers on a redirect; its host-change
+ # protection only covers basic_auth. The credential lives in a custom header
+ # here, so a redirect off the allow-listed host would carry it along.
+ test "does not follow a redirect off the allow-listed host with the credential" do
+ # Pagination appends ?limit=…, so match the path rather than the exact URL:
+ # an unmatched stub would make WebMock raise, Brex would wrap that as a
+ # BrexError, and this test would pass without a redirect ever happening.
+ stub_request(:get, %r{\Ahttps://api\.brex\.com/v2/accounts/cash})
+ .to_return(status: 302, headers: { "Location" => "https://evil.example/collect" })
+ leak = stub_request(:get, "https://evil.example/collect").to_return(status: 200, body: "{}")
+
+ assert_raises(Provider::Brex::BrexError) { Provider::Brex.new("test_token").get_accounts }
+ assert_not_requested leak
+ end
+
test "BrexError includes error_type" do
error = Provider::Brex::BrexError.new("Test error", :unauthorized)
assert_equal "Test error", error.message
diff --git a/test/models/provider/mercury_test.rb b/test/models/provider/mercury_test.rb
index 98306a5a91..90fbb090da 100644
--- a/test/models/provider/mercury_test.rb
+++ b/test/models/provider/mercury_test.rb
@@ -16,6 +16,24 @@ class Provider::MercuryTest < ActiveSupport::TestCase
assert_equal "https://api-sandbox.mercury.com/api/v1", @provider.base_url
end
+ test "refuses a base_url off the allow-list instead of sending the token there" do
+ [ "https://evil.example.com/api/v1", "https://169.254.169.254/api/v1", "http://api.mercury.com/api/v1" ].each do |base_url|
+ assert_raises(ArgumentError, base_url) { Provider::Mercury.new("test_token", base_url: base_url) }
+ end
+ end
+
+ # HTTParty resends caller-supplied headers on a redirect; its host-change
+ # protection only covers basic_auth. The credential lives in a custom header
+ # here, so a redirect off the allow-listed host would carry it along.
+ test "does not follow a redirect off the allow-listed host with the credential" do
+ stub_request(:get, "https://api-sandbox.mercury.com/api/v1/accounts")
+ .to_return(status: 302, headers: { "Location" => "https://evil.example/collect" })
+ leak = stub_request(:get, "https://evil.example/collect").to_return(status: 200, body: "{}")
+
+ assert_raises(Provider::Mercury::MercuryError) { @provider.get_accounts }
+ assert_not_requested leak
+ end
+
test "MercuryError includes error_type" do
error = Provider::Mercury::MercuryError.new("Test error", :unauthorized)
assert_equal "Test error", error.message
diff --git a/test/models/provider/sophtron_test.rb b/test/models/provider/sophtron_test.rb
index dfdeeb806c..6b3bf90ff9 100644
--- a/test/models/provider/sophtron_test.rb
+++ b/test/models/provider/sophtron_test.rb
@@ -20,6 +20,40 @@ class Provider::SophtronTest < ActiveSupport::TestCase
assert_equal "FIApiAUTH:developer-user:#{expected_signature}:#{expected_auth_path}", auth_header
end
+ # HTTParty resends caller-supplied headers on a redirect; its host-change
+ # protection only covers basic_auth. The credential lives in a custom header
+ # here, so a redirect off the allow-listed host would carry it along.
+ test "does not follow a redirect off the allow-listed host with the credential" do
+ stub_request(:get, "https://api.sophtron.com/api/v2/customers")
+ .to_return(status: 302, headers: { "Location" => "https://evil.example/collect" })
+ leak = stub_request(:get, "https://evil.example/collect").to_return(status: 200, body: "[]")
+
+ response = @provider.list_customers
+
+ assert_not response.success?
+ assert_not_requested leak
+ end
+
+ # The shapes the provider tolerates are rebuilt before the allow-list
+ # decides, so the rebuilding must not turn a foreign value into an accepted one.
+ test "tolerates shapes of its own URL but nothing that leaves its host" do
+ [ "https://api.sophtron.com", "https://API.sophtron.com/api/", "https://api.sophtron.com/api/v2" ].each do |value|
+ assert_equal Provider::Sophtron::DEFAULT_BASE_URL, Provider::Sophtron.normalize_base_url(value), value
+ end
+
+ [
+ "https://evil.example.com",
+ "https://api.sophtron.com.evil.test",
+ "https://[email protected]",
+ "https://api.sophtron.com?x=1",
+ "https://api.sophtron.com/other",
+ "http://api.sophtron.com/api"
+ ].each do |value|
+ assert_nil Provider::Sophtron.normalize_base_url(value), "#{value} must be refused"
+ assert_equal Provider::Sophtron::DEFAULT_BASE_URL, Provider::Sophtron.new("u", "a2V5", base_url: value).base_url
+ end
+ end
+
test "lists v2 customers" do
stub_request(:get, "https://api.sophtron.com/api/v2/customers")
.to_return(status: 200, body: [ { CustomerID: "cust-1", CustomerName: "Sure family 1" } ].to_json)
diff --git a/test/models/sophtron_item_test.rb b/test/models/sophtron_item_test.rb
index e42ddb95ca..5434fffa99 100644
--- a/test/models/sophtron_item_test.rb
+++ b/test/models/sophtron_item_test.rb
@@ -227,4 +227,52 @@ class SophtronItemTest < ActiveSupport::TestCase
assert_includes encrypted, "raw_customer_payload"
assert_includes encrypted, "raw_job_payload"
end
+
+ # base_url decides where the app sends this connection's access key, so a
+ # value pointing anywhere else is refused at save time and ignored at read
+ # time. Both layers matter: rows can be written by console or raw SQL.
+ test "refuses a base_url that is not Sophtron's own host" do
+ [
+ "https://evil.example.com/api",
+ "http://169.254.169.254/api",
+ "https://localhost/api"
+ ].each do |value|
+ @item.base_url = value
+
+ assert_not @item.valid?, "#{value} must be refused"
+ assert_includes @item.errors.attribute_names, :base_url
+ end
+ end
+
+ test "a value that slipped past validation is not used" do
+ @item.update_column(:base_url, "https://evil.example.com/api")
+
+ assert_equal Provider::Sophtron::DEFAULT_BASE_URL, @item.reload.effective_base_url
+ end
+
+ # Rows saved before the allow-list existed may hold a value it now refuses.
+ # The read-time fallback already keeps them safe, so they must not make every
+ # later save of the row fail (status changes during a sync, a rename).
+ test "a stored value the allow-list refuses does not block unrelated saves" do
+ @item.update_column(:base_url, "https://evil.example.com/api")
+
+ assert @item.reload.update(name: "Renamed")
+ assert_equal Provider::Sophtron::DEFAULT_BASE_URL, @item.effective_base_url
+ end
+
+ # The provider has always accepted these shapes of its own URL. The form has
+ # to accept the same ones, or an operator is refused a value that works.
+ test "accepts the shapes of Sophtron's URL that the provider accepts" do
+ [
+ "https://api.sophtron.com",
+ "https://api.sophtron.com/",
+ "https://api.sophtron.com/api/",
+ "https://api.sophtron.com/api/v2"
+ ].each do |value|
+ @item.base_url = value
+
+ assert @item.valid?, "#{value} must be accepted"
+ assert_equal Provider::Sophtron::DEFAULT_BASE_URL, @item.effective_base_url
+ end
+ end
end