load_yaml: Support jw.include-map-no-absolute
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:
parent
5d94a1fcee
commit
2e85f3ccf8
1 changed files with 133 additions and 8 deletions
|
|
@ -31,6 +31,13 @@ supports three extensions:
|
|||
values may reference environment variables as ``$(VAR)`` or
|
||||
``${VAR}``. Only the top-level document may declare
|
||||
``jw.include-map``.
|
||||
- ``jw.include-map-no-absolute`` — when true, an absolute include
|
||||
spec that matched no map entry raises ValueError, naming the
|
||||
spec and the file, guarding a test against silently reading a
|
||||
real production path. An identity entry (LHS and RHS the
|
||||
same) whitelists a path that may be read. The key defaults to
|
||||
false and applies to the whole include tree; only the
|
||||
top-level document may set it.
|
||||
- ``jw.link`` — replaces a node with the value found at a dot-separated
|
||||
target path. A prefix before the first ``|`` names the resulting
|
||||
key instead of replacing the node in place.
|
||||
|
|
@ -323,6 +330,7 @@ def _parse_include_spec(
|
|||
path: Path,
|
||||
include_map: IncludeMap,
|
||||
expand_vars: bool,
|
||||
no_absolute: bool,
|
||||
) -> list[IncludeSpec]:
|
||||
"""Parse a jw.include spec into one or more IncludeSpec entries.
|
||||
|
||||
|
|
@ -333,7 +341,9 @@ def _parse_include_spec(
|
|||
*include_map* before resolution; the rewritten specs are resolved
|
||||
and glob-expanded by the normal pipeline. When *expand_vars* is
|
||||
true (the top-level document only), $(VAR) and ${VAR} references
|
||||
in the file part are expanded from the environment first.
|
||||
in the file part are expanded from the environment first. When
|
||||
*no_absolute* is true, an absolute spec that matched no entry
|
||||
raises ValueError instead of being read from disk.
|
||||
"""
|
||||
if not spec:
|
||||
raise ValueError('Empty include spec')
|
||||
|
|
@ -354,6 +364,11 @@ def _parse_include_spec(
|
|||
qualified = _canonicalize_path(spec, path.parent)
|
||||
rewrites = _apply_include_map(qualified, include_map)
|
||||
if rewrites is None:
|
||||
if no_absolute and spec.startswith('/'):
|
||||
raise ValueError(
|
||||
f'jw.include-map-no-absolute: absolute include '
|
||||
f'{spec!r} in {path} is not covered by the include map'
|
||||
)
|
||||
rewrites = [qualified]
|
||||
paths: list[Path] = []
|
||||
for rewrite in rewrites:
|
||||
|
|
@ -367,6 +382,7 @@ def _normalize_include_specs(
|
|||
path: Path,
|
||||
include_map: IncludeMap,
|
||||
expand_vars: bool,
|
||||
no_absolute: bool,
|
||||
) -> Includes:
|
||||
if specs is None:
|
||||
return []
|
||||
|
|
@ -378,7 +394,9 @@ def _normalize_include_specs(
|
|||
for spec in specs:
|
||||
if not isinstance(spec, str):
|
||||
raise TypeError(f'Unsupported include entry: {spec!r}')
|
||||
ret.extend(_parse_include_spec(spec, path, include_map, expand_vars))
|
||||
ret.extend(
|
||||
_parse_include_spec(spec, path, include_map, expand_vars, no_absolute)
|
||||
)
|
||||
return ret
|
||||
|
||||
# -- Include target-path merging -----------------------------------------------
|
||||
|
|
@ -594,6 +612,7 @@ def _load_yaml(
|
|||
seen: set[Path],
|
||||
depth: int,
|
||||
include_map: IncludeMap | None,
|
||||
no_absolute: bool,
|
||||
) -> dict[str, Any]:
|
||||
path = Path(path).resolve()
|
||||
if path in seen:
|
||||
|
|
@ -618,19 +637,22 @@ def _load_yaml(
|
|||
return {}
|
||||
raise
|
||||
if include_map is None:
|
||||
no_absolute = bool(ret.pop('jw.include-map-no-absolute', False))
|
||||
include_map = _parse_include_map(
|
||||
ret.pop('jw.include-map', None), path.parent, str(path)
|
||||
)
|
||||
elif 'jw.include-map' in ret:
|
||||
elif 'jw.include-map' in ret \
|
||||
or 'jw.include-map-no-absolute' in ret:
|
||||
raise ValueError(
|
||||
f'jw.include-map is only allowed in the top-level '
|
||||
f'document: {path}'
|
||||
f'jw.include-map and jw.include-map-no-absolute are '
|
||||
f'only allowed in the top-level document: {path}'
|
||||
)
|
||||
includes = _normalize_include_specs(
|
||||
ret.get('jw.include', []),
|
||||
path,
|
||||
include_map = include_map,
|
||||
expand_vars = depth == 0
|
||||
expand_vars = depth == 0,
|
||||
no_absolute = no_absolute
|
||||
)
|
||||
ret.pop('jw.include', None)
|
||||
included_all: dict[str, Any] = {}
|
||||
|
|
@ -641,7 +663,8 @@ def _load_yaml(
|
|||
optional = opt,
|
||||
seen = seen,
|
||||
depth = depth + 1,
|
||||
include_map = include_map
|
||||
include_map = include_map,
|
||||
no_absolute = no_absolute
|
||||
)
|
||||
if not included_next:
|
||||
# An empty document contributes nothing; in particular
|
||||
|
|
@ -660,7 +683,9 @@ def _load_yaml(
|
|||
def load_yaml(path: str | Path, optional: bool = False) -> dict[str, Any]: # export
|
||||
seen: set[Path] = set()
|
||||
return _expand_links(
|
||||
_load_yaml(path, optional, seen, depth = 0, include_map = None)
|
||||
_load_yaml(
|
||||
path, optional, seen, depth = 0, include_map = None, no_absolute = False
|
||||
)
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Reference in a new issue