Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #408: validate characters of document field names #429

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.stargate.sgv2.jsonapi.config.constants;

import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.JsonType;
import java.util.regex.Pattern;

public interface DocumentConstants {
/** Names of "special" fields in Documents */
interface Fields {
/** Primary key for Documents stored; has special handling for many operations. */
String DOC_ID = "_id";

// Current definition of valid JSON API names: note that this only validates
// characters, not length limits (nor empty nor "too long" allowed but validated
// separately)
Pattern VALID_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_]*");
}

interface KeyTypeId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public enum ErrorCode {

SHRED_DOC_LIMIT_VIOLATION("Document size limitation violated"),

SHRED_DOC_KEY_NAME_VIOLATION("Document key name constraints violated"),

SHRED_BAD_EJSON_VALUE("Bad EJSON value"),

INVALID_FILTER_EXPRESSION("Invalid filter expression"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,30 @@ private void validateObjectValue(DocumentLimitsConfig limits, JsonNode objectVal
var it = objectValue.fields();
while (it.hasNext()) {
var entry = it.next();
final String key = entry.getKey();
if (key.length() > documentLimits.maxPropertyNameLength()) {
throw new JsonApiException(
ErrorCode.SHRED_DOC_LIMIT_VIOLATION,
String.format(
"%s: Property name length (%d) exceeds maximum allowed (%s)",
ErrorCode.SHRED_DOC_LIMIT_VIOLATION.getMessage(),
key.length(),
limits.maxPropertyNameLength()));
}
validateObjectKey(limits, entry.getKey());
validateDocValue(limits, entry.getValue(), depth);
}
}

private void validateObjectKey(DocumentLimitsConfig limits, String key) {
if (key.length() > documentLimits.maxPropertyNameLength()) {
throw new JsonApiException(
ErrorCode.SHRED_DOC_LIMIT_VIOLATION,
String.format(
"%s: Property name length (%d) exceeds maximum allowed (%s)",
ErrorCode.SHRED_DOC_LIMIT_VIOLATION.getMessage(),
key.length(),
limits.maxPropertyNameLength()));
}
if (!DocumentConstants.Fields.VALID_NAME_PATTERN.matcher(key).matches()) {
throw new JsonApiException(
ErrorCode.SHRED_DOC_KEY_NAME_VIOLATION,
String.format(
"%s: Property name ('%s') contains character(s) not allowed",
ErrorCode.SHRED_DOC_KEY_NAME_VIOLATION.getMessage(), key));
}
}

private void validateStringValue(DocumentLimitsConfig limits, JsonNode stringValue) {
final String value = stringValue.textValue();
if (value.length() > limits.maxStringLength()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class ValidationDocAtomicSizeViolations {
public void allowNotTooLongNames() {
final ObjectNode doc = objectMapper.createObjectNode();
doc.put("_id", 123);
doc.put("prop-123456789-123456789-123456789-123456789", true);
doc.put("prop_123456789_123456789_123456789_123456789", true);
assertThat(shredder.shred(doc)).isNotNull();
}

Expand All @@ -188,7 +188,7 @@ public void catchTooLongNames() {
doc.put("_id", 123);
ObjectNode ob = doc.putObject("subdoc");
final String propName =
"property-with-way-too-long-name-123456789-123456789-123456789-123456789";
"property_with_way_too_long_name_123456789_123456789_123456789_123456789";
ob.put(propName, true);

Exception e = catchException(() -> shredder.shred(doc));
Expand All @@ -205,11 +205,28 @@ public void catchTooLongNames() {
+ ")");
}

@Test
public void catchInvalidNames() {
final ObjectNode doc = objectMapper.createObjectNode();
doc.put("_id", 123);
final String invalidName = "$date"; // property names must not start with $
doc.put(invalidName, 123456);

Exception e = catchException(() -> shredder.shred(doc));
assertThat(e)
.isNotNull()
.isInstanceOf(JsonApiException.class)
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.SHRED_DOC_KEY_NAME_VIOLATION)
.hasMessageStartingWith(ErrorCode.SHRED_DOC_KEY_NAME_VIOLATION.getMessage())
.hasMessageEndingWith(
"Document key name constraints violated: Property name ('$date') contains character(s) not allowed");
}

@Test
public void allowNotTooLongStringValues() {
final ObjectNode doc = objectMapper.createObjectNode();
doc.put("_id", 123);
// Max is 16_000 so do bit less
// Max is 16_000 so do a bit less
doc.put("text", RandomStringUtils.randomAscii(12_000));
assertThat(shredder.shred(doc)).isNotNull();
}
Expand Down