Skip to content

Commit

Permalink
Mark the port as implicit for empty values in Cookie (#76143)
Browse files Browse the repository at this point in the history
* [Bug] Fix issue #70227

* Fix cookie tests cases to reflect correct behavior.

* Remove trailing semicolon in expected result of Cookie string tests.
  • Loading branch information
dsisco11 authored Oct 13, 2022
1 parent 6556178 commit a41ac2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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());
}
}
}

0 comments on commit a41ac2c

Please sign in to comment.