v0.7.5: socket.io polling-only shell, busybox realpath fallback chain
This commit is contained in:
1 parent
91944a0213
commit
ce241a283d
3 files changed
+75
-13
No files matched your search
+33
-11
@@ -43,6 +43,27 @@ def fb_within_roots(canonical, roots):
|
||||
return False
|
||||
|
||||
|
||||
def fb_resolve_canonical(raw, lexical, run_fn, timeout=10):
|
||||
"""Pick a canonical path using the best realpath the target supports.
|
||||
|
||||
- GNU coreutils: ``realpath -m`` works, even for not-yet-existing paths.
|
||||
- BusyBox (e.g. Alpine): no ``-m`` flag, so it is retried as plain
|
||||
``realpath`` (works for existing paths, resolves symlinks).
|
||||
- Otherwise the lexical path is used (``..`` already collapsed, but
|
||||
symlinks stay unresolved).
|
||||
|
||||
``run_fn(command, timeout)`` executes in the target context and returns
|
||||
``(stdout, stderr, returncode)``. Returns the canonical path string.
|
||||
"""
|
||||
out, err, rc = run_fn(f'realpath -m -- {quote_remote(raw)}', timeout)
|
||||
if rc == 0 and (out or '').strip():
|
||||
return (out or '').strip().split('\n')[0].strip()
|
||||
out, err, rc = run_fn(f'realpath -- {quote_remote(raw)}', timeout)
|
||||
if rc == 0 and (out or '').strip():
|
||||
return (out or '').strip().split('\n')[0].strip()
|
||||
return lexical
|
||||
|
||||
|
||||
def register_api_routes(app, services, settings):
|
||||
db = services['db']
|
||||
ssh = services['ssh']
|
||||
@@ -1041,14 +1062,15 @@ def register_api_routes(app, services, settings):
|
||||
import posixpath
|
||||
roots = _fb_allowed_roots()
|
||||
lexical = posixpath.normpath(raw)
|
||||
out, err, rc = _fb_exec(f'realpath -m -- {quote_remote(raw)}', pod_name, timeout=timeout)
|
||||
if rc == 0 and (out or '').strip():
|
||||
canonical = (out or '').strip().split('\n')[0].strip()
|
||||
else:
|
||||
# realpath unavailable in this context: fall back to the lexical
|
||||
# path (still blocks '..' escapes; symlinks stay unresolved).
|
||||
logger.warning('File browser: realpath unavailable, using lexical path check')
|
||||
canonical = lexical
|
||||
|
||||
def _run(command, timeout):
|
||||
# Probe commands are expected to fail on minimal toolchains
|
||||
# (e.g. BusyBox realpath without -m): keep them out of the logs.
|
||||
return _fb_exec(command, pod_name, timeout=timeout, quiet=True)
|
||||
|
||||
canonical = fb_resolve_canonical(raw, lexical, _run, timeout)
|
||||
if canonical == lexical:
|
||||
logger.debug('File browser: using lexical path check (no realpath -m support)')
|
||||
if not fb_within_roots(canonical, roots):
|
||||
return '', 'Invalid path: outside allowed directories'
|
||||
return canonical, ''
|
||||
@@ -1083,10 +1105,10 @@ def register_api_routes(app, services, settings):
|
||||
return str(e)
|
||||
return ''
|
||||
|
||||
def _fb_exec(command, pod_name=None, timeout=10):
|
||||
def _fb_exec(command, pod_name=None, timeout=10, quiet=False):
|
||||
"""Execute a command on the VM host or in a K8s pod."""
|
||||
if pod_name in _FB_VM_PODS:
|
||||
return k8s.ssh.run(command, timeout=timeout)
|
||||
return k8s.ssh.run(command, timeout=timeout, quiet=quiet)
|
||||
pod = pod_name or _get_fb_pod()
|
||||
if not pod:
|
||||
return '', 'No pod specified or found', 1
|
||||
@@ -1094,7 +1116,7 @@ def register_api_routes(app, services, settings):
|
||||
if err:
|
||||
return '', f'Invalid pod name: {err}', 1
|
||||
full_cmd = f'sudo kubectl exec {shlex.quote(pod)} -n {shlex.quote(str(k8s.namespace))} -- {command}'
|
||||
return k8s.ssh.run(full_cmd, timeout=timeout)
|
||||
return k8s.ssh.run(full_cmd, timeout=timeout, quiet=quiet)
|
||||
|
||||
def _fb_write_file(content_b64, path, pod_name=None, timeout=30):
|
||||
"""Write a file to the pod filesystem."""
|
||||
|
||||
@@ -116,7 +116,10 @@
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@4.7.2/dist/socket.io.min.js"></script>
|
||||
<script>
|
||||
const socket = io();
|
||||
// Polling only: the server runs in threading mode, which has no true
|
||||
// websocket support. Letting the client attempt a websocket upgrade only
|
||||
// produces "Cannot obtain socket from WSGI environment" tracebacks.
|
||||
const socket = io({ transports: ['polling'] });
|
||||
|
||||
let termVM = null;
|
||||
let termPod = null;
|
||||
|
||||
@@ -2,10 +2,47 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from app.routes.api import fb_within_roots
|
||||
from app.routes.api import fb_resolve_canonical, fb_within_roots
|
||||
from app.services.backup import is_valid_backup_name
|
||||
|
||||
|
||||
class TestCanonicalResolution:
|
||||
def test_gnu_realpath_m(self):
|
||||
calls = []
|
||||
|
||||
def run(cmd, timeout):
|
||||
calls.append(cmd)
|
||||
return '/srv/sub/../file.txt\n', '', 0
|
||||
|
||||
assert fb_resolve_canonical('/srv/file.txt', '/srv/file.txt', run) == \
|
||||
'/srv/sub/../file.txt'
|
||||
assert calls[0].startswith('realpath -m -- ')
|
||||
|
||||
def test_busybox_falls_back_to_plain_realpath(self):
|
||||
def run(cmd, timeout):
|
||||
if cmd.startswith('realpath -m -- '):
|
||||
return '', 'realpath: -m: No such file or directory', 1
|
||||
assert cmd.startswith('realpath -- ')
|
||||
return '/srv/file.txt\n', '', 0
|
||||
|
||||
assert fb_resolve_canonical('/srv/file.txt', '/srv/file.txt', run) == \
|
||||
'/srv/file.txt'
|
||||
|
||||
def test_missing_binary_falls_back_to_lexical(self):
|
||||
def run(cmd, timeout):
|
||||
return '', 'realpath: command not found', 127
|
||||
|
||||
assert fb_resolve_canonical('/srv/a/../b', '/srv/b', run) == '/srv/b'
|
||||
|
||||
def test_missing_file_without_m_support(self):
|
||||
# New file on BusyBox: plain realpath fails too, lexical wins.
|
||||
def run(cmd, timeout):
|
||||
return '', 'No such file or directory', 1
|
||||
|
||||
assert fb_resolve_canonical('/srv/new.txt', '/srv/new.txt', run) == \
|
||||
'/srv/new.txt'
|
||||
|
||||
|
||||
class TestFileBrowserJail:
|
||||
ROOTS = ['/srv', '/home/dune/.dune']
|
||||
|
||||
|
||||
Reference in new issue
Block a user