Skip to content

Inconsistent IPNetwork.Contains behavior for prefixes which are not at start of subnet range. #6674

Closed
@pavel12345

Description

@pavel12345

Describe the bug

In AspNetCore/src/Middleware/HttpOverrides/src/IPNetwork.cs

The code below assumes that the prefix will have all bits in position > mask length set to 0. I do not believe that this a requirement for CIDR notation. If it is, it is not applied consistently since the routine will break if the mask is == 0 at a given byte.

        for (int i = 0; i < PrefixBytes.Length && Mask[i] != 0; i++)
        {
            if (PrefixBytes[i] != (addressBytes[i] & Mask[i]))
            {
                return false;
            }
        }

To Reproduce

Create instance with Prefix/Prefix Length 192.168.0.1/31.
Contains returns false for 192.168.0.0.

Expected behavior

Expectation is that 192.168.0.1/31 would match 192.168.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresbugThis issue describes a behavior which is not expected - a bug.good first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions