aeat.adapters.persistence.storage._path_safety module¶
Typed path-containment helpers for the persistence substrate.
The wider project ships core.paths.resolve_relative_subpath() and
core.paths.resolve_record_json_path() which raise plain
ValueError on traversal violations. New persistence code uses
the typed wrappers in this module instead so:
the failure carries the registered
PathContainmentErrorcode (INTEGRITY_STORAGE_PATH_CONTAINMENT) and lands in the standard CLI error envelope;callers can write narrow
except PathContainmentErrorclauses rather than broadexcept ValueError;PathContainmentErrorstill inherits fromValueErrorso callers that handle Python path-shape errors remain correct.
- safe_subpath(root, relative_path, *, context)[source]¶
Resolve
relative_pathunderrootand enforce containment.Wraps
core.paths.resolve_relative_subpath(). AnyValueErrorraised by the wrapped helper is re-raised as a localizedPathContainmentErrorwith the same diagnosticargsmessage and__cause__.- Parameters:
- Return type:
Path- Returns:
The resolved absolute
Pathknown to live underroot.- Raises:
PathContainmentError – On any traversal or shape violation.
- safe_record_path(root, record_id, *, context)[source]¶
Resolve a record-id-keyed JSON file under
root.Wraps
core.paths.resolve_record_json_path()and re-raises itsValueErroras a localizedPathContainmentError.- Parameters:
- Return type:
Path- Returns:
The resolved absolute
Pathof<root>/<record_id>.json.- Raises:
PathContainmentError – When
record_idis not a safe token or when the resolved path escapesroot.
- safe_repository_id(token, *, context)[source]¶
Reject repository-id tokens that would compose into an unsafe filename.
Repositories store records as
<store_dir>/<token>.envelope.json. A token containing a path separator, a dot-prefix, or one of the relative-path tokens ("."/"..") would either escape the store dir or collide with a hidden file. This helper is the early-rejection layer at the public-method boundary; the substrate’ssafe_record_path()enforces containment downstream.The validation is intentionally minimal — non-empty, no path separator, no dot-token. It does not claim knowledge of any domain-specific id alphabet (UUIDs, AEAT CSVs, modelo numerics, etc.) so a single helper covers every governance repository.
- Parameters:
- Return type:
- Returns:
tokenunchanged. Returning the validated value lets the helper appear inline (safe_repository_id(record_id, ...)as both check and pass-through).- Raises:
PathContainmentError – When
tokenis empty, contains a path separator, is the bare./..token, or starts with a dot.