Skip to content

Commit eb81294

Browse files
fix(sca): fix sbom dependency name report [backport 3.3] (#12882)
Backport dcd571b from #12875 to 3.3. The module name could be used inaccurately instead of the package name in some case for sbom reports. This PR fixes that. APPSEC-56692 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Christophe Papazian <[email protected]>
1 parent 7989724 commit eb81294

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

ddtrace/internal/packages.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ def get_module_distribution_versions(module_name: str) -> t.Optional[t.Tuple[str
7777
pkgs = get_package_distributions()
7878
while names == []:
7979
try:
80-
return (
81-
module_name,
82-
importlib_metadata.distribution(module_name).version,
83-
)
80+
package = importlib_metadata.distribution(module_name)
81+
metadata = package.metadata
82+
name = metadata["name"]
83+
version = metadata["version"]
84+
if name and version:
85+
return (name, version)
8486
except Exception: # nosec
8587
pass
8688
names = pkgs.get(module_name, [])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
SCA: This fix resolves an issue where some dependencies where reported with an inaccurate name.

tests/telemetry/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_update_imported_dependencies():
203203
import pytest
204204

205205
res = update_imported_dependencies(already_imported, [xmltodict.__name__, typing.__name__, pytest.__name__])
206-
assert len(res) == 1 # typing is stdlib so should not be in the result
206+
assert len(res) == 1, res # typing is stdlib so should not be in the result
207207
assert res[0]["name"] == "pytest"
208208
assert res[0]["version"]
209209
assert len(already_imported) == 2

0 commit comments

Comments
 (0)