From e8382813880512dd26d0a58a22da0efcdba0b420 Mon Sep 17 00:00:00 2001 From: Paul Harris Date: Wed, 15 Jun 2022 13:10:17 +1000 Subject: [PATCH] Fix handling of invalid eth1Address for setFeeRecipient. (#5795) Signed-off-by: Paul Harris --- .../client/restapi/ValidatorRestApi.java | 3 ++ .../restapi/apis/SetFeeRecipientTest.java | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/ValidatorRestApi.java b/validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/ValidatorRestApi.java index 6beab023cbf..cd0817bf762 100644 --- a/validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/ValidatorRestApi.java +++ b/validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/ValidatorRestApi.java @@ -74,6 +74,9 @@ public static RestApi create( .exceptionHandler( BadRequestException.class, (throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage())) + .exceptionHandler( + IllegalArgumentException.class, + (throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage())) .exceptionHandler( JsonProcessingException.class, (throwable) -> new HttpErrorResponse(SC_BAD_REQUEST, throwable.getMessage())) diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetFeeRecipientTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetFeeRecipientTest.java index f66ac990e8f..3d6ac972809 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetFeeRecipientTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetFeeRecipientTest.java @@ -27,15 +27,28 @@ import java.io.IOException; import java.util.Optional; import org.junit.jupiter.api.Test; -import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest; +import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest; +import tech.pegasys.teku.spec.Spec; +import tech.pegasys.teku.spec.TestSpecFactory; import tech.pegasys.teku.spec.datastructures.eth1.Eth1Address; +import tech.pegasys.teku.spec.util.DataStructureUtil; import tech.pegasys.teku.validator.client.BeaconProposerPreparer; public class SetFeeRecipientTest { private final BeaconProposerPreparer beaconProposerPreparer = mock(BeaconProposerPreparer.class); private final SetFeeRecipient handler = new SetFeeRecipient(Optional.of(beaconProposerPreparer)); - private final RestApiRequest request = mock(RestApiRequest.class); + private final StubRestApiRequest request = new StubRestApiRequest(handler.getMetadata()); + + private final Spec spec = TestSpecFactory.createMinimalAltair(); + private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec); + + @Test + void badPubkey_shouldGiveIllegalArgument() { + request.setPathParameter("pubkey", "pubkey"); + assertThatThrownBy(() -> handler.handleRequest(request)) + .isInstanceOf(IllegalArgumentException.class); + } @Test void metadata_shouldHandle400() throws JsonProcessingException { @@ -49,6 +62,9 @@ void metadata_shouldHandle500() throws JsonProcessingException { @Test void shouldShareContextIfBellatrixNotEnabled() { + request.setPathParameter("pubkey", dataStructureUtil.randomPublicKey().toString()); + request.setRequestBody( + new SetFeeRecipient.SetFeeRecipientBody(dataStructureUtil.randomEth1Address())); assertThatThrownBy( () -> { SetFeeRecipient handler = new SetFeeRecipient(Optional.empty()); @@ -73,4 +89,13 @@ void metadata_shouldReadRequestBody() throws IOException { new SetFeeRecipient.SetFeeRecipientBody( Eth1Address.fromHexString("0xabcf8e0d4e9587369b2301d0790347320302cc09"))); } + + @Test + void metadata_shoulThrowInvalidArgument() { + assertThatThrownBy( + () -> + getRequestBodyFromMetadata( + handler, "{\"ethaddress\":\"0xabcF8e0d4E9587369b2301d0790347320302CC09\"}")) + .isInstanceOf(IllegalArgumentException.class); + } }