untrusted_fs
untrusted_fs
¶
Read and replace files in directories an untrusted task can modify.
Callers choose the path; these helpers do not accept an attacker-selected absolute path as authorization. Every directory component is opened without following links and held by descriptor. Reads are bounded and require a single-link regular file. Writes publish a fresh inode instead of modifying an existing one. Linux/POSIX descriptor-relative operations are required.
These operations prevent link redirection, not concurrent task edits. A task can still replace its own files after publication. Keep authoritative host state outside task-writable mounts.
__all__ = ['UnsafePathError', 'open_untrusted_directory', 'read_untrusted_bytes', 'read_untrusted_text', 'remove_untrusted_file', 'untrusted_file_lock', 'write_untrusted_bytes', 'write_untrusted_text']
module-attribute
¶
UnsafePathError
¶
Bases: OSError
A task-owned input is linked, not regular, oversized or traverses a parent.
read_untrusted_text(path, *, max_bytes, encoding='utf-8')
¶
Read at most max_bytes from a plain file, then decode it.
Missing files raise FileNotFoundError. Unsafe paths and oversized
inputs raise UnsafePathError.
Decoding errors propagate to the caller.
Source code in src/terok_util/untrusted_fs.py
read_untrusted_bytes(path, *, max_bytes)
¶
Read a bounded single-link regular file without following any path links.
Source code in src/terok_util/untrusted_fs.py
write_untrusted_text(path, text, *, mode=384, encoding='utf-8')
¶
Encode text and atomically replace path with a fresh private inode.
Source code in src/terok_util/untrusted_fs.py
write_untrusted_bytes(path, content, *, mode=384)
¶
Publish content without truncating or appending to a task-owned inode.
Missing parents are created with mode 0o700. An observed symlink,
non-regular file or hardlink at the destination is refused. A subsequent
leaf replacement cannot redirect the write: the final rename replaces
the directory entry itself. Only the new inode receives mode.
Source code in src/terok_util/untrusted_fs.py
untrusted_file_lock(path)
¶
Lock a plain file for cooperating host writers without following links.
The file is created privately if missing and is never truncated. This advisory lock does not exclude a malicious task that ignores the lock or replaces its directory entry.
Source code in src/terok_util/untrusted_fs.py
remove_untrusted_file(path, *, missing_ok=True)
¶
Unlink a plain leaf through its pinned parent, never through an ancestor link.
An observed symlink, hardlink or special file is refused. With missing_ok, an absent parent or leaf is already the requested result.
Source code in src/terok_util/untrusted_fs.py
open_untrusted_directory(path, *, create=False)
¶
Hold a directory descriptor reached without following any ancestor link.
Relative paths start at the process's current directory. Parent traversal
components are refused. With create, missing directories get 0o700;
existing directories retain their permissions. The yielded descriptor is
borrowed and closes when the context exits.