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): add code to filter out ddtrace stuff from dir() on patched modules #11490

Merged
merged 33 commits into from
Nov 22, 2024

Conversation

juanjux
Copy link
Collaborator

@juanjux juanjux commented Nov 21, 2024

Signed-off-by: Juanjo Alvarez [email protected]

Description

While testing on dd-source CI we found an issue where a module was doing a dir(other_module) and the changed results from the patched module were breaking stuff (because our patching would add ddtrace_aspects, ddtrace_sink_points, et cetera to the results and the original module was expecting all other_module symbols to have some members like id).

This PRs fixes this problem by:

  • Creating a custom __dir__ function (that will override any pre-existing ones) removing from the results all the symbols that we add ourselves while patching.
  • Renaming all added ddtrace symbols to __ddtrace.

Also:

  • Adds a _DD_IAST_NO_DIR_PATCH config var to disable the wrapping of the patched module __dir__ functions in case the user have some side-effect problem.
  • The return type of visit_ast has been fixed (it wrongly was str while is in fact a ast.Module type).

Checklist

  • 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
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • 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 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

@juanjux juanjux self-assigned this Nov 21, 2024
@juanjux juanjux requested review from a team as code owners November 21, 2024 16:40
Copy link
Contributor

github-actions bot commented Nov 21, 2024

CODEOWNERS have been resolved as:

releasenotes/notes/code-security-patch-dir-54cc85f18e31f45c.yaml        @DataDog/apm-python
tests/appsec/iast/fixtures/ast/other/with_implemented_dir.py            @DataDog/asm-python
tests/appsec/iast/fixtures/ast/other/without_implemented_dir.py         @DataDog/asm-python
ddtrace/appsec/_constants.py                                            @DataDog/asm-python
ddtrace/appsec/_iast/_ast/ast_patching.py                               @DataDog/asm-python
ddtrace/appsec/_iast/_ast/visitor.py                                    @DataDog/asm-python
ddtrace/appsec/_iast/_loader.py                                         @DataDog/asm-python
tests/appsec/appsec_utils.py                                            @DataDog/asm-python
tests/appsec/iast/_ast/test_ast_patching.py                             @DataDog/asm-python
tests/appsec/iast/aspects/test_add_aspect_fixtures.py                   @DataDog/asm-python
tests/appsec/iast/aspects/test_add_inplace_aspect_fixtures.py           @DataDog/asm-python
tests/appsec/iast_packages/inside_env_runner.py                         @DataDog/asm-python

Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
@pr-commenter
Copy link

pr-commenter bot commented Nov 21, 2024

Benchmarks

Benchmark execution time: 2024-11-22 13:00:35

Comparing candidate commit 7d3dbd2 in PR branch juanjux/APPSEC-55939-fix-patched-dir with baseline commit a664aab in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 388 metrics, 2 unstable metrics.

Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
Signed-off-by: Juanjo Alvarez <[email protected]>
@juanjux juanjux merged commit a9a6ad7 into main Nov 22, 2024
200 of 201 checks passed
@juanjux juanjux deleted the juanjux/APPSEC-55939-fix-patched-dir branch November 22, 2024 15:43
Copy link
Contributor

The backport to 2.15 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.15 2.15
# Navigate to the new working tree
cd .worktrees/backport-2.15
# Create a new branch
git switch --create backport-11490-to-2.15
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 a9a6ad712e37e832d0e43a0c9bf6541f7bded8d4
# Push it to GitHub
git push --set-upstream origin backport-11490-to-2.15
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.15

Then, create a pull request where the base branch is 2.15 and the compare/head branch is backport-11490-to-2.15.

