Skip to content

Commit be942de

Browse files
author
Philip Kaufmann
committed
[Qt] add verifySize() function to PaymentServer
- add static verifySize() function to PaymentServer and move the logging on error into the function - also use the new function in the unit test - the function checks if the size is allowed as per BIP70
1 parent c0f66ce commit be942de

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/qt/paymentserver.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,7 @@ bool PaymentServer::readPaymentRequestFromFile(const QString& filename, PaymentR
509509
}
510510

511511
// BIP70 DoS protection
512-
if (f.size() > BIP70_MAX_PAYMENTREQUEST_SIZE) {
513-
qWarning() << QString("PaymentServer::%1: Payment request %2 is too large (%3 bytes, allowed %4 bytes).")
514-
.arg(__func__)
515-
.arg(filename)
516-
.arg(f.size())
517-
.arg(BIP70_MAX_PAYMENTREQUEST_SIZE);
512+
if (!verifySize(f.size())) {
518513
return false;
519514
}
520515

@@ -685,14 +680,13 @@ void PaymentServer::netRequestFinished(QNetworkReply* reply)
685680
reply->deleteLater();
686681

687682
// BIP70 DoS protection
688-
if (reply->size() > BIP70_MAX_PAYMENTREQUEST_SIZE) {
689-
QString msg = tr("Payment request %1 is too large (%2 bytes, allowed %3 bytes).")
690-
.arg(reply->request().url().toString())
691-
.arg(reply->size())
692-
.arg(BIP70_MAX_PAYMENTREQUEST_SIZE);
693-
694-
qWarning() << QString("PaymentServer::%1:").arg(__func__) << msg;
695-
Q_EMIT message(tr("Payment request DoS protection"), msg, CClientUIInterface::MSG_ERROR);
683+
if (!verifySize(reply->size())) {
684+
Q_EMIT message(tr("Payment request rejected"),
685+
tr("Payment request %1 is too large (%2 bytes, allowed %3 bytes).")
686+
.arg(reply->request().url().toString())
687+
.arg(reply->size())
688+
.arg(BIP70_MAX_PAYMENTREQUEST_SIZE),
689+
CClientUIInterface::MSG_ERROR);
696690
return;
697691
}
698692

@@ -790,6 +784,18 @@ bool PaymentServer::verifyExpired(const payments::PaymentDetails& requestDetails
790784
return fVerified;
791785
}
792786

787+
bool PaymentServer::verifySize(qint64 requestSize)
788+
{
789+
bool fVerified = (requestSize <= BIP70_MAX_PAYMENTREQUEST_SIZE);
790+
if (!fVerified) {
791+
qWarning() << QString("PaymentServer::%1: Payment request too large (%2 bytes, allowed %3 bytes).")
792+
.arg(__func__)
793+
.arg(requestSize)
794+
.arg(BIP70_MAX_PAYMENTREQUEST_SIZE);
795+
}
796+
return fVerified;
797+
}
798+
793799
bool PaymentServer::verifyAmount(const CAmount& requestAmount)
794800
{
795801
bool fVerified = MoneyRange(requestAmount);

src/qt/paymentserver.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ class PaymentServer : public QObject
8888
// OptionsModel is used for getting proxy settings and display unit
8989
void setOptionsModel(OptionsModel *optionsModel);
9090

91-
// This is now public, because we use it in paymentservertests.cpp
92-
static bool readPaymentRequestFromFile(const QString& filename, PaymentRequestPlus& request);
93-
9491
// Verify that the payment request network matches the client network
9592
static bool verifyNetwork(const payments::PaymentDetails& requestDetails);
9693
// Verify if the payment request is expired
9794
static bool verifyExpired(const payments::PaymentDetails& requestDetails);
95+
// Verify the payment request size is valid as per BIP70
96+
static bool verifySize(qint64 requestSize);
9897
// Verify the payment request amount is valid
9998
static bool verifyAmount(const CAmount& requestAmount);
10099

@@ -131,6 +130,7 @@ private Q_SLOTS:
131130
bool eventFilter(QObject *object, QEvent *event);
132131

133132
private:
133+
static bool readPaymentRequestFromFile(const QString& filename, PaymentRequestPlus& request);
134134
bool processPaymentRequest(const PaymentRequestPlus& request, SendCoinsRecipient& recipient);
135135
void fetchRequest(const QUrl& url);
136136

src/qt/test/paymentservertests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ void PaymentServerTests::paymentServerTests()
185185
tempFile.open();
186186
tempFile.write((const char*)randData, sizeof(randData));
187187
tempFile.close();
188-
QCOMPARE(PaymentServer::readPaymentRequestFromFile(tempFile.fileName(), r.paymentRequest), false);
188+
// compares 50001 <= BIP70_MAX_PAYMENTREQUEST_SIZE == false
189+
QCOMPARE(PaymentServer::verifySize(tempFile.size()), false);
189190

190191
// Payment request with amount overflow (amount is set to 21000001 BTC):
191192
data = DecodeBase64(paymentrequest5_cert2_BASE64);

0 commit comments

Comments
 (0)