From 252d27690d2d7fe1504ae68674f4097eb67bdcff Mon Sep 17 00:00:00 2001 From: Boss_1s <95505913+Boss-1s@users.noreply.github.com> Date: Thu, 7 May 2026 17:23:01 -0500 Subject: [PATCH 1/4] refactor: Add try-except blocks for loading json on connect and reading data #537 Signed-off-by: Boss_1s <95505913+Boss-1s@users.noreply.github.com> --- scratchattach/eventhandlers/cloud_server.py | 32 ++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/scratchattach/eventhandlers/cloud_server.py b/scratchattach/eventhandlers/cloud_server.py index d950f56f..10c78502 100644 --- a/scratchattach/eventhandlers/cloud_server.py +++ b/scratchattach/eventhandlers/cloud_server.py @@ -131,18 +131,30 @@ def handleMessage(self): if self.server.check_for_ip_ban(self): return - data = json.loads(self.data) + try: + data = json.loads(self.data) + except json.decoder.JSONDecodeError: + print(f"Warning! Client {self.address[0] + ":" + str(self.address[1])} sent invalid JSON to the server. ", + "The client may be unsafe, please stay alert." + ) print(data) - if data["method"] == "set": - self.handle_set(data) - elif data["method"] == "handshake": - self.handle_handshake(data) - else: + try: + if data["method"] == "set": + self.handle_set(data) + elif data["method"] == "handshake": + self.handle_handshake(data) + else: + print( + "Error:", + self.address[0] + ":" + str(self.address[1]), + "sent a message without providing a valid method (set, handshake)", + ) + except KeyError: print( - "Error:", - self.address[0] + ":" + str(self.address[1]), - "sent a message without providing a valid method (set, handshake)", + "Error:", + self.address[0] + ":" + str(self.address[1]), + "sent a message without providing a valid method (set, handshake)", ) except Exception as e: @@ -344,7 +356,7 @@ def resume(self): self.running = True def stop(self, wait_call_threads: bool = True): - BaseEventHandler.stop(self, wait_call_threads) + BaseEventHandler.stop(self, wait_call_threads) # wait_call_threads does not exist in BaseEventHandler.stop self.close() From 7fdfe0fcef8e0ca85cf78b24a3f1d90945b7af33 Mon Sep 17 00:00:00 2001 From: Boss_1s <95505913+Boss-1s@users.noreply.github.com> Date: Thu, 7 May 2026 18:10:37 -0500 Subject: [PATCH 2/4] Finish debug for json loading and reading 'method' key Signed-off-by: Boss_1s <95505913+Boss-1s@users.noreply.github.com> --- scratchattach/eventhandlers/cloud_server.py | 24 +++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scratchattach/eventhandlers/cloud_server.py b/scratchattach/eventhandlers/cloud_server.py index 10c78502..621b23e9 100644 --- a/scratchattach/eventhandlers/cloud_server.py +++ b/scratchattach/eventhandlers/cloud_server.py @@ -137,9 +137,17 @@ def handleMessage(self): print(f"Warning! Client {self.address[0] + ":" + str(self.address[1])} sent invalid JSON to the server. ", "The client may be unsafe, please stay alert." ) - print(data) + return - try: + print(f"Data recieved: {data}") + if data == {}: + print( + "Error:", + self.address[0] + ":" + str(self.address[1]), + "sent a blank JSON message. If this seems suspicious, ban the IP.", + ) + return + if 'method' in data: if data["method"] == "set": self.handle_set(data) elif data["method"] == "handshake": @@ -149,16 +157,18 @@ def handleMessage(self): "Error:", self.address[0] + ":" + str(self.address[1]), "sent a message without providing a valid method (set, handshake)", + f"but provided method {list(data.values())[0]} instead.", ) - except KeyError: + else: print( - "Error:", - self.address[0] + ":" + str(self.address[1]), - "sent a message without providing a valid method (set, handshake)", + "Error:", + self.address[0] + ":" + str(self.address[1]), + "sent a message without providing a valid 'method' key,", + f"but provided key {list(data.keys())[0]} instead.", ) except Exception as e: - print("Internal error in handleMessage:", e, traceback.format_exc()) + print("Internal error in handleMessage:", e, "\n", traceback.format_exc()) def handleConnected(self): if not self.server.running: From a44c3f5be3d3da946d696c960123009b0b1a2891 Mon Sep 17 00:00:00 2001 From: Boss_1s <95505913+Boss-1s@users.noreply.github.com> Date: Fri, 8 May 2026 15:52:51 -0500 Subject: [PATCH 3/4] a little more context on the ip banned message Signed-off-by: Boss_1s <95505913+Boss-1s@users.noreply.github.com> --- scratchattach/eventhandlers/cloud_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scratchattach/eventhandlers/cloud_server.py b/scratchattach/eventhandlers/cloud_server.py index 621b23e9..33834d71 100644 --- a/scratchattach/eventhandlers/cloud_server.py +++ b/scratchattach/eventhandlers/cloud_server.py @@ -250,7 +250,7 @@ def check_for_ip_ban(self, client): ): client.sendMessage("You have been banned from this server") client.close(4002) - print(client.address[0] + ":" + str(client.address[1]), "(IP-banned) was disconnected") + print(client.address[0] + ":" + str(client.address[1]), "(IP-banned) was forced disconnected") return True return False From a05828e4661eb9301048c8f2110712644117ae11 Mon Sep 17 00:00:00 2001 From: Boss_1s <95505913+Boss-1s@users.noreply.github.com> Date: Fri, 8 May 2026 21:14:30 -0500 Subject: [PATCH 4/4] [UNIMPORTANT] notated unsued import Signed-off-by: Boss_1s <95505913+Boss-1s@users.noreply.github.com> --- scratchattach/eventhandlers/cloud_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scratchattach/eventhandlers/cloud_server.py b/scratchattach/eventhandlers/cloud_server.py index 33834d71..12bc7cbe 100644 --- a/scratchattach/eventhandlers/cloud_server.py +++ b/scratchattach/eventhandlers/cloud_server.py @@ -1,7 +1,7 @@ from __future__ import annotations from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket -from threading import Thread +from threading import Thread # unused threading.Thread? not changing, just noting -Boss_1s from scratchattach.utils import exceptions import json import time