This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new fa5f1c8484 JAMES-3893 Identity routes should not return 500 code when invalid username (#2519) fa5f1c8484 is described below commit fa5f1c8484901fad629a13cb209c515717e7489a Author: Trần Hồng Quân <55171818+quantranhong1...@users.noreply.github.com> AuthorDate: Mon Nov 25 22:43:21 2024 +0700 JAMES-3893 Identity routes should not return 500 code when invalid username (#2519) --- .../integration/WebAdminServerIntegrationTest.java | 62 ++++++++++++++++++++++ .../webadmin/data/jmap/UserIdentityRoutes.java | 58 +++++++++++++++----- 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/server/protocols/webadmin-integration-test/webadmin-integration-test-common/src/main/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java b/server/protocols/webadmin-integration-test/webadmin-integration-test-common/src/main/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java index 0a8ebef450..d0349e2142 100644 --- a/server/protocols/webadmin-integration-test/webadmin-integration-test-common/src/main/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java +++ b/server/protocols/webadmin-integration-test/webadmin-integration-test-common/src/main/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java @@ -326,6 +326,68 @@ public abstract class WebAdminServerIntegrationTest { .body("message", is("Default identity can not be found")); } + @Test + void getIdentitiesOfInvalidUserShouldReturnBadRequest() { + given() + .get(String.format("/users/%s/identities?default=true", "John Doe")) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400); + } + + @Test + void createIdentitiesForInvalidUserShouldReturnBadRequest() { + given() + .body("{\n" + + " \"name\": \"create name 1\",\n" + + " \"email\": \"b...@domain.tld\",\n" + + " \"textSignature\": \"create textSignature1\",\n" + + " \"htmlSignature\": \"create htmlSignature1\",\n" + + " \"sortOrder\": 99,\n" + + " \"bcc\": [\n" + + " {\n" + + " \"name\": \"create bcc 1\",\n" + + " \"email\": \"create_boss_bc...@domain.tld\"\n" + + " }\n" + + " ],\n" + + " \"replyTo\": [\n" + + " {\n" + + " \"name\": \"create replyTo 1\",\n" + + " \"email\": \"create_bo...@domain.tld\"\n" + + " }\n" + + " ]\n" + + "}") + .post(String.format("/users/%s/identities", "John Doe")) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400); + } + + @Test + void updateIdentitiesForInvalidUserShouldReturnBadRequest() { + given() + .body("{\n" + + " \"name\": \"create name 1\",\n" + + " \"email\": \"b...@domain.tld\",\n" + + " \"textSignature\": \"create textSignature1\",\n" + + " \"htmlSignature\": \"create htmlSignature1\",\n" + + " \"sortOrder\": 99,\n" + + " \"bcc\": [\n" + + " {\n" + + " \"name\": \"create bcc 1\",\n" + + " \"email\": \"create_boss_bc...@domain.tld\"\n" + + " }\n" + + " ],\n" + + " \"replyTo\": [\n" + + " {\n" + + " \"name\": \"create replyTo 1\",\n" + + " \"email\": \"create_bo...@domain.tld\"\n" + + " }\n" + + " ]\n" + + "}") + .put(String.format("/users/%s/identities/b1c924a3-5b86-44fa-a036-77825ec0e3e6", "John Doe")) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400); + } + // Immutable @Test void validateHealthChecksShouldReturnOk() { diff --git a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/UserIdentityRoutes.java b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/UserIdentityRoutes.java index 4be3ba89be..473318385a 100644 --- a/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/UserIdentityRoutes.java +++ b/server/protocols/webadmin/webadmin-jmap/src/main/java/org/apache/james/webadmin/data/jmap/UserIdentityRoutes.java @@ -42,6 +42,8 @@ import org.apache.james.webadmin.utils.ErrorResponder; import org.apache.james.webadmin.utils.JsonTransformer; import org.apache.james.webadmin.utils.ParametersExtractor; import org.eclipse.jetty.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -62,6 +64,7 @@ public class UserIdentityRoutes implements Routes { private static final String USER_NAME = ":userName"; private static final String IDENTITY_ID = ":identityId"; public static final String USERS_IDENTITY_BASE_PATH = USERS + SEPARATOR + USER_NAME + SEPARATOR + IDENTITIES; + private static final Logger LOGGER = LoggerFactory.getLogger(UserIdentityRoutes.class); private Service service; private final IdentityRepository identityRepository; @@ -105,20 +108,31 @@ public class UserIdentityRoutes implements Routes { } private List<UserIdentity> listIdentities(Request request, Response response) { - Username username = extractUsername(request); - Optional<Boolean> defaultFilter = ParametersExtractor.extractBoolean(request, "default"); - - List<UserIdentity> identities = Flux.from(identityRepository.list(username)) - .map(UserIdentity::from) - .collectList() - .block(); - - return defaultFilter - .filter(FunctionalUtils.identityPredicate()) - .map(queryDefault -> getDefaultIdentity(identities) - .map(List::of) - .orElseThrow(() -> throw404("Default identity can not be found"))) - .orElse(identities); + try { + Username username = extractUsername(request); + Optional<Boolean> defaultFilter = ParametersExtractor.extractBoolean(request, "default"); + + List<UserIdentity> identities = Flux.from(identityRepository.list(username)) + .map(UserIdentity::from) + .collectList() + .block(); + + return defaultFilter + .filter(FunctionalUtils.identityPredicate()) + .map(queryDefault -> getDefaultIdentity(identities) + .map(List::of) + .orElseThrow(() -> throw404("Default identity can not be found"))) + .orElse(identities); + } catch (IllegalStateException e) { + LOGGER.info("Invalid argument while listing identities at {}", request.uri(), e); + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message("Invalid argument while listing identities") + .cause(e) + .haltError(); + } + } private HaltException createIdentity(Request request, Response response) { @@ -134,6 +148,14 @@ public class UserIdentityRoutes implements Routes { .message("JSON payload of the request is not valid") .cause(e) .haltError(); + } catch (IllegalStateException e) { + LOGGER.info("Invalid argument while creating identity at {}", request.uri(), e); + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message("Invalid argument while creating identity") + .cause(e) + .haltError(); } } @@ -156,6 +178,14 @@ public class UserIdentityRoutes implements Routes { .haltError(); } catch (IdentityNotFoundException notFoundException) { throw throw404(String.format("IdentityId '%s' can not be found", identityId.id().toString())); + } catch (IllegalStateException e) { + LOGGER.info("Invalid argument while updating identity at {}", request.uri(), e); + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message("Invalid argument while updating identity") + .cause(e) + .haltError(); } } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org