-
Notifications
You must be signed in to change notification settings - Fork 0
security: require opt-in for automatic npm restore to avoid build-time script execution #54
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| <Project> | ||
|
|
||
| <ItemGroup> | ||
| <NpmPackageFile Condition="'$(EnableDefaultNpmPackageFile)' == '' AND Exists('$(MSBuildProjectDirectory)/package.json')" Include="$(MSBuildProjectDirectory)/package.json"/> | ||
| <NpmPackageFile Condition="'$(EnableDefaultNpmPackageFile)' == 'true' AND Exists('$(MSBuildProjectDirectory)/package.json')" Include="$(MSBuildProjectDirectory)/package.json"/> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Gating the default Useful? React with 👍 / 👎. |
||
| </ItemGroup> | ||
|
|
||
| <!-- Compute additional metadata for the NpmPackageFile items --> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1002,7 +1002,8 @@ public async Task SetTargetFramework(string propName, string version) | |||||
| [Fact] | ||||||
| public async Task NpmInstall() | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
Comment on lines
+1005
to
+1006
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve maintainability and reduce code duplication, consider creating a helper method for setting up projects that require npm. This pattern of You could add a private helper method to the private SdkProjectBuilder CreateProjectWithNpmEnabled() =>
CreateProject(SdkWebName)
.WithProperty("EnableDefaultNpmPackageFile", "true");Then, you can simplify the test setup. await using var project = CreateProjectWithNpmEnabled();
Comment on lines
+1005
to
+1006
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Extract repeated npm opt-in setup into a helper to avoid drift. The same Refactor sketch+ private SdkProjectBuilder CreateWebProjectWithDefaultNpmEnabled() =>
+ CreateProject(SdkWebName).WithProperty("EnableDefaultNpmPackageFile", "true");
- await using var project = CreateProject(SdkWebName)
- .WithProperty("EnableDefaultNpmPackageFile", "true");
+ await using var project = CreateWebProjectWithDefaultNpmEnabled();Also applies to: 1033-1034, 1084-1085, 1118-1119, 1202-1203, 1228-1229 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| project.AddFile("package.json", """ | ||||||
| { | ||||||
|
|
@@ -1029,7 +1030,8 @@ public async Task NpmInstall() | |||||
| [Fact] | ||||||
| public async Task NpmRestore() | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
|
||||||
| project.AddFile("package.json", """ | ||||||
| { | ||||||
|
|
@@ -1052,10 +1054,36 @@ public async Task NpmRestore() | |||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public async Task Npm_Dotnet_Build_sln() | ||||||
| public async Task NpmRestore_DefaultAutoDetectionDisabled() | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
|
|
||||||
| project.AddFile("package.json", """ | ||||||
| { | ||||||
| "name": "sample", | ||||||
| "version": "1.0.0", | ||||||
| "private": true, | ||||||
| "devDependencies": { | ||||||
| "is-number": "7.0.0" | ||||||
| } | ||||||
| } | ||||||
| """); | ||||||
|
|
||||||
| var result = await project | ||||||
| .AddSource("Program.cs", "Console.WriteLine();") | ||||||
| .RestoreAsync(); | ||||||
|
|
||||||
| Assert.Equal(0, result.ExitCode); | ||||||
| Assert.False(File.Exists(project.RootFolder / "package-lock.json")); | ||||||
| Assert.False(File.Exists(project.RootFolder / "node_modules" / ".npm-install-stamp")); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public async Task Npm_Dotnet_Build_sln() | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this test to the required scenario/expected naming format. Line 1082 ( Suggested rename- public async Task Npm_Dotnet_Build_sln()
+ public async Task NpmRestore_DotnetBuildSln_WithDefaultNpmPackageFileEnabled_CreatesNpmArtifacts()As per coding guidelines, "Use descriptive test names: MethodName_Scenario_ExpectedResult." 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
|
||||||
| var slnFile = project.AddFile("sample.slnx", """ | ||||||
| <Solution> | ||||||
| <Project Path="sample.csproj" /> | ||||||
|
|
@@ -1087,7 +1115,8 @@ public async Task Npm_Dotnet_Build_sln() | |||||
| [InlineData("publish")] | ||||||
| public async Task Npm_Dotnet_sln(string command) | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
|
||||||
| var slnFile = project.AddFile("sample.slnx", """ | ||||||
| <Solution> | ||||||
|
|
@@ -1170,7 +1199,8 @@ public async Task NpmRestore_MultipleFiles() | |||||
| [Fact] | ||||||
| public async Task Npm_Dotnet_Build_RestoreLockedMode_Fail() | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
|
||||||
| project.AddFile("package.json", """ | ||||||
| { | ||||||
|
|
@@ -1195,7 +1225,8 @@ public async Task Npm_Dotnet_Build_RestoreLockedMode_Fail() | |||||
| [InlineData("/p:ContinuousIntegrationBuild=true")] | ||||||
| public async Task Npm_Dotnet_Build_Ci_Success(string command) | ||||||
| { | ||||||
| await using var project = CreateProject(SdkWebName); | ||||||
| await using var project = CreateProject(SdkWebName) | ||||||
| .WithProperty("EnableDefaultNpmPackageFile", "true"); | ||||||
|
|
||||||
| project.AddFile("package.json", """ | ||||||
| { | ||||||
|
|
||||||
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.
This condition is evaluated when
Npm.targetsis imported, so the new opt-in only works ifEnableDefaultNpmPackageFileis already set before that import. In the repo's supportedSdkElementDirectoryBuildPropsmode, the SDK is injected fromDirectory.Build.props(tests/ANcpLua.Sdk.Tests/Helpers/SdkProjectBuilder.cs:329-346) and project properties are emitted later in the generated.csproj(:357-382), so.WithProperty("EnableDefaultNpmPackageFile", "true")never repopulates@(NpmPackageFile). That leaves theSdk100DirectoryBuildPropsTestsnpm cases still broken and means real projects using the Directory.Build.props import style cannot re-enable the defaultpackage.jsonbehavior from their project file.Useful? React with 👍 / 👎.