fix: _matchesCombo crashes on non-string keybind from server (#2049)

This commit is contained in:
Afonso Coutinho
2026-07-11 03:15:19 +01:00
committed by GitHub
parent 7b25b6dbdc
commit 667f9e4cae
2 changed files with 48 additions and 1 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ const _defaultKeybinds = {
};
export function _matchesCombo(e, combo, isMac = IS_MAC) {
if (!combo) return false;
if (typeof combo !== 'string' || !combo) return false;
// Drop AltGr keystrokes so typing characters on non-US layouts can't fire a
// Ctrl+Alt shortcut — e.g. the destructive delete_session. See platform.js.
if (isAltGrEvent(e, isMac)) return false;
+47
View File
@@ -0,0 +1,47 @@
"""Pin _matchesCombo (static/js/keyboard-shortcuts.js) against a non-string
keybind. Driven through `node --input-type=module` (same approach as
tests/test_markdown_table_row_js.py); skips when `node` is missing.
Regression: keybinds are merged from the server response of
`/api/auth/settings` (`{ ..._defaultKeybinds, ...s.keybinds }`). A corrupt
or malformed `keybinds` value (e.g. a number instead of "ctrl+k") reached
`combo.split('+')` and threw "combo.split is not a function", breaking the
whole keydown handler. The guard treats any non-string combo as "no match".
"""
import json
import shutil
import subprocess
from pathlib import Path
import pytest
_REPO = Path(__file__).resolve().parent.parent
_MOD = _REPO / "static" / "js" / "keyboard-shortcuts.js"
_HAS_NODE = shutil.which("node") is not None
_EVENT = "{key:'k',ctrlKey:false,altKey:false,shiftKey:false,metaKey:false}"
def _match(combo_js):
js = f"""
import {{ _matchesCombo }} from '{_MOD.as_posix()}';
console.log(JSON.stringify(_matchesCombo({_EVENT}, {combo_js})));
"""
proc = subprocess.run(
["node", "--input-type=module"],
input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout.strip())
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_non_string_combo_is_no_match():
assert _match("123") is False
assert _match("{}") is False
assert _match("null") is False
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_matching_combo_still_fires():
assert _match("'k'") is True