Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion app/modeline_instructions/call_view_function.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):

Expand All @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_sublime_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading