MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / validateJsonMessage

Method validateJsonMessage

app/src/API/Server.cpp:840–883  ·  view source on GitHub ↗

* @brief Validates JSON message size, depth, and rate limits. */

Source from the content-addressed store, hash-verified

838 * @brief Validates JSON message size, depth, and rate limits.
839 */
840bool API::Server::validateJsonMessage(QTcpSocket* socket,
841 ConnectionState& state,
842 const QByteArray& jsonBytes)
843{
844 Q_ASSERT(socket);
845 Q_ASSERT(!jsonBytes.isEmpty());
846
847 if (jsonBytes.size() > kMaxApiMessageBytes) {
848 qWarning() << "[API] Message size limit exceeded:" << state.peerAddress << ":" << state.peerPort
849 << "- Message size:" << jsonBytes.size() << "- Limit:" << kMaxApiMessageBytes;
850
851 sendResponseToSocket(
852 socket,
853 CommandResponse::makeError(
854 QString(), ErrorCode::ExecutionError, QStringLiteral("API message exceeds size limit"))
855 .toJsonBytes());
856 return false;
857 }
858
859 if (exceedsJsonDepthLimit(jsonBytes, kMaxApiJsonDepth)) {
860 qWarning() << "[API] JSON depth limit exceeded:" << state.peerAddress << ":" << state.peerPort
861 << "- Max depth:" << kMaxApiJsonDepth;
862
863 sendResponseToSocket(
864 socket,
865 CommandResponse::makeError(
866 QString(), ErrorCode::ExecutionError, QStringLiteral("JSON nesting depth exceeds limit"))
867 .toJsonBytes());
868 return false;
869 }
870
871 if (state.messageCount >= kMaxApiMessagesPerWindow) {
872 qWarning() << "[API] Message rate limit exceeded:" << state.peerAddress << ":" << state.peerPort
873 << "- Messages in window:" << state.messageCount
874 << "- Limit:" << kMaxApiMessagesPerWindow << "- Disconnecting client";
875
876 disconnectClient(
877 socket, state, ErrorCode::ExecutionError, QStringLiteral("API rate limit exceeded"));
878 return false;
879 }
880
881 ++state.messageCount;
882 return true;
883}
884
885/**
886 * @brief Dispatches a validated JSON message to the appropriate handler.

Callers

nothing calls this directly

Calls 4

exceedsJsonDepthLimitFunction · 0.85
isEmptyMethod · 0.80
sizeMethod · 0.45
toJsonBytesMethod · 0.45

Tested by

no test coverage detected