Skip to content

Commit ef9a366

Browse files
author
John Luo
authored
Also set no-store when we set no-cache in response cache control headers (#22842)
1 parent 2077081 commit ef9a366

File tree

10 files changed

+23
-17
lines changed

10 files changed

+23
-17
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ private RequestDelegate BuildErrorPageApplication(Exception exception)
187187
return context =>
188188
{
189189
context.Response.StatusCode = 500;
190-
context.Response.Headers[HeaderNames.CacheControl] = "no-cache";
190+
context.Response.Headers[HeaderNames.CacheControl] = "no-cache,no-store";
191+
context.Response.Headers[HeaderNames.Pragma] = "no-cache";
191192
context.Response.ContentType = "text/html; charset=utf-8";
192193
return errorPage.ExecuteAsync(context);
193194
};

src/Hosting/Hosting/src/Internal/WebHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ private RequestDelegate BuildApplication()
284284
return context =>
285285
{
286286
context.Response.StatusCode = 500;
287-
context.Response.Headers[HeaderNames.CacheControl] = "no-cache";
287+
context.Response.Headers[HeaderNames.CacheControl] = "no-cache,no-store";
288+
context.Response.Headers[HeaderNames.Pragma] = "no-cache";
288289
return errorPage.ExecuteAsync(context);
289290
};
290291
}

