New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Source Code Generator for PSVersionInfo class #15603
base: master
Are you sure you want to change the base?
Conversation
|
It seems test fails are not related to the PR - pwsh.exe has Preview.8 in product version instead of Preview.7. @rjmholt @TravisEz13 Could you please look the issue? |
| var result = CreatePSVersionInfoPartialClass(context); | ||
|
|
||
| // We must use specific file name suffix (*.g.cs,*.g, *.i.cs, *.generated.cs, *.designer.cs) | ||
| // so that Roslyng analizers skip the file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo. Should be - Roslyn analyzers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Fixed.
| @@ -42,6 +41,13 @@ public class PSVersionInfo | |||
|
|
|||
| private static readonly PSVersionHashTable s_psVersionTable; | |||
|
|
|||
| /* | |||
| // The property and fields are generated by PSVersionInfoGenerator source generator. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe tweak to The following property ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| @@ -42,6 +41,13 @@ public class PSVersionInfo | |||
|
|
|||
| private static readonly PSVersionHashTable s_psVersionTable; | |||
|
|
|||
| /* | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use of multiline comment seems a bit out-of-place. Consider using //
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using // raise CodeFactor issue (blank line after the comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think CodeFactor recommends using //// to comment out code specifically, which bypasses the usual warning for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xtqqczze Have you thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that for consistency we should use // and ignore the codefactor issue
cbb19d0
to
5abcd36
Compare
|
@rjmholt I resolved the issue with release tag. It turned out that ci.psm1 always assigns ReleaseTag property directly to dotnet. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
| const string SourceTemplate = @" | ||
| // <auto-generated> | ||
| // This file is auto-generated by PSVersionInfoGenerator. | ||
| // </auto-generated> | ||
|
|
||
| namespace System.Management.Automation | ||
| {{ | ||
| public static partial class PSVersionInfo | ||
| {{ | ||
| // Defined in PowerShell.Common.props as ProductVersion | ||
| // '6.0.0-beta.7 Commits: 29 SHA: 52c6b...' | ||
| internal static string ProductVersion {{ get; }} = ""{0}""; | ||
|
|
||
| // Defined in PowerShell.Common.props as | ||
| // git describe --abbrev=60 --long | ||
| // 'v6.0.0-beta.7-29-g52c6b...' | ||
| private static readonly string _rawGitCommitId = ""{1}""; | ||
|
|
||
| // Defined in PowerShell.Common.props as PSCoreBuildVersion | ||
| // '6.0.0-beta.7' | ||
| private static readonly string _mainVersion = ""{2}""; | ||
| }} | ||
| }}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tagging for @wg-security review @PaulHigin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice, the file is written to disk and it is accessible for compliance tools.
@PaulHigin Friendly ping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TravisEz13 , @iSazonov
The concern, of course, is code injection. I don't immediately see an issue, but this needs to be discussed in the next WG-Security meeting. However, a meeting may not occur until December, due to the holidays. Since this is an optional change, I feel it is not high priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WG-Security We looked into this and our main concern is that this is a potential code injection vulnerability. The version properties are all passed in as string types, and there is no validation. In addition the properties are passed in a very non-transparent manner, and unwanted injection would be difficult to detect.
We feel using something like a codedom to generate the source rather than string concatenation, is desirable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PaulHigin Could you please clarify what code you have reviewed - the source generator itself or resulting code generated by the source generator?
The SG is only used at compile time (not at runtime) and it is not distributed to end PowerShell users. I don't think it can be a security problem like you said.
The generated code contains only static strings and the file (.\src\System.Management.Automation\gen\SourceGeneratedFiles\PSVersionInfoGenerator\System.Management.Automation.Internal.Generators.PSVersionInfoGenerator\PSVersionInfo.generated.cs) is:
// <auto-generated>
// This file is auto-generated by PSVersionInfoGenerator.
// </auto-generated>
namespace System.Management.Automation
{
public static partial class PSVersionInfo
{
// Defined in PowerShell.Common.props as ProductVersion
// '6.0.0-beta.7 Commits: 29 SHA: 52c6b...'
internal static string ProductVersion { get; } = "7.2.0-preview.10 Commits: 169 SHA: 795a1ca556caa9cf90e43e63b9696b2437dc722e";
// Defined in PowerShell.Common.props as
// git describe --abbrev=60 --long
// 'v6.0.0-beta.7-29-g52c6b...'
private static readonly string _rawGitCommitId = "7.2.0-preview.10-169-g795a1ca556caa9cf90e43e63b9696b2437dc722e";
// Defined in PowerShell.Common.props as PSCoreBuildVersion
// '6.0.0-beta.7'
private static readonly string _mainVersion = "7.2.0-preview.10";
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, this is the opinion of the @WG-Security group, not just me. We looked at const string SourceTemplate where the above property values are concatenated into the source template as strings. This is a supply chain source code injection opportunity, that would be difficult to detect. Our understanding is that using a CodeDom is the preferred way to build up source code for compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PaulHigin I really don't understand about CodeDom. The best comment from .Net team I saw about using CodeDom in SGs is dotnet/roslyn#48214 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not an expert either, and I'll defer to @TravisEz13.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For code injection to happen here, there would need to be a double quote, right? Can we just reject any strings that contain a double quote?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't understand. We could implement this as TypeGen and ResGen which we already use. Only difference is the TypeGen and ResGen are our custom utilities but the source generator is .Net feature.
To do injection, someone would have to compromise the entire repository in order to corrupt both build scripts and the source code.
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@iSazonov Sorry for the delay. I am unavailable today but will look at this early next week. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
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? |
|
@iSazonov I really like you trying out the source generator!
The time it takes for Not sure if the gain worth the extra complexity though. |
It's no more complicated than our ResGen or TypeGen. It's just something new to get used to. We could replace some of our Regex with a standard .Net Regex SG to get benefits in startup scenario and other scenarios. |


PR Summary
The custom source generator allows us to exclude reflection and to directly get current PowerShell version from MSBuild.
PerfView doesn't work well on current .Net Preview. A fix has just been merged in .Net Runtime repo for this. I hope we get it in .Net 6.0 Preview6 or Preview7. So today I can not share reliable result from PerfView to confirm perf win. Obviously using static string is more faster then reflection.
PR Context
Contribute to #13540
Related #14268
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).