-
Notifications
You must be signed in to change notification settings - Fork 115
refactor(npm): separate lockfile 3 detector #504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
418 changes: 28 additions & 390 deletions
418
src/Microsoft.ComponentDetection.Detectors/npm/NpmComponentDetectorWithRoots.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
160 changes: 160 additions & 0 deletions
160
src/Microsoft.ComponentDetection.Detectors/npm/NpmLockfile3Detector.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Npm; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.ComponentDetection.Contracts; | ||
| using Microsoft.ComponentDetection.Contracts.TypedComponent; | ||
| using Microsoft.Extensions.Logging; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| public class NpmLockfile3Detector : NpmLockfileDetectorBase, IExperimentalDetector | ||
| { | ||
| private static readonly string NodeModules = NpmComponentUtilities.NodeModules; | ||
|
|
||
| public NpmLockfile3Detector( | ||
| IComponentStreamEnumerableFactory componentStreamEnumerableFactory, | ||
| IObservableDirectoryWalkerFactory walkerFactory, | ||
| IPathUtilityService pathUtilityService, | ||
| ILogger<NpmLockfile3Detector> logger) | ||
| : base( | ||
| componentStreamEnumerableFactory, | ||
| walkerFactory, | ||
| pathUtilityService, | ||
| logger) | ||
| { | ||
| } | ||
|
|
||
| public NpmLockfile3Detector(IPathUtilityService pathUtilityService) | ||
| : base(pathUtilityService) | ||
| { | ||
| } | ||
|
|
||
| public override string Id => "NpmLockfile3"; | ||
|
|
||
| public override int Version => 1; | ||
|
|
||
| protected override bool IsSupportedLockfileVersion(int lockfileVersion) => lockfileVersion == 3; | ||
|
|
||
| protected override JToken ResolveDependencyObject(JToken packageLockJToken) => packageLockJToken["packages"]; | ||
|
|
||
| protected override bool TryEnqueueFirstLevelDependencies( | ||
| Queue<(JProperty DependencyProperty, TypedComponent ParentComponent)> queue, | ||
| JToken dependencies, | ||
| IDictionary<string, JProperty> dependencyLookup, | ||
| TypedComponent parentComponent = null, | ||
| bool skipValidation = false) | ||
| { | ||
| if (dependencies == null) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| var isValid = true; | ||
|
|
||
| foreach (var dependency in dependencies.Cast<JProperty>()) | ||
| { | ||
| if (dependency?.Name == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var inLock = dependencyLookup.TryGetValue($"{NodeModules}/{dependency.Name}", out var dependencyProperty); | ||
| if (inLock) | ||
| { | ||
| queue.Enqueue((dependencyProperty, parentComponent)); | ||
| } | ||
| else if (skipValidation) | ||
| { | ||
| } | ||
| else | ||
| { | ||
| isValid = false; | ||
| } | ||
| } | ||
|
|
||
| return isValid; | ||
| } | ||
|
|
||
| protected override void EnqueueAllDependencies( | ||
| IDictionary<string, JProperty> dependencyLookup, | ||
| ISingleFileComponentRecorder singleFileComponentRecorder, | ||
| Queue<(JProperty CurrentSubDependency, TypedComponent ParentComponent)> subQueue, | ||
| JProperty currentDependency, | ||
| TypedComponent typedComponent) => | ||
| this.TryEnqueueFirstLevelDependenciesLockfile3( | ||
| subQueue, | ||
| currentDependency.Value["dependencies"], | ||
| dependencyLookup, | ||
| singleFileComponentRecorder, | ||
| parentComponent: typedComponent); | ||
|
|
||
| private void TryEnqueueFirstLevelDependenciesLockfile3( | ||
| Queue<(JProperty DependencyProperty, TypedComponent ParentComponent)> queue, | ||
| JToken dependencies, | ||
| IDictionary<string, JProperty> dependencyLookup, | ||
| ISingleFileComponentRecorder componentRecorder, | ||
| TypedComponent parentComponent) | ||
| { | ||
| if (dependencies == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var dependency in dependencies.Cast<JProperty>()) | ||
| { | ||
| if (dependency?.Name == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // First, check if there is an entry in the lockfile for this dependency nested in its ancestors | ||
| var ancestors = componentRecorder.DependencyGraph.GetAncestors(parentComponent.Id); | ||
| ancestors.Add(parentComponent.Id); | ||
|
|
||
| // remove version information | ||
| ancestors = ancestors.Select(x => x.Split(' ')[0]).ToList(); | ||
|
|
||
| var possibleDepPaths = ancestors | ||
| .Select((t, i) => ancestors.TakeLast(ancestors.Count - i)); // depth-first search | ||
|
|
||
| var inLock = false; | ||
| JProperty dependencyProperty; | ||
| foreach (var possibleDepPath in possibleDepPaths) | ||
| { | ||
| var ancestorNodeModulesPath = string.Format( | ||
| "{0}/{1}/{0}/{2}", | ||
| NodeModules, | ||
| string.Join($"/{NodeModules}/", possibleDepPath), | ||
| dependency.Name); | ||
|
|
||
| // Does this exist? | ||
| inLock = dependencyLookup.TryGetValue(ancestorNodeModulesPath, out dependencyProperty); | ||
|
|
||
| if (!inLock) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| this.Logger.LogDebug("Found nested dependency {Dependency} in {AncestorNodeModulesPath}", dependency.Name, ancestorNodeModulesPath); | ||
| queue.Enqueue((dependencyProperty, parentComponent)); | ||
| break; | ||
| } | ||
|
|
||
| if (inLock) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // If not, check if there is an entry in the lockfile for this dependency at the top level | ||
| inLock = dependencyLookup.TryGetValue($"{NodeModules}/{dependency.Name}", out dependencyProperty); | ||
| if (inLock) | ||
| { | ||
| queue.Enqueue((dependencyProperty, parentComponent)); | ||
| } | ||
| else | ||
| { | ||
| this.Logger.LogWarning("Could not find dependency {Dependency} in lockfile", dependency.Name); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.