Skip to content

Commit

Permalink
chore(asm): migrate appsec handlers to drop python2 compatibility (#7137
Browse files Browse the repository at this point in the history
)

drop python 2 and six dependency in `appsec/_handlers.py`

follows #7136

## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist

- [ ] Title is accurate.
- [ ] No unnecessary changes are introduced.
- [ ] Description motivates each change.
- [ ] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [ ] Testing strategy adequately addresses listed risk(s).
- [ ] Change is maintainable (easy to change, telemetry, documentation).
- [ ] Release note makes sense to a user of the library.
- [ ] Reviewer has explicitly 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)
- [ ] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [ ] This PR doesn't touch any of that.
  • Loading branch information
christophe-papazian authored Oct 3, 2023
1 parent 6d0b9d1 commit a20fd11
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions ddtrace/appsec/_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools
import io
import json

from six import BytesIO
from wrapt import wrap_function_wrapper as _w
from wrapt.importer import when_imported
import xmltodict
Expand All @@ -16,12 +16,6 @@
from ddtrace.internal.logger import get_logger


try:
from json import JSONDecodeError
except ImportError:
# handling python 2.X import error
JSONDecodeError = ValueError # type: ignore

log = get_logger(__name__)
_BODY_METHODS = {"POST", "PUT", "DELETE", "PATCH"}

Expand All @@ -43,7 +37,7 @@ def _on_request_span_modifier(
if not seekable:
content_length = int(environ.get("CONTENT_LENGTH", 0))
body = wsgi_input.read(content_length) if content_length else wsgi_input.read()
environ["wsgi.input"] = BytesIO(body)
environ["wsgi.input"] = io.BytesIO(body)

try:
if content_type == "application/json" or content_type == "text/json":
Expand All @@ -64,7 +58,7 @@ def _on_request_span_modifier(
RuntimeError,
TypeError,
ValueError,
JSONDecodeError,
json.JSONDecodeError,
xmltodict.expat.ExpatError,
xmltodict.ParsingInterrupted,
):
Expand All @@ -75,7 +69,7 @@ def _on_request_span_modifier(
if seekable:
wsgi_input.seek(0)
else:
environ["wsgi.input"] = BytesIO(body)
environ["wsgi.input"] = io.BytesIO(body)
return req_body


Expand Down

0 comments on commit a20fd11

Please sign in to comment.