You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/04 11:24:00 UTC
[james-project] 24/30: JAMES-2958 Limit the domain creation to a
number of 255 characters
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
commit c55b4afad0e0327ac63f0b7d21fb7d5f83ac3d05
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Nov 1 14:18:17 2019 +0700
JAMES-2958 Limit the domain creation to a number of 255 characters
---
core/src/main/java/org/apache/james/core/Domain.java | 3 +++
.../java/org/apache/james/domainlist/api/DomainTest.java | 12 ++++++++++++
.../java/org/apache/james/webadmin/routes/DomainsRoutes.java | 9 +--------
3 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/core/src/main/java/org/apache/james/core/Domain.java b/core/src/main/java/org/apache/james/core/Domain.java
index 1bcea59..3bf831a 100644
--- a/core/src/main/java/org/apache/james/core/Domain.java
+++ b/core/src/main/java/org/apache/james/core/Domain.java
@@ -28,6 +28,7 @@ import com.google.common.base.Preconditions;
public class Domain implements Serializable {
public static final Domain LOCALHOST = Domain.of("localhost");
+ public static final int MAXIMUM_DOMAIN_LENGTH = 255;
private static String removeBrackets(String domainName) {
if (!(domainName.startsWith("[") && domainName.endsWith("]"))) {
@@ -40,6 +41,8 @@ public class Domain implements Serializable {
Preconditions.checkNotNull(domain, "Domain can not be null");
Preconditions.checkArgument(!domain.isEmpty() && !domain.contains("@"),
"Domain can not be empty nor contain `@`");
+ Preconditions.checkArgument(domain.length() <= MAXIMUM_DOMAIN_LENGTH,
+ "Domain name length should not exceed " + MAXIMUM_DOMAIN_LENGTH + " characters");
return new Domain(domain);
}
diff --git a/core/src/test/java/org/apache/james/domainlist/api/DomainTest.java b/core/src/test/java/org/apache/james/domainlist/api/DomainTest.java
index 8230223..1abffc6 100644
--- a/core/src/test/java/org/apache/james/domainlist/api/DomainTest.java
+++ b/core/src/test/java/org/apache/james/domainlist/api/DomainTest.java
@@ -22,6 +22,7 @@ package org.apache.james.domainlist.api;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import org.apache.commons.lang3.StringUtils;
import org.apache.james.core.Domain;
import org.junit.jupiter.api.Test;
@@ -101,4 +102,15 @@ class DomainTest {
assertThatThrownBy(() -> Domain.of(null)).isInstanceOf(NullPointerException.class);
}
+ @Test
+ void shouldAllow255LongDomain() {
+ assertThat(Domain.of(StringUtils.repeat('a', 255)).asString())
+ .hasSize(255);
+ }
+
+ @Test
+ void shouldThrowWhenTooLong() {
+ assertThatThrownBy(() -> Domain.of(StringUtils.repeat('a', 256)))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
}
\ No newline at end of file
diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
index 1aee7e9..936b3ea 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
@@ -48,7 +48,6 @@ import org.eclipse.jetty.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import io.swagger.annotations.Api;
@@ -244,7 +243,7 @@ public class DomainsRoutes implements Routes {
private String addDomain(Request request, Response response) {
Domain domain = checkValidDomain(request.params(DOMAIN_NAME));
try {
- addDomain(domain);
+ domainList.addDomain(domain);
return Responses.returnNoContent(response);
} catch (DomainListException e) {
LOGGER.info("{} already exists", domain);
@@ -278,12 +277,6 @@ public class DomainsRoutes implements Routes {
}
}
- private void addDomain(Domain domain) throws DomainListException {
- Preconditions.checkArgument(domain.name().length() < MAXIMUM_DOMAIN_SIZE,
- "Domain name length should not exceed " + (MAXIMUM_DOMAIN_SIZE - 1) + " characters");
- domainList.addDomain(domain);
- }
-
private String exists(Request request, Response response) throws DomainListException {
Domain domain = checkValidDomain(request.params(DOMAIN_NAME));
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org