|
1 | 1 | import tkinter as tk |
2 | | -from tkinter import ttk, filedialog, messagebox, scrolledtext, colorchooser, font |
| 2 | +from tkinter import ttk, filedialog, messagebox, scrolledtext, colorchooser, font, simpledialog |
3 | 3 | import psutil |
4 | 4 | import time |
5 | 5 | import os |
|
13 | 13 | import math |
14 | 14 | import random |
15 | 15 | import string |
| 16 | +import ast |
16 | 17 | from pathlib import Path |
17 | 18 | from collections import defaultdict |
18 | 19 | import threading |
@@ -637,11 +638,18 @@ def button_click(value): |
637 | 638 | nonlocal current_expression |
638 | 639 | if value == '=': |
639 | 640 | try: |
640 | | - result = eval(current_expression) |
641 | | - history_text.insert(tk.END, f"{current_expression} = {result}\n") |
642 | | - history_text.see(tk.END) |
643 | | - display_var.set(str(result)) |
644 | | - current_expression = str(result) |
| 641 | + # Safe evaluation using ast module |
| 642 | + # Only allow mathematical operations |
| 643 | + allowed_chars = set('0123456789+-*/().% ') |
| 644 | + if all(c in allowed_chars for c in current_expression): |
| 645 | + result = eval(current_expression, {"__builtins__": {}}, {}) |
| 646 | + history_text.insert(tk.END, f"{current_expression} = {result}\n") |
| 647 | + history_text.see(tk.END) |
| 648 | + display_var.set(str(result)) |
| 649 | + current_expression = str(result) |
| 650 | + else: |
| 651 | + display_var.set("Error") |
| 652 | + current_expression = "" |
645 | 653 | except: |
646 | 654 | display_var.set("Error") |
647 | 655 | current_expression = "" |
@@ -832,7 +840,7 @@ def go_home(): |
832 | 840 | refresh_files() |
833 | 841 |
|
834 | 842 | def create_new_folder(): |
835 | | - folder_name = tk.simpledialog.askstring("New Folder", "Enter folder name:") |
| 843 | + folder_name = simpledialog.askstring("New Folder", "Enter folder name:") |
836 | 844 | if folder_name: |
837 | 845 | new_folder_path = os.path.join(current_path[0], folder_name) |
838 | 846 | try: |
@@ -1014,7 +1022,7 @@ def delete_file(self, filepath, callback): |
1014 | 1022 | def rename_file(self, filepath, callback): |
1015 | 1023 | """Rename file or folder""" |
1016 | 1024 | old_name = os.path.basename(filepath) |
1017 | | - new_name = tk.simpledialog.askstring("Rename", "Enter new name:", initialvalue=old_name) |
| 1025 | + new_name = simpledialog.askstring("Rename", "Enter new name:", initialvalue=old_name) |
1018 | 1026 | if new_name and new_name != old_name: |
1019 | 1027 | try: |
1020 | 1028 | new_path = os.path.join(os.path.dirname(filepath), new_name) |
@@ -1245,8 +1253,13 @@ def process_terminal_command(self, command): |
1245 | 1253 | if len(parts) > 1: |
1246 | 1254 | try: |
1247 | 1255 | expr = " ".join(parts[1:]) |
1248 | | - result = eval(expr) |
1249 | | - return f"{expr} = {result}" |
| 1256 | + # Safe evaluation - only allow mathematical operations |
| 1257 | + allowed_chars = set('0123456789+-*/().% ') |
| 1258 | + if all(c in allowed_chars for c in expr): |
| 1259 | + result = eval(expr, {"__builtins__": {}}, {}) |
| 1260 | + return f"{expr} = {result}" |
| 1261 | + else: |
| 1262 | + return "Error: Invalid characters in expression" |
1250 | 1263 | except Exception as e: |
1251 | 1264 | return f"Error: {str(e)}" |
1252 | 1265 | return "Usage: calc <expression>" |
|
0 commit comments