Skip to content

Commit 3e9bb96

Browse files
Feedback: Add IPv6 address handling, including unbracketed.
1 parent 1f183aa commit 3e9bb96

File tree

1 file changed

+42
-3
lines changed
  • libwebauthn/src/ops/webauthn/idl

1 file changed

+42
-3
lines changed

libwebauthn/src/ops/webauthn/idl/rpid.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ impl TryFrom<&str> for RelyingPartyId {
5252
return Err(Error::EmptyRelyingPartyId);
5353
}
5454

55+
// Check for IP addresses (both IPv4 and IPv6)
56+
// IPv6 addresses may be bracketed (e.g., "[::1]") per RFC 2732
5557
if value.parse::<IpAddr>().is_ok() {
5658
return Err(Error::IpAddressNotAllowed(value.to_string()));
5759
}
60+
if value.starts_with('[') && value.ends_with(']') {
61+
let inner = &value[1..value.len() - 1];
62+
if inner.parse::<IpAddr>().is_ok() {
63+
return Err(Error::IpAddressNotAllowed(value.to_string()));
64+
}
65+
}
5866

5967
let ascii = idna::domain_to_ascii(value)
6068
.map_err(|_| Error::InvalidRelyingPartyId(value.to_string()))?;
@@ -128,9 +136,40 @@ mod tests {
128136
}
129137

130138
#[test]
131-
fn test_relying_party_id_rejects_ip_address() {
132-
let result = RelyingPartyId::try_from("127.0.0.1");
133-
assert!(matches!(result, Err(Error::IpAddressNotAllowed(_))));
139+
fn test_relying_party_id_rejects_ipv4_address() {
140+
let ipv4_addresses = ["127.0.0.1", "192.168.1.1", "10.0.0.1", "255.255.255.255"];
141+
for ip in ipv4_addresses {
142+
let result = RelyingPartyId::try_from(ip);
143+
assert!(
144+
matches!(result, Err(Error::IpAddressNotAllowed(_))),
145+
"Expected IPv4 address '{}' to be rejected",
146+
ip
147+
);
148+
}
149+
}
150+
151+
#[test]
152+
fn test_relying_party_id_rejects_ipv6_address() {
153+
let ipv6_addresses = [
154+
// Bracketed format (RFC 2732)
155+
"[::1]",
156+
"[2001:db8::1]",
157+
"[fe80::1]",
158+
"[::ffff:192.168.1.1]",
159+
// Unbracketed format
160+
"::1",
161+
"2001:db8::1",
162+
"fe80::1",
163+
"::ffff:192.168.1.1",
164+
];
165+
for ip in ipv6_addresses {
166+
let result = RelyingPartyId::try_from(ip);
167+
assert!(
168+
matches!(result, Err(Error::IpAddressNotAllowed(_))),
169+
"Expected IPv6 address '{}' to be rejected",
170+
ip
171+
);
172+
}
134173
}
135174

136175
#[test]

0 commit comments

Comments
 (0)