Skip to content

Commit

Permalink
feat(urn) Add validator to reject URNs which contain the character we…
Browse files Browse the repository at this point in the history
… plan to u… (#7859)

Co-authored-by: Indy Prentice <[email protected]>
  • Loading branch information
iprentic and Indy Prentice authored Apr 20, 2023
1 parent bf86d79 commit e9364ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public static class IngestProposalResult {

private static final int URN_NUM_BYTES_LIMIT = 512;

// TODO(iprentic): Move this to a common utils location once used in other places
private static final String DELIMITER_SEPARATOR = "␟";

public EntityService(
@Nonnull final AspectDao aspectDao,
@Nonnull final EventProducer producer,
Expand Down Expand Up @@ -690,6 +693,9 @@ static void validateUrn(@Nonnull final Urn urn) {
if (URLEncoder.encode(urn.toString()).length() > URN_NUM_BYTES_LIMIT) {
throw new IllegalArgumentException("Error: cannot provide an URN longer than " + Integer.toString(URN_NUM_BYTES_LIMIT) + " bytes (when URL encoded)");
}
if (urn.toString().contains(DELIMITER_SEPARATOR)) {
throw new IllegalArgumentException("Error: URN cannot contain " + DELIMITER_SEPARATOR + " character");
}
}

public void ingestAspects(@Nonnull final Urn urn, @Nonnull List<Pair<String, RecordTemplate>> aspectRecordsToIngest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,17 @@ public void testValidateUrn() throws Exception {
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Error: cannot provide an URN longer than 512 bytes (when URL encoded)");
}

// Urn containing disallowed character
Urn testUrnSpecialCharValid = new Urn("li", "testType", new TupleKey("entity␇"));
Urn testUrnSpecialCharInvalid = new Urn("li", "testType", new TupleKey("entity␟"));
EntityService.validateUrn(testUrnSpecialCharValid);
try {
EntityService.validateUrn(testUrnSpecialCharInvalid);
Assert.fail("Should have raised IllegalArgumentException for URN containing the illegal char");
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Error: URN cannot contain ␟ character");
}
}

@Nonnull
Expand Down

0 comments on commit e9364ed

Please sign in to comment.