github-actions bot pushed a commit that referenced this pull request Nov 22, 2024
… modules (#11490)

Signed-off-by: Juanjo Alvarez <[email protected]>

## Description

While testing on dd-source CI we found an issue where a module was doing
a `dir(other_module)` and the changed results from the patched module
were breaking stuff (because our patching would add `ddtrace_aspects`,
`ddtrace_sink_points`, et cetera to the results and the original module
was expecting all `other_module` symbols to have some members like
`id`).

This PRs fixes this problem by:

- Creating a custom `__dir__` function (that will override any
pre-existing ones) removing from the results all the symbols that we add
ourselves while patching.
- Renaming all added `ddtrace` symbols to `__ddtrace`.

Also:
- Adds a `_DD_IAST_NO_DIR_PATCH` config var to disable the wrapping of
the patched module `__dir__` functions in case the user have some
side-effect problem.
- The return type of `visit_ast` has been fixed (it wrongly was `str`
while is in fact a `ast.Module` type).

## 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)

---------

Signed-off-by: Juanjo Alvarez <[email protected]>
(cherry picked from commit a9a6ad7)
github-actions bot pushed a commit that referenced this pull request Nov 22, 2024
… modules (#11490)

Signed-off-by: Juanjo Alvarez <[email protected]>

## Description

While testing on dd-source CI we found an issue where a module was doing
a `dir(other_module)` and the changed results from the patched module
were breaking stuff (because our patching would add `ddtrace_aspects`,
`ddtrace_sink_points`, et cetera to the results and the original module
was expecting all `other_module` symbols to have some members like
`id`).

This PRs fixes this problem by:

- Creating a custom `__dir__` function (that will override any
pre-existing ones) removing from the results all the symbols that we add
ourselves while patching.
- Renaming all added `ddtrace` symbols to `__ddtrace`.

Also:
- Adds a `_DD_IAST_NO_DIR_PATCH` config var to disable the wrapping of
the patched module `__dir__` functions in case the user have some
side-effect problem.
- The return type of `visit_ast` has been fixed (it wrongly was `str`
while is in fact a `ast.Module` type).

## 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)

---------

Signed-off-by: Juanjo Alvarez <[email protected]>
(cherry picked from commit a9a6ad7)
juanjux added a commit that referenced this pull request Nov 25, 2024
… modules (#11490)

Signed-off-by: Juanjo Alvarez <[email protected]>

While testing on dd-source CI we found an issue where a module was doing
a `dir(other_module)` and the changed results from the patched module
were breaking stuff (because our patching would add `ddtrace_aspects`,
`ddtrace_sink_points`, et cetera to the results and the original module
was expecting all `other_module` symbols to have some members like
`id`).

This PRs fixes this problem by:

- Creating a custom `__dir__` function (that will override any
pre-existing ones) removing from the results all the symbols that we add
ourselves while patching.
- Renaming all added `ddtrace` symbols to `__ddtrace`.

Also:
- Adds a `_DD_IAST_NO_DIR_PATCH` config var to disable the wrapping of
the patched module `__dir__` functions in case the user have some
side-effect problem.
- The return type of `visit_ast` has been fixed (it wrongly was `str`
while is in fact a `ast.Module` type).

- [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))

- [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)

---------

Signed-off-by: Juanjo Alvarez <[email protected]>
(cherry picked from commit a9a6ad7)
juanjux added a commit that referenced this pull request Nov 25, 2024
… modules [backport 2.17] (#11504)

Backport a9a6ad7 from #11490 to 2.17.

Signed-off-by: Juanjo Alvarez <[email protected]>

## Description

While testing on dd-source CI we found an issue where a module was doing
a `dir(other_module)` and the changed results from the patched module
were breaking stuff (because our patching would add `ddtrace_aspects`,
`ddtrace_sink_points`, et cetera to the results and the original module
was expecting all `other_module` symbols to have some members like
`id`).

This PRs fixes this problem by:

- Creating a custom `__dir__` function (that will override any
pre-existing ones) removing from the results all the symbols that we add
ourselves while patching.
- Renaming all added `ddtrace` symbols to `__ddtrace`.

Also:
- Adds a `_DD_IAST_NO_DIR_PATCH` config var to disable the wrapping of
the patched module `__dir__` functions in case the user have some
side-effect problem.
- The return type of `visit_ast` has been fixed (it wrongly was `str`
while is in fact a `ast.Module` type).

## 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: Juanjo Alvarez Martinez <[email protected]>
juanjux added a commit that referenced this pull request Nov 25, 2024
… modules [backport 2.15] (#11521)

Backport
a9a6ad7
from #11490 to 2.15.

Signed-off-by: Juanjo Alvarez
[[email protected]](mailto:[email protected])

## Description
While testing on dd-source CI we found an issue where a module was doing
a dir(other_module) and the changed results from the patched module were
breaking stuff (because our patching would add ddtrace_aspects,
ddtrace_sink_points, et cetera to the results and the original module
was expecting all other_module symbols to have some members like id).

This PRs fixes this problem by:

Creating a custom __dir__ function (that will override any pre-existing
ones) removing from the results all the symbols that we add ourselves
while patching.
Renaming all added ddtrace symbols to __ddtrace.
Also:

Adds a _DD_IAST_NO_DIR_PATCH config var to disable the wrapping of the
patched module __dir__ functions in case the user have some side-effect
problem.
The return type of visit_ast has been fixed (it wrongly was str while is
in fact a ast.Module type).

## 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)

---------

Signed-off-by: Juanjo Alvarez <[email protected]>
juanjux added a commit that referenced this pull request Nov 25, 2024
… modules [backport 2.16] (#11503)

Backport a9a6ad7 from #11490 to 2.16.

Signed-off-by: Juanjo Alvarez <[email protected]>

## Description

While testing on dd-source CI we found an issue where a module was doing
a `dir(other_module)` and the changed results from the patched module
were breaking stuff (because our patching would add `ddtrace_aspects`,
`ddtrace_sink_points`, et cetera to the results and the original module
was expecting all `other_module` symbols to have some members like
`id`).

This PRs fixes this problem by:

- Creating a custom `__dir__` function (that will override any
pre-existing ones) removing from the results all the symbols that we add
ourselves while patching.
- Renaming all added `ddtrace` symbols to `__ddtrace`.

Also:
- Adds a `_DD_IAST_NO_DIR_PATCH` config var to disable the wrapping of
the patched module `__dir__` functions in case the user have some
side-effect problem.
- The return type of `visit_ast` has been fixed (it wrongly was `str`
while is in fact a `ast.Module` type).

## 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: Juanjo Alvarez Martinez <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants