Skip to content

Commit

Permalink
Handle invalid Json bad request body in PostStateValidators (#7739)
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh authored Nov 21, 2023
1 parent 5ec5d34 commit 982c442
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Consensys Software Inc., 2023
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.beaconrestapi.v1.beacon;

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST;

import okhttp3.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest;
import tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.GetStateValidators;

public class PostStateValidatorsIntegrationTest extends AbstractDataBackedRestAPIIntegrationTest {

@BeforeEach
public void setup() {
startRestAPIAtGenesis();
}

@Test
public void shouldReturnBadRequestWhenRequestBodyIsInvalidJson() throws Exception {
Response response = post(GetStateValidators.ROUTE, "{\"ids\": [\"1, \"2\"]}");

assertThat(response.code()).isEqualTo(SC_BAD_REQUEST);
assertThat(response.body().string()).contains("Unexpected character");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

import static tech.pegasys.teku.beaconrestapi.BeaconRestApiTypes.PARAMETER_STATE_ID;
import static tech.pegasys.teku.beaconrestapi.handlers.v1.beacon.StatusParameter.getApplicableValidatorStatuses;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_BAD_REQUEST;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.TAG_BEACON;
import static tech.pegasys.teku.infrastructure.json.types.CoreTypes.STRING_TYPE;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Throwables;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -76,7 +79,20 @@ public PostStateValidators(final DataProvider dataProvider) {

@Override
public void handleRequest(RestApiRequest request) throws JsonProcessingException {
final Optional<RequestBody> requestBody = request.getOptionalRequestBody();
final Optional<RequestBody> requestBody;

try {
requestBody = request.getOptionalRequestBody();
} catch (RuntimeException e) {
final Throwable throwable = Throwables.getRootCause(e);
if (throwable instanceof JsonParseException) {
request.respondError(SC_BAD_REQUEST, throwable.getMessage());
} else {
throw e;
}
return;
}

final List<String> validators = requestBody.map(RequestBody::getIds).orElse(List.of());
final List<StatusParameter> statusParameters =
requestBody.map(RequestBody::getStatuses).orElse(List.of());
Expand Down

0 comments on commit 982c442

Please sign in to comment.