Skip to content

Commit 1833936

Browse files
committed
Restrict local HTTP ref resolver
1 parent 9ed0742 commit 1833936

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,7 +3877,6 @@ def _get_ref_body_from_local_http_path(self, ref: str) -> dict[str, YamlValue]:
38773877
def _get_ref_body_from_url(self, ref: str) -> dict[str, YamlValue]:
38783878
"""Get reference body from a URL (HTTP, HTTPS, or file scheme)."""
38793879
if ref.startswith("file://"):
3880-
from urllib.parse import urlparse # noqa: PLC0415
38813880
from urllib.request import url2pathname # noqa: PLC0415
38823881

38833882
parsed = urlparse(ref)
@@ -3890,7 +3889,7 @@ def _get_ref_body_from_url(self, ref: str) -> dict[str, YamlValue]:
38903889
return self.remote_object_cache.get_or_put(
38913890
ref, default_factory=lambda _: load_data_from_path(file_path, self.encoding)
38923891
)
3893-
if self.http_local_ref_path is not None:
3892+
if self.http_local_ref_path is not None and urlparse(ref).scheme in {"http", "https"}:
38943893
return self._get_ref_body_from_local_http_path(ref)
38953894
return self.remote_object_cache.get_or_put(
38963895
ref, default_factory=lambda key: load_data(self._get_text_from_url(key))

tests/parser/test_jsonschema.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ def test_json_schema_ref_url_from_local_http_path_missing_file(tmp_path: Path) -
309309
parser._get_ref_body_from_url("http://example.com/schema")
310310

311311

312+
def test_json_schema_ref_url_from_local_http_path_ignores_non_http_scheme(
313+
tmp_path: Path, mocker: MockerFixture
314+
) -> None:
315+
"""Test local HTTP path resolution does not handle non-HTTP URL schemes."""
316+
parser = JsonSchemaParser("", http_local_ref_path=tmp_path)
317+
mocker.patch.object(parser, "_get_text_from_url", return_value='{"type": "object"}')
318+
local_http_path = mocker.patch.object(parser, "_get_ref_body_from_local_http_path")
319+
320+
assert parser._get_ref_body_from_url("ftp://example.com/schema.json") == {"type": "object"}
321+
local_http_path.assert_not_called()
322+
323+
312324
def test_json_schema_ref_url_from_local_http_path_symlink_escape(tmp_path: Path) -> None:
313325
"""Test local HTTP JSON schema references cannot escape the schema store through symlinks."""
314326
schema_store = tmp_path / "schemas"
@@ -318,7 +330,7 @@ def test_json_schema_ref_url_from_local_http_path_symlink_escape(tmp_path: Path)
318330
outside_schema.write_text('{"type": "object"}', encoding="utf-8")
319331
try:
320332
local_schema.symlink_to(outside_schema)
321-
except OSError as exc:
333+
except OSError as exc: # pragma: no cover
322334
pytest.skip(f"symlink creation is not supported: {exc}")
323335

324336
parser = JsonSchemaParser("", allow_remote_refs=False, http_local_ref_path=schema_store)

0 commit comments

Comments
 (0)