-
Notifications
You must be signed in to change notification settings - Fork 58
Fix crash when HttpResponseDecoder receives non-JSON response #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
lib/http/HttpResponseDecoder.cpp
Outdated
| std::string body(response.GetBody().begin(), response.GetBody().end()); | ||
|
|
||
| // Validate that the body is valid JSON before attempting to parse | ||
| if (body.empty() || !nlohmann::json::accept(body)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wrapper try/catch seems no longer needed with this validation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! With the validation approach using parse() with allow_exceptions=false, the try/catch wrapper is no longer needed. I've removed it in the latest commit.
The function now handles invalid JSON gracefully through return value checking rather than exception handling, making the code cleaner and more efficient.
Changes pushed in commit ab8790c.
Replace double-parsing approach (accept() then parse()) with single parse() call using allow_exceptions=false and is_discarded() check. This eliminates redundant parsing and removes the need for try/catch. Addresses review comments from lalitb and ThomsonTan. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add validation before JSON parsing in processBody to prevent crashes when receiving non-JSON HTTP responses (e.g., HTML from captive portals). Uses nlohmann::json::accept() to validate response body before parsing. Fixes #1338 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Replace double-parsing approach (accept() then parse()) with single parse() call using allow_exceptions=false and is_discarded() check. This eliminates redundant parsing and removes the need for try/catch. Addresses review comments from lalitb and ThomsonTan. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
ab8790c to
ca99f1c
Compare
Pass body iterators directly to nlohmann::json::parse() instead of copying into std::string first. This improves performance by avoiding unnecessary memory allocation and copy. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Remove the leftover scope braces from the try/catch removal and fix indentation for cleaner code structure. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Summary
This PR fixes issue #1338 where the application crashes when
HttpResponseDecoder::processBodyreceives a non-JSON HTTP response (e.g., HTML from captive portals or network sign-in pages).Changes
processBodymethodnlohmann::json::accept()to validate response body before attempting to parseProblem
When a network request returns a non-JSON response (such as HTML from a captive portal), the library attempts to parse it as JSON, causing a crash in the
nlohmann::json::parse()function.Solution
The fix adds a check using
nlohmann::json::accept()which safely validates whether the response body is valid JSON before attempting to parse it. If validation fails, the function logs an error and returns early.Testing
nlohmann::json::accept()method which is designed for safe validationFixes #1338