Skip to content
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

fix(iast): None as module_path at astpatch_module [backport 2.7] #8977

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ddtrace/appsec/_iast/_ast/ast_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ def _remove_flask_run(text): # type (str) -> str

def astpatch_module(module: ModuleType, remove_flask_run: bool = False) -> Tuple[str, str]:
module_name = module.__name__
module_path = str(origin(module))

module_origin = origin(module)
if module_origin is None:
log.debug("astpatch_source couldn't find the module: %s", module_name)
return "", ""

module_path = str(module_origin)
try:
if os.stat(module_path).st_size == 0:
if module_origin.stat().st_size == 0:
# Don't patch empty files like __init__.py
log.debug("empty file: %s", module_path)
return "", ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Code Security: Fixes an issue where the AST patching process fails when the origin of a module is reported as None, raising a ``FileNotFoundError``.
9 changes: 9 additions & 0 deletions tests/appsec/iast/_ast/test_ast_patching.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env python3
import logging

import astunparse
import mock
import pytest

from ddtrace.appsec._iast._ast.ast_patching import _in_python_stdlib_or_third_party
Expand Down Expand Up @@ -146,3 +149,9 @@ def test_module_should_iast_patch():
)
def test_module_in_python_stdlib_or_third_party(module_name, result):
assert _in_python_stdlib_or_third_party(module_name) == result


def test_module_path_none(caplog):
with caplog.at_level(logging.DEBUG), mock.patch("ddtrace.internal.module.Path.resolve", side_effect=AttributeError):
assert ("", "") == astpatch_module(__import__("tests.appsec.iast.fixtures.ast.str.class_str", fromlist=[None]))
assert "astpatch_source couldn't find the module: tests.appsec.iast.fixtures.ast.str.class_str" in caplog.text
Loading