Skip to content

Commit

Permalink
cidrnetmask() produce an error with IPv6 (#30703)
Browse files Browse the repository at this point in the history
* Add error handling for IPv6



Co-authored-by: kmoe <5575356+kmoe@users.noreply.github.com>
  • Loading branch information
shinbunbun and kmoe committed Mar 25, 2022
1 parent 4b516bb commit a1ead44
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions internal/lang/funcs/cidr.go
Expand Up @@ -60,6 +60,10 @@ var CidrNetmaskFunc = function.New(&function.Spec{
return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err)
}

if network.IP.To4() == nil {
return cty.UnknownVal(cty.String), fmt.Errorf("IPv6 addresses cannot have a netmask: %s", args[0].AsString())
}

return cty.StringVal(ipaddr.IP(network.Mask).String()), nil
},
})
Expand Down
10 changes: 5 additions & 5 deletions internal/lang/funcs/cidr_test.go
Expand Up @@ -118,11 +118,6 @@ func TestCidrNetmask(t *testing.T) {
cty.StringVal("0.0.0.0"),
false,
},
{
cty.StringVal("1::/64"),
cty.StringVal("ffff:ffff:ffff:ffff::"),
false,
},
{
// We inadvertently inherited a pre-Go1.17 standard library quirk
// if parsing zero-prefix parts as decimal rather than octal.
Expand All @@ -144,6 +139,11 @@ func TestCidrNetmask(t *testing.T) {
cty.UnknownVal(cty.String),
true, // can't have an octet >255
},
{
cty.StringVal("1::/64"),
cty.UnknownVal(cty.String),
true, // IPv6 is invalid
},
}

for _, test := range tests {
Expand Down

0 comments on commit a1ead44

Please sign in to comment.