diff --git a/docs/changelog.rst b/docs/changelog.rst index 831291a8a..fd3a64f1a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -44,6 +44,9 @@ Detailed list of changes - A new option :opt:`modify_font` to adjust various font metrics like underlines, cell sizes etc. (:pull:`5265`) +- Graphics protocol: Only delete temp files if they have the string + :code:`tty-graphics-protocol` in their file paths. This prevents deletion of arbitrary files in :file:`/tmp`. + - Deprecate the ``adjust_baseline``, ``adjust_line_height`` and ``adjust_column_width`` options in favor of :opt:`modify_font` - Wayland: Fix a regression in the previous release that caused mouse cursor diff --git a/docs/graphics-protocol.rst b/docs/graphics-protocol.rst index 43adb68d4..fe39cefb3 100644 --- a/docs/graphics-protocol.rst +++ b/docs/graphics-protocol.rst @@ -237,7 +237,8 @@ Value of `t` Meaning the terminal emulator should only delete the file if it is in a known temporary directory, such as :file:`/tmp`, :file:`/dev/shm`, :file:`TMPDIR env var if present` and any platform - specific temporary directories. + specific temporary directories and the file has the + string :code:`tty-graphics-protocol` in its full file path. ``s`` A *shared memory object*, which on POSIX systems is a `POSIX shared memory object `_ and on Windows is a diff --git a/kittens/icat/main.py b/kittens/icat/main.py index 4b46f947e..6e25473de 100644 --- a/kittens/icat/main.py +++ b/kittens/icat/main.py @@ -353,7 +353,7 @@ def process(path: str, args: IcatCLIOptions, parsed_opts: ParsedOpts, is_tempfil else: import struct use_number = max(1, struct.unpack('@I', os.urandom(4))[0]) - with NamedTemporaryFile() as f: + with NamedTemporaryFile(prefix='tty-graphics-protocol-') as f: prefix = f.name frame_data = render_image( path, prefix, m, available_width, available_height, args.scale_up, @@ -405,7 +405,7 @@ def detect_support(wait_for: float = 10, silent: bool = False) -> bool: parse_responses() return 1 not in responses or 2 not in responses - with NamedTemporaryFile() as f: + with NamedTemporaryFile(prefix='tty-graphics-protocol') as f: f.write(b'abcd') f.flush() gc = GraphicsCommand() @@ -472,13 +472,13 @@ def process_single_item( file_removed = False try: if isinstance(item, bytes): - with NamedTemporaryFile(prefix='stdin-image-data-', delete=False) as tf: + with NamedTemporaryFile(prefix='tty-graphics-protocol-', delete=False) as tf: tf.write(item) item = tf.name is_tempfile = True if url_pat is not None and url_pat.match(item) is not None: from urllib.request import urlretrieve - with NamedTemporaryFile(prefix='url-image-data-', delete=False) as tf: + with NamedTemporaryFile(prefix='tty-graphics-protocol-', delete=False) as tf: try: with socket_timeout(30): urlretrieve(item, filename=tf.name) diff --git a/kittens/tui/images.py b/kittens/tui/images.py index c3c714c28..57b90a6c4 100644 --- a/kittens/tui/images.py +++ b/kittens/tui/images.py @@ -301,7 +301,7 @@ def render_as_single_image( remove_alpha: str = '', flip: bool = False, flop: bool = False, ) -> Tuple[str, int, int]: import tempfile - fd, output = tempfile.mkstemp(prefix='icat-', suffix=f'.{m.mode}', dir=tdir) + fd, output = tempfile.mkstemp(prefix='tty-graphics-protocol-', suffix=f'.{m.mode}', dir=tdir) os.close(fd) result = render_image( path, output, m, available_width, available_height, scale_up, diff --git a/kitty/graphics.c b/kitty/graphics.c index 3b151dd35..f6dd42e5e 100644 --- a/kitty/graphics.c +++ b/kitty/graphics.c @@ -419,7 +419,7 @@ load_image_data(GraphicsManager *self, Image *img, const GraphicsCommand *g, con if (fd == -1) ABRT("EBADF", "Failed to open file for graphics transmission with error: [%d] %s", errno, strerror(errno)); load_data->loading_completed_successfully = mmap_img_file(self, fd, g->data_sz, g->data_offset); safe_close(fd, __FILE__, __LINE__); - if (transmission_type == 't') { + if (transmission_type == 't' && strstr(fname, "tty-graphics-protocol") != NULL) { if (global_state.boss) { call_boss(safe_delete_temp_file, "s", fname); } else unlink(fname); } diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 9293492b5..73136fd7b 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -360,7 +360,7 @@ class TestGraphics(BaseTest): self.ae(img['data'], random_data) # Test loading from file - f = tempfile.NamedTemporaryFile() + f = tempfile.NamedTemporaryFile(prefix='tty-graphics-protocol-') f.write(random_data), f.flush() sl(f.name, s=24, v=32, t='f', expecting_data=random_data) self.assertTrue(os.path.exists(f.name))