Skip to content

Commit

Permalink
restrict creating things with too large metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Jul 20, 2022
1 parent 23d71f7 commit 5a00b1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ private CreateThing(final Thing thing, @Nullable final JsonObject initialPolicy,
this.initialPolicy = initialPolicy;
this.policyIdOrPlaceholder = null;

final JsonObject thingJsonObject = thing.toJson();
final JsonObject thingJsonObject = thing.toJson(FieldType.notHidden()
.or(jsonField -> Objects.equals(Thing.JsonFields.METADATA.getPointer(), jsonField.getKey().asPointer())));

ThingCommandSizeValidator.getInstance().ensureValidSize(
thingJsonObject::getUpperBoundForStringSize,
Expand All @@ -118,7 +119,8 @@ private CreateThing(final Thing thing, final String policyIdOrPlaceholder, final
PolicyId.of(policyIdOrPlaceholder); //validates
}

final JsonObject thingJsonObject = thing.toJson();
final JsonObject thingJsonObject = thing.toJson(FieldType.notHidden()
.or(jsonField -> Objects.equals(Thing.JsonFields.METADATA.getPointer(), jsonField.getKey().asPointer())));

ThingCommandSizeValidator.getInstance().ensureValidSize(
thingJsonObject::getUpperBoundForStringSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import java.lang.ref.SoftReference;
import java.text.MessageFormat;

import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.GlobalCommandRegistry;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingTooLargeException;
Expand Down Expand Up @@ -125,6 +128,28 @@ public void createTooLargeThing() {
.isInstanceOf(ThingTooLargeException.class);
}

@Test
public void createTooLargeMetadata() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < TestConstants.THING_SIZE_LIMIT_BYTES; i++) {
sb.append('a');
}
final Thing thing = ThingsModelFactory.newThingBuilder()
.setLifecycle(TestConstants.Thing.LIFECYCLE)
.setPolicyId(TestConstants.Thing.POLICY_ID)
.setId(ThingId.of("test.ns", "foo-bar"))
.setAttribute(JsonPointer.of("foo"), JsonValue.of("bar"))
.setMetadata(Metadata.newBuilder()
.set("attributes/foo/meta", JsonObject.newBuilder()
.set("description", sb.toString())
.build())
.build())
.build();

softly.assertThatThrownBy(() -> CreateThing.of(thing, null, DittoHeaders.empty()))
.isInstanceOf(ThingTooLargeException.class);
}

@Test
public void initializeWithInitialPolicyNullAndWithCopiedPolicyNull() {
final CreateThing createThing =
Expand Down

0 comments on commit 5a00b1a

Please sign in to comment.