summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-04-14 16:54:44 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-04-14 16:54:44 +0000
commit9125d1651742481bdd359b7288a171c57e7b6b54 (patch)
treeeea4a2d09f8f17154eddebe6386773a5a30590b9 /document
parentd8298478feb34ba096ee1452270a92f29f178439 (diff)
Ensure that documentid is legal.
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/idstring/IdIdString.java4
-rw-r--r--document/src/main/java/com/yahoo/document/idstring/IdString.java3
-rw-r--r--document/src/test/java/com/yahoo/document/IdIdStringTest.java32
3 files changed, 39 insertions, 0 deletions
diff --git a/document/src/main/java/com/yahoo/document/idstring/IdIdString.java b/document/src/main/java/com/yahoo/document/idstring/IdIdString.java
index 4c7f71dd712..28573296370 100644
--- a/document/src/main/java/com/yahoo/document/idstring/IdIdString.java
+++ b/document/src/main/java/com/yahoo/document/idstring/IdIdString.java
@@ -47,6 +47,10 @@ public class IdIdString extends IdString {
super(Scheme.id, namespace, localId);
this.type = type;
boolean hasSetLocation = false;
+ if (namespace.length() + type.length() + keyValues.length() + 5 >= IdString.MAX_LENGTH_EXCEPT_NAMESPACE_SPECIFIC) {
+ throw new IllegalArgumentException("Length of namespace(" + namespace.length() + ") + doctype(" + type.length() +
+ ") + key/values(" + keyValues.length() +"), is longer than " + (MAX_LENGTH_EXCEPT_NAMESPACE_SPECIFIC - 5));
+ }
for(String pair : keyValues.split(",")) {
int pos = pair.indexOf('=');
if (pos == -1) {
diff --git a/document/src/main/java/com/yahoo/document/idstring/IdString.java b/document/src/main/java/com/yahoo/document/idstring/IdString.java
index 0fe382be914..d25c39f3b44 100644
--- a/document/src/main/java/com/yahoo/document/idstring/IdString.java
+++ b/document/src/main/java/com/yahoo/document/idstring/IdString.java
@@ -42,6 +42,7 @@ public abstract class IdString {
private final String namespace;
private final String namespaceSpecific;
private Utf8String cache;
+ static final int MAX_LENGTH_EXCEPT_NAMESPACE_SPECIFIC = 0xfffe;
/**
* Creates a IdString based on the given document id string.
@@ -115,6 +116,8 @@ public abstract class IdString {
colonPos = id.indexOf(":", currPos);
if (colonPos < 0) {
throw new IllegalArgumentException("Unparseable id '" + id + "': Key/value section missing");
+ } else if (colonPos >= MAX_LENGTH_EXCEPT_NAMESPACE_SPECIFIC) {
+ throw new IllegalArgumentException("Document id prior to the namespace specific part, " + colonPos + ", is longer than " + MAX_LENGTH_EXCEPT_NAMESPACE_SPECIFIC + " id: " + id);
}
String keyValues = id.substring(currPos, colonPos);
diff --git a/document/src/test/java/com/yahoo/document/IdIdStringTest.java b/document/src/test/java/com/yahoo/document/IdIdStringTest.java
index 88b5f38ec0c..b321d91e6e9 100644
--- a/document/src/test/java/com/yahoo/document/IdIdStringTest.java
+++ b/document/src/test/java/com/yahoo/document/IdIdStringTest.java
@@ -2,9 +2,11 @@
package com.yahoo.document;
import com.yahoo.document.idstring.IdIdString;
+import com.yahoo.document.idstring.IdString;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
@@ -50,6 +52,7 @@ public class IdIdStringTest {
new IdIdString("namespace", "type", "illegal=key", "foo");
fail();
} catch (IllegalArgumentException e) {
+ assertEquals("Illegal key 'illegal'", e.getMessage());
}
}
@@ -59,6 +62,35 @@ public class IdIdStringTest {
new IdIdString("namespace", "type", "illegal-pair", "foo");
fail();
} catch (IllegalArgumentException e) {
+ assertEquals("Illegal key-value pair 'illegal-pair'", e.getMessage());
+ }
+ }
+
+ @Test
+ public void requireThatTooLongPreNamspaceSpecificThrowsWhileParsing() throws Exception {
+ StringBuilder builder = new StringBuilder("id:");
+ for (int i = 0; i < 0x10000; i++) {
+ builder.append('n');
+ }
+ builder.append(":type::namespacespecificpart_01");
+ try {
+ IdString.createIdString(builder.toString());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertEquals("Document id prior to the namespace specific part, 65545, is longer than 65534", e.getMessage().substring(0, 77));
+ }
+ }
+ @Test
+ public void requireThatTooLongPreNamspaceSpecificThrowsOnConstruction() {
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < 0x10000; i++) {
+ builder.append('n');
+ }
+ try {
+ new IdIdString(builder.toString(), "type", "", "namespacespecificpart_01");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertEquals("Length of namespace(65536) + doctype(4) + key/values(0), is longer than 65529", e.getMessage());
}
}