Preserve content with Get-AuthenticodeSignature#18774
Preserve content with Get-AuthenticodeSignature#18774daxian-dbw merged 2 commits intoPowerShell:masterfrom
Conversation
2b4cfd7 to
ccb78f7
Compare
Do not try and roundtrip the -Content bytes to a Unicode encoded string and then back to bytes. Instead just use the raw input bytes when validating the content signature.
ccb78f7 to
b912500
Compare
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
Any updates on this one, have been a few months with CI being green. |
| string verificationContents = Encoding.Unicode.GetString(script.OriginalEncoding.GetPreamble()) + script.ScriptContents; | ||
| signature = SignatureHelper.GetSignature(path, verificationContents); | ||
| signature = SignatureHelper.GetSignature(path, Encoding.Unicode.GetBytes(verificationContents)); |
There was a problem hiding this comment.
The existing code seems wrong here -- the OriginalEncoding could be an encoding other than Unicode, and that means Encoding.Unicode.GetBytes(verificationContents) could be different byte sequences than the original bytes from the file.
I will make a change and see if all tests still pass.
daxian-dbw
left a comment
There was a problem hiding this comment.
LGTM.
@SeeminglyScience I made a change on how we get the byte sequences for the content of a script file because the existing code seems incorrect to me. CIs still passed with my changes, but I'd like you to take a look just to make sure the changes make sense. Thanks!
| private static byte[] GetContentBytesWithBom(Encoding encoding, string scriptContent) | ||
| { | ||
| ReadOnlySpan<byte> bomBytes = encoding.Preamble; | ||
| byte[] contentBytes = encoding.GetBytes(scriptContent); |
There was a problem hiding this comment.
Unsure if this code path warrants it, but you could ArrayPool<byte>.Shared.Rent(encoding.GetMaxCharCount(scriptContent)) here to avoid some potentially LOH allocations here
There was a problem hiding this comment.
It's been using System.Text.Encoding.Unicode.GetBytes(fileContent); in GetWinTrustData method, so I guess the LOH allocation has not become a concern, but yes, it's a potential perf issue.
Yeah I think your changes make sense. It may actually solve some finickyness with encoding requirements and signing that I vaguely remember hearing about. LGTM! |
|
🎉 Handy links: |
PR Summary
Do not try and roundtrip the
-Contentbytes to a Unicode encoded string and then back to bytes. Instead just use the raw input bytes when validating the content signature.PR Context
Fixes #18773
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).