src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class MigrationsEndPointMiddleware
2828
/// <param name="logger">The <see cref="Logger{T}"/> to write messages to.</param>
2929
/// <param name="options">The options to control the behavior of the middleware.</param>
3030
public MigrationsEndPointMiddleware(
31-
RequestDelegate next,
32-
ILogger<MigrationsEndPointMiddleware> logger,
31+
RequestDelegate next,
32+
ILogger<MigrationsEndPointMiddleware> logger,
3333
IOptions<MigrationsEndPointOptions> options)
3434
{
3535
if (next == null)
@@ -80,7 +80,7 @@ public virtual async Task Invoke(HttpContext context)
8080

8181
context.Response.StatusCode = (int)HttpStatusCode.NoContent;
8282
context.Response.Headers.Add("Pragma", new[] { "no-cache" });
83-
context.Response.Headers.Add("Cache-Control", new[] { "no-cache" });
83+
context.Response.Headers.Add("Cache-Control", new[] { "no-cache,no-store" });
8484

8585
_logger.MigrationsApplied(db.GetType().FullName);
8686
}
@@ -147,7 +147,7 @@ private static async Task WriteErrorToResponse(HttpResponse response, string err
147147
{
148148
response.StatusCode = (int)HttpStatusCode.BadRequest;
149149
response.Headers.Add("Pragma", new[] { "no-cache" });
150-
response.Headers.Add("Cache-Control", new[] { "no-cache" });
150+
response.Headers.Add("Cache-Control", new[] { "no-cache,no-store" });
151151
response.ContentType = "text/plain";
152152

153153
// Padding to >512 to ensure IE doesn't hide the message

src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private static void ClearHttpContext(HttpContext context)
153153
private static Task ClearCacheHeaders(object state)
154154
{
155155
var headers = ((HttpResponse)state).Headers;
156-
headers[HeaderNames.CacheControl] = "no-cache";
156+
headers[HeaderNames.CacheControl] = "no-cache,no-store";
157157
headers[HeaderNames.Pragma] = "no-cache";
158158
headers[HeaderNames.Expires] = "-1";
159159
headers.Remove(HeaderNames.ETag);

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ public async Task ClearsResponseBuffer_BeforeRequestIsReexecuted()
164164
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
165165
Assert.Equal(expectedResponseBody, await response.Content.ReadAsStringAsync());
166166
IEnumerable<string> values;
167-
Assert.True(response.Headers.TryGetValues("Cache-Control", out values));
168-
Assert.Single(values);
169-
Assert.Equal("no-cache", values.First());
167+
Assert.True(response.Headers.CacheControl.NoCache);
168+
Assert.True(response.Headers.CacheControl.NoStore);
170169
Assert.True(response.Headers.TryGetValues("Pragma", out values));
171170
Assert.Single(values);
172171
Assert.Equal("no-cache", values.First());
@@ -214,9 +213,8 @@ public async Task ClearsCacheHeaders_SetByReexecutionPathHandlers()
214213
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
215214
Assert.Equal(expectedResponseBody, await response.Content.ReadAsStringAsync());
216215
IEnumerable<string> values;
217-
Assert.True(response.Headers.TryGetValues("Cache-Control", out values));
218-
Assert.Single(values);
219-
Assert.Equal("no-cache", values.First());
216+
Assert.True(response.Headers.CacheControl.NoCache);
217+
Assert.True(response.Headers.CacheControl.NoStore);
220218
Assert.True(response.Headers.TryGetValues("Pragma", out values));
221219
Assert.Single(values);
222220
Assert.Equal("no-cache", values.First());

src/Middleware/Session/src/SessionMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private void SetCookie()
161161
response.Cookies.Append(_options.Cookie.Name, _cookieValue, cookieOptions);
162162

163163
var responseHeaders = response.Headers;
164-
responseHeaders[HeaderNames.CacheControl] = "no-cache";
164+
responseHeaders[HeaderNames.CacheControl] = "no-cache,no-store";
165165
responseHeaders[HeaderNames.Pragma] = "no-cache";
166166
responseHeaders[HeaderNames.Expires] = "-1";
167167
}

src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.AspNetCore.Authentication.Cookies
1717
public class CookieAuthenticationHandler : SignInAuthenticationHandler<CookieAuthenticationOptions>
1818
{
1919
private const string HeaderValueNoCache = "no-cache";
20+
private const string HeaderValueNoCacheNoStore = "no-cache,no-store";
2021
private const string HeaderValueEpocDate = "Thu, 01 Jan 1970 00:00:00 GMT";
2122
private const string SessionIdClaim = "Microsoft.AspNetCore.Authentication.Cookies-SessionId";
2223

@@ -374,7 +375,7 @@ protected async override Task HandleSignOutAsync(AuthenticationProperties proper
374375

375376
private async Task ApplyHeaders(bool shouldRedirectToReturnUrl, AuthenticationProperties properties)
376377
{
377-
Response.Headers[HeaderNames.CacheControl] = HeaderValueNoCache;
378+
Response.Headers[HeaderNames.CacheControl] = HeaderValueNoCacheNoStore;
378379
Response.Headers[HeaderNames.Pragma] = HeaderValueNoCache;
379380
Response.Headers[HeaderNames.Expires] = HeaderValueEpocDate;
380381

src/Security/Authentication/test/CookieTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public async Task SignInCausesDefaultCookieToBeCreated()
138138
Assert.DoesNotContain("; expires=", setCookie);
139139
Assert.DoesNotContain("; domain=", setCookie);
140140
Assert.DoesNotContain("; secure", setCookie);
141+
Assert.True(transaction.Response.Headers.CacheControl.NoCache);
142+
Assert.True(transaction.Response.Headers.CacheControl.NoStore);
143+
Assert.Equal("no-cache", transaction.Response.Headers.Pragma.ToString());
141144
}
142145

143146
[Fact]

src/SignalR/common/Http.Connections/src/Internal/Transports/ServerSentEventsServerTransport.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public ServerSentEventsServerTransport(PipeReader application, string connection
3535
public async Task ProcessRequestAsync(HttpContext context, CancellationToken token)
3636
{
3737
context.Response.ContentType = "text/event-stream";
38-
context.Response.Headers[HeaderNames.CacheControl] = "no-cache";
38+
context.Response.Headers[HeaderNames.CacheControl] = "no-cache,no-store";
39+
context.Response.Headers[HeaderNames.Pragma] = "no-cache";
3940

4041
// Make sure we disable all response buffering for SSE
4142
var bufferingFeature = context.Features.Get<IHttpResponseBodyFeature>();

src/SignalR/common/Http.Connections/test/ServerSentEventsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public async Task SSESetsContentType()
3131
await sse.ProcessRequestAsync(context, context.RequestAborted);
3232

3333
Assert.Equal("text/event-stream", context.Response.ContentType);
34-
Assert.Equal("no-cache", context.Response.Headers["Cache-Control"]);
34+
Assert.Equal("no-cache,no-store", context.Response.Headers["Cache-Control"]);
35+
Assert.Equal("no-cache", context.Response.Headers["Pragma"]);
3536
}
3637
}
3738

0 commit comments

Comments
 (0)