Skip to content

Mask the SDK key in the invalid-key exception and fetch errors - #50

Open
orien wants to merge 1 commit into
configcat:masterfrom
orien:mask-sdk-key-in-errors
Open

orien wants to merge 1 commit into
configcat:masterfrom
orien:mask-sdk-key-in-errors

Conversation

@orien

@orien orien commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Describe the purpose of your pull request

The SDK key can appear unmasked in an exception and in the error logged as event 1103, which error trackers and log pipelines then record. The changes mask it in both places.

  • Invalid-key exception: the ConfigCatClientException message shows the masked key, as the other messages already do.
  • Invalid request URL: when the request URL cannot be used, the fetcher logs a fixed message with the masked key. It no longer logs the URI exception text, which quotes the whole URL, including any credentials in base_url. This covers a key the URL parser rejects and a base_url with no http(s) scheme.
  • Other fetch errors: the fetcher masks the key in the exception text before logging it. Some libraries quote the request URL in their errors, for example WebMock when real connections are disabled.
  • Key format check: a key must be printable ASCII and match the whole string.

Two changes are visible to callers:

  • A key with surrounding whitespace, such as a trailing newline from a file, raises when the client is created. Before, the client was created and each fetch failed.
  • A key with a control character or a non-ASCII character also raises when the client is created.

Related issues (only if applicable)

Fixes #49

How to test? (only if applicable)

  • spec/configcat/configcatclient_spec.rb has new cases for the masked exception, the invalid request URL message, the masked fetch error, and the stricter key format.
  • The full suite passes on Ruby 3.3, and the client and fetcher specs pass on Ruby 3.2. I have not run Ruby 2.5 to 3.1 locally.

Security (only if applicable)

  • The fetch-error masking matches the key as a literal string. It misses a key that an exception quotes in escaped form, which needs a key containing ", \ or #{ and a library that quotes the URL through inspect. URI errors do not take this path.
  • The mask keeps the last six characters, so a value of six characters or fewer still appears in full in the invalid-key exception.
  • An invalid request URL is still treated as a transient failure, so the fetcher retries it once and logs the error twice.

Requirement checklist (only if applicable)

  • I have covered the applied changes with automated tests.
  • I have executed the full automated test set against my changes.
  • I have validated my changes against all supported platform versions.
  • I have read and accepted the contribution agreement.
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.
@orien
orien marked this pull request as ready for review November 24, 2026 03:54
@orien
orien requested a review from a team as a code owner November 24, 2026 03:54
@kp-cat

kp-cat commented Nov 25, 2026

Copy link
Copy Markdown
Member

Hey @orien,
Thanks for the PR! We'll take a look with the dev team and get back to you.

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.91667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.57%. Comparing base (305e080) to head (979de4f).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
lib/configcat/configfetcher.rb 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #50      +/-   ##
==========================================
- Coverage   98.58%   98.57%   -0.02%     
==========================================
  Files          40       40              
  Lines        3466     3511      +45     
==========================================
+ Hits         3417     3461      +44     
- Misses         49       50       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Comment on lines +73 to +74
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) ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components of standard SDK Keys are unpadded Base64URL-encoded strings, so these regexes can and ideally should be made stricter.

Suggested change
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) ||
is_valid_sdk_key = /\A[0-9a-zA-Z_-]{22}\/[0-9a-zA-Z_-]{22}\z/.match?(sdk_key) ||
/\Aconfigcat-sdk-1\/[0-9a-zA-Z_-]{22}\/[0-9a-zA-Z_-]{22}\z/.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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The varying part of this special SDK Key can be an SDK identifier, whose character set is not restricted AFAIK.

Regardless, this regex won't work anyway. There are characters in the \x21-\x7E range that causes problems down the line if not URI encoded. For example: \x23 ('#' - starts a fragment), \x3F ('?' - starts a query string), \x25 ('%' - starts percent-encoding), etc.

So we should URI escape this either here or later when the config JSON URL is composed and parsed.

However, there's a further question: what about slashes (\x2F)? @z4kn4fein What format does the Proxy expect exactly? Should slashes be escaped too?

Suggested change
(options.base_url && /\Aconfigcat-proxy\/[\x21-\x7E]+\z/.match?(sdk_key))
(options.base_url && /^configcat-proxy\/.+$/.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."

@adams85 adams85 Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an early error occurring when the user creates a ConfigCat client using an SDK Key in an invalid format. This is not logged by the SDK, it's the callers responsibility to decide what to do with the resulting exception. And to be able to decide, they should know what the invalid input was.

Therefore, I advise against masking in this case.

Comment on lines +195 to +196
# 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think introducing another error handling branch and doing some hacky manipulation to the error message is the ideal way to handle this.

IMO, the correct way is to make sure that the user-provided parts (base URL and SDK Key) are valid, plus URI encode the latter.

So, just like SDK Key, base URL should be validated at client construction and an early error should be thrown if it's invalid. (It doesn't make any sense to allow creating a client with a wrong base URL as the client can't do anything meaningful in that case.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants