Skip to content

feat(appsec): enable request blocking #630

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

florentinl
Copy link
Contributor

@florentinl florentinl commented Jul 10, 2025

What does this PR do?

This PR enables Appsec to block request if they are deemed dangerous by the in-app WAF.

There are three cases:

  • Blocking before executing the wrapped function, after calling asm_start_request
  • Blocking during execution of the wrapped function, by raising a BlockingException
  • Blocking after execution of the function, after calling asm_start_response

Motivation

Expand the capabilities of Appsec inside the datadog-lambda-python layer.

Testing Guidelines

I added unit tests to both test_asm.py and test_wrapper.py to ensure requests are blocked only if necessary and at the right stage.

Integration tests will be run as part of the system-test, APPSEC_BLOCKING scenario.

Bug Fix

The appsec waf expects the raw URI to contain the query parameters. This is an omission from the previous Appsec PRs. The fix is included in this one.

Some rules in the system-tests use the presence of query parameters in the URI to block requests. This is therefore required to pass the APPSEC_BLOCKING test suite of the system tests which this PR relates to.

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@florentinl florentinl requested review from a team as code owners July 10, 2025 09:27
@florentinl florentinl marked this pull request as draft July 10, 2025 09:27
@florentinl florentinl marked this pull request as ready for review July 10, 2025 09:33
Copy link
Contributor

@purple4reina purple4reina left a comment

Choose a reason for hiding this comment

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

One small issue to fix.

@florentinl florentinl force-pushed the florentinl/APPSEC-58224/block-requests branch 2 times, most recently from c3fb11f to 06c1e34 Compare July 11, 2025 12:10
@florentinl florentinl force-pushed the florentinl/APPSEC-58224/block-requests branch from 06c1e34 to cba994e Compare July 17, 2025 07:27
@florentinl florentinl requested a review from a team July 17, 2025 08:04
@@ -126,6 +130,14 @@ def asm_start_request(
span.set_tag_str("http.client_ip", request_ip)
span.set_tag_str("network.client.ip", request_ip)

# Encode the parsed query and append it to reconstruct the original raw URI expected by AppSec.
if parsed_query:
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 Jul 18, 2025

Choose a reason for hiding this comment

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

Could you please explain a bit in the PR description how this part is related? Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Bug Fix

The appsec waf expects the raw URI to contain the query parameters. This is an omission from the previous Appsec PRs. The fix is included in this one.

Some rules in the system-tests use the presence of query parameters in the URI to block requests. This is therefore required to pass the APPSEC_BLOCKING test suite of the system tests which this PR relates to.

@@ -155,12 +162,21 @@ def __init__(self, func):
except Exception as e:
logger.error(format_err_with_traceback(e))

def _get_blocking_response(self):
if not config.appsec_enabled:
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 Jul 18, 2025

Choose a reason for hiding this comment

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

I think we might not need this method at all. In its 3 usages, 2 actually have the config.appsec_enabled right before calling it anyway. The only place left is in the BlockingException case, which implies that appsec already enabled? Or we just add the config.appsec_enabled check there?
in short, I think we can just call get_asm_blocked_response(self.event_source) directly in the 3 usages so far to avoid some double if statements..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it makes total sense to avoid checking it twice.
For the BlockingException case, it can only be raised when the ASM context is set therefore only when appsec is already enabled. No need to do the check in this case either.

Comment on lines 13 to 18
from datadog_lambda.asm import (
asm_set_context,
asm_start_response,
asm_start_request,
get_asm_blocked_response,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from datadog_lambda.asm import (
asm_set_context,
asm_start_response,
asm_start_request,
get_asm_blocked_response,
)
if config.appsec_enabled:
from datadog_lambda.asm import (
asm_set_context,
asm_start_response,
asm_start_request,
get_asm_blocked_response,
)

Add move it below from datadog_lambda.config import config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. This also implies some changes in the test_wapper.py where I have to reload the import to test appsec.

blocked = get_blocked()
if not blocked:
return None
set_blocked(blocked)
Copy link
Contributor

Choose a reason for hiding this comment

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

i need some help understanding this part. Since we already got the blocked above, does the set_blocked have any effect here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that calling set_blocked added the expected content-type calculated from the accept headers gathered by Appsec. In fact it is already done by the WAF and unnecessary outside of certain frameworks.
I removed the set_blocked line as it is useless.

@@ -199,6 +208,30 @@
),
]

ASM_BLOCKED_RESPONSE_TEST_CASES = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Claude gave me these following cases below. Could you add them here if they make sense? Thank you.

 # Default blocking configuration
  {
      "status_code": 403,
      "type": "auto",
      "content-type": "application/json"  # or "text/html" based on Accept header
  }

  # HTML blocking response
  {
      "status_code": 403,
      "type": "html",
      "content-type": "text/html"
  }

  # Redirect blocking
  {
      "status_code": 403,
      "type": "none",
      "location": "https://example.com/blocked"
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I cleaned up the test cases by adding yours and removing duplicates.

@florentinl florentinl force-pushed the florentinl/APPSEC-58224/block-requests branch from cba994e to 29bb61d Compare July 18, 2025 15:05
@florentinl florentinl requested a review from joeyzhao2018 July 18, 2025 15:23
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

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

LGTM

@florentinl florentinl merged commit c7739eb into main Jul 21, 2025
61 checks passed
@florentinl florentinl deleted the florentinl/APPSEC-58224/block-requests branch July 21, 2025 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants