-
Notifications
You must be signed in to change notification settings - Fork 6
fix: escape error JSON responses, check write/OTA return values (#77, #78) #89
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
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.
Incomplete escaping: newlines and control characters are not handled.
Issue
#77explicitly mentions escaping "quotes, backslashes, newlines" but this implementation only handles quotes and backslashes. If an upstream error contains\n,\r,\t, or other control characters, the JSON will still be malformed.🛠️ Proposed fix to escape control characters
void WebServerManager::sendError(int code, const char* msg) { _server.sendHeader("Access-Control-Allow-Origin", "*"); // Escape quotes and backslashes in msg to produce valid JSON - char escaped[128]; + char escaped[256]; size_t j = 0; - for (size_t i = 0; msg[i] && j < sizeof(escaped) - 2; i++) { - if (msg[i] == '"' || msg[i] == '\\') { - escaped[j++] = '\\'; - } - if (j < sizeof(escaped) - 1) { - escaped[j++] = msg[i]; + for (size_t i = 0; msg[i] && j < sizeof(escaped) - 6; i++) { + char c = msg[i]; + if (c == '"' || c == '\\') { + escaped[j++] = '\\'; + escaped[j++] = c; + } else if (c == '\n') { + escaped[j++] = '\\'; + escaped[j++] = 'n'; + } else if (c == '\r') { + escaped[j++] = '\\'; + escaped[j++] = 'r'; + } else if (c == '\t') { + escaped[j++] = '\\'; + escaped[j++] = 't'; + } else if ((unsigned char)c < 0x20) { + // Other control chars: encode as \uXXXX + j += snprintf(escaped + j, sizeof(escaped) - j, "\\u%04X", (unsigned char)c); + } else { + escaped[j++] = c; } } escaped[j] = '\0'; - char body[192]; + char body[320]; snprintf(body, sizeof(body), "{\"success\":false,\"error\":\"%s\"}", escaped); _server.send(code, "application/json", body); }Alternatively, consider using ArduinoJson (already included) to build the error response safely:
This leverages ArduinoJson's built-in escaping and is consistent with other response handlers in this file.
🧰 Tools
🪛 Clang (14.0.6)
[warning] 1527-1527: do not declare C-style arrays, use std::array<> instead
(modernize-avoid-c-arrays)
[warning] 1528-1528: variable 'j' is not initialized
(cppcoreguidelines-init-variables)
[warning] 1528-1528: variable name 'j' is too short, expected at least 3 characters
(readability-identifier-length)
[warning] 1538-1538: do not declare C-style arrays, use std::array<> instead
(modernize-avoid-c-arrays)
[warning] 1539-1539: escaped string literal can be written as a raw string literal
(modernize-raw-string-literal)
🤖 Prompt for AI Agents