Skip to content
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

net/netip: ParsePrefix allows "+", "-", leading zeroes in prefix length #63850

Closed
danwinship opened this issue Oct 31, 2023 · 12 comments
Closed
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.

Comments

@danwinship
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.21.3 linux/amd64

Does this issue reproduce with the latest release?

Yes, and the code is unchanged in master.

What operating system and processor architecture are you using (go env)?

(not relevant)

What did you do?

p, err := netip.ParsePrefix("1.2.3.0/+24")

What did you expect to see?

an error about the fact that "+" is not a valid character in a CIDR string

What did you see instead?

It accepted it as though I'd said netip.ParsePrefix("1.2.3.0/24").

The code is using strconv.Atoi() to parse the length, and not otherwise validating it, so it accepts anything that strconv.Atoi() would. (Which means netip.ParsePrefix("1.2.3.0/000024") is also accepted, and maybe shouldn't be, though it's not quite so obviously wrong, and that matches the behavior of net.ParseCIDR anyway.)

@aojea
Copy link
Contributor

aojea commented Oct 31, 2023

This somehow similar to the previous issue with IPs with leading zeros #30999

/cc @bradfitz @rsc @ianlancetaylor

@danwinship
Copy link
Author

(It's not that similar... the problem there was that other software interpreted IPs with leading 0s as being a different address, whereas here, we're just accepting values that other software would reject as invalid.)

@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 31, 2023
@mauri870
Copy link
Member

RFC 4632 Section 3.1 states that only valid prefixes are integer values, also reinforced by RFC 4291 Section 2.3:

is a decimal value specifying how many of the leftmost contiguous bits of the address comprise the prefix.

I feel like we should be more rigorous in the validation of what we accept as a valid prefix, instead of relying simply on what the Atoi function can parse.

The doc comment for ParsePrefix also assumes only valid values are the ones that conform to this:

The string can be in the form "192.168.1.0/24" or "2001:db8::/32"

cc @neild

@bradfitz
Copy link
Contributor

Yeah, I think we should tighten this down and not accept + or leading zeros.

/cc @danderson

@danderson
Copy link
Contributor

Yup, makes sense to me. I assume we're accepting + here due to strconv.ParseUint or somesuch?

@mauri870
Copy link
Member

Yup, makes sense to me. I assume we're accepting + here due to strconv.ParseUint or somesuch?

Exactly, we rely on strconv.Atoi to parse the bits, which allow +, - and leading zeroes.

@mauri870 mauri870 changed the title net/netip: ParsePrefix allows "+" in prefix length net/netip: ParsePrefix allows "+", "-", leading zeroes in prefix length Oct 31, 2023
@mauri870
Copy link
Member

Question now is, we want to transform these into an error or silently ignore non-digits and leading zeroes from the prefix?

@aojea
Copy link
Contributor

aojea commented Oct 31, 2023

This somehow similar to the previous issue with IPs with leading zeros #30999

netip.ParsePrefix("1.2.3.0/+24")

that should be an error IMHO

@mauri870 mauri870 self-assigned this Oct 31, 2023
@mauri870 mauri870 added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 31, 2023
@gopherbot
Copy link

Change https://go.dev/cl/538860 mentions this issue: net/netip: allow only valid prefix digits in ParsePrefix

@aojea
Copy link
Contributor

aojea commented Nov 7, 2023

@bradfitz is this something can be backported?

@neild
Copy link
Contributor

neild commented Nov 7, 2023

Backports are for critical issues with no workaround, and minor releases prioritize backwards compatibility as much as possible. (https://github.com/golang/go/wiki/Go-Release-Cycle#release-maintenance) This doesn't seem critical and can be worked around, so I don't think a backport is apprpriate.

@bradfitz
Copy link
Contributor

bradfitz commented Nov 9, 2023

@neild, it probably should be backported. It's low risk and there's no good workaround when the invalid input is a dozen layers deep in some JSON structure you're unmarshaling and expected to be well-formed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants