8000 fix: using regex to expand local ipv6 matching (#35736) by shawkins · Pull Request #35737 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public class SecureContextResolver {

private static final Pattern LOCALHOST_IPV4 = Pattern.compile("127.\\d{1,3}.\\d{1,3}.\\d{1,3}");
private static final Pattern LOCALHOST_IPV6 = Pattern.compile("\\[(0{0,4}:){1,7}0{0,3}1\\]");


/**
* Determines if a session is within a 'secure context', meaning its origin is considered potentially trustworthy by user-agents.
Expand Down Expand Up @@ -78,15 +80,15 @@ public static boolean isLocalAddress(String address) {
return false;
}
// The host matches a CIDR notation of ::1/128
if (address.equals("[::1]") || address.equals("[0000:0000:0000:0000:0000:0000:0000:0001]")) {
return true;
if (address.startsWith("[")) {
return LOCALHOST_IPV6.matcher(address).matches();
}

// The host matches a CIDR notation of 127.0.0.0/8
if (LOCALHOST_IPV4.matcher(address).matches()) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void testIp4() {
public void testIp6() {
assertSecureContext("http://[::1]", true);
assertSecureContext("http://[0000:0000:0000:0000:0000:0000:0000:0001]", true);
assertSecureContext("http://[0:0:0:0:0:0:0:1]", true);
assertSecureContext("http://[0:0:0::1]", true);
assertSecureContext("http://[::2]", false);
assertSecureContext("http://[2001:0000:130F:0000:0000:09C0:876A:130B]", false);
assertSecureContext("http://::1", false);
Expand All @@ -63,7 +65,7 @@ public void testLocalhost() {
assertSecureContext("http://test.localhostn", false);
assertSecureContext("http://test.localhost.not", false);
}

@Test
public void testIsLocalhost() {
assertTrue(SecureContextResolver.isLocalAddress("127.0.0.1"));
Expand Down
0