Skip to content

Commit b5b9af8

Browse files
committed
Log if Partitioned is set but Path != /
1 parent 58caa90 commit b5b9af8

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/Http/Http/src/Internal/ResponseCookies.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,23 @@ private static MessagesToLog GetMessagesToLog(CookieOptions options)
174174
toLog |= MessagesToLog.SameSiteNotSecure;
175175
}
176176

177-
if (!options.Secure && options.Partitioned)
177+
if (options.Partitioned)
178178
{
179-
toLog |= MessagesToLog.PartitionedNotSecure;
180-
}
179+
if (!options.Secure)
180+
{
181+
toLog |= MessagesToLog.PartitionedNotSecure;
182+
}
181183

182-
if (options.Partitioned && options.SameSite != SameSiteMode.None)
183-
{
184-
toLog |= MessagesToLog.PartitionedNotSameSiteNone;
184+
if (options.SameSite != SameSiteMode.None)
185+
{
186+
toLog |= MessagesToLog.PartitionedNotSameSiteNone;
187+
}
188+
189+
// Chromium checks this
190+
if (options.Path != "/")
191+
{
192+
toLog |= MessagesToLog.PartitionedNotPathRoot;
193+
}
185194
}
186195

187196
return toLog;
@@ -203,6 +212,11 @@ private static void LogMessages(ILogger logger, MessagesToLog messages, string c
203212
{
204213
Log.PartitionedCookieNotSameSiteNone(logger, cookieName);
205214
}
215+
216+
if ((messages & MessagesToLog.PartitionedNotPathRoot) != 0)
217+
{
218+
Log.PartitionedCookieNotPathRoot(logger, cookieName);
219+
}
206220
}
207221

208222
[Flags]
@@ -212,6 +226,7 @@ private enum MessagesToLog
212226
SameSiteNotSecure = 1 << 0,
213227
PartitionedNotSecure = 1 << 1,
214228
PartitionedNotSameSiteNone = 1 << 2,
229+
PartitionedNotPathRoot = 1 << 3,
215230
}
216231

217232
private static partial class Log
@@ -224,5 +239,8 @@ private static partial class Log
224239

225240
[LoggerMessage(3, LogLevel.Debug, "The cookie '{name}' has set 'Partitioned' and should also set 'SameSite=None'. This cookie will likely be rejected by the client.", EventName = "PartitionedNotSameSiteNone")]
226241
public static partial void PartitionedCookieNotSameSiteNone(ILogger logger, string name);
242+
243+
[LoggerMessage(4, LogLevel.Debug, "The cookie '{name}' has set 'Partitioned' and should also set 'Path=/'. This cookie may be rejected by the client.", EventName = "PartitionedNotPathRoot")]
244+
public static partial void PartitionedCookieNotPathRoot(ILogger logger, string name);
227245
}
228246
}

src/Http/Http/test/ResponseCookiesTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public void AppendPartitionedLogsWarnings()
120120
Partitioned = true,
121121
// Missing SameSite = SameSiteMode.None,
122122
// Missing Secure = true,
123+
Path = "/a", // Should be Path = "/",
123124
});
124125

125126
var cookieHeaderValues = headers.SetCookie;
@@ -132,6 +133,7 @@ public void AppendPartitionedLogsWarnings()
132133
[
133134
entry => Assert.Equal($"The cookie '{testCookie}' has set 'Partitioned' and must also set 'Secure'. This cookie will likely be rejected by the client.", entry.Message),
134135
entry => Assert.Equal($"The cookie '{testCookie}' has set 'Partitioned' and should also set 'SameSite=None'. This cookie will likely be rejected by the client.", entry.Message),
136+
entry => Assert.Equal($"The cookie '{testCookie}' has set 'Partitioned' and should also set 'Path=/'. This cookie may be rejected by the client.", entry.Message),
135137
]);
136138
}
137139

@@ -164,6 +166,7 @@ public void AppendPartitionedLogsWarningsForEachCookie()
164166
Partitioned = true,
165167
// Missing SameSite = SameSiteMode.None,
166168
// Missing Secure = true,
169+
Path = "/a", // Should be Path = "/",
167170
});
168171

169172
var cookieHeaderValues = headers.SetCookie;
@@ -178,8 +181,10 @@ public void AppendPartitionedLogsWarningsForEachCookie()
178181
[
179182
entry => Assert.Equal($"The cookie '{testCookie1}' has set 'Partitioned' and must also set 'Secure'. This cookie will likely be rejected by the client.", entry.Message),
180183
entry => Assert.Equal($"The cookie '{testCookie1}' has set 'Partitioned' and should also set 'SameSite=None'. This cookie will likely be rejected by the client.", entry.Message),
184+
entry => Assert.Equal($"The cookie '{testCookie1}' has set 'Partitioned' and should also set 'Path=/'. This cookie may be rejected by the client.", entry.Message),
181185
entry => Assert.Equal($"The cookie '{testCookie2}' has set 'Partitioned' and must also set 'Secure'. This cookie will likely be rejected by the client.", entry.Message),
182186
entry => Assert.Equal($"The cookie '{testCookie2}' has set 'Partitioned' and should also set 'SameSite=None'. This cookie will likely be rejected by the client.", entry.Message),
187+
entry => Assert.Equal($"The cookie '{testCookie2}' has set 'Partitioned' and should also set 'Path=/'. This cookie may be rejected by the client.", entry.Message),
183188
]);
184189
}
185190

@@ -205,13 +210,15 @@ public void AppendPartitionedCorrectlyDoesNotLog()
205210
Partitioned = true,
206211
SameSite = SameSiteMode.None,
207212
Secure = true,
213+
// Path = "/", // implied
208214
});
209215

210216
var cookieHeaderValues = headers.SetCookie;
211217
Assert.Single(cookieHeaderValues);
212218
Assert.Contains("partitioned", cookieHeaderValues[0]);
213219
Assert.Contains("secure", cookieHeaderValues[0]);
214220
Assert.Contains("samesite=none", cookieHeaderValues[0]);
221+
Assert.Contains("path=/", cookieHeaderValues[0]);
215222

216223
Assert.Empty(sink.Writes);
217224
}

0 commit comments

Comments
 (0)