Skip to content

Commit a1ead44

Browse files
shinbunbunkmoe
andauthored
cidrnetmask() produce an error with IPv6 (#30703)
* Add error handling for IPv6 Co-authored-by: kmoe <5575356+kmoe@users.noreply.github.com>
1 parent 4b516bb commit a1ead44

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

internal/lang/funcs/cidr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ var CidrNetmaskFunc = function.New(&function.Spec{
6060
return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err)
6161
}
6262

63+
if network.IP.To4() == nil {
64+
return cty.UnknownVal(cty.String), fmt.Errorf("IPv6 addresses cannot have a netmask: %s", args[0].AsString())
65+
}
66+
6367
return cty.StringVal(ipaddr.IP(network.Mask).String()), nil
6468
},
6569
})

internal/lang/funcs/cidr_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ func TestCidrNetmask(t *testing.T) {
118118
cty.StringVal("0.0.0.0"),
119119
false,
120120
},
121-
{
122-
cty.StringVal("1::/64"),
123-
cty.StringVal("ffff:ffff:ffff:ffff::"),
124-
false,
125-
},
126121
{
127122
// We inadvertently inherited a pre-Go1.17 standard library quirk
128123
// if parsing zero-prefix parts as decimal rather than octal.
@@ -144,6 +139,11 @@ func TestCidrNetmask(t *testing.T) {
144139
cty.UnknownVal(cty.String),
145140
true, // can't have an octet >255
146141
},
142+
{
143+
cty.StringVal("1::/64"),
144+
cty.UnknownVal(cty.String),
145+
true, // IPv6 is invalid
146+
},
147147
}
148148

149149
for _, test := range tests {

0 commit comments

Comments
 (0)