|
8 | 8 | import java.security.SecureRandom; |
9 | 9 | import java.util.ArrayList; |
10 | 10 | import java.util.List; |
| 11 | +import java.util.regex.Pattern; |
11 | 12 | import lombok.extern.slf4j.Slf4j; |
12 | 13 | import org.apache.commons.lang3.StringUtils; |
13 | 14 | import org.bouncycastle.util.encoders.Hex; |
14 | 15 | import org.tron.api.GrpcAPI.AssetIssueList; |
15 | 16 | import org.tron.common.crypto.Hash; |
| 17 | +import org.tron.common.math.StrictMathWrapper; |
16 | 18 | import org.tron.common.parameter.CommonParameter; |
17 | 19 | import org.tron.common.runtime.vm.DataWord; |
18 | 20 | import org.tron.common.utils.ByteArray; |
@@ -60,6 +62,7 @@ public class JsonRpcApiUtil { |
60 | 62 | public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported"; |
61 | 63 | public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported"; |
62 | 64 | public static final String BLOCK_NUM_ERROR = "invalid block number"; |
| 65 | + public static final String TX_INDEX_ERROR = "invalid index value"; |
63 | 66 |
|
64 | 67 | private static final SecureRandom random = new SecureRandom(); |
65 | 68 |
|
@@ -395,46 +398,90 @@ public static long getUnfreezeAssetAmount(byte[] addressBytes, Wallet wallet) { |
395 | 398 | */ |
396 | 399 | public static byte[] addressCompatibleToByteArray(String hexAddress) |
397 | 400 | throws JsonRpcInvalidParamsException { |
| 401 | + // ADDRESS_SIZE (42) is the hex length of a 21-byte address; +2 leaves room for the optional |
| 402 | + // "0x" prefix, so a 0x-prefixed 21-byte address (0x41..., 44 chars) is still accepted. |
| 403 | + if (hexAddress == null || hexAddress.length() > DecodeUtil.ADDRESS_SIZE + 2) { |
| 404 | + throw new JsonRpcInvalidParamsException("invalid address"); |
| 405 | + } |
398 | 406 | byte[] addressByte; |
399 | 407 | try { |
400 | 408 | addressByte = ByteArray.fromHexString(hexAddress); |
401 | 409 | if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 |
402 | 410 | && addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { |
403 | | - throw new JsonRpcInvalidParamsException("invalid address hash value"); |
| 411 | + throw new JsonRpcInvalidParamsException("invalid address"); |
404 | 412 | } |
405 | 413 |
|
406 | 414 | if (addressByte.length == DecodeUtil.ADDRESS_SIZE / 2 - 1) { |
407 | 415 | addressByte = ByteUtil.merge(new byte[] {DecodeUtil.addressPreFixByte}, addressByte); |
408 | 416 | } else if (addressByte[0] != ByteArray.fromHexString(DecodeUtil.addressPreFixString)[0]) { |
409 | 417 | // addressByte.length == DecodeUtil.ADDRESS_SIZE / 2 |
410 | | - throw new JsonRpcInvalidParamsException("invalid address hash value"); |
| 418 | + throw new JsonRpcInvalidParamsException("invalid address"); |
411 | 419 | } |
412 | 420 | } catch (Exception e) { |
413 | 421 | throw new JsonRpcInvalidParamsException(e.getMessage()); |
414 | 422 | } |
415 | 423 | return addressByte; |
416 | 424 | } |
417 | 425 |
|
| 426 | + /** Matches a 32-byte hash hex string: optional 0x prefix + 64 hex chars (also caps length). */ |
| 427 | + public static final String HASH_REGEX = "^(0x)?[0-9a-fA-F]{64}$"; |
| 428 | + |
418 | 429 | /** |
419 | | - * convert 40 hex string of address to byte array, padding 0 ahead if length is odd. |
| 430 | + * Convert a hash hex string (optional 0x prefix) to a byte array, validating |
| 431 | + * format and length via {@link #HASH_REGEX} first. |
420 | 432 | */ |
421 | | - public static byte[] addressToByteArray(String hexAddress) throws JsonRpcInvalidParamsException { |
422 | | - byte[] addressByte = ByteArray.fromHexString(hexAddress); |
423 | | - if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { |
424 | | - throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); |
| 433 | + public static byte[] hashToByteArray(String hash) throws JsonRpcInvalidParamsException { |
| 434 | + if (hash == null || !Pattern.matches(HASH_REGEX, hash)) { |
| 435 | + throw new JsonRpcInvalidParamsException("invalid hash value"); |
425 | 436 | } |
426 | | - return new DataWord(addressByte).getLast20Bytes(); |
| 437 | + byte[] bHash; |
| 438 | + try { |
| 439 | + bHash = ByteArray.fromHexString(hash); |
| 440 | + } catch (Exception e) { |
| 441 | + throw new JsonRpcInvalidParamsException(e.getMessage()); |
| 442 | + } |
| 443 | + return bHash; |
427 | 444 | } |
428 | 445 |
|
429 | 446 | /** |
430 | | - * check if topic is hex string of size 64, padding 0 ahead if length is odd. |
| 447 | + * Matches a 32-byte topic hex string: optional 0x prefix + 63 or 64 hex chars. |
| 448 | + */ |
| 449 | + public static final String TOPIC_REGEX = "^(0x)?[0-9a-fA-F]{63,64}$"; |
| 450 | + |
| 451 | + /** |
| 452 | + * Convert a topic hex string (optional 0x prefix, leading zero may be omitted) to a 32-byte |
| 453 | + * array, validating format and length via {@link #TOPIC_REGEX} first. |
431 | 454 | */ |
432 | 455 | public static byte[] topicToByteArray(String hexTopic) throws JsonRpcInvalidParamsException { |
433 | | - byte[] topicByte = ByteArray.fromHexString(hexTopic); |
434 | | - if (topicByte.length != 32) { |
| 456 | + if (hexTopic == null || !Pattern.matches(TOPIC_REGEX, hexTopic)) { |
| 457 | + throw new JsonRpcInvalidParamsException("invalid topic: " + hexTopic); |
| 458 | + } |
| 459 | + try { |
| 460 | + return ByteArray.fromHexString(hexTopic); |
| 461 | + } catch (Exception e) { |
435 | 462 | throw new JsonRpcInvalidParamsException("invalid topic: " + hexTopic); |
436 | 463 | } |
437 | | - return topicByte; |
| 464 | + } |
| 465 | + |
| 466 | + /** |
| 467 | + * convert 40 hex string of address to byte array, padding 0 ahead if length is odd. |
| 468 | + */ |
| 469 | + public static byte[] addressToByteArray(String hexAddress) throws JsonRpcInvalidParamsException { |
| 470 | + if (hexAddress == null) { |
| 471 | + throw new JsonRpcInvalidParamsException("address is null"); |
| 472 | + } else if (hexAddress.length() > DecodeUtil.ADDRESS_SIZE) { |
| 473 | + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); |
| 474 | + } |
| 475 | + byte[] addressByte; |
| 476 | + try { |
| 477 | + addressByte = ByteArray.fromHexString(hexAddress); |
| 478 | + } catch (Exception e) { |
| 479 | + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); |
| 480 | + } |
| 481 | + if (addressByte.length != DecodeUtil.ADDRESS_SIZE / 2 - 1) { |
| 482 | + throw new JsonRpcInvalidParamsException("invalid address: " + hexAddress); |
| 483 | + } |
| 484 | + return new DataWord(addressByte).getLast20Bytes(); |
438 | 485 | } |
439 | 486 |
|
440 | 487 | public static boolean paramStringIsNull(String string) { |
@@ -499,7 +546,10 @@ public static long parseQuantityValue(String value) throws JsonRpcInvalidParamsE |
499 | 546 | throw new JsonRpcInvalidParamsException("invalid param value: invalid hex number"); |
500 | 547 | } |
501 | 548 | } |
502 | | - |
| 549 | + // QUANTITY is unsigned; reject a signed ("0x-..") value instead of returning a negative. |
| 550 | + if (callValue < 0) { |
| 551 | + throw new JsonRpcInvalidParamsException("invalid param value: negative"); |
| 552 | + } |
503 | 553 | return callValue; |
504 | 554 | } |
505 | 555 |
|
@@ -603,10 +653,11 @@ public static long parseBlockTag(String tag, Wallet wallet) |
603 | 653 | /** |
604 | 654 | * Max allowed length for a JSON-RPC block number hex/decimal input. |
605 | 655 | * API-level DoS guard: rejects pathological inputs before BigInteger parsing, |
606 | | - * whose cost grows quadratically with length. Covers hex (0x + 64 chars for |
607 | | - * uint256) and decimal (78 chars for uint256) representations with headroom. |
| 656 | + * whose cost grows quadratically with length. A block number fits a signed long, |
| 657 | + * so the longest valid input is 19 chars (decimal Long.MAX_VALUE) or 18 (0x + 16 |
| 658 | + * hex); 20 leaves a small margin. |
608 | 659 | */ |
609 | | - private static final int MAX_BLOCK_NUM_HEX_LEN = 100; |
| 660 | + private static final int MAX_BLOCK_NUM_HEX_LEN = 20; |
610 | 661 |
|
611 | 662 | /** |
612 | 663 | * Parse a JSON-RPC block number (hex "0x..." or decimal) into a long, |
@@ -636,16 +687,50 @@ public static long parseBlockNumber(String blockNum) |
636 | 687 | } |
637 | 688 |
|
638 | 689 | /** |
639 | | - * Parse a block tag or hex number. Uses strict jsonHexToLong (requires 0x prefix) for hex. |
640 | | - * Callers needing flexible hex parsing (0x -> hex, bare number -> decimal) should use |
641 | | - * isBlockTag/parseBlockTag and handle hex separately with hexToBigInteger. |
| 690 | + * Parse a block tag, or a 0x-prefixed hex block number. |
642 | 691 | */ |
643 | 692 | public static long parseBlockNumber(String blockNumOrTag, Wallet wallet) |
644 | 693 | throws JsonRpcInvalidParamsException { |
645 | 694 | if (isBlockTag(blockNumOrTag)) { |
646 | 695 | return parseBlockTag(blockNumOrTag, wallet); |
647 | 696 | } |
648 | | - return ByteArray.jsonHexToLong(blockNumOrTag); |
| 697 | + if (blockNumOrTag == null || !blockNumOrTag.startsWith("0x")) { |
| 698 | + throw new JsonRpcInvalidParamsException("Incorrect hex syntax"); |
| 699 | + } |
| 700 | + return parseBlockNumber(blockNumOrTag); |
| 701 | + } |
| 702 | + |
| 703 | + /** |
| 704 | + * Max hex digits of a 32-bit int (0x7FFFFFFF). A transaction index fits a signed int, so the |
| 705 | + * longest valid input is "0x" + 8 hex digits; the +2 in the guard covers the prefix. |
| 706 | + */ |
| 707 | + private static final int MAX_TX_INDEX_HEX_LEN = 8; |
| 708 | + |
| 709 | + /** |
| 710 | + * Parse a 0x-prefixed hex transaction index at the JSON-RPC boundary. |
| 711 | + */ |
| 712 | + public static int parseTxIndex(String index) throws JsonRpcInvalidParamsException { |
| 713 | + if (index == null || index.length() > MAX_TX_INDEX_HEX_LEN + 2) { |
| 714 | + throw new JsonRpcInvalidParamsException(TX_INDEX_ERROR); |
| 715 | + } |
| 716 | + try { |
| 717 | + return ByteArray.jsonHexToInt(index); |
| 718 | + } catch (Exception e) { |
| 719 | + throw new JsonRpcInvalidParamsException(TX_INDEX_ERROR); |
| 720 | + } |
| 721 | + } |
| 722 | + |
| 723 | + /** |
| 724 | + * Compute feeLimit = gas * energyFee with overflow protection. A gas value large enough to |
| 725 | + * overflow a signed 64-bit feeLimit is rejected as invalid-params instead of silently wrapping |
| 726 | + * to a bogus (possibly negative) value. |
| 727 | + */ |
| 728 | + public static long calcFeeLimit(long gas, long energyFee) throws JsonRpcInvalidParamsException { |
| 729 | + try { |
| 730 | + return StrictMathWrapper.multiplyExact(gas, energyFee); |
| 731 | + } catch (ArithmeticException e) { |
| 732 | + throw new JsonRpcInvalidParamsException("invalid gas: fee limit overflow"); |
| 733 | + } |
649 | 734 | } |
650 | 735 |
|
651 | 736 | public static String generateFilterId() { |
|
0 commit comments