Repository navigation
[release/17.0] Set Secure/HttpOnly/SameSite on the dashboard culture cookie (CodeQL SM02373) - #22040
Closed
Mitch Denny (mitchdenny) wants to merge 1 commit into
Conversation
…SM02373) (#21894) ## Description Fixes the CodeQL finding `CodeQL.SM02373` (`cs/usesecurecookiesaspnetcore`, "CookieOptionsAspNetCore use secure cookies", SFI-PS3.1.1) in `src/Aspire.Dashboard/DashboardEndpointsBuilder.cs`. The `/api/set-language` endpoint appended the request culture cookie with `new CookieOptions { Expires = ... }`, so `Secure`, `HttpOnly` and `SameSite` were all unset. The cookie is only read on the server by the request localization middleware, so it never needs to be script-readable or sent cross-site. The cookie is now written with: - `HttpOnly = true`. Nothing in `wwwroot/js` reads the culture cookie. - `SameSite = Lax`. This is the same value the dashboard experience cookie already uses (`setDashboardExperience` in `app.js`). - `Secure = true` **only when `httpContext.Request.IsHttps`**. ### Why `Secure` is conditional and not unconditional The dashboard is regularly served over plain HTTP: `http://localhost:*` during local development, and the standalone container on `http://host:18888`. Browsers silently drop `Secure` cookies set from a non-localhost HTTP origin, so an unconditional `Secure = true` would break language selection for those users. Conditional `Secure` matches the dashboard auth and antiforgery cookies (`CookieSecurePolicy.SameAsRequest`) and the experience cookie in `app.js`. `Request.IsHttps` is correct behind a TLS-terminating reverse proxy as well. When `ASPIRE_DASHBOARD_FORWARDEDHEADERS_ENABLED=true`, `UseForwardedHeaders()` (`XForwardedProto`) runs before the endpoints and rewrites the request scheme, so the cookie is `Secure`. When forwarded headers are not enabled, `X-Forwarded-Proto` is intentionally not trusted and the cookie is not `Secure`. That is the same behavior as the auth cookies. ### How this addresses the flagged pattern The flagged pattern is a `CookieOptions` that flows to `Response.Cookies.Append(...)` without `Secure` ever being set. The `CookieOptions` instance now has `Secure = true` assigned on the HTTPS path, and every other `CookieOptions` writer in the dashboard goes through ASP.NET Core `CookieBuilder`/cookie authentication (`SameAsRequest`) and was not flagged. I wrote it as an explicit `Secure = true` assignment rather than `Secure = httpContext.Request.IsHttps` so the property is assigned an unambiguous constant `true` that static analysis can recognize. I could not run Microsoft's internal CodeQL queries locally. Based on the public `cs/web/cookie-secure-not-set` query, which treats a `Secure` property that is set to anything other than constant `false` as set, I expect either form to be accepted, but the internal query may be stricter. No suppression comment is used. Other cookie writers reviewed and left unchanged: - `app.js` experience cookie: already `SameSite=Lax` and `Secure` on HTTPS. - `app-theme.js` theme cookie: written from script, not by `CookieOptions`, and not part of this alert. - Cookie authentication and antiforgery (`DashboardWebApplication.cs`): `CookieBuilder` defaults (`SameAsRequest`, `HttpOnly`), and `AspireDashboardCookieManager` delegates to `ChunkingCookieManager`. ### Validation New integration tests in `tests/Aspire.Dashboard.Tests/Integration/SetLanguageEndpointTests.cs` assert the `Set-Cookie` attributes (`HttpOnly`, `SameSite=Lax`, expiry) and `Secure` over: - plain HTTP (not `Secure`) - an HTTPS endpoint (`Secure`) - HTTP with `X-Forwarded-Proto: https` and forwarded headers enabled (`Secure`) - HTTP with `X-Forwarded-Proto: https` and forwarded headers disabled (not `Secure`) They also cover an unsupported language (400, no cookie). ```powershell dotnet test --project tests/Aspire.Dashboard.Tests/Aspire.Dashboard.Tests.csproj --no-launch-profile -- --filter-class "*.SetLanguageEndpointTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true" ``` Result: passed, 5/5. With the source change reverted, 4 of the 5 tests fail, as expected. ```powershell dotnet test --project tests/Aspire.Dashboard.Tests/Aspire.Dashboard.Tests.csproj --no-launch-profile -- --filter-namespace "Aspire.Dashboard.Tests.Integration" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true" ``` Result: passed, 186/186. The change touches the same file as #21707 (browser-token authentication handler refactor) but only the `/api/set-language` handler body, to keep conflicts minimal. ## Checklist - Is this feature complete? - [x] Yes. Ready to ship. - [ ] No. Follow-up changes expected. - Are you including unit tests for the changes and scenario tests if relevant? - [x] Yes - [ ] No - Did you add public API? - [ ] Yes - If yes, did you have an API Review for it? - [ ] Yes - [ ] No - Did you add `<remarks />` and `<code />` elements on your triple slash comments? - [ ] Yes - [ ] No - [x] No - Does the change make any security assumptions or guarantees? - [ ] Yes - If yes, have you done a threat model and had a security review? - [ ] Yes - [ ] No - [x] No Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 1a5ce09)
14 tasks
This comment has been minimized.
This comment has been minimized.
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-42-pr21894
branch
2 times, most recently
from
November 10, 2026 04:25
bc4a507 to
1dd8f0e
Compare
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22040Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22040" |
Mitch Denny (mitchdenny)
marked this pull request as ready for review
November 10, 2026 05:10
Mitch Denny (mitchdenny)
requested review from
James Newton-King (JamesNK) and
Adam Ratzman (adamint)
as code owners
November 10, 2026 05:10
Member
Author
|
Approved for inclusion in release/17.0. Do not merge yet. |
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-42-pr21894
branch
from
November 10, 2026 06:10
1dd8f0e to
c0ccb37
Compare
This comment has been minimized.
This comment has been minimized.
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-42-pr21894
branch
from
November 10, 2026 06:27
c0ccb37 to
1dd8f0e
Compare
Contributor
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-42-pr21894
branch
from
November 10, 2026 07:54
1dd8f0e to
c0ccb37
Compare
Mitch Denny (mitchdenny)
force-pushed
the
mitchdenny-backport-17-0-42-pr21894
branch
from
November 10, 2026 08:37
c0ccb37 to
1dd8f0e
Compare
Contributor
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Member
|
Closing in favor of #22067 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Backport of #21894 to
release/17.0(cherry-picked from 1a5ce09 with no modifications).Layer 42 of 55 in the release/17.0 backport stack; stacked on #22039.
Backport recommendation: Yes — Security fix (culture cookie flags) in shipped dashboard.
Backport of #21894
Checklist
<remarks />and<code />elements on your triple slash comments?