Skip to content

Commit 4e5cf35

Browse files
committed
add hex validation for data field
1 parent 1ddf348 commit 4e5cf35

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

tests/unit/core/binarycodec/types/test_blob.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ def test_from_value(self):
1717
def test_raises_invalid_value_type(self):
1818
invalid_value = [1, 2, 3]
1919
self.assertRaises(XRPLBinaryCodecException, Blob.from_value, invalid_value)
20+
21+
def test_raises_invalid_non_hex_input(self):
22+
invalid_value = "Z"
23+
self.assertRaises(ValueError, Blob.from_value, invalid_value)

tests/unit/models/transactions/test_loan_broker_set.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ def test_invalid_data_too_long(self):
1414
LoanBrokerSet(
1515
account=_SOURCE,
1616
vault_id=_VAULT_ID,
17-
data="A" * 257,
17+
data="A" * 257 * 2,
1818
)
1919
self.assertEqual(
2020
error.exception.args[0],
2121
"{'LoanBrokerSet:data': 'Data must be less than 256 bytes.'}",
2222
)
2323

24+
def test_invalid_data_non_hex_string(self):
25+
with self.assertRaises(XRPLModelException) as error:
26+
LoanBrokerSet(
27+
account=_SOURCE,
28+
vault_id=_VAULT_ID,
29+
data="Z",
30+
)
31+
self.assertEqual(
32+
error.exception.args[0],
33+
"{'LoanBrokerSet:data': 'Data must be a valid hex string.'}",
34+
)
35+
2436
def test_invalid_management_fee_rate_too_low(self):
2537
with self.assertRaises(XRPLModelException) as error:
2638
LoanBrokerSet(

tests/unit/models/transactions/test_loan_set.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ def test_invalid_data_too_long(self):
1616
loan_broker_id=_ISSUER,
1717
principal_requested="100000000",
1818
start_date=int(datetime.datetime.now().timestamp()),
19-
data="A" * 257,
19+
data="A" * 257 * 2,
2020
)
2121
self.assertEqual(
2222
error.exception.args[0],
23-
"{'LoanSet:data': 'Data must be less than 512 bytes.'}",
23+
"{'LoanSet:data': 'Data must be less than 256 bytes.'}",
24+
)
25+
26+
def test_invalid_data_non_hex_string(self):
27+
with self.assertRaises(XRPLModelException) as error:
28+
LoanSet(
29+
account=_SOURCE,
30+
loan_broker_id=_ISSUER,
31+
principal_requested="100000000",
32+
start_date=int(datetime.datetime.now().timestamp()),
33+
data="Z",
34+
)
35+
self.assertEqual(
36+
error.exception.args[0],
37+
"{'LoanSet:data': 'Data must be a valid hex string.'}",
2438
)
2539

2640
def test_invalid_overpayment_fee_too_low(self):

xrpl/models/transactions/loan_broker_set.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from typing_extensions import Self
99

10+
from xrpl.constants import HEX_REGEX
1011
from xrpl.models.required import REQUIRED
1112
from xrpl.models.transactions.transaction import Transaction
1213
from xrpl.models.transactions.types import TransactionType
@@ -63,7 +64,7 @@ class LoanBrokerSet(Transaction):
6364
init=False,
6465
)
6566

66-
MAX_DATA_PAYLOAD_LENGTH = 256
67+
MAX_DATA_PAYLOAD_LENGTH = 256 * 2
6768
MAX_MANAGEMENT_FEE_RATE = 10_000
6869
MAX_COVER_RATE_MINIMUM = 100_000
6970
MAX_COVER_RATE_LIQUIDATION = 100_000
@@ -75,6 +76,9 @@ def _get_errors(self: Self) -> Dict[str, str]:
7576
if self.data is not None and len(self.data) > self.MAX_DATA_PAYLOAD_LENGTH:
7677
errors["LoanBrokerSet:data"] = "Data must be less than 256 bytes."
7778

79+
if self.data is not None and not HEX_REGEX.fullmatch(self.data):
80+
errors["LoanBrokerSet:data"] = "Data must be a valid hex string."
81+
7882
if self.management_fee_rate is not None and (
7983
self.management_fee_rate < 0
8084
or self.management_fee_rate > self.MAX_MANAGEMENT_FEE_RATE

xrpl/models/transactions/loan_set.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from typing_extensions import Self
1010

11+
from xrpl.constants import HEX_REGEX
1112
from xrpl.models.base_model import BaseModel
1213
from xrpl.models.required import REQUIRED
1314
from xrpl.models.transactions.transaction import (
@@ -155,7 +156,7 @@ class LoanSet(Transaction):
155156
init=False,
156157
)
157158

158-
MAX_DATA_LENGTH = 256
159+
MAX_DATA_LENGTH = 256 * 2
159160
MAX_OVER_PAYMENT_FEE_RATE = 100_000
160161
MAX_INTEREST_RATE = 100_000
161162
MAX_LATE_INTEREST_RATE = 100_000
@@ -173,7 +174,10 @@ def _get_errors(self: Self) -> Dict[str, str]:
173174
}
174175

175176
if self.data is not None and len(self.data) > self.MAX_DATA_LENGTH:
176-
parent_class_errors["LoanSet:data"] = "Data must be less than 512 bytes."
177+
parent_class_errors["LoanSet:data"] = "Data must be less than 256 bytes."
178+
179+
if self.data is not None and not HEX_REGEX.fullmatch(self.data):
180+
parent_class_errors["LoanSet:data"] = "Data must be a valid hex string."
177181

178182
if self.overpayment_fee is not None and (
179183
self.overpayment_fee < 0

0 commit comments

Comments
 (0)