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

chore(iast): create a context guard for pausing Python errors #10755

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

juanjux
Copy link
Collaborator

@juanjux juanjux commented Sep 23, 2024

Description

Create a new PythonErrorGuard RAII object (defined in Utils/PythonErrorGuard.h) that will pause and store the current error on creation and restore it when it goes out of context, allowing to import or call Python functions. It also has members for getting the error and traceback as std::string or py::str.

Example usage:

#include "Utils/PythonErrorGuard.h"

void some_func() 
{
    // operations that could set an error
    {
        PythonErrorGuard error_guard; // The error is stored and removed here
        auto split = safe_import("os.path", "split"); // We can now call Python stuff
        auto res = split("/foo/bar/naz");

        // Bonus: print the errors.
        if (error_guard.has_error()) {
            py::print(error_guard.error_as_pystr());
            py::print(error_guard.traceback_as_pystr());
            cerr << error_guard.error_as_stdstring() << endl;
            cerr << error_guard.traceback_as_stdstring() << endl;
        }
    } --> Out of context, the error is restored.
}

Bonus: a memleak on the first exception error handler of iast_taint_log_error should be fixed now.

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

Signed-off-by: Juanjo Alvarez <juanjo.alvarezmartinez@datadoghq.com>
@juanjux juanjux added changelog/no-changelog A changelog entry is not required for this PR. ASM Application Security Monitoring labels Sep 23, 2024
@juanjux juanjux self-assigned this Sep 23, 2024
@juanjux juanjux requested a review from a team as a code owner September 23, 2024 10:01
Copy link
Contributor

CODEOWNERS have been resolved as:

ddtrace/appsec/_iast/_taint_tracking/Utils/PythonErrorGuard.cpp         @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/Utils/PythonErrorGuard.h           @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/Aspects/Helpers.cpp                @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/Aspects/Helpers.h                  @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/Utils/GenericUtils.cpp             @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/Utils/GenericUtils.h               @DataDog/asm-python
ddtrace/appsec/_iast/_taint_tracking/tests/test_helpers.cpp             @DataDog/asm-python

Signed-off-by: Juanjo Alvarez <juanjo.alvarezmartinez@datadoghq.com>
Signed-off-by: Juanjo Alvarez <juanjo.alvarezmartinez@datadoghq.com>
@pr-commenter
Copy link

pr-commenter bot commented Sep 23, 2024

Benchmarks

Benchmark execution time: 2024-09-23 19:55:47

Comparing candidate commit aabbf9c in PR branch juanjux/APPSEC-54990-python-error-guard-native with baseline commit 5546c23 in branch main.

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

Copy link
Member

@avara1986 avara1986 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASM Application Security Monitoring changelog/no-changelog A changelog entry is not required for this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants