From a6147a6745d6c85d27ab069332a6707e35981a51 Mon Sep 17 00:00:00 2001 From: Aaron Boxer Date: Fri, 17 Apr 2026 09:31:00 -0400 Subject: [PATCH] Replace os.system() in clear_screen() with ANSI escape codes - Eliminates shell injection risk from os.system('clear'/'cls') - Uses ANSI escape sequence \033[H\033[2J instead - Removed unused os import - Added test verifying ANSI codes are used --- tests/test_utils.py | 8 +++++++- whisper_live/utils.py | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 2dc42e8..d7d01d4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,7 +4,7 @@ import unittest from io import StringIO from unittest.mock import patch -from whisper_live.utils import format_time, create_srt_file, print_transcript +from whisper_live.utils import format_time, create_srt_file, print_transcript, clear_screen class TestFormatTime(unittest.TestCase): @@ -97,6 +97,12 @@ class TestCreateSrtFile(unittest.TestCase): class TestPrintTranscript(unittest.TestCase): + @patch("sys.stdout", new_callable=StringIO) + def test_clear_screen_uses_ansi(self, mock_stdout): + clear_screen() + output = mock_stdout.getvalue() + self.assertIn("\033[H\033[2J", output) + @patch("sys.stdout", new_callable=StringIO) def test_print_plain_text(self, mock_stdout): text = ["Hello", " world"] diff --git a/whisper_live/utils.py b/whisper_live/utils.py index 43fefae..c7f85ec 100644 --- a/whisper_live/utils.py +++ b/whisper_live/utils.py @@ -1,4 +1,3 @@ -import os import textwrap import scipy import numpy as np @@ -8,7 +7,7 @@ from pathlib import Path def clear_screen(): """Clears the console screen.""" - os.system("cls" if os.name == "nt" else "clear") + print("\033[H\033[2J", end="", flush=True) def print_transcript(text, translated=False, timestamps=False):