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

Prevent parsing overly deep Node values #442

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -72,6 +72,9 @@

final class IdlModelParser {

/** Only allow nesting up to 250 arrays/objects in node values. */
private static final int MAX_NESTING_LEVEL = 250;

private static final String PUT_KEY = "put";
private static final String CREATE_KEY = "create";
private static final String READ_KEY = "read";
Expand Down Expand Up @@ -111,6 +114,7 @@ final class IdlModelParser {
private String namespace;
private String definedVersion;
private TraitEntry pendingDocumentationComment;
private int nestingLevel;

/** Map of shape aliases to their targets. */
private final Map<String, ShapeId> useShapes = new HashMap<>();
Expand Down Expand Up @@ -910,4 +914,14 @@ void skip() {
int position() {
return position;
}

void increaseNestingLevel() {
if (++nestingLevel >= MAX_NESTING_LEVEL) {
throw syntax("Node value nesting too deep");
}
}

void decreaseNestingLevel() {
nestingLevel--;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ static Node parseTextBlock(IdlModelParser parser) {
}

static ObjectNode parseObjectNode(IdlModelParser parser) {
parser.increaseNestingLevel();
SourceLocation location = parser.currentLocation();
Map<StringNode, Node> entries = new LinkedHashMap<>();
parser.expect('{');
Expand Down Expand Up @@ -132,6 +133,7 @@ static ObjectNode parseObjectNode(IdlModelParser parser) {
}

parser.expect('}');
parser.decreaseNestingLevel();
return new ObjectNode(entries, location);
}

Expand All @@ -144,6 +146,7 @@ static String parseNodeObjectKey(IdlModelParser parser) {
}

private static ArrayNode parseArrayNode(IdlModelParser parser) {
parser.increaseNestingLevel();
SourceLocation location = parser.currentLocation();
List<Node> items = new ArrayList<>();
parser.expect('[');
Expand All @@ -166,6 +169,7 @@ private static ArrayNode parseArrayNode(IdlModelParser parser) {
}

parser.expect(']');
parser.decreaseNestingLevel();
return new ArrayNode(items, location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
package software.amazon.smithy.model.loader;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ResourceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.validation.ValidatedResultException;

public class IdlModelLoaderTest {
@Test
Expand Down Expand Up @@ -94,4 +97,23 @@ public void defersApplyTargetAndTrait() {
assertThat(shape.findTrait(ShapeId.from("smithy.example#bar")), not(Optional.empty()));
assertThat(shape.findTrait(ShapeId.from("smithy.example.b#baz")), not(Optional.empty()));
}

@Test
public void limitsRecursion() {
StringBuilder nodeBuilder = new StringBuilder("metadata foo = ");
for (int i = 0; i < 251; i++) {
nodeBuilder.append('[');
}
nodeBuilder.append("true");
for (int i = 0; i < 251; i++) {
nodeBuilder.append(']');
}
nodeBuilder.append("\n");

ValidatedResultException e = Assertions.assertThrows(ValidatedResultException.class, () -> {
Model.assembler().addUnparsedModel("/foo.smithy", nodeBuilder.toString()).assemble().unwrap();
});

assertThat(e.getMessage(), containsString("Node value nesting too deep"));
}
}