This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9942c901ac7d93b623c5cb2ae4f22927fac8e2fc Author: Tran Tien Duc <[email protected]> AuthorDate: Tue Apr 23 11:01:50 2019 +0700 JAMES-2729 ForwardRoutes validating source domain existence --- .../james/webadmin/routes/ForwardRoutes.java | 8 ++++ .../james/webadmin/routes/ForwardRoutesTest.java | 54 ++++++++++++++-------- src/site/markdown/server/manage-webadmin.md | 2 +- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/ForwardRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/ForwardRoutes.java index 85d6142..ee9f8d6 100644 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/ForwardRoutes.java +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/ForwardRoutes.java @@ -36,6 +36,7 @@ import org.apache.james.core.User; import org.apache.james.rrt.api.MappingAlreadyExistsException; import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTableException; +import org.apache.james.rrt.api.SourceDomainIsNotInDomainListException; import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.MappingSource; import org.apache.james.rrt.lib.Mappings; @@ -142,6 +143,7 @@ public class ForwardRoutes implements Routes { @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = FORWARD_BASE_ADDRESS + " or forward structure format is not valid"), + @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain in the source is not managed by the DomainList"), @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "requested base forward address does not match a user"), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") @@ -160,6 +162,12 @@ public class ForwardRoutes implements Routes { recipientRewriteTable.addForwardMapping(source, destinationAddress.asString()); } catch (MappingAlreadyExistsException e) { // ignore + } catch (SourceDomainIsNotInDomainListException e) { + throw ErrorResponder.builder() + .statusCode(HttpStatus.BAD_REQUEST_400) + .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) + .message(e.getMessage()) + .haltError(); } } diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java index f254860..9a1e007 100644 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/ForwardRoutesTest.java @@ -29,9 +29,10 @@ import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.CoreMatchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import java.util.List; import java.util.Map; @@ -43,7 +44,6 @@ import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.lib.DomainListConfiguration; import org.apache.james.domainlist.memory.MemoryDomainList; import org.apache.james.metrics.logger.DefaultMetricFactory; -import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTableException; import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.MappingSource; @@ -68,6 +68,7 @@ import io.restassured.http.ContentType; class ForwardRoutesTest { private static final Domain DOMAIN = Domain.of("b.com"); + private static final Domain ALIAS_DOMAIN = Domain.of("alias"); public static final String CEDRIC = "cedric@" + DOMAIN.name(); public static final String ALICE = "alice@" + DOMAIN.name(); public static final String ALICE_WITH_SLASH = "alice/@" + DOMAIN.name(); @@ -114,6 +115,8 @@ class ForwardRoutesTest { .autoDetect(false) .autoDetectIp(false)); domainList.addDomain(DOMAIN); + domainList.addDomain(ALIAS_DOMAIN); + memoryRecipientRewriteTable.setDomainList(domainList); MappingSourceModule mappingSourceModule = new MappingSourceModule(); usersRepository = MemoryUsersRepository.withVirtualHosting(); @@ -438,7 +441,7 @@ class ForwardRoutesTest { super.setUp(); memoryRecipientRewriteTable.addErrorMapping(MappingSource.fromUser("error", DOMAIN), "disabled"); memoryRecipientRewriteTable.addRegexMapping(MappingSource.fromUser("regex", DOMAIN), ".*@b\\.com"); - memoryRecipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(Domain.of("alias")), DOMAIN); + memoryRecipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(ALIAS_DOMAIN), DOMAIN); } } @@ -446,14 +449,18 @@ class ForwardRoutesTest { @Nested class ExceptionHandling { - private RecipientRewriteTable memoryRecipientRewriteTable; + private MemoryRecipientRewriteTable memoryRecipientRewriteTable; + private DomainList domainList; @BeforeEach void setUp() throws Exception { - memoryRecipientRewriteTable = mock(RecipientRewriteTable.class); + memoryRecipientRewriteTable = spy(new MemoryRecipientRewriteTable()); UsersRepository userRepository = mock(UsersRepository.class); - Mockito.when(userRepository.contains(eq(ALICE))).thenReturn(true); - DomainList domainList = mock(DomainList.class); + doReturn(true) + .when(userRepository).contains(any()); + + domainList = mock(DomainList.class); + memoryRecipientRewriteTable.setDomainList(domainList); Mockito.when(domainList.containsDomain(any())).thenReturn(true); createServer(new ForwardRoutes(memoryRecipientRewriteTable, userRepository, new JsonTransformer())); } @@ -497,6 +504,27 @@ class ForwardRoutesTest { } @Test + void putWithSourceDomainNotInDomainListShouldReturnBadRequest() throws Exception { + doReturn(false) + .when(domainList).containsDomain(any()); + + Map<String, Object> errors = when() + .put("[email protected]" + SEPARATOR + "targets" + SEPARATOR + BOB) + .then() + .statusCode(HttpStatus.BAD_REQUEST_400) + .contentType(ContentType.JSON) + .extract() + .body() + .jsonPath() + .getMap("."); + + assertThat(errors) + .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400) + .containsEntry("type", "InvalidArgument") + .containsEntry("message", "Source domain 'not-managed-domain.tld' is not managed by the domainList"); + } + + @Test void putUserInForwardWithSlashShouldReturnNotFound() { when() .put(ALICE_WITH_SLASH + SEPARATOR + "targets" + SEPARATOR + BOB) @@ -664,18 +692,6 @@ class ForwardRoutesTest { } @Test - void getShouldReturnErrorWhenRecipientRewriteTableExceptionIsThrown() throws Exception { - doThrow(RecipientRewriteTableException.class) - .when(memoryRecipientRewriteTable) - .getStoredMappings(any()); - - when() - .get(ALICE) - .then() - .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500); - } - - @Test void getShouldReturnErrorWhenRuntimeExceptionIsThrown() throws Exception { doThrow(RuntimeException.class) .when(memoryRecipientRewriteTable) diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md index c6bfcef..bebce84 100644 --- a/src/site/markdown/server/manage-webadmin.md +++ b/src/site/markdown/server/manage-webadmin.md @@ -1288,7 +1288,7 @@ Response codes: - 204: Success - 400: Forward structure or member is not valid - - 403: Server does not own the requested domain + - 400: Domain in the source is not managed by the DomainList - 404: Requested forward address does not match an existing user ### Removing a destination of a forward --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
