Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions services/converter-api/converters/yaml_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,28 @@ def check_yaml_limits(text: str, max_depth: int = MAX_DEPTH, max_nodes: int = MA

Raises yaml.YAMLError for syntax errors, so callers can report them as usual.
"""
too_deep = YamlLimitError(f"YAML nested more than {max_depth} levels deep is not supported.")
depth = 0
nodes = 0
for event in yaml.parse(text, Loader=FAST_LOADER):
if isinstance(event, yaml.NodeEvent):
nodes += 1
if nodes > max_nodes:
raise YamlLimitError(
f"The YAML is too large: it has more than {max_nodes:,} values. "
"Make it smaller and try again."
)
if isinstance(event, yaml.CollectionStartEvent):
depth += 1
if depth > max_depth:
raise YamlLimitError(
f"The YAML is nested too deeply. Use at most {max_depth} levels of lists "
"and mappings."
)
elif isinstance(event, yaml.CollectionEndEvent):
depth -= 1
try:
for event in yaml.parse(text, Loader=FAST_LOADER):
if isinstance(event, yaml.NodeEvent):
nodes += 1
if nodes > max_nodes:
raise YamlLimitError(
f"The YAML is too large: it has more than {max_nodes:,} values. "
"Make it smaller and try again."
)
if isinstance(event, yaml.CollectionStartEvent):
depth += 1
if depth > max_depth:
raise too_deep
elif isinstance(event, yaml.CollectionEndEvent):
depth -= 1
except yaml.scanner.ScannerError as err:
if err.problem == "exceeded maximum nesting depth":
raise too_deep from err
raise


def check_node_count(data: Any, limit: int = MAX_NODES) -> None:
Expand Down
2 changes: 1 addition & 1 deletion services/converter-api/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
pytest==9.2.0
pytest-cov==7.1.0
httpx==0.28.1
ruff==0.17.5
ruff==0.17.6
6 changes: 3 additions & 3 deletions services/converter-api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fastapi==0.163.2
fastapi==0.165.0
uvicorn[standard]==0.54.0
pyyaml==6.0.3
pydantic==2.14.3
pyyaml==6.0.4
pydantic==2.14.4
2 changes: 1 addition & 1 deletion services/converter-api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_deep_nesting_is_not_a_server_error():
assert response.status_code == 200
assert response.json() == {
"valid": False,
"error": "The YAML is nested too deeply. Use at most 100 levels of lists and mappings.",
"error": "YAML nested more than 100 levels deep is not supported.",
}


Expand Down
2 changes: 1 addition & 1 deletion services/converter-api/tests/test_cfn_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_invalid_templates(text, message, line):
def test_size_and_depth_limits():
with pytest.raises(CfnError, match="longer than"):
load_template("a" * (MAX_TEMPLATE_CHARS + 1))
with pytest.raises(CfnError, match="too deeply"):
with pytest.raises(CfnError, match="levels deep is not supported"):
load_template("Resources: " + "[" * 150 + "]" * 150)
with pytest.raises(CfnError, match="too deeply"):
load_template('{"Resources": ' + "[" * 150 + "]" * 150 + "}")
Expand Down
2 changes: 1 addition & 1 deletion services/converter-api/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def test_normal_anchors_and_merge_keys_still_work():
["[" * 5000 + "]" * 5000, "- " * 5000 + "x", "a:\n" + "- " * 200 + "x"],
)
def test_deeply_nested_yaml_rejected(text):
with pytest.raises(ComposeError, match="nested too deeply"):
with pytest.raises(ComposeError, match="more than 100 levels deep is not supported"):
parse_compose(text)


Expand Down
2 changes: 1 addition & 1 deletion services/web/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r requirements.txt
pytest==9.2.0
pytest-cov==7.1.0
ruff==0.17.5
ruff==0.17.6
2 changes: 1 addition & 1 deletion services/web/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fastapi==0.163.2
fastapi==0.165.0
uvicorn[standard]==0.54.0
jinja2==3.1.6
httpx==0.28.1
Expand Down