Class: SkillBench::Clients::BaseUrlValidator
- Inherits:
-
Object
- Object
- SkillBench::Clients::BaseUrlValidator
- Defined in:
- lib/skill_bench/clients/base_url_validator.rb
Overview
Validates a provider base_url before it is used to build an HTTP
connection that may carry an API key / bearer token.
Security rationale: base_url is taken verbatim from config/env input and
the authenticated request attaches a credential to whatever host it names.
Left unchecked this is an SSRF surface, and an http:// URL would transmit
the credential in cleartext. This service enforces:
- the URL must be an absolute
http/httpsURL with a host (empty/relative /garbage values are rejected); - when a credential will be attached, non-loopback hosts MUST use
https; loopback hosts (localhost,127.0.0.1,::1) MAY usehttp— the legitimate self-hosted/Ollama case — and an explicit opt-in (allow_insecure_base_url) can permit cleartext for non-loopback hosts.
A blank (nil/empty) base_url is allowed so providers may supply their
own (https) default downstream. Error messages describe only the transport
and never include the credential.
Defined Under Namespace
Classes: InvalidBaseURLError
Constant Summary collapse
- LOOPBACK_HOSTS =
Hosts permitted to use cleartext
httpeven with a credential attached. %w[localhost 127.0.0.1 ::1].freeze
Class Method Summary collapse
-
.call(base_url:, has_credential: false, allow_insecure: false) ⇒ String?
Validates a base URL and returns it unchanged when valid.
Instance Method Summary collapse
-
#call ⇒ String?
Runs the validation.
-
#initialize(base_url, has_credential, allow_insecure) ⇒ BaseUrlValidator
constructor
A new instance of BaseUrlValidator.
Constructor Details
#initialize(base_url, has_credential, allow_insecure) ⇒ BaseUrlValidator
Returns a new instance of BaseUrlValidator.
50 51 52 53 54 |
# File 'lib/skill_bench/clients/base_url_validator.rb', line 50 def initialize(base_url, has_credential, allow_insecure) @base_url = base_url @has_credential = has_credential @allow_insecure = allow_insecure end |
Class Method Details
.call(base_url:, has_credential: false, allow_insecure: false) ⇒ String?
Validates a base URL and returns it unchanged when valid.
43 44 45 |
# File 'lib/skill_bench/clients/base_url_validator.rb', line 43 def self.call(base_url:, has_credential: false, allow_insecure: false) new(base_url, has_credential, allow_insecure).call end |
Instance Method Details
#call ⇒ String?
Runs the validation.
60 61 62 63 64 65 66 |
# File 'lib/skill_bench/clients/base_url_validator.rb', line 60 def call return @base_url if blank?(@base_url) validate_absolute_http_url! validate_secure_transport! @base_url end |