Skip to content

Make stricter IP format check in IpAddressMatcher #17500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
*
* @author Luke Taylor
* @author Steve Riesenberg
* @author Andrey Litvitski
* @since 3.0.2
*/
public final class IpAddressMatcher implements RequestMatcher {

private static Pattern IPV4 = Pattern.compile("\\d{0,3}.\\d{0,3}.\\d{0,3}.\\d{0,3}(/\\d{0,3})?");
private static Pattern IPV4 = Pattern.compile("^\\d{1,3}(?:\\.\\d{1,3}){0,3}(?:/\\d{1,2})?$");

private final InetAddress requiredAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

/**
* @author Luke Taylor
* @author Andrey Litvitski
*/
public class IpAddressMatcherTests {

Expand Down Expand Up @@ -167,4 +168,12 @@ public void toStringWhenOnlyIpIsProvidedThenReturnsIpAddressOnly() {
assertThat(matcher.toString()).hasToString("IpAddress [127.0.0.1]");
}

// gh-17499
@Test
public void constructorRejectsInvalidIpv4WithX() {
String badIp = "10x1x1x1";
assertThatIllegalArgumentException().isThrownBy(() -> new IpAddressMatcher(badIp))
.withMessage("ipAddress 10x1x1x1 doesn't look like an IP Address. Is it a host name?");
}

}
Loading