diff --git a/services/converter-api/converters/yaml_safety.py b/services/converter-api/converters/yaml_safety.py index 23c74c1..ef80d48 100644 --- a/services/converter-api/converters/yaml_safety.py +++ b/services/converter-api/converters/yaml_safety.py @@ -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: diff --git a/services/converter-api/requirements-dev.txt b/services/converter-api/requirements-dev.txt index c907310..d3949e6 100644 --- a/services/converter-api/requirements-dev.txt +++ b/services/converter-api/requirements-dev.txt @@ -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 diff --git a/services/converter-api/requirements.txt b/services/converter-api/requirements.txt index 640fec2..8a0a007 100644 --- a/services/converter-api/requirements.txt +++ b/services/converter-api/requirements.txt @@ -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 diff --git a/services/converter-api/tests/test_api.py b/services/converter-api/tests/test_api.py index c85ec9d..98d8367 100644 --- a/services/converter-api/tests/test_api.py +++ b/services/converter-api/tests/test_api.py @@ -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.", } diff --git a/services/converter-api/tests/test_cfn_core.py b/services/converter-api/tests/test_cfn_core.py index cfd356e..75cc93b 100644 --- a/services/converter-api/tests/test_cfn_core.py +++ b/services/converter-api/tests/test_cfn_core.py @@ -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 + "}") diff --git a/services/converter-api/tests/test_converter.py b/services/converter-api/tests/test_converter.py index 59d6c52..c87f99e 100644 --- a/services/converter-api/tests/test_converter.py +++ b/services/converter-api/tests/test_converter.py @@ -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) diff --git a/services/web/requirements-dev.txt b/services/web/requirements-dev.txt index d163402..bf592dd 100644 --- a/services/web/requirements-dev.txt +++ b/services/web/requirements-dev.txt @@ -1,4 +1,4 @@ -r requirements.txt pytest==9.2.0 pytest-cov==7.1.0 -ruff==0.17.5 +ruff==0.17.6 diff --git a/services/web/requirements.txt b/services/web/requirements.txt index 87fc84e..e251262 100644 --- a/services/web/requirements.txt +++ b/services/web/requirements.txt @@ -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