Download research/expansion/worker.py from Helinity/python-security-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 4.05 kB
-
https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/expansion/worker.py
- Command line
-
hf download hf://datasets/Helinity/python-security-benchmark/research/expansion/worker.py
-
curl -L -o worker.py https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/expansion/worker.py
4.05 kB
| """Container-only worker: real framework clients, no mocked application boundaries.""" | |
| import importlib | |
| import importlib.metadata | |
| import json | |
| import os | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| assert sys.platform == 'linux' and os.getuid() == 65534 | |
| status = dict(line.split(':', 1) for line in Path('/proc/self/status').read_text().splitlines() if ':' in line) | |
| assert int(status['CapEff'].strip(), 16) == 0 | |
| assert status['NoNewPrivs'].strip() == '1' | |
| interfaces = sorted(p.name for p in Path('/sys/class/net').iterdir()) | |
| assert interfaces == ['lo'] | |
| root_mount = next(line.split() for line in Path('/proc/self/mountinfo').read_text().splitlines() if line.split()[4] == '/') | |
| assert 'ro' in root_mount[5].split(',') | |
| isolation = {'uid': os.getuid(), 'effective_capabilities': status['CapEff'].strip(), | |
| 'no_new_privileges': status['NoNewPrivs'].strip(), | |
| 'interfaces': interfaces, 'root_read_only': True, | |
| 'memory_max': Path('/sys/fs/cgroup/memory.max').read_text().strip(), | |
| 'pids_max': Path('/sys/fs/cgroup/pids.max').read_text().strip(), | |
| 'cpu_max': Path('/sys/fs/cgroup/cpu.max').read_text().strip()} | |
| assert isolation['memory_max'] == str(256 * 1024 * 1024) | |
| assert isolation['pids_max'] == '32' | |
| payload = json.load(sys.stdin) | |
| results = {} | |
| for row in payload: | |
| with tempfile.TemporaryDirectory(dir='/tmp') as directory: | |
| root = Path(directory) | |
| for item in row['code_files']: | |
| assert item['path'] in {'app.py', 'service.py'} | |
| (root / item['path']).write_text(item['code']) | |
| sys.path.insert(0, directory) | |
| observations = {} | |
| try: | |
| app = importlib.import_module('app') | |
| if row['framework'] == 'Flask': | |
| client = app.app.test_client() | |
| def invoke(body): | |
| response = client.post('/run', json=body) | |
| assert response.status_code == 200, response.data.decode() | |
| return response.get_json()['result'] | |
| elif row['framework'] == 'FastAPI': | |
| from fastapi.testclient import TestClient | |
| client = TestClient(app.app) | |
| def invoke(body): | |
| response = client.post('/run', json=body) | |
| assert response.status_code == 200, response.text | |
| return response.json()['result'] | |
| else: | |
| def invoke(body): | |
| return app.request(body)['result'] | |
| for name in ('normal', 'adversarial'): | |
| contract = row['contract'][name] | |
| if row['pattern'] == 'default-admin-password': | |
| if contract['request']['configured']: | |
| os.environ['CODEDNA_ADMIN_PASSWORD'] = 'provisioned-demo-only' | |
| else: | |
| os.environ.pop('CODEDNA_ADMIN_PASSWORD', None) | |
| actual = json.loads(json.dumps(invoke(contract['request']))) | |
| observations[name + '_observed'] = actual | |
| observations[name + '_passed'] = actual == contract['expected'] | |
| results[row['sample_id']] = { | |
| **observations, 'code_sha256': row['code_sha256'], | |
| 'method': 'isolated_linux_runtime', | |
| } | |
| except Exception as error: | |
| results[row['sample_id']] = { | |
| **observations, 'code_sha256': row['code_sha256'], | |
| 'method': 'isolated_linux_runtime', 'normal_passed': False, | |
| 'adversarial_passed': False, 'error': type(error).__name__ + ': ' + str(error), | |
| } | |
| finally: | |
| sys.path.remove(directory) | |
| for name in ('app', 'service'): | |
| sys.modules.pop(name, None) | |
| print(json.dumps({'results': results, 'isolation': isolation, 'python': sys.version.split()[0], | |
| 'packages': {name: importlib.metadata.version(name) | |
| for name in ('Flask', 'fastapi', 'sqlalchemy', 'httpx')}}, sort_keys=True)) | |