-
Notifications
You must be signed in to change notification settings - Fork 201
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
Import data from OpenSSF Malicious Packages #1412
Closed
shravankshenoy
wants to merge
7
commits into
aboutcode-org:main
from
shravankshenoy:feat/1409/create-openssf-importer
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e75164
Create OpenSSF importer
shravankshenoy 879a49d
Add OpenSSF importer to importers registry
shravankshenoy b2d6bf0
Create OpenSSF improver and add to improvers registry
shravankshenoy 348829c
Add test data for OpenSSF importer
shravankshenoy f199a5b
Add tests for OpenSSF importer
shravankshenoy fb0086c
Format files
shravankshenoy 17d22b0
Call repr of execption in logger
shravankshenoy 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 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,55 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# VulnerableCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/nexB/vulnerablecode for support or download. | ||
# See https://aboutcode.org for more information about nexB OSS projects. | ||
# | ||
import json | ||
import logging | ||
from pathlib import Path | ||
from typing import Iterable | ||
|
||
from vulnerabilities.importer import AdvisoryData | ||
from vulnerabilities.importer import Importer | ||
from vulnerabilities.importers.osv import parse_advisory_data | ||
from vulnerabilities.utils import get_advisory_url | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OpenSSFImporter(Importer): | ||
license_url = "https://github.com/ossf/malicious-packages/blob/main/LICENSE" | ||
spdx_license_expression = "CC-BY-4.0" | ||
url = "git+https://github.com/ossf/malicious-packages" | ||
importer_name = "OpenSSF Malacious Packages Importer" | ||
|
||
def advisory_data(self) -> Iterable[AdvisoryData]: | ||
try: | ||
supported_ecosystems = ["crates.io", "npm", "pypi", "rubygems"] | ||
self.clone(repo_url=self.url) | ||
base_path = Path(self.vcs_response.dest_dir) | ||
|
||
for supported_ecosystem in supported_ecosystems: | ||
path = base_path / "osv" / "malicious" / supported_ecosystem | ||
|
||
for file in path.glob("**/*.json"): | ||
try: | ||
with open(file) as f: | ||
json_data = json.load(f) | ||
advisory_url = get_advisory_url( | ||
file=file, | ||
base_path=base_path, | ||
url="https://github.com/ossf/malicious-packages/blob/main", | ||
) | ||
yield parse_advisory_data( | ||
json_data, | ||
supported_ecosystem=supported_ecosystem, | ||
advisory_url=advisory_url, | ||
) | ||
except Exception as e: | ||
logger.debug(f"Filepath {file} threw an Exception {type(e).__name__} {e!r}") | ||
finally: | ||
if self.vcs_response: | ||
self.vcs_response.delete() |
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
43 changes: 43 additions & 0 deletions
43
vulnerabilities/tests/test_data/openssf/openssf-data1.json
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,43 @@ | ||
{ | ||
"modified": "2023-07-19T06:30:24Z", | ||
"published": "2023-07-19T06:30:24Z", | ||
"schema_version": "1.5.0", | ||
"id": "MAL-2023-1426", | ||
"summary": "Malicious code in google-apis-androidpublisher_v2 (RubyGems)", | ||
"details": "\n---\n_-= Per source details. Do not edit below this line.=-_\n\n## Source: ossf-package-analysis (715b9e91530380e15e848bc0374f342584cdd61853308582683eb214e0da9927)\nThe OpenSSF Package Analysis project identified 'google-apis-androidpublisher_v2' @ 0.0 (rubygems) as malicious.\n\nIt is considered malicious because:\n- The package communicates with a domain associated with malicious activity.\n", | ||
"affected": [ | ||
{ | ||
"package": { | ||
"ecosystem": "RubyGems", | ||
"name": "google-apis-androidpublisher_v2" | ||
}, | ||
"versions": [ | ||
"0.0" | ||
] | ||
} | ||
], | ||
"credits": [ | ||
{ | ||
"name": "OpenSSF: Package Analysis", | ||
"type": "FINDER", | ||
"contact": [ | ||
"https://github.com/ossf/package-analysis", | ||
"https://openssf.slack.com/channels/package_analysis" | ||
] | ||
} | ||
], | ||
"database_specific": { | ||
"malicious-packages-origins": [ | ||
{ | ||
"import_time": "2023-08-10T06:16:36.932649705Z", | ||
"modified_time": "2023-07-19T06:30:24.337173817Z", | ||
"sha256": "715b9e91530380e15e848bc0374f342584cdd61853308582683eb214e0da9927", | ||
"source": "ossf-package-analysis", | ||
"versions": [ | ||
"0.0" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
vulnerabilities/tests/test_data/openssf/openssf-data1.json-expected.json
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,9 @@ | ||
{ | ||
"aliases": ["MAL-2023-1426"], | ||
"summary": "Malicious code in google-apis-androidpublisher_v2 (RubyGems)\n---\n_-= Per source details. Do not edit below this line.=-_\n\n## Source: ossf-package-analysis (715b9e91530380e15e848bc0374f342584cdd61853308582683eb214e0da9927)\nThe OpenSSF Package Analysis project identified 'google-apis-androidpublisher_v2' @ 0.0 (rubygems) as malicious.\n\nIt is considered malicious because:\n- The package communicates with a domain associated with malicious activity.", | ||
"affected_packages": [], | ||
"references": [], | ||
"date_published": "2023-07-19T06:30:24+00:00", | ||
"weaknesses": [], | ||
"url": "http://test.com" | ||
} |
43 changes: 43 additions & 0 deletions
43
vulnerabilities/tests/test_data/openssf/openssf-data2.json
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,43 @@ | ||
{ | ||
"modified": "2023-07-12T14:53:05Z", | ||
"published": "2023-07-12T14:53:05Z", | ||
"schema_version": "1.5.0", | ||
"id": "MAL-2023-1077", | ||
"summary": "Malicious code in 1password-postman-integration (npm)", | ||
"details": "\n---\n_-= Per source details. Do not edit below this line.=-_\n\n## Source: ossf-package-analysis (93be26c7bf7ce939a783f5c98c26ecd585430bdb1c993cf3f78dd213a316a058)\nThe OpenSSF Package Analysis project identified '1password-postman-integration' @ 1.0.0 (npm) as malicious.\n\nIt is considered malicious because:\n- The package communicates with a domain associated with malicious activity.\n", | ||
"affected": [ | ||
{ | ||
"package": { | ||
"ecosystem": "npm", | ||
"name": "1password-postman-integration" | ||
}, | ||
"versions": [ | ||
"1.0.0" | ||
] | ||
} | ||
], | ||
"credits": [ | ||
{ | ||
"name": "OpenSSF: Package Analysis", | ||
"type": "FINDER", | ||
"contact": [ | ||
"https://github.com/ossf/package-analysis", | ||
"https://openssf.slack.com/channels/package_analysis" | ||
] | ||
} | ||
], | ||
"database_specific": { | ||
"malicious-packages-origins": [ | ||
{ | ||
"import_time": "2023-08-10T06:16:31.337614734Z", | ||
"modified_time": "2023-07-12T14:53:05.954866979Z", | ||
"sha256": "93be26c7bf7ce939a783f5c98c26ecd585430bdb1c993cf3f78dd213a316a058", | ||
"source": "ossf-package-analysis", | ||
"versions": [ | ||
"1.0.0" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
vulnerabilities/tests/test_data/openssf/openssf-data2.json-expected.json
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,9 @@ | ||
{ | ||
"aliases": ["MAL-2023-1077"], | ||
"summary": "Malicious code in 1password-postman-integration (npm)\n---\n_-= Per source details. Do not edit below this line.=-_\n\n## Source: ossf-package-analysis (93be26c7bf7ce939a783f5c98c26ecd585430bdb1c993cf3f78dd213a316a058)\nThe OpenSSF Package Analysis project identified '1password-postman-integration' @ 1.0.0 (npm) as malicious.\n\nIt is considered malicious because:\n- The package communicates with a domain associated with malicious activity.", | ||
"affected_packages": [], | ||
"references": [], | ||
"date_published": "2023-07-12T14:53:05+00:00", | ||
"weaknesses": [], | ||
"url": "http://test.com" | ||
} |
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,39 @@ | ||
# | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# VulnerableCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/nexB/vulnerablecode for support or download. | ||
# See https://aboutcode.org for more information about nexB OSS projects. | ||
# | ||
import json | ||
import os | ||
from unittest import TestCase | ||
|
||
from vulnerabilities.importers.osv import parse_advisory_data | ||
from vulnerabilities.tests import util_tests | ||
|
||
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
TEST_DATA = os.path.join(BASE_DIR, "test_data/openssf") | ||
|
||
|
||
class TestOpenSSFImporter(TestCase): | ||
def test_to_advisories1(self): | ||
with open(os.path.join(TEST_DATA, "openssf-data1.json")) as f: | ||
mock_response = json.load(f) | ||
expected_file = os.path.join(TEST_DATA, "openssf-data1.json-expected.json") | ||
imported_data = parse_advisory_data( | ||
mock_response, "openssf", advisory_url="http://test.com" | ||
) | ||
result = imported_data.to_dict() | ||
util_tests.check_results_against_json(result, expected_file) | ||
|
||
def test_to_advisories2(self): | ||
with open(os.path.join(TEST_DATA, "openssf-data2.json")) as f: | ||
mock_response = json.load(f) | ||
expected_file = os.path.join(TEST_DATA, "openssf-data2.json-expected.json") | ||
imported_data = parse_advisory_data( | ||
mock_response, "openssf", advisory_url="http://test.com" | ||
) | ||
result = imported_data.to_dict() | ||
util_tests.check_results_against_json(result, expected_file) |
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.
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.
Should this be used as an alias? What other options we have?
cc @pombredanne
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.
@TG1999 yes, as an alias sounds fine