-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from juliendoutre/julien.doutre/npm-direct-ur…
…l-dependency [SINT-1536] Add new NPM metadata detector to catch dependencies fetched from URLs
- Loading branch information
Showing
7 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,66 @@ | ||
""" Direct URL Dependency Detector | ||
Detects if a package depends on direct URL dependencies | ||
""" | ||
from typing import Optional | ||
import re | ||
|
||
from guarddog.analyzer.metadata.detector import Detector | ||
|
||
from urllib.parse import urlparse | ||
|
||
|
||
github_project_pattern = re.compile(r"^([\w\-\.]+)/([\w\-\.]+)") | ||
|
||
|
||
class NPMDirectURLDependencyDetector(Detector): | ||
"""This heuristic detects packages with direct URL dependencies. | ||
Dependencies fetched this way are not immutable and can be used to inject untrusted code | ||
or reduce the likelihood of a reproducible install.""" | ||
|
||
def __init__(self): | ||
super().__init__( | ||
name="direct_url_dependency", | ||
description="Identify packages with direct URL dependencies. \ | ||
Dependencies fetched this way are not immutable and can be used to \ | ||
inject untrusted code or reduce the likelihood of a reproducible install.", | ||
) | ||
|
||
def detect( | ||
self, | ||
package_info, | ||
path: Optional[str] = None, | ||
name: Optional[str] = None, | ||
version: Optional[str] = None, | ||
) -> tuple[bool, str]: | ||
findings = [] | ||
|
||
for dep_name, dep_version in ( | ||
package_info.get("versions", {}) | ||
.get(version, {}) | ||
.get("dependencies", {}) | ||
.items() | ||
): | ||
# According to npm documentation, HTTP(s) and Git are accepted URL schemes when specifying dependencies: | ||
# https://docs.npmjs.com/cli/v10/configuring-npm/package-json#urls-as-dependencies | ||
# https://docs.npmjs.com/cli/v10/configuring-npm/package-json#git-urls-as-dependencies | ||
if urlparse(dep_version).scheme in [ | ||
"http", | ||
"https", | ||
"git", | ||
"git+ssh", | ||
"git+http", | ||
"git+https", | ||
"git+file", | ||
]: | ||
findings.append( | ||
f"Dependency {dep_name} refers to a direct Git or HTTP URL {dep_version}." | ||
) | ||
# According to npm documentation, Github repositories are accepted when specifying dependencies: | ||
# https://docs.npmjs.com/cli/v10/configuring-npm/package-json#github-urls | ||
elif github_project_pattern.match(dep_version): | ||
findings.append( | ||
f"Dependency {dep_name} refers to a direct Github repository {dep_version}." | ||
) | ||
|
||
return len(findings) != 0, "\n".join(findings) |
This file contains 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
This file contains 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,33 @@ | ||
from copy import deepcopy | ||
|
||
import pytest | ||
|
||
from guarddog.analyzer.metadata.npm.direct_url_dependency import ( | ||
NPMDirectURLDependencyDetector, | ||
) | ||
from tests.analyzer.metadata.resources.sample_project_info import NPM_PACKAGE_INFO | ||
|
||
|
||
class TestDirectURLDependency: | ||
npm_detector = NPMDirectURLDependencyDetector() | ||
|
||
test_data = [ | ||
("2.0.1", False), | ||
("http://asdf.com/asdf.tar.gz", True), | ||
("git+ssh://[email protected]:npm/cli.git#v1.0.27", True), | ||
("git+ssh://[email protected]:npm/cli#semver:^5.0", True), | ||
("git+https://[email protected]/npm/cli.git", True), | ||
("git://github.com/npm/cli.git#v1.0.27", True), | ||
("expressjs/express", True), | ||
("mochajs/mocha#4727d357ea", True), | ||
("user/repo#feature\/branch", True), | ||
] | ||
|
||
@pytest.mark.parametrize("version,expected_matches", test_data) | ||
def test_npm_direct_url_dependencies(self, version, expected_matches): | ||
package_info = deepcopy(NPM_PACKAGE_INFO) | ||
package_info["versions"]["2.0.0"]["dependencies"]["foo"] = version | ||
matches, _ = self.npm_detector.detect( | ||
package_info, name="", path="", version="2.0.0" | ||
) | ||
assert matches == expected_matches |
This file contains 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
This file contains 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