fix(urn-validator): update urn validation logic (#11952)

This commit is contained in:
david-leifker 2024-11-26 00:02:06 -06:00 committed by GitHub
parent 9f9a8b1006
commit 301e5cd18f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 2 deletions

View File

@ -115,7 +115,8 @@ public class ValidationApiUtils {
/** Recursively process URN parts with URL decoding */
private static Stream<String> processUrnPartRecursively(String urnPart) {
String decodedPart = URLDecoder.decode(urnPart, StandardCharsets.UTF_8);
String decodedPart =
URLDecoder.decode(URLEncodingFixer.fixURLEncoding(urnPart), StandardCharsets.UTF_8);
if (decodedPart.startsWith("urn:li:")) {
// Recursively process nested URN after decoding
return UrnUtils.getUrn(decodedPart).getEntityKey().getParts().stream()
@ -177,4 +178,53 @@ public class ValidationApiUtils {
RecordTemplateValidator.validate(aspect, resultFunction, validator);
}
}
/**
* Fixes malformed URL encoding by escaping unescaped % characters while preserving valid
* percent-encoded sequences.
*/
private static class URLEncodingFixer {
/**
* @param input The potentially malformed URL-encoded string
* @return A string with proper URL encoding that can be safely decoded
*/
public static String fixURLEncoding(String input) {
if (input == null) {
return null;
}
StringBuilder result = new StringBuilder(input.length() * 2);
int i = 0;
while (i < input.length()) {
char currentChar = input.charAt(i);
if (currentChar == '%') {
if (i + 2 < input.length()) {
// Check if the next two characters form a valid hex pair
String hexPair = input.substring(i + 1, i + 3);
if (isValidHexPair(hexPair)) {
// This is a valid percent-encoded sequence, keep it as is
result.append(currentChar);
} else {
// Invalid sequence, escape the % character
result.append("%25");
}
} else {
// % at the end of string, escape it
result.append("%25");
}
} else {
result.append(currentChar);
}
i++;
}
return result.toString();
}
private static boolean isValidHexPair(String pair) {
return pair.matches("[0-9A-Fa-f]{2}");
}
}
}

View File

@ -89,7 +89,24 @@ public class ValidationApiUtilsTest {
@Test(expectedExceptions = NullPointerException.class)
public void testUrnNull() {
// Act
ValidationApiUtils.validateUrn(entityRegistry, null);
}
@Test
public void testValidPartialUrlEncode() {
Urn validUrn = UrnUtils.getUrn("urn:li:assertion:123=-%28__% weekly__%29");
ValidationApiUtils.validateUrn(entityRegistry, validUrn);
// If no exception is thrown, test passes
}
@Test
public void testValidPartialUrlEncode2() {
Urn validUrn =
UrnUtils.getUrn(
"urn:li:dataset:(urn:li:dataPlatform:s3,urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts%prog_maintenance%2CPROD%29,PROD)");
ValidationApiUtils.validateUrn(entityRegistry, validUrn);
// If no exception is thrown, test passes
}
}