mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
"""Descriptor-relative local reads; refuse symlinks at every path component.
|
|
|
|
No fall back to pathname reads on platforms without these primitives.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import stat
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from typing import Iterator
|
|
|
|
|
|
@contextmanager
|
|
def open_directory(path: Path) -> Iterator[int]:
|
|
if not path.is_absolute() or '..' in path.parts:
|
|
raise ValueError('invalid directory')
|
|
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
|
fd = os.open('/', flags)
|
|
try:
|
|
for part in path.parts[1:]:
|
|
child = os.open(part, flags, dir_fd=fd)
|
|
os.close(fd)
|
|
fd = child
|
|
yield fd
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
@contextmanager
|
|
def child_directory(parent: int, name: str) -> Iterator[int]:
|
|
if not name or name in {'.', '..'} or '/' in name:
|
|
raise ValueError('invalid directory')
|
|
fd = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent)
|
|
try:
|
|
yield fd
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def read_json_bytes(parent: int, name: str, maximum: int) -> bytes:
|
|
"""Read one fixed regular file, bounded even if it grows while reading."""
|
|
fd = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent)
|
|
try:
|
|
info = os.fstat(fd)
|
|
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1 or info.st_size > maximum:
|
|
raise ValueError('invalid metadata file')
|
|
chunks = []
|
|
remaining = maximum + 1
|
|
while remaining:
|
|
chunk = os.read(fd, min(remaining, 65536))
|
|
if not chunk:
|
|
break
|
|
chunks.append(chunk)
|
|
remaining -= len(chunk)
|
|
data = b''.join(chunks)
|
|
if len(data) > maximum:
|
|
raise ValueError('metadata limit exceeded')
|
|
return data
|
|
finally:
|
|
os.close(fd)
|