Skip to content

Commit ee22450

Browse files
committed
Harden Windows notification delivery
1 parent 4309d85 commit ee22450

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

TkEasyGUI/dialogs.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import subprocess
77
import sys
8-
import tempfile
98
import tkinter
109
from datetime import datetime, timedelta
1110
from re import Pattern
@@ -1362,25 +1361,33 @@ def to_base64(s: str) -> str:
13621361

13631362
encoded_title = to_base64(title)
13641363
encoded_message = to_base64(message)
1365-
app_id = sys.executable.replace("\\", "\\\\")
1364+
script_content = r"""
1365+
$ErrorActionPreference = "Stop"
13661366
1367-
# PowerShell Script using Base64 (embedded and executed via -EncodedCommand)
1368-
script_content = rf"""
1369-
$encodedTitle = "{encoded_title}"
1370-
$encodedMessage = "{encoded_message}"
1371-
$appPath = "{app_id}"
1367+
$encodedTitle = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_TITLE_B64")
1368+
$encodedMessage = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_MESSAGE_B64")
1369+
$appId = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_APP_ID")
1370+
1371+
if ([string]::IsNullOrEmpty($encodedTitle) -or [string]::IsNullOrEmpty($encodedMessage) -or [string]::IsNullOrEmpty($appId)) {
1372+
exit 1
1373+
}
13721374
13731375
$decodedTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedTitle))
13741376
$decodedMessage = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedMessage))
1375-
$bodyText = "$decodedTitle`n$decodedMessage"
13761377
1377-
$ToastText01 = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText01
1378-
$TemplateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText01)
1379-
$TemplateContent.SelectSingleNode('//text[@id="1"]').InnerText = $bodyText
1380-
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appPath).Show($TemplateContent)
1378+
$toastType = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText02
1379+
$templateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($toastType)
1380+
$textNodes = $templateContent.GetElementsByTagName("text")
1381+
$textNodes.Item(0).AppendChild($templateContent.CreateTextNode($decodedTitle)) | Out-Null
1382+
$textNodes.Item(1).AppendChild($templateContent.CreateTextNode($decodedMessage)) | Out-Null
1383+
$toast = [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]::new($templateContent)
1384+
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appId).Show($toast)
13811385
""".strip()
13821386

1383-
encoded_script = base64.b64encode(script_content.encode("utf-16le")).decode("ascii")
1387+
ps_env = os.environ.copy()
1388+
ps_env["TKEASYGUI_NOTIFY_TITLE_B64"] = encoded_title
1389+
ps_env["TKEASYGUI_NOTIFY_MESSAGE_B64"] = encoded_message
1390+
ps_env["TKEASYGUI_NOTIFY_APP_ID"] = sys.executable
13841391

13851392
try:
13861393
subprocess.run(
@@ -1391,9 +1398,13 @@ def to_base64(s: str) -> str:
13911398
"-NonInteractive",
13921399
"-ExecutionPolicy",
13931400
"Bypass",
1394-
"-EncodedCommand",
1395-
encoded_script,
1401+
"-Command",
1402+
"-",
13961403
],
1404+
input=script_content,
1405+
text=True,
1406+
encoding="utf-8",
1407+
env=ps_env,
13971408
check=True,
13981409
)
13991410
return True

0 commit comments

Comments
 (0)