Fix false positive error detection when JSON data contains ClickHouse exception text#261
Open
jradtilbrook wants to merge 1 commit into
Open
Conversation
Author
|
Related to #234 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hasErrorClickhouse()can falsely flag valid responses as errors if the exception text appears in the last 4096 bytes of the response body.json_validate()as a confirmation step when the tail-regex matches: if the response is structurally valid JSON, the query succeeded and the match is just data.Problem
The
> 4096 bytebranch inhasErrorClickhouse()checks the last 4096 bytes of a JSON response againstCLICKHOUSE_ERROR_REGEX. This exists to detect mid-stream errors (where ClickHouse appends error text to an already-streaming HTTP 200 response, breaking the JSON structure).However, if the response data legitimately contains ClickHouse exception strings (e.g. an error tracking system storing exceptions in a ClickHouse table), and that text lands in the tail of the response, it triggers a false positive.
Solution
When the regex matches in the tail, call
json_validate($body)(PHP 8.3+) before concluding there's an error:falsetruejson_validate()is O(1) memory (no decoded structure allocated) and only runs in the case where the regex already matched.