Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/configcat/configcatclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ def self.close_all

# In case of local only flag overrides mode, we accept any SDK Key format.
if @_override_data_source.nil? || @_override_data_source.get_behaviour() != OverrideBehaviour::LOCAL_ONLY
is_valid_sdk_key = /^.{22}\/.{22}$/.match?(sdk_key) ||
/^configcat-sdk-1\/.{22}\/.{22}$/.match?(sdk_key) ||
(options.base_url && /^configcat-proxy\/.+$/.match?(sdk_key))
# Printable ASCII only: whitespace and control characters cannot form a request URL.
is_valid_sdk_key = /\A[\x21-\x7E]{22}\/[\x21-\x7E]{22}\z/.match?(sdk_key) ||
/\Aconfigcat-sdk-1\/[\x21-\x7E]{22}\/[\x21-\x7E]{22}\z/.match?(sdk_key) ||
(options.base_url && /\Aconfigcat-proxy\/[\x21-\x7E]+\z/.match?(sdk_key))
unless is_valid_sdk_key
raise ConfigCatClientException, "SDK Key `#{sdk_key}` is invalid."
raise ConfigCatClientException, "SDK Key `#{ConfigCatLogger.mask_sdk_key(sdk_key)}` is invalid."
end
end

Expand Down
22 changes: 21 additions & 1 deletion lib/configcat/configfetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def _fetch_http(etag)
begin
@log.debug("Fetching configuration from ConfigCat")
uri = URI.parse((((@_base_url + ("/")) + BASE_PATH) + @_sdk_key) + BASE_EXTENSION)
# A URL with no http(s) scheme or no host can parse, but cannot be requested.
raise URI::InvalidURIError, "not an HTTP URL" unless uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
headers = @_headers
headers["If-None-Match"] = etag.empty? ? nil : etag
_create_http()
Expand Down Expand Up @@ -240,16 +242,34 @@ def _fetch_http(etag)
error = "%s (Ray ID: %s)" % [error, response_cf_ray_id] if response_cf_ray_id
@log.error(1102, error)
return FetchResponse.failure(error, true, response_cf_ray_id)
rescue URI::Error
# The exception message is left out of the error: it quotes the request URL, which contains the SDK Key
# and any credentials in the base URL.
masked_sdk_key = ConfigCatLogger.mask_sdk_key(@_sdk_key)
error = "Unexpected error occurred while trying to fetch config JSON. The request URL is invalid. " \
"Please check the SDK Key ('#{masked_sdk_key}') and the base URL."
@log.error(1103, error)
return FetchResponse.failure(error, true, response_cf_ray_id)
rescue Exception => e
error = "Unexpected error occurred while trying to fetch config JSON. It is most likely due to a local network " \
"issue. Please make sure your application can reach the ConfigCat CDN servers (or your proxy server) " \
"over HTTP. #{e}"
"over HTTP. #{_mask_sdk_key_in(e)}"
error = "%s (Ray ID: %s)" % [error, response_cf_ray_id] if response_cf_ray_id
@log.error(1103, error)
return FetchResponse.failure(error, true, response_cf_ray_id)
end
end

# An exception message can quote the request URL, which contains the SDK Key.
def _mask_sdk_key_in(exception)
message = exception.to_s
return message if @_sdk_key.nil? || @_sdk_key.empty?

message.gsub(@_sdk_key) { ConfigCatLogger.mask_sdk_key(@_sdk_key) }
rescue StandardError
exception.class.to_s
end

def _create_http
uri = URI.parse(@_base_url)
use_ssl = true if uri.scheme == 'https'
Expand Down
67 changes: 67 additions & 0 deletions spec/configcat/configcatclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
["configcat-proxy/", true, false],
["configcat-proxy/sdk-key-90123456789012", false, false],
["configcat-proxy/sdk-key-90123456789012", true, true],
["sdk-key 90123456789012/1234567890123456789012", false, false],
["sdk-key-90123456789012/1234567890123456789012\n", false, false],
["x\nsdk-key-90123456789012/1234567890123456789012", false, false],
["configcat-sdk-1/sdk-key 90123456789012/1234567890123456789012", false, false],
["configcat-proxy/sdk key", true, false],
["configcat-proxy/sdk-key-90123456789012\u00A0", true, false],
["sdk-key-9012345678\e[2J/1234567890123456789012", false, false],
["sdk-key-9012345678901\u00E9/1234567890123456789012", false, false],
].each do |sdk_key, custom_base_url, is_valid|
it "test_sdk_key_format_validation (#{sdk_key}, #{custom_base_url}, #{is_valid})" do
if custom_base_url
Expand All @@ -79,6 +87,65 @@
end
end

it "test_invalid_sdk_key_is_masked_in_exception" do
sdk_key = "configcat-sdk-1/sdk-key-90123456789012/123456789012345678901"
expect {
ConfigCatClient.get(sdk_key)
}.to raise_error(ConfigCatClientException) { |e|
expect(e.message).not_to include(sdk_key)
expect(e.message).to include(ConfigCatLogger.mask_sdk_key(sdk_key))
}
end

[
["configcat-proxy/\"sdk-key-90123456789012\"", "https://my-configcat-proxy"],
["sdk-key-9012345678901\\/1234567890123456789012", nil],
["sdk-key-90123456789012/1234567890123456789012", "https://user:pass@my-configcat-proxy "],
["sdk-key-90123456789012/1234567890123456789012", "my-configcat-proxy"],
["sdk-key-90123456789012/1234567890123456789012", "user:pass@my-configcat-proxy:8050"],
["sdk-key-90123456789012/1234567890123456789012", "mailto:x"],
].each do |sdk_key, base_url|
it "test_invalid_request_url_is_not_logged (#{sdk_key.inspect}, #{base_url.inspect})" do
ConfigCatClient.close_all
errors = []
hooks = Hooks.new(on_error: ->(error) { errors << error })
client = ConfigCatClient.get(sdk_key, ConfigCatOptions.new(base_url: base_url,
polling_mode: PollingMode.manual_poll,
hooks: hooks))
client.force_refresh
client.close

expect(errors).not_to be_empty
errors.each do |error|
expect(error).to include("The request URL is invalid")
expect(error).to include(ConfigCatLogger.mask_sdk_key(sdk_key))
expect(error).not_to include(sdk_key)
expect(error).not_to include(sdk_key.inspect[1..-2])
expect(error).not_to include("user:pass")
end
end
end

it "test_sdk_key_is_masked_in_unexpected_fetch_error" do
sdk_key = "sdk-key-90123456789012/1234567890123456789012"
WebMock.stub_request(:get, Regexp.new('https://.*'))
.to_raise(StandardError.new("Unregistered request: GET https://host/configuration-files/#{sdk_key}/config_v6.json"))

ConfigCatClient.close_all
errors = []
hooks = Hooks.new(on_error: ->(error) { errors << error })
client = ConfigCatClient.get(sdk_key, ConfigCatOptions.new(polling_mode: PollingMode.manual_poll, hooks: hooks))
client.force_refresh
client.close

expect(errors).not_to be_empty
errors.each do |error|
expect(error).to include("Unregistered request")
expect(error).to include(ConfigCatLogger.mask_sdk_key(sdk_key))
expect(error).not_to include(sdk_key)
end
end

it "test_bool" do
client = ConfigCatClient.get(TEST_SDK_KEY, ConfigCatOptions.new(polling_mode: PollingMode.manual_poll,
config_cache: ConfigCacheMock.new))
Expand Down
Loading