Skip to content

Commit c933376

Browse files
committed
BE: SYNC API logging
1 parent b5d2806 commit c933376

3 files changed

Lines changed: 83 additions & 21 deletions

File tree

docs/SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This includes (but is not limited to):
88
- Running NetAlertX only on networks where you have legal authorization
99
- Keeping your deployment up to date with the latest patches
1010

11-
> NetAlertX is not responsible for misuse, misconfiguration, or unsecure deployments. Always test and secure your setup before exposing it to the outside world.
11+
> NetAlertX is not responsible for misuse, misconfiguration, or insecure deployments. Always test and secure your setup before exposing it to the outside world. Users interacting with the UI are treated as trusted actors within the deployment model. Always properly secure and isolate your deployment before exposing it externally.
1212
1313
# 🔐 Securing Your NetAlertX Instance
1414

server/api_server/sync_endpoint.py

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,102 @@ def handle_sync_get():
4747

4848
def handle_sync_post():
4949
"""Handle POST requests for SYNC (HUB receiving from NODE)."""
50-
body = request.get_json(silent=True) or {}
50+
51+
mylog("verbose", [
52+
"[SYNC API] ENTER handle_sync_post",
53+
f"method={request.method}",
54+
f"content_type={request.content_type}",
55+
f"content_length={request.content_length}",
56+
f"remote_addr={request.remote_addr}"
57+
])
58+
59+
# ---- RAW BODY (critical for debugging encoding / encryption issues)
60+
try:
61+
raw = request.get_data(cache=False)
62+
mylog("verbose", [
63+
f"[SYNC API] raw_bytes_len={len(raw)}",
64+
f"[SYNC API] raw_preview={raw[:200]}"
65+
])
66+
except Exception as e:
67+
mylog("none", [f"[SYNC API] FAILED reading raw body: {e}"])
68+
return jsonify({"error": "failed reading body"}), 400
69+
70+
# ---- JSON PARSE (this is a very common failure point)
71+
try:
72+
body = request.get_json(force=False, silent=False)
73+
mylog("verbose", [f"[SYNC API] parsed_json={body}"])
74+
except Exception as e:
75+
mylog("none", [f"[SYNC API] JSON_PARSE_FAILED={e}"])
76+
return jsonify({"error": "invalid json"}), 400
77+
78+
# ---- EXTRACT FIELDS
5179
data = body.get("data", "")
5280
node_name = body.get("node_name", "")
5381
plugin = body.get("plugin", "")
5482

83+
mylog("verbose", [
84+
f"[SYNC API] node_name={repr(node_name)}",
85+
f"[SYNC API] plugin={repr(plugin)}",
86+
f"[SYNC API] data_type={type(data).__name__}",
87+
f"[SYNC API] data_len={len(data) if isinstance(data, str) else 'non-string'}"
88+
])
89+
5590
storage_path = INSTALL_PATH + "/log/plugins"
56-
os.makedirs(storage_path, exist_ok=True)
57-
58-
encoded_files = [
59-
f
60-
for f in os.listdir(storage_path)
61-
if f.startswith(f"last_result.{plugin}.encoded.{node_name}")
62-
]
63-
decoded_files = [
64-
f
65-
for f in os.listdir(storage_path)
66-
if f.startswith(f"last_result.{plugin}.decoded.{node_name}")
67-
]
68-
file_count = len(encoded_files + decoded_files) + 1
6991

92+
try:
93+
os.makedirs(storage_path, exist_ok=True)
94+
mylog("verbose", [f"[SYNC API] storage_path_ready={storage_path}"])
95+
except Exception as e:
96+
mylog("none", [f"[SYNC API] MKDIR_FAILED={e}"])
97+
return jsonify({"error": "storage path error"}), 500
98+
99+
# ---- FILE COUNT LOGIC
100+
try:
101+
encoded_files = [
102+
f for f in os.listdir(storage_path)
103+
if f.startswith(f"last_result.{plugin}.encoded.{node_name}")
104+
]
105+
decoded_files = [
106+
f for f in os.listdir(storage_path)
107+
if f.startswith(f"last_result.{plugin}.decoded.{node_name}")
108+
]
109+
file_count = len(encoded_files + decoded_files) + 1
110+
111+
mylog("verbose", [
112+
f"[SYNC API] encoded_files={len(encoded_files)}",
113+
f"[SYNC API] decoded_files={len(decoded_files)}",
114+
f"[SYNC API] file_count={file_count}"
115+
])
116+
except Exception as e:
117+
mylog("none", [f"[SYNC API] LISTDIR_FAILED={e}"])
118+
return jsonify({"error": "listdir failed"}), 500
119+
120+
# ---- FILE PATH
70121
file_path_new = os.path.join(
71-
storage_path, f"last_result.{plugin}.encoded.{node_name}.{file_count}.log"
122+
storage_path,
123+
f"last_result.{plugin}.encoded.{node_name}.{file_count}.log"
72124
)
73125

126+
mylog("verbose", [f"[SYNC API] file_path_new={file_path_new}"])
127+
128+
# ---- WRITE FILE (final critical point)
74129
try:
130+
if not isinstance(data, str):
131+
data = str(data)
132+
75133
with open(file_path_new, "w") as f:
76134
f.write(data)
135+
77136
except Exception as e:
78-
msg = f"[Plugin: SYNC] Failed to store data: {e}"
79-
write_notification(msg, "alert", timeNowUTC())
80-
mylog("verbose", [msg])
81-
return jsonify({"error": msg}), 500
137+
import traceback
138+
mylog("none", [
139+
f"[SYNC API] WRITE_FAILED={e}",
140+
traceback.format_exc()
141+
])
142+
return jsonify({"error": str(e)}), 500
82143

83144
msg = f"[Plugin: SYNC] Data received ({file_path_new})"
84145
write_notification(msg, "info", timeNowUTC())
85146
mylog("verbose", [msg])
147+
86148
return jsonify({"message": "Data received and stored successfully"}), 200

server/utils/datetime_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import pytz
88
from typing import Union, Optional
9-
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
9+
from zoneinfo import ZoneInfo
1010
import email.utils
1111
import conf
1212
# from const import *

0 commit comments

Comments
 (0)