From 979de4ff5968c054426e9bd79cce8ed7c980a677 Mon Sep 17 00:00:00 2001 From: Orien Madgwick Date: Tue, 24 Nov 2026 13:52:48 +1100 Subject: [PATCH] Mask the SDK key in the invalid-key exception and fetch error The invalid-key exception printed the whole SDK key. A request URL that could not be used, because the key contained a character such as a space or a quote or because base_url had no http(s) scheme, appeared in full in the error logged as event 1103, along with any credentials in base_url. Any other exception that quoted the request URL put the key in the same error. Mask the key in the exception, accept only printable ASCII in the format check, log a fixed message for an invalid request URL in place of the exception text that quotes it, and mask the key in the text of other fetch exceptions. --- lib/configcat/configcatclient.rb | 9 ++-- lib/configcat/configfetcher.rb | 22 ++++++++- spec/configcat/configcatclient_spec.rb | 67 ++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/lib/configcat/configcatclient.rb b/lib/configcat/configcatclient.rb index fa726f6..93c152b 100644 --- a/lib/configcat/configcatclient.rb +++ b/lib/configcat/configcatclient.rb @@ -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 diff --git a/lib/configcat/configfetcher.rb b/lib/configcat/configfetcher.rb index 41dd3cb..674e2c2 100644 --- a/lib/configcat/configfetcher.rb +++ b/lib/configcat/configfetcher.rb @@ -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() @@ -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' diff --git a/spec/configcat/configcatclient_spec.rb b/spec/configcat/configcatclient_spec.rb index 689424a..f20d09c 100644 --- a/spec/configcat/configcatclient_spec.rb +++ b/spec/configcat/configcatclient_spec.rb @@ -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 @@ -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))