Skip to content

Commit

Permalink
Make xonsh tolerant to inaccessible paths: history backend, script ca…
Browse files Browse the repository at this point in the history
…che (#5430)

Closes #5319

### Before

```xsh
mkdir -p /tmp/noaccess
chmod 000 /tmp/noaccess
echo 'print(1)' > /tmp/1.xsh

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess'
# Json History error

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess' /tmp/1.xsh
# Script cache error
```

### After

```xsh
mkdir -p /tmp/noaccess
chmod 000 /tmp/noaccess
echo 'print(1)' > /tmp/1.xsh

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess'
# Error during load <class 'xonsh.history.json.JsonHistory'>: [Errno 13] Permission denied: '/tmp/noaccess/history_json'
# Set $XONSH_HISTORY_BACKEND='dummy' to disable history.
# History disabled.
# @

xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess /tmp/1.xsh
# xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
# update_cache: Cache file is not writable: /tmp/noaccess/xonsh_script_cache/private/tmp/1_.xsh.cpython-311
# Set $XONSH_CACHE_SCRIPTS=0, $XONSH_CACHE_EVERYTHING=0 to disable cache.
# 1
```
And no warnings when everything is switched off:
```xsh
xonsh % python -m xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_HISTORY_BACKEND=dummy
# @

xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_CACHE_SCRIPTS=0 -DXONSH_CACHE_EVERYTHING=0 /tmp/1.xsh
# 1
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed May 20, 2024
1 parent e03bda4 commit 9073a4c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
24 changes: 24 additions & 0 deletions news/fix_noaccess.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**Added:**

* Added catching an exceptions during load a history backend to avoid shell exiting e.g. on permission error.
* Added catching an exception when cache file is not writable.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
11 changes: 10 additions & 1 deletion xonsh/codecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from xonsh.built_ins import XSH
from xonsh.lazyasd import lazyobject
from xonsh.platform import PYTHON_VERSION_INFO_BYTES
from xonsh.tools import is_writable_file, print_warning


def _splitpath(path, sofar=()):
Expand Down Expand Up @@ -94,6 +95,13 @@ def update_cache(ccode, cache_file_name):
represented by ``ccode``.
"""
if cache_file_name is not None:
if not is_writable_file(cache_file_name):
if XSH.env.get("XONSH_DEBUG", "False"):
print_warning(
f"update_cache: Cache file is not writable: {cache_file_name}\n"
f"Set $XONSH_CACHE_SCRIPTS=0, $XONSH_CACHE_EVERYTHING=0 to disable cache."
)
return
os.makedirs(os.path.dirname(cache_file_name), exist_ok=True)
with open(cache_file_name, "wb") as cfile:
cfile.write(XONSH_VERSION.encode() + b"\n")
Expand Down Expand Up @@ -166,7 +174,8 @@ def run_script_with_cache(filename, execer, glb=None, loc=None, mode="exec"):
with open(filename, encoding="utf-8") as f:
code = f.read()
ccode = compile_code(filename, code, execer, glb, loc, mode)
update_cache(ccode, cachefname)
if use_cache:
update_cache(ccode, cachefname)
return run_compiled_code(ccode, glb, loc, mode)


Expand Down
11 changes: 10 additions & 1 deletion xonsh/history/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ def construct_history(backend=None, **kwargs) -> "History":
file=sys.stderr,
)
kls_history = JsonHistory
return kls_history(**kwargs)

try:
return kls_history(**kwargs)
except Exception as e:
xt.print_exception(
f"Error during load {kls_history}: {e}\n"
f"Set $XONSH_HISTORY_BACKEND='dummy' to disable history.\n"
f"History disabled."
)
return DummyHistory()


def _xh_session_parser(hist=None, newest_first=False, **kwargs):
Expand Down

0 comments on commit 9073a4c

Please sign in to comment.