Skip to content

Commit 156ae02

Browse files
Fix security issues: safe eval, proper imports
Co-authored-by: codingwithnsh <138281862+codingwithnsh@users.noreply.github.com>
1 parent 6e0c902 commit 156ae02

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

main.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
33
import psutil
44
import time
55
import os
@@ -13,6 +13,7 @@
1313
import math
1414
import random
1515
import string
16+
import ast
1617
from pathlib import Path
1718
from collections import defaultdict
1819
import threading
@@ -637,11 +638,18 @@ def button_click(value):
637638
nonlocal current_expression
638639
if value == '=':
639640
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 = ""
645653
except:
646654
display_var.set("Error")
647655
current_expression = ""
@@ -832,7 +840,7 @@ def go_home():
832840
refresh_files()
833841

834842
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:")
836844
if folder_name:
837845
new_folder_path = os.path.join(current_path[0], folder_name)
838846
try:
@@ -1014,7 +1022,7 @@ def delete_file(self, filepath, callback):
10141022
def rename_file(self, filepath, callback):
10151023
"""Rename file or folder"""
10161024
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)
10181026
if new_name and new_name != old_name:
10191027
try:
10201028
new_path = os.path.join(os.path.dirname(filepath), new_name)
@@ -1245,8 +1253,13 @@ def process_terminal_command(self, command):
12451253
if len(parts) > 1:
12461254
try:
12471255
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"
12501263
except Exception as e:
12511264
return f"Error: {str(e)}"
12521265
return "Usage: calc <expression>"

0 commit comments

Comments
 (0)