ConvertTo-Html: assign HtmlEncode result to -Title#27103
Open
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
Open
ConvertTo-Html: assign HtmlEncode result to -Title#27103cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
Conversation
BeginProcessing called WebUtility.HtmlEncode(_title) but discarded the return value, so HTML special characters were not escaped in the document title element. Add a Pester regression test that asserts the emitted <title> matches WebUtility.HtmlEncode output.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes ConvertTo-Html -Title output so the generated <title> element is HTML-encoded, preventing special characters from breaking markup or injecting unintended content.
Changes:
- Assign the
WebUtility.HtmlEncode(_title)result back to_titleduringBeginProcessing. - Add a Pester regression test validating
<title>contains the encoded form of the input title.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs |
Ensures _title is actually HTML-encoded before being emitted into the default <title> tag. |
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 |
Adds regression coverage verifying encoded <title> output for special characters. |
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.
Summary
Ensures
-Titletext is HTML-encoded in the generated document so characters such as<,>,&, and quotes do not break the<title>element or inject unintended markup.Changes
WebUtility.HtmlEncode(_title)back to_titleinBeginProcessing.<title>content to[System.Net.WebUtility]::HtmlEncodefor the same input.Testing
Invoke-Pesterontest/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1(newItblock).