Skip to content

Commit 5b4923e

Browse files
committed
Taken into account PR review comments
1 parent e02000c commit 5b4923e

File tree

1 file changed

+47
-37
lines changed
  • org.restlet.java/org.restlet/src/main/java/org/restlet/data

1 file changed

+47
-37
lines changed

org.restlet.java/org.restlet/src/main/java/org/restlet/data/Reference.java

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,52 +2855,36 @@ private void validateAuthority(String authority) {
28552855
return;
28562856
}
28572857

2858+
authority = validateUserInfoAndReturnRemaining(authority);
2859+
authority = validateIpV6AndReturnRemaining(authority);
2860+
2861+
int portIndex = authority.indexOf(':');
2862+
if (portIndex != -1) {
2863+
validateHostPort(authority.substring(portIndex + 1));
2864+
}
2865+
}
2866+
2867+
/**
2868+
* Validate the user info part of the authority, and return the remaining part of the authority.
2869+
*/
2870+
private String validateUserInfoAndReturnRemaining(final String authority) {
28582871
int atIndex = authority.indexOf('@');
2859-
int ipV6StartIndex = authority.indexOf('[');
28602872

28612873
if (atIndex != -1) {
2874+
final int ipV6StartIndex = authority.indexOf('[');
2875+
28622876
if (ipV6StartIndex != -1) {
2863-
if (atIndex < ipV6StartIndex) {
2864-
authority = authority.substring(atIndex + 1);
2865-
ipV6StartIndex -= atIndex + 1;
2866-
} else {
2877+
if (atIndex >= ipV6StartIndex) {
28672878
throw new IllegalArgumentException("Invalid authority format");
28682879
}
2869-
} else {
2870-
authority = authority.substring(atIndex + 1);
28712880
}
2881+
return authority.substring(atIndex + 1);
28722882
}
28732883

2874-
if (ipV6StartIndex == -1) {
2875-
int portIndex = authority.indexOf(':');
2876-
2877-
if (portIndex != -1) {
2878-
validateHostPort(authority.substring(portIndex + 1));
2879-
}
2880-
} else if (ipV6StartIndex == 0) {
2881-
int ipV6EndIndex = authority.indexOf(']');
2882-
2883-
if (ipV6EndIndex == -1) {
2884-
throw new IllegalArgumentException("Invalid IPv6 address format: no closing bracket");
2885-
}
2886-
2887-
validateIpV6(authority.substring(1, ipV6EndIndex)); // trim brackets
2888-
2889-
if (ipV6EndIndex + 1 < authority.length()) {
2890-
if (authority.charAt(ipV6EndIndex + 1) == ':') {
2891-
validateHostPort(authority.substring(ipV6EndIndex + 2));
2892-
} else {
2893-
throw new IllegalArgumentException(
2894-
"Invalid authority format: unexpected character after closing bracket");
2895-
}
2896-
}
2897-
} else {
2898-
throw new IllegalArgumentException(
2899-
"Invalid IPv6 address format: unexpected character before opening bracket");
2900-
}
2884+
return authority;
29012885
}
29022886

2903-
/**
2887+
/**
29042888
* Validate an host port.
29052889
*
29062890
* @param hostPort The port to validate.
@@ -2915,16 +2899,42 @@ private void validateHostPort(final String hostPort) {
29152899
}
29162900
}
29172901

2902+
private String validateIpV6AndReturnRemaining(String authority) {
2903+
int ipV6StartIndex = authority.indexOf('[');
2904+
int ipV6EndIndex = authority.indexOf(']');
2905+
2906+
if (ipV6StartIndex > 0) {
2907+
throw new IllegalArgumentException(
2908+
"Invalid IPv6 address format: unexpected character before opening bracket");
2909+
}
2910+
if (ipV6StartIndex == -1) {
2911+
return authority;
2912+
}
2913+
2914+
if (ipV6EndIndex == -1) {
2915+
throw new IllegalArgumentException("Invalid IPv6 address format: no closing bracket");
2916+
}
2917+
validateIpV6(authority.substring(1, ipV6EndIndex)); // trim brackets
2918+
2919+
if (ipV6EndIndex + 1 < authority.length()) {
2920+
if (authority.charAt(ipV6EndIndex + 1) != ':') {
2921+
throw new IllegalArgumentException(
2922+
"Invalid authority format: unexpected character after closing bracket");
2923+
}
2924+
}
2925+
2926+
return authority.substring(ipV6EndIndex + 1);
2927+
}
2928+
29182929
/**
29192930
* Validate an IP v6 according to RFC 2373.
2920-
*
2921-
* @param ipV6 The IP v6 to validate.
29222931
*/
29232932
private void validateIpV6(String ipV6) {
29242933
if (ipV6 == null || ipV6.isEmpty()) {
29252934
throw new IllegalArgumentException("Invalid IPv6 address");
29262935
}
29272936

2937+
29282938
// Check for double colon compression (only one allowed)
29292939
int doubleColonCount = 0;
29302940
int idx = ipV6.indexOf("::");

0 commit comments

Comments
 (0)