load_yaml: Support jw.include-map-no-absolute
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m5s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m30s
CI / Packaging test (pull_request) Successful in 0s

A test that redirects production includes with jw.include-map can silently
read a real production path when the map misses a spec, making the test
depend on the host's /etc without any warning.

Add a jw.include-map-no-absolute key next to the map. When true, an
absolute include spec that matched no map entry raises ValueError, naming
the spec and the including file; an identity entry whitelists an absolute
path that may be read. The key defaults to false and applies to the whole
include tree: _load_yaml() pops it in the top-level document, rejects it
in included documents, and threads it to _parse_include_spec(), where the
check runs before the normal disk lookup.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
This commit is contained in:
Jan Lindemann 2026-11-13 13:19:32 +01:00
commit 2e85f3ccf8
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -1487,6 +1487,106 @@ class TestIncludeMap:
data = load_yaml(main)
assert data['ok'] == 1
class TestIncludeMapNoAbsolute:
"""Tests for the jw.include-map-no-absolute guard."""
def test_absolute_uncovered_raises(self, temp_dir: _TempDir) -> None:
"""An absolute spec with no matching entry raises."""
base = temp_dir(
[
(
'wrapper.yaml',
'jw.include-map: []\n'
'jw.include-map-no-absolute: true\n'
'jw.include: /etc/app.yaml\n'
),
]
)
with pytest.raises(ValueError, match = 'not covered by the include map'):
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
def test_absolute_covered_ok(self, temp_dir: _TempDir) -> None:
"""An absolute spec covered by the map loads."""
base = temp_dir(
[
('app.yaml', 'ok: 1\n'),
(
'wrapper.yaml',
'jw.include-map:\n'
' - /etc/app.yaml: app.yaml\n'
'jw.include-map-no-absolute: true\n'
'jw.include: /etc/app.yaml\n'
),
]
)
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
assert data['ok'] == 1
def test_nested_absolute_uncovered_raises(self, temp_dir: _TempDir) -> None:
"""The guard covers nested includes, not just the wrapper's own."""
base = temp_dir(
[
('prod.yaml', 'jw.include: /etc/app.yaml\n'),
(
'wrapper.yaml',
'jw.include-map: []\n'
'jw.include-map-no-absolute: true\n'
'jw.include: prod.yaml\n'
),
]
)
with pytest.raises(ValueError, match = 'not covered by the include map'):
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
def test_relative_specs_pass(self, temp_dir: _TempDir) -> None:
"""Relative specs are unaffected by the guard."""
base = temp_dir(
[
('app.yaml', 'ok: 1\n'),
(
'wrapper.yaml',
'jw.include-map: []\n'
'jw.include-map-no-absolute: true\n'
'jw.include: app.yaml\n'
),
]
)
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
assert data['ok'] == 1
def test_identity_entry_whitelists(self, temp_dir: _TempDir) -> None:
"""An identity entry whitelists an absolute path."""
base = temp_dir([('real.yaml', 'ok: 1\n')])
main = pathlib.Path(base) / 'wrapper.yaml'
main.write_text(
'jw.include-map:\n'
f' - {base}/real.yaml: {base}/real.yaml\n'
'jw.include-map-no-absolute: true\n'
f'jw.include: {base}/real.yaml\n'
)
data = load_yaml(main)
assert data['ok'] == 1
def test_default_is_false(self, temp_dir: _TempDir) -> None:
"""Without the flag, an uncovered absolute spec is read from disk."""
base = temp_dir(
[('wrapper.yaml', 'jw.include-map: []\n'
'jw.include: /etc/app.yaml\n')]
)
with pytest.raises(FileNotFoundError):
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
def test_nested_flag_raises(self, temp_dir: _TempDir) -> None:
"""The flag in a non-top-level document raises."""
base = temp_dir(
[
('inner.yaml', 'jw.include-map-no-absolute: true\n'),
('wrapper.yaml', 'jw.include: inner.yaml\n'),
]
)
with pytest.raises(ValueError, match = 'top-level'):
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
# -- jw.include with target path ------------------------------------------------
class TestPathResolution: