Datasets:
Download awkweave/execution.py from asterbraid/awkweave-so-data: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/datasets/asterbraid/awkweave-so-data/resolve/main/awkweave/execution.py
- Command line
-
hf download hf://datasets/asterbraid/awkweave-so-data/awkweave/execution.py
-
curl -L -o execution.py https://huggingface.co/datasets/asterbraid/awkweave-so-data/resolve/main/awkweave/execution.py
2.3 kB
| from __future__ import annotations | |
| import re | |
| import resource | |
| import shutil | |
| import subprocess | |
| MAX_INPUT_BYTES = 1_000_000 | |
| MAX_PROGRAM_CHARS = 8_000 | |
| FORBIDDEN_PATTERN = re.compile( | |
| r"\b(?:system|getline|ARGV|ARGC|ENVIRON|FILENAME|FNR|nextfile|close)\b", | |
| re.IGNORECASE, | |
| ) | |
| OUTPUT_REDIRECT_PATTERN = re.compile( | |
| r"\bprintf?\b[^;\n}]*(?:>\s*(?:[\"/]|[A-Za-z_$])|(?<!\|)\|(?!\|))", | |
| re.IGNORECASE, | |
| ) | |
| class UnsafeProgramError(ValueError): | |
| pass | |
| def check_program(program: str) -> str: | |
| program = program.strip() | |
| if not program: | |
| raise UnsafeProgramError("The awk program is empty.") | |
| if len(program) > MAX_PROGRAM_CHARS: | |
| raise UnsafeProgramError("The awk program is too long.") | |
| if "\x00" in program: | |
| raise UnsafeProgramError("The awk program contains a null byte.") | |
| match = FORBIDDEN_PATTERN.search(program) | |
| if match: | |
| raise UnsafeProgramError(f"File, environment, and process access are not allowed: {match.group()}") | |
| if OUTPUT_REDIRECT_PATTERN.search(program): | |
| raise UnsafeProgramError("Output redirection and pipes are not allowed.") | |
| return program | |
| def limit_resources() -> None: | |
| resource.setrlimit(resource.RLIMIT_CPU, (2, 2)) | |
| resource.setrlimit(resource.RLIMIT_AS, (256_000_000, 256_000_000)) | |
| resource.setrlimit(resource.RLIMIT_FSIZE, (1_000_000, 1_000_000)) | |
| resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) | |
| resource.setrlimit(resource.RLIMIT_NOFILE, (16, 16)) | |
| def run_awk(program: str, input_text: str, timeout: float = 3.0) -> str: | |
| program = check_program(program) | |
| if len(input_text.encode("utf-8")) > MAX_INPUT_BYTES: | |
| raise ValueError("Input text exceeds the 1 MB limit.") | |
| executable = shutil.which("awk") | |
| if executable is None: | |
| raise RuntimeError("No awk executable is available on PATH.") | |
| result = subprocess.run( | |
| [executable, program], | |
| input=input_text, | |
| text=True, | |
| capture_output=True, | |
| timeout=timeout, | |
| env={"LC_ALL": "C", "PATH": "/usr/bin:/bin"}, | |
| preexec_fn=limit_resources, | |
| start_new_session=True, | |
| check=False, | |
| ) | |
| if result.returncode != 0: | |
| raise ValueError(result.stderr.strip() or f"awk exited with status {result.returncode}") | |
| return result.stdout | |