Skip to content

Commit 070157f

Browse files
avara1986mabdinur
authored andcommitted
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]> (cherry picked from commit f807b8e)
1 parent a6b200a commit 070157f

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
@@ -450,8 +450,8 @@ def format_value_aspect(
450450
return format(new_text, format_spec)
451451
return format(new_text)
452452

453-
try:
454-
if format_spec:
453+
if format_spec:
454+
try:
455455
# Apply formatting
456456
text_ranges = get_tainted_ranges(new_text)
457457
if text_ranges:
@@ -467,11 +467,11 @@ def format_value_aspect(
467467
return ("{:%s}" % format_spec).format(new_text)
468468
else:
469469
return ("{:%s}" % format_spec).format(new_text)
470-
else:
471-
return format(new_text)
472-
except Exception as e:
473-
iast_propagation_error_log(f"format_value_aspect. {e}")
474-
return new_text
470+
except Exception as e:
471+
iast_propagation_error_log(f"format_value_aspect. {e}")
472+
return ("{:%s}" % format_spec).format(new_text)
473+
474+
return format(new_text)
475475

476476

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