Skip to content

[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
mitchdenny-backport-17-0-41-pr21952from
mitchdenny-backport-17-0-42-pr21894
Closed

Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-41-pr21952from
mitchdenny-backport-17-0-42-pr21894

Conversation

@mitchdenny

Copy link
Copy Markdown
Member

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

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • 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
    • 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
    • No

…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)
@github-actions

This comment has been minimized.

@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-42-pr21894 branch 2 times, most recently from bc4a507 to 1dd8f0e Compare November 10, 2026 04:25
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22040

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22040"

@mitchdenny

Copy link
Copy Markdown
Member Author

Approved for inclusion in release/17.0. Do not merge yet.

@github-actions

This comment has been minimized.

@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-42-pr21894 branch from c0ccb37 to 1dd8f0e Compare November 10, 2026 06:27
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-42-pr21894 branch from 1dd8f0e to c0ccb37 Compare November 10, 2026 07:54
@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-42-pr21894 branch from c0ccb37 to 1dd8f0e Compare November 10, 2026 08:37
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@eerhardt

Copy link
Copy Markdown
Member

Closing in favor of #22067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants