Repository navigation
Conversation
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.
|
|
Hey @orien, |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
| 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) || |
There was a problem hiding this comment.
Components of standard SDK Keys are unpadded Base64URL-encoded strings, so these regexes can and ideally should be made stricter.
| 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)) |
There was a problem hiding this comment.
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?
| (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." |
There was a problem hiding this comment.
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.
| # 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? |
There was a problem hiding this comment.
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.)



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.
ConfigCatClientExceptionmessage shows the masked key, as the other messages already do.base_url. This covers a key the URL parser rejects and abase_urlwith nohttp(s)scheme.Two changes are visible to callers:
Related issues (only if applicable)
Fixes #49
How to test? (only if applicable)
spec/configcat/configcatclient_spec.rbhas new cases for the masked exception, the invalid request URL message, the masked fetch error, and the stricter key format.Security (only if applicable)
",\or#{and a library that quotes the URL throughinspect. URI errors do not take this path.Requirement checklist (only if applicable)