Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json RPC method parameter validation #2097

Merged
merged 59 commits into from
Oct 12, 2023
Merged

Conversation

asoto-iov
Copy link
Contributor

Description

Motivation and Context

How Has This Been Tested?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:

… blockHash, Tx Hash and Hex Index parameters and deserializers
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class JsonRPCParamValidationTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: JsonRPCParamValidationTest is not referenced within this codebase. If not used as an external API it should be removed.
….. related methods having parameter validation

@Test
void eth_getTransactionByHash_invalidHash_returnsError() throws Exception {
TransactionResultDTO resultDTO = mock(TransactionResultDTO.class);

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'TransactionResultDTO resultDTO' is never read.
@asoto-iov asoto-iov marked this pull request as ready for review August 10, 2023 15:29

String preResult = removeHexPrefix(param);

return Integer.parseInt(preResult, 16);

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
@casiojapi
Copy link
Contributor

pipeline:run

Comment on lines 71 to 74
public BlockRefParam deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
BlockRef blockRef = mapper.readValue(jp, BlockRef.class);
return new BlockRefParam(blockRef);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

here we'd need to support two cases and validate them:

  • value is a blockId, eg. number "0x123" or tag "latest" (String)
  • value is an JSON object with all supported fields, eg. blockNumber or requireCanonical (Map<String, String>)

rmoreliovlabs
rmoreliovlabs previously approved these changes Sep 19, 2023
public interface EthModuleWallet {

String[] accounts();

String sign(String addr, String data);
String sign(HexAddressParam addr, HexDataParam data);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

About this change, I think we shouldn't add the specific JsonRPC parameters here, we could keep the string or directly update to the address and data classes.
The Json PRC parameter classes should be used only under the jsonRPC domain.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand, I will revert the changes for this one.

try {
this.rawDataBytes = HexUtils.stringHexToByteArray(rawData);
} catch (Exception e) {
throw RskJsonRpcRequestException.invalidParamError("Invalid data format. " + e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we should not expose an Exception's message to a user, I'd rather do it this way:

throw RskJsonRpcRequestException.invalidParamError("Invalid data format", e);

Messages are returned in JSON-RPC responses

Copy link
Contributor

Choose a reason for hiding this comment

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

Please also check other param classes, eg HashParam32, HexAddressParam etc

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

@Vovchyk @asoto-iov I overlooked that using the e object will still expose the exception message, so I updated these errors to "Invalid data format: invalid hex value". I think this also will keep it consistent with the rest of the code since we don't use the exception message for any of the other validation errors. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

@rmoreliovlabs, you can still pass exception as the last parameter in the list, so it'd be printed in logs later - like in the example above

@@ -0,0 +1,51 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,102 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,127 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,48 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,49 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,41 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,45 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,50 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

@@ -0,0 +1,16 @@
package org.ethereum.rpc.parameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

(C) header is missing

Copy link
Contributor

Choose a reason for hiding this comment

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

I've added the (C) header to all new files that were missing it, thanks for catching that.

@rmoreliovlabs
Copy link
Contributor

pipeline:run

" \"topics\":[\"0x000000000000000000000000000000006d696e696e675f6665655f746f706963\", null, [\"0x000000000000000000000000000000006d696e696e675f6665655f746f706963\",null]]}";
JsonNode jsonNode = objectMapper.readTree(filterRequestInput);
FilterRequestParam filterRequestParam = objectMapper.convertValue(jsonNode, FilterRequestParam.class);
FilterRequest fr = objectMapper.convertValue(jsonNode, FilterRequest.class);

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'FilterRequest fr' is never read.
@Vovchyk
Copy link
Contributor

Vovchyk commented Oct 12, 2023

pipeline:run

@sonarcloud
Copy link

sonarcloud bot commented Oct 12, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 59 Code Smells

82.3% 82.3% Coverage
0.0% 0.0% Duplication

@Vovchyk Vovchyk merged commit 5c5fe3d into master Oct 12, 2023
11 checks passed
@Vovchyk Vovchyk deleted the json_rpc_param_validation branch October 12, 2023 15:38
@aeidelman aeidelman added this to the Fingerroot 5.4.0 milestone Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants