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

Mark the port as implicit for empty values in Cookie #76143

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,17 @@ public string Port
}
set
{
m_port_implicit = false;
if (string.IsNullOrEmpty(value))
{
// "Port" is present but has no value.
// Therefore; the effective port value is implicit.
m_port_implicit = true;
m_port = string.Empty;
}
else
{
// "Port" value is present, so we use the provided value rather than an implicit one.
m_port_implicit = false;
// Parse port list
if (!value.StartsWith('\"') || !value.EndsWith('\"'))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,14 @@ public static void ToString_Compare_Success()
c.Version = 0;
Assert.Equal("name=value; $Path=path; $Domain=domain; $Port=\"80\"", c.ToString());

// If a cookie string specifies either an empty string or no value for the port, then the port should be considered implicit.
// Otherwise such cookies will have no valid ports and also be incapable of assuming a usable port which in turn means they cannot be matched to ANY Uris and will effectively become nonfunctional.
c.Port = "";
Assert.Equal("name=value; $Path=path; $Domain=domain; $Port", c.ToString());
Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());

// Test null also, for sanity.
c.Port = null;
Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());
}
}
}