26 lines
1.9 KiB
Python
26 lines
1.9 KiB
Python
"""Validate adapter gating without importing OpenWebUI or touching real services."""
|
|
import ast,pathlib,shlex,unittest
|
|
from urllib.parse import urlparse
|
|
source=pathlib.Path(__file__).with_name('bridge.py')
|
|
fn=next(n for n in ast.walk(ast.parse(source.read_text())) if isinstance(n,ast.FunctionDef) and n.name=='safe_command')
|
|
ns={'shlex':shlex,'urlparse':urlparse}
|
|
exec(compile(ast.Module(body=[fn],type_ignores=[]),str(source),'exec'),ns)
|
|
safe=ns['safe_command']
|
|
class GateTests(unittest.TestCase):
|
|
def test_mutation_endpoints_and_downloads_denied(self):
|
|
for cmd in ['curl http://192.168.1.89/api/powerstate?newstate=4','curl http://192.168.1.89/api/timerdelete','curl http://evil.example/api/statusinfo','yt-dlp --skip-download --write-thumbnail https://www.youtube.com/watch?v=test','yt-dlp https://www.youtube.com/watch?v=test','rm -rf /tmp/example']:
|
|
with self.subTest(command=cmd):self.assertIsNone(safe(cmd))
|
|
def test_curl_never_executes_supplied_shell_or_output_arguments(self):
|
|
for cmd in ['curl http://192.168.1.89/api/statusinfo && touch /tmp/unsafe','curl -o /tmp/unsafe http://192.168.1.89/api/statusinfo']:
|
|
actual=safe(cmd);self.assertEqual(shlex.split(actual),['curl','--silent','--show-error','--max-time','10','http://192.168.1.89/api/statusinfo'])
|
|
def test_flat_playlist_on_single_video_cannot_download(self):
|
|
args=shlex.split(safe('yt-dlp --flat-playlist https://www.youtube.com/watch?v=test'))
|
|
self.assertIn('--skip-download',args)
|
|
self.assertIn('--ignore-config',args)
|
|
def test_reading_queries_allowed_and_literal(self):
|
|
efa='python3 /home/user/Skripte/efa_bahn.py journeys --origin Rastatt --destination Karlsruhe --date tomorrow --time 05:20'
|
|
self.assertEqual(shlex.split(safe(efa)),shlex.split(efa))
|
|
y='yt-dlp --flat-playlist --playlist-end 5 -J https://www.youtube.com/@TheProperPeople/videos'
|
|
self.assertIn('--no-cache-dir',shlex.split(safe(y)))
|
|
if __name__=='__main__':unittest.main()
|