Warn if running as admin/root

This commit is contained in:
unknown 2022-11-16 09:34:25 +01:00
parent 38651d7ca7
commit a2648f5be3
2 changed files with 50 additions and 1 deletions

View file

@ -1,5 +1,6 @@
import os
import sys
import ctypes
import platform
try:
@ -39,6 +40,21 @@ def osVer():
return " ".join(platform.linux_distribution())
def isRoot():
"""Return True if running with elevated privileges."""
# Windows
try:
if isWin32():
return ctypes.windll.shell32.IsUserAnAdmin() == 1
except OSError as win_issue:
print(win_issue)
# Unix
if hasattr(os, "getuid"):
return not os.getuid() # 0 if root
# Just assume it's fine otherwise ig
return False
def validateDataDir():
"""Checks if data directory is present"""
# Define paths

View file

@ -1304,7 +1304,6 @@ class PesterWindow(MovingWindow):
| QtCore.Qt.WindowType.FramelessWindowHint
),
)
# For debugging
_CONSOLE_ENV.PAPP = self
# TODO: karxi: SO! At the end of this function it seems like that
@ -1370,6 +1369,10 @@ class PesterWindow(MovingWindow):
)
self.theme = self.userprofile.getTheme()
# Silly guy prevention pt. 2
# We really shouldn't run as root.
self.root_check()
# karxi: For the record, these are set via commandline arguments. By
# default, they aren't usable any other way - you can't set them via
# the config files.
@ -1700,6 +1703,36 @@ class PesterWindow(MovingWindow):
self.updatemenu = None
"""
def root_check(self):
"""Raise a warning message box if Pesterchum has admin/root privileges."""
if ostools.isRoot():
msgbox = QtWidgets.QMessageBox()
msg = (
"Running with elevated privileges, "
"this is potentially a security risk."
"\nThere is no valid reason to run Pesterchum as an administrator or as root."
"\n\nQuit?"
)
msgbox.setWindowTitle("Unnecessary permissions warning")
msgbox.setStyleSheet(
"QMessageBox{ %s }" % self.theme["main/defaultwindow/style"]
)
msgbox.setInformativeText(msg)
msgbox.setIcon(QtWidgets.QMessageBox.Icon.Warning)
msgbox.setStandardButtons(
QtWidgets.QMessageBox.StandardButton.Yes
| QtWidgets.QMessageBox.StandardButton.No
)
continue_anyway = msgbox.button(QtWidgets.QMessageBox.StandardButton.No)
continue_anyway.setText(
"I'm a silly little guy and want to continue anyway"
)
msgbox.setDefaultButton(QtWidgets.QMessageBox.StandardButton.Yes)
ret = msgbox.exec()
if ret == QtWidgets.QMessageBox.StandardButton.Yes:
self.app.quit() # Optional
sys.exit()
@QtCore.pyqtSlot()
def checkPing(self):
"""Check if server is alive on app level,