diff --git a/Readme.md b/Readme.md index ab2f282..d33f0f0 100644 --- a/Readme.md +++ b/Readme.md @@ -51,6 +51,19 @@ This is a simple example, that disable tabs auto-translation to spaces, set the # ~*~ sublime: syntax=Python; tab_size=3; translate_tabs_to_spaces=false ~*~ ``` +### View Functions Are Restricted + +A key ending in `()` is a call on the buffer’s `sublime.View`, used by the VIM and Emacs mappings for +`set_line_endings()` and `set_encoding()`. Because a modeline lives in the file being opened, only a +fixed set of methods may be called this way — the ones that change that buffer’s own state and nothing +else: + +`set_encoding`, `set_line_endings`, `set_name`, `set_read_only`, `set_scratch`, `set_syntax_file` + +Any other name is refused, logged, and skipped; the other instructions on the same line still apply. +Without that restriction a file could declare `run_command()=…` and run an arbitrary command as soon +as it was opened. + ## Developer Note To get proper completion and errors in the editor when working on this repo, diff --git a/app/modeline_instructions/call_view_function.py b/app/modeline_instructions/call_view_function.py index 580d27b..c1891d0 100644 --- a/app/modeline_instructions/call_view_function.py +++ b/app/modeline_instructions/call_view_function.py @@ -1,4 +1,4 @@ -from typing import final +from typing import Final, FrozenSet, final from sublime import View as SublimeView from sublime_types import Value as SublimeValue @@ -7,6 +7,21 @@ +# Modeline content comes from the file being opened, so the function name in “name()=argument” is +# attacker-controlled: an unbounded getattr() over the view exposes every method, including +# run_command(), which runs an arbitrary command. Only the methods a modeline has a reason to call +# are allowed, and all of them change the buffer’s own state and nothing else. +ALLOWED_VIEW_FUNCTIONS: Final[FrozenSet[str]] = frozenset({ + "set_encoding", + "set_line_endings", + "set_name", + "set_read_only", + "set_scratch", + "set_syntax_file", +}) + + + @final class ModelineInstruction_CallViewFunction(ModelineInstruction): @@ -20,6 +35,8 @@ def __init__(self, key: str, value: SublimeValue, modifier: ModelineInstruction. self.function_arg = value def apply(self, view: SublimeView) -> None: + if self.function_name not in ALLOWED_VIEW_FUNCTIONS: + raise ValueError(f"Refusing to call view function “{self.function_name}”: it is not in the allowlist ({', '.join(sorted(ALLOWED_VIEW_FUNCTIONS))}).") f = getattr(view, self.function_name) f(self.function_arg) diff --git a/tests/test_sublime_parser.py b/tests/test_sublime_parser.py index 240dcb0..2dc434d 100644 --- a/tests/test_sublime_parser.py +++ b/tests/test_sublime_parser.py @@ -100,3 +100,19 @@ def test_modelines_1(self): self.window.run_command("modelines_apply") self.assertEqual(self.view.settings().get("tab_size"), 3) self.assertEqual(self.view.settings().get("translate_tabs_to_spaces"), False) + + def test_view_function_outside_the_allowlist_is_refused(self): + original_size = self.view.size() + line = '/* ~*~ sublime: run_command()="insert"; tab_size=9 ~*~ */\n' + self.view.run_command("insert", {"characters": line}) + self.window.run_command("modelines_apply") + # The buffer is exactly what we typed: the refused instruction did not run a command… + self.assertEqual(self.view.size(), original_size + len(line)) + self.assertEqual(self.view.substr(sublime.Region(0, self.view.size())), line) + # …and the settings on the same line still applied. + self.assertEqual(self.view.settings().get("tab_size"), 9) + + def test_view_function_inside_the_allowlist_still_applies(self): + self.view.run_command("insert", {"characters": '/* ~*~ sublime: set_name()="renamed by modeline" ~*~ */\n'}) + self.window.run_command("modelines_apply") + self.assertEqual(self.view.name(), "renamed by modeline")