climate_ref_core.paths
#
Path-safety primitive for containing untrusted relative paths.
:func:safe_path guards every place that joins an externally
supplied string onto a trusted base directory before reading or writing files.
It is a sanity check that the executions are not colliding or escaping the intended directories.
safe_path(relpath, base=None, *, label='path', single_segment=False)
#
Validate relpath is a contained relative path and return it.
The check has two layers:
- a lexical layer (always applied) that rejects empty strings, absolute paths,
..components and NUL bytes, so the path cannot escape upwards or smuggle a path terminator - a containment layer (applied only when
baseis given) that joinsrelpathontobase, resolves symlinks and..segments, and confirms the result still lives underbase. This requires filesystem access but catches escapes the lexical check cannot see (e.g. symlinks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relpath
|
str | Path
|
The candidate relative path (e.g. a manifest native key or an output fragment). |
required |
base
|
Path | None
|
The trusted base directory |
None
|
label
|
str
|
A human-readable description of |
'path'
|
single_segment
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If When |