Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ QJsonObject CRpcServer::CreateJsonRpcErrorReply ( int code, QString message )
return object;
}

// Maximum size of a single JSON-RPC request line. An unauthenticated client that

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorter comment, please. This declaration only needs a comment saying what it is, not where it's used.

The reasoning is repeated later anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'd like to save tokens, use the Add a suggestion button, which might shortcut to Ctrl+g. The AI will still learn your preference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size should definitely allow about a welcome message/chat message + some slack. That's how I would define the maximum.

// sends data without a terminating newline is only ever consumed on a complete line
// (canReadLine()), so without a bound the received bytes accumulate in the socket read
// buffer without limit until the process is killed by the allocator. Requests larger
// than this, or unterminated data that fills the buffer, are rejected instead of held.
static constexpr int MAX_JSON_RPC_REQUEST_BYTES = 64 * 1024;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This number needs explaining - where does the 64K bound actually come from?

Is there a bound in the Server GUI Welcome Message text box? This shouldn't exceed that, for example.

I don't know if there's a bound on the command line welcome message file size. The command line bounds as plain text would be bounded by the shell -- I've probably cut-n-paste over 64K into bash before now.

So those other sources should be checked, too.


void CRpcServer::OnNewConnection()
{
QTcpSocket* pSocket = pTransportServer->nextPendingConnection();
Expand All @@ -122,6 +129,9 @@ void CRpcServer::OnNewConnection()
vecClients.append ( pSocket );
isAuthenticated[pSocket] = false;

// Bound the per-connection read buffer so unterminated input cannot exhaust memory.
pSocket->setReadBufferSize ( MAX_JSON_RPC_REQUEST_BYTES );

connect ( pSocket, &QTcpSocket::disconnected, [this, pSocket]() {
qDebug() << "- JSON-RPC: connection from:" << pSocket->peerAddress().toString() << "closed";
vecClients.removeAll ( pSocket );
Expand Down Expand Up @@ -197,6 +207,14 @@ void CRpcServer::OnNewConnection()
pSocket->disconnectFromHost();
return;
}

// A full buffer with no complete line is an oversized or unterminated request:
// reject and close rather than hold the bytes indefinitely.
if ( !pSocket->canReadLine() && pSocket->bytesAvailable() >= MAX_JSON_RPC_REQUEST_BYTES )
{
Send ( pSocket, QJsonDocument ( CreateJsonRpcErrorReply ( iErrParseError, "Parse error: Request exceeds maximum size" ) ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maximum size of which length -> return it.

pSocket->disconnectFromHost();
}
} );
}

Expand Down
Loading