Skip to content
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
11 changes: 7 additions & 4 deletions src/main/java/build/buf/protovalidate/CustomOverload.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.common.primitives.Bytes;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import org.projectnessie.cel.common.types.BoolT;
Expand Down Expand Up @@ -463,6 +462,10 @@ private static boolean isPort(String str) {
return false;
}

if (str.length() > 1 && str.charAt(0) == '0') {
return false;
}

for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if ('0' <= c && c <= '9') {
Expand Down Expand Up @@ -557,7 +560,7 @@ private static boolean isHostname(String val) {

boolean allDigits = false;

String[] parts = str.toLowerCase(Locale.getDefault()).split("\\.", -1);
String[] parts = str.split("\\.", -1);

// split hostname on '.' and validate each part
for (String part : parts) {
Expand All @@ -572,8 +575,8 @@ private static boolean isHostname(String val) {
// for each character in part
for (int i = 0; i < part.length(); i++) {
char c = part.charAt(i);
// if the character is not a-z, 0-9, or '-', it is invalid
if ((c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-') {
// if the character is not a-z, A-Z, 0-9, or '-', it is invalid
if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '-') {
return false;
}

Expand Down
60 changes: 29 additions & 31 deletions src/main/java/build/buf/protovalidate/Uri.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private boolean scheme() {
if (this.alpha()) {
while (this.alpha() || this.digit() || this.take('+') || this.take('-') || this.take('.')) {}

if (this.str.charAt(this.index) == ':') {
if (this.peek(':')) {
return true;
}
}
Expand Down Expand Up @@ -253,10 +253,8 @@ private boolean userinfo() {
continue;
}

if (this.index < this.str.length()) {
if (this.str.charAt(this.index) == '@') {
return true;
}
if (this.peek('@')) {
return true;
}

this.index = start;
Expand Down Expand Up @@ -335,15 +333,11 @@ private boolean checkHostPctEncoded(String str) {
* <pre>host = IP-literal / IPv4address / reg-name.
*/
private boolean host() {
if (this.index >= this.str.length()) {
return true;
}

int start = this.index;
this.pctEncodedFound = false;

// Note: IPv4address is a subset of reg-name
if ((this.str.charAt(this.index) == '[' && this.ipLiteral()) || this.regName()) {
if ((this.peek('[') && this.ipLiteral()) || this.regName()) {
if (this.pctEncodedFound) {
String rawHost = this.str.substring(start, this.index);
// RFC 3986:
Expand Down Expand Up @@ -446,7 +440,7 @@ private boolean ipv6Address() {
/**
* Determines whether the current position is a valid IPv6addrz.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>IPv6addrz = IPv6address "%25" ZoneID
*/
Expand All @@ -465,7 +459,7 @@ private boolean ipv6Addrz() {
/**
* Determines whether the current position is a valid zone ID.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>ZoneID = 1*( unreserved / pct-encoded )
*/
Expand All @@ -486,7 +480,7 @@ private boolean zoneID() {
/**
* Determines whether the current position is a valid IPvFuture.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
*/
Expand Down Expand Up @@ -517,7 +511,7 @@ private boolean ipvFuture() {
/**
* Determines whether the current position is a valid reg-name.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>reg-name = *( unreserved / pct-encoded / sub-delims )
*
Expand All @@ -536,7 +530,7 @@ private boolean regName() {
return true;
}

if (this.str.charAt(this.index) == ':') {
if (this.peek(':')) {
return true;
}

Expand Down Expand Up @@ -570,7 +564,7 @@ private boolean isPathEnd() {
/**
* Determines whether the current position is a valid path-abempty.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>path-abempty = *( "/" segment )
*
Expand All @@ -593,7 +587,7 @@ private boolean pathAbempty() {
/**
* Determines whether the current position is a valid path-absolute.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>path-absolute = "/" [ segment-nz *( "/" segment ) ]
*
Expand All @@ -620,7 +614,7 @@ private boolean pathAbsolute() {
/**
* Determines whether the current position is a valid path-noscheme.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>path-noscheme = segment-nz-nc *( "/" segment )
*
Expand All @@ -645,7 +639,7 @@ private boolean pathNoscheme() {
/**
* Determines whether the current position is a valid path-rootless.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>path-rootless = segment-nz *( "/" segment )
*
Expand All @@ -670,7 +664,7 @@ private boolean pathRootless() {
/**
* Determines whether the current position is a valid path-empty.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>path-empty = 0<pchar>
*
Expand All @@ -683,7 +677,7 @@ private boolean pathEmpty() {
/**
* Determines whether the current position is a valid segment.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>segment = *pchar
*/
Expand All @@ -696,7 +690,7 @@ private boolean segment() {
/**
* Determines whether the current position is a valid segment-nz.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>segment-nz = 1*pchar
*/
Expand All @@ -716,7 +710,7 @@ private boolean segmentNz() {
/**
* Determines whether the current position is a valid segment-nz-nc.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
* ; non-zero-length segment without any colon ":"
Expand All @@ -738,7 +732,7 @@ private boolean segmentNzNc() {
/**
* Determines whether the current position is a valid pchar.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
*/
Expand All @@ -753,7 +747,7 @@ private boolean pchar() {
/**
* Determines whether the current position is a valid query.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>query = *( pchar / "/" / "?" )
*
Expand All @@ -767,7 +761,7 @@ private boolean query() {
continue;
}

if (this.index == this.str.length() || this.str.charAt(this.index) == '#') {
if (this.peek('#') || this.index == this.str.length()) {
return true;
}

Expand All @@ -780,7 +774,7 @@ private boolean query() {
/**
* Determines whether the current position is a valid fragment.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>fragment = *( pchar / "/" / "?" )
*
Expand All @@ -807,7 +801,7 @@ private boolean fragment() {
/**
* Determines whether the current position is a valid pct-encoded.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>pct-encoded = "%"+HEXDIG+HEXDIG
*
Expand All @@ -830,7 +824,7 @@ private boolean pctEncoded() {
/**
* Determines whether the current position is an unreserved character.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
*/
Expand All @@ -846,7 +840,7 @@ private boolean unreserved() {
/**
* Determines whether the current position is a sub-delim.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
Expand All @@ -868,7 +862,7 @@ private boolean subDelims() {
/**
* Determines whether the current position is an alpha character.
*
* Parses the rule:
* <p>Parses the rule:
*
* <pre>ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
*/
Expand Down Expand Up @@ -942,4 +936,8 @@ private boolean take(char c) {

return false;
}

private boolean peek(char c) {
return this.index < this.str.length() && this.str.charAt(this.index) == c;
}
}