Skip to content

Harden SocialSample FailureMessage rendering and add READMEs into samples - #67995

Merged
cincuranet merged 2 commits into
mainfrom
copilot/harden-socialsample-handling-failuremessage
Jul 31, 2026
Merged

cincuranet merged 2 commits into
mainfrom
copilot/harden-socialsample-handling-failuremessage

Conversation

Copilot AI commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Harden SocialSample FailureMessage rendering

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

HTML-encode SocialSample FailureMessage before rendering

Description

SocialSample wrote Request.Query["FailureMessage"] directly into an HTML response. This change encodes that value before rendering so query content cannot break the page or inject markup.

  • Endpoint hardening

    • Encodes the /error endpoint's FailureMessage query value with HtmlEncoder.Default.Encode(...) before writing it into the response body.
  • Coverage

    • Adds a focused test that hosts SocialSample, calls /error with markup-bearing input, and verifies the encoded value is rendered instead of the raw string.
await response.WriteAsync(
    "An remote failure has occurred: " +
    HtmlEncoder.Default.Encode(context.Request.Query["FailureMessage"]) +
    "<br>");

Copilot AI linked an issue Jul 24, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix escaping of FailureMessage in SocialSample Harden SocialSample FailureMessage rendering Jul 24, 2026
Copilot AI requested a review from cincuranet July 24, 2026 12:01

@javiercn javiercn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We likely don't need the test for the sample

@cincuranet

Copy link
Copy Markdown
Contributor

@copilot Look around in other samples for similar missing escaping. If you find some, fix it as well.

@javiercn javiercn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I retract my comment. I didn't realize that we already have a test project for these. If that is the case, it is ok to have the test. @copilot

Copilot AI commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Look around in other samples for similar missing escaping. If you find some, fix it as well.

Addressed in a2cb944. I checked the other sample HTML responses for similar request-derived rendering and hardened two additional spots: CookiePolicySample now encodes request path/path-base values before using them in links, and HotAddSample now encodes PathBase before writing it into link/form attributes.

Copilot AI requested a review from javiercn July 24, 2026 12:45

@cincuranet cincuranet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot And add test back per #67995 (review).

Copilot AI commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot And add test back per #67995 (review).

Addressed in b3e62c5.

Copilot AI requested a review from cincuranet July 24, 2026 13:12
Copilot AI requested a review from cincuranet July 24, 2026 13:25
@cincuranet
cincuranet marked this pull request as ready for review July 29, 2026 07:39
Copilot AI review requested due to automatic review settings July 29, 2026 07:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens sample apps that render user-controlled or request-derived values into HTML by encoding those values before writing them to the response body, with added test coverage specifically for SocialSample’s /error endpoint.

Changes:

  • Encode FailureMessage (and other request/user-derived values) before rendering in SocialSample sample HTML responses.
  • Add HTML/URL encoding for rendered values in HotAddSample and HTML encoding for rendered values in CookiePolicySample.
  • Add a new integration-style test that hosts SocialSample and verifies /error renders the encoded failure message, plus a test-project reference to the sample.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Servers/HttpSys/samples/HotAddSample/Startup.cs Encodes query inputs and displayed prefixes in generated HTML; URL-encodes remove links.
src/Security/CookiePolicy/samples/CookiePolicySample/Startup.cs HTML-encodes request path/cookie key/value before rendering into HTML.
src/Security/Authentication/test/SocialSampleTests.cs Adds a focused test validating /error HTML-encodes FailureMessage.
src/Security/Authentication/test/Microsoft.AspNetCore.Authentication.Test.csproj References the SocialSample project so the new test can host it.
src/Security/Authentication/samples/SocialSample/Startup.cs HTML/URL-encodes multiple user/request-derived values, including FailureMessage.

@cincuranet
cincuranet requested review from a team as code owners July 30, 2026 07:39
@cincuranet cincuranet changed the title Harden SocialSample FailureMessage rendering Harden SocialSample FailureMessage rendering and add READMEs into samples Jul 30, 2026
@cincuranet
cincuranet force-pushed the copilot/harden-socialsample-handling-failuremessage branch 2 times, most recently from c68d1c7 to 3ee291f Compare July 31, 2026 07:40
@cincuranet
cincuranet enabled auto-merge (squash) July 31, 2026 17:38
@cincuranet

Copy link
Copy Markdown
Contributor

/ba-g Failures are unrelated.

@cincuranet
cincuranet merged commit 9f99606 into main Jul 31, 2026
25 of 27 checks passed
@cincuranet
cincuranet deleted the copilot/harden-socialsample-handling-failuremessage branch July 31, 2026 17:39
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 3, 2026
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.

Harden SocialSample's handling of FailureMessage

5 participants