Skip to content

Commit f807b8e

Browse files
avara1986mabdinur
andauthored
fix(iast): invalid f-string type conversions exception in format_value_aspect (#13156)
This fixes a bug with f-strings when IAST is enabled: when building an invalid string that should raise an "Unknown format code X" exception, the error wasn't being triggered in certain cases. like ``` f"{text:05d}" ``` Should raise a ValueError("Unknown format code 'd' for object of type 'str'") ## 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: Munir Abdinur <[email protected]>
1 parent 0de8b0e commit f807b8e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

ddtrace/appsec/_iast/_taint_tracking/aspects.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ def format_value_aspect(
449449
return format(new_text, format_spec)
450450
return format(new_text)
451451

452-
try:
453-
if format_spec:
452+
if format_spec:
453+
try:
454454
# Apply formatting
455455
text_ranges = get_tainted_ranges(new_text)
456456
if text_ranges:
@@ -466,11 +466,11 @@ def format_value_aspect(
466466
return ("{:%s}" % format_spec).format(new_text)
467467
else:
468468
return ("{:%s}" % format_spec).format(new_text)
469-
else:
470-
return format(new_text)
471-
except Exception as e:
472-
iast_propagation_error_log(f"format_value_aspect. {e}")
473-
return new_text
469+
except Exception as e:
470+
iast_propagation_error_log(f"format_value_aspect. {e}")
471+
return ("{:%s}" % format_spec).format(new_text)
472+
473+
return format(new_text)
474474

475475

476476
def incremental_translation(self, incr_coder, funcode, empty):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
IAST: Fixes a bug where invalid f-strings didn’t raise the expected "Unknown format code" error when IAST was enabled.

tests/appsec/iast/aspects/test_str_py3.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from hypothesis import given
2+
from hypothesis.strategies import text
13
import pytest
24

35
from ddtrace.appsec._iast._taint_tracking import as_formatted_evidence
@@ -10,6 +12,17 @@
1012
mod_py3 = _iast_patched_module("benchmarks.bm.iast_fixtures.str_methods_py3")
1113

1214

15+
@given(text())
16+
def test_int_fstring_zero_padding_text(text):
17+
with pytest.raises(ValueError) as excinfo:
18+
f"{text:05d}"
19+
assert str(excinfo.value) == "Unknown format code 'd' for object of type 'str'"
20+
21+
with pytest.raises(ValueError) as excinfo:
22+
mod_py3.do_zero_padding_fstring(text)
23+
assert str(excinfo.value) == "Unknown format code 'd' for object of type 'str'"
24+
25+
1326
class TestOperatorsReplacement(BaseReplacement):
1427
@staticmethod
1528
def test_taint(): # type: () -> None

0 commit comments

Comments
 (0)