bstkchat 2.4.0__tar.gz → 2.4.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bstkchat-2.4.0 → bstkchat-2.4.2}/PKG-INFO +1 -1
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/cli.py +26 -4
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/web.py +53 -2
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/web_ui.html +80 -7
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/PKG-INFO +1 -1
- {bstkchat-2.4.0 → bstkchat-2.4.2}/pyproject.toml +1 -1
- {bstkchat-2.4.0 → bstkchat-2.4.2}/setup.py +1 -1
- {bstkchat-2.4.0 → bstkchat-2.4.2}/tests/test_web.py +31 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/README.md +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/__init__.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/chain.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/config.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat/crypto.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/SOURCES.txt +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/dependency_links.txt +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/entry_points.txt +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/requires.txt +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/bstkchat.egg-info/top_level.txt +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/setup.cfg +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/tests/test_chain.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/tests/test_cli.py +0 -0
- {bstkchat-2.4.0 → bstkchat-2.4.2}/tests/test_crypto.py +0 -0
|
@@ -57,7 +57,7 @@ from bstkchat.crypto import (
|
|
|
57
57
|
sanitize_room_id,
|
|
58
58
|
)
|
|
59
59
|
|
|
60
|
-
APP_VERSION = "2.4.
|
|
60
|
+
APP_VERSION = "2.4.2"
|
|
61
61
|
AUTO_AWAY_SECONDS = 300
|
|
62
62
|
TYPING_TTL_SECONDS = 5.0
|
|
63
63
|
TYPING_THROTTLE_SECONDS = 3.0
|
|
@@ -266,6 +266,7 @@ class ChatClient:
|
|
|
266
266
|
self.peers: Dict[str, Dict[str, object]] = {}
|
|
267
267
|
self.typing_peers: Dict[str, float] = {}
|
|
268
268
|
self.pending_files: Dict[str, Dict[str, object]] = {}
|
|
269
|
+
self.shared_files: Dict[str, Dict[str, object]] = {}
|
|
269
270
|
self._file_seq = 0
|
|
270
271
|
|
|
271
272
|
self.running = True
|
|
@@ -843,12 +844,14 @@ class ChatClient:
|
|
|
843
844
|
with self._lock:
|
|
844
845
|
self._file_seq += 1
|
|
845
846
|
file_id = str(self._file_seq)
|
|
846
|
-
|
|
847
|
+
file_entry = {
|
|
847
848
|
"sender": sender,
|
|
848
849
|
"filename": filename,
|
|
849
850
|
"data": raw_bytes,
|
|
850
851
|
"size": len(raw_bytes),
|
|
851
852
|
}
|
|
853
|
+
self.pending_files[file_id] = file_entry
|
|
854
|
+
self.shared_files[file_id] = file_entry
|
|
852
855
|
|
|
853
856
|
size_kb = len(raw_bytes) / 1024.0
|
|
854
857
|
self.print_system(
|
|
@@ -864,6 +867,7 @@ class ChatClient:
|
|
|
864
867
|
"sender": sender,
|
|
865
868
|
"filename": filename,
|
|
866
869
|
"size": len(raw_bytes),
|
|
870
|
+
"own": False,
|
|
867
871
|
"time": datetime.now().strftime("%H:%M:%S"),
|
|
868
872
|
},
|
|
869
873
|
record_history=True,
|
|
@@ -871,6 +875,10 @@ class ChatClient:
|
|
|
871
875
|
self._emit_event("state_update", self.get_web_state())
|
|
872
876
|
self._ring_bell()
|
|
873
877
|
|
|
878
|
+
def get_shared_file(self, file_id: str) -> Optional[Dict[str, object]]:
|
|
879
|
+
with self._lock:
|
|
880
|
+
return self.shared_files.get(file_id)
|
|
881
|
+
|
|
874
882
|
def accept_file(self, file_id: Optional[str] = None, target_dir: str = "") -> Optional[str]:
|
|
875
883
|
"""Saves a pending encrypted file transfer to ~/Downloads/bstkchat."""
|
|
876
884
|
with self._lock:
|
|
@@ -909,15 +917,28 @@ class ChatClient:
|
|
|
909
917
|
"data": base64.b64encode(raw_bytes).decode("ascii"),
|
|
910
918
|
})
|
|
911
919
|
if self._publish_encrypted("file", payload_json):
|
|
920
|
+
with self._lock:
|
|
921
|
+
self._file_seq += 1
|
|
922
|
+
file_id = str(self._file_seq)
|
|
923
|
+
self.shared_files[file_id] = {
|
|
924
|
+
"sender": self.nickname,
|
|
925
|
+
"filename": clean_name,
|
|
926
|
+
"data": raw_bytes,
|
|
927
|
+
"size": len(raw_bytes),
|
|
928
|
+
}
|
|
912
929
|
self.print_system(
|
|
913
930
|
f"📤 Sent encrypted file [bold white]{escape_markup(clean_name)}[/] "
|
|
914
931
|
f"({len(raw_bytes) / 1024:.1f} KB).",
|
|
915
932
|
log_text=f"Sent file {clean_name}",
|
|
916
933
|
)
|
|
917
934
|
self._emit_event(
|
|
918
|
-
"
|
|
935
|
+
"file_offer",
|
|
919
936
|
{
|
|
920
|
-
"
|
|
937
|
+
"id": file_id,
|
|
938
|
+
"sender": self.nickname,
|
|
939
|
+
"filename": clean_name,
|
|
940
|
+
"size": len(raw_bytes),
|
|
941
|
+
"own": True,
|
|
921
942
|
"time": datetime.now().strftime("%H:%M:%S"),
|
|
922
943
|
},
|
|
923
944
|
record_history=True,
|
|
@@ -1091,6 +1112,7 @@ class ChatClient:
|
|
|
1091
1112
|
self.peers.clear()
|
|
1092
1113
|
self.typing_peers.clear()
|
|
1093
1114
|
self.pending_files.clear()
|
|
1115
|
+
self.shared_files.clear()
|
|
1094
1116
|
new_msg_topic = self.msg_topic
|
|
1095
1117
|
new_presence_topic = self.presence_topic
|
|
1096
1118
|
new_sync_topic = self.sync_topic
|
|
@@ -7,15 +7,23 @@ import json
|
|
|
7
7
|
import os
|
|
8
8
|
import queue
|
|
9
9
|
import secrets
|
|
10
|
+
import sys
|
|
10
11
|
import threading
|
|
11
12
|
from http import HTTPStatus
|
|
12
13
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
13
14
|
from typing import Any, Dict, Optional
|
|
14
15
|
from urllib.parse import parse_qs, urlparse
|
|
15
16
|
|
|
16
|
-
from bstkchat.config import MAX_FILE_BYTES, search_log
|
|
17
|
+
from bstkchat.config import MAX_FILE_BYTES, sanitize_filename, search_log
|
|
17
18
|
|
|
18
19
|
MAX_BODY_BYTES = 1024 * 1024 # 1 MB max JSON request body
|
|
20
|
+
SAFE_INLINE_IMAGES = {
|
|
21
|
+
".png": "image/png",
|
|
22
|
+
".jpg": "image/jpeg",
|
|
23
|
+
".jpeg": "image/jpeg",
|
|
24
|
+
".gif": "image/gif",
|
|
25
|
+
".webp": "image/webp",
|
|
26
|
+
}
|
|
19
27
|
|
|
20
28
|
|
|
21
29
|
def _load_web_html() -> bytes:
|
|
@@ -33,6 +41,18 @@ def _load_web_html() -> bytes:
|
|
|
33
41
|
return f.read()
|
|
34
42
|
|
|
35
43
|
|
|
44
|
+
class QuietThreadingHTTPServer(ThreadingHTTPServer):
|
|
45
|
+
"""ThreadingHTTPServer that suppresses benign browser socket-abort tracebacks."""
|
|
46
|
+
|
|
47
|
+
def handle_error(self, request, client_address):
|
|
48
|
+
_, exc, _ = sys.exc_info()
|
|
49
|
+
if isinstance(exc, (ConnectionAbortedError, ConnectionResetError, BrokenPipeError)):
|
|
50
|
+
return
|
|
51
|
+
if isinstance(exc, OSError) and getattr(exc, "winerror", None) in (10053, 10054):
|
|
52
|
+
return
|
|
53
|
+
super().handle_error(request, client_address)
|
|
54
|
+
|
|
55
|
+
|
|
36
56
|
class WebServer:
|
|
37
57
|
"""Localhost-only HTTP + SSE server bound to 127.0.0.1 with per-session token auth."""
|
|
38
58
|
|
|
@@ -57,6 +77,12 @@ class WebServer:
|
|
|
57
77
|
class Handler(BaseHTTPRequestHandler):
|
|
58
78
|
protocol_version = "HTTP/1.1"
|
|
59
79
|
|
|
80
|
+
def handle(self):
|
|
81
|
+
try:
|
|
82
|
+
super().handle()
|
|
83
|
+
except (ConnectionAbortedError, ConnectionResetError, BrokenPipeError):
|
|
84
|
+
pass
|
|
85
|
+
|
|
60
86
|
def log_message(self, format, *args):
|
|
61
87
|
# Suppress noisy stdout access logs so terminal TUI stays clean
|
|
62
88
|
return
|
|
@@ -117,6 +143,31 @@ class WebServer:
|
|
|
117
143
|
self._send_json(HTTPStatus.OK, {"matches": matches})
|
|
118
144
|
return
|
|
119
145
|
|
|
146
|
+
if parsed.path == "/api/file/preview":
|
|
147
|
+
qs = parse_qs(parsed.query)
|
|
148
|
+
file_id = qs.get("id", [""])[0]
|
|
149
|
+
force_download = qs.get("download", ["0"])[0] == "1"
|
|
150
|
+
entry = server_ref.chat.get_shared_file(file_id)
|
|
151
|
+
if not entry:
|
|
152
|
+
self._send_json(HTTPStatus.NOT_FOUND, {"error": "File not found"})
|
|
153
|
+
return
|
|
154
|
+
filename = sanitize_filename(str(entry["filename"]))
|
|
155
|
+
ext = os.path.splitext(filename)[1].lower()
|
|
156
|
+
raw_bytes = bytes(entry["data"])
|
|
157
|
+
mime = SAFE_INLINE_IMAGES.get(ext, "application/octet-stream")
|
|
158
|
+
is_inline = (ext in SAFE_INLINE_IMAGES) and not force_download
|
|
159
|
+
disp = f'inline; filename="{filename}"' if is_inline else f'attachment; filename="{filename}"'
|
|
160
|
+
|
|
161
|
+
self.send_response(HTTPStatus.OK)
|
|
162
|
+
self.send_header("Content-Type", mime)
|
|
163
|
+
self.send_header("Content-Length", str(len(raw_bytes)))
|
|
164
|
+
self.send_header("Content-Disposition", disp)
|
|
165
|
+
self.send_header("X-Content-Type-Options", "nosniff")
|
|
166
|
+
self.send_header("Cache-Control", "no-store")
|
|
167
|
+
self.end_headers()
|
|
168
|
+
self.wfile.write(raw_bytes)
|
|
169
|
+
return
|
|
170
|
+
|
|
120
171
|
if parsed.path == "/events":
|
|
121
172
|
self.send_response(HTTPStatus.OK)
|
|
122
173
|
self.send_header("Content-Type", "text/event-stream; charset=utf-8")
|
|
@@ -241,7 +292,7 @@ class WebServer:
|
|
|
241
292
|
|
|
242
293
|
self._send_json(HTTPStatus.NOT_FOUND, {"error": "Not found"})
|
|
243
294
|
|
|
244
|
-
self._httpd =
|
|
295
|
+
self._httpd = QuietThreadingHTTPServer((self.host, self.port), Handler)
|
|
245
296
|
self._thread = threading.Thread(target=self._httpd.serve_forever, daemon=True)
|
|
246
297
|
self._thread.start()
|
|
247
298
|
return self.url
|
|
@@ -260,6 +260,44 @@
|
|
|
260
260
|
outline: none;
|
|
261
261
|
}
|
|
262
262
|
.composer textarea:focus { border-color: var(--accent); }
|
|
263
|
+
.chat-img {
|
|
264
|
+
display: block;
|
|
265
|
+
max-width: 320px;
|
|
266
|
+
max-height: 260px;
|
|
267
|
+
border-radius: 10px;
|
|
268
|
+
margin: 6px 0;
|
|
269
|
+
cursor: zoom-in;
|
|
270
|
+
border: 1px solid var(--border);
|
|
271
|
+
object-fit: cover;
|
|
272
|
+
}
|
|
273
|
+
.file-card {
|
|
274
|
+
display: flex;
|
|
275
|
+
align-items: center;
|
|
276
|
+
gap: 10px;
|
|
277
|
+
padding: 8px 10px;
|
|
278
|
+
border-radius: 10px;
|
|
279
|
+
background: var(--surface-2);
|
|
280
|
+
border: 1px solid var(--border);
|
|
281
|
+
margin-top: 4px;
|
|
282
|
+
flex-wrap: wrap;
|
|
283
|
+
}
|
|
284
|
+
.lightbox {
|
|
285
|
+
position: fixed;
|
|
286
|
+
inset: 0;
|
|
287
|
+
background: rgba(2, 6, 23, 0.88);
|
|
288
|
+
display: none;
|
|
289
|
+
align-items: center;
|
|
290
|
+
justify-content: center;
|
|
291
|
+
z-index: 999;
|
|
292
|
+
padding: 24px;
|
|
293
|
+
cursor: zoom-out;
|
|
294
|
+
}
|
|
295
|
+
.lightbox img {
|
|
296
|
+
max-width: 92vw;
|
|
297
|
+
max-height: 90vh;
|
|
298
|
+
border-radius: 12px;
|
|
299
|
+
box-shadow: 0 20px 60px rgba(0,0,0,0.6);
|
|
300
|
+
}
|
|
263
301
|
@media (max-width: 820px) {
|
|
264
302
|
body { grid-template-columns: 1fr; }
|
|
265
303
|
.sidebar { display: none; }
|
|
@@ -329,7 +367,7 @@
|
|
|
329
367
|
<option value="">Everyone in room</option>
|
|
330
368
|
</select>
|
|
331
369
|
<label class="btn-ghost" style="cursor:pointer;">
|
|
332
|
-
📎 Send File (≤512KB)
|
|
370
|
+
📎 Send Image / File (≤512KB)
|
|
333
371
|
<input id="fileInput" type="file" style="display:none;" onchange="uploadFile(this)" />
|
|
334
372
|
</label>
|
|
335
373
|
</div>
|
|
@@ -340,6 +378,10 @@
|
|
|
340
378
|
</footer>
|
|
341
379
|
</main>
|
|
342
380
|
|
|
381
|
+
<div id="lightbox" class="lightbox" onclick="this.style.display='none'">
|
|
382
|
+
<img id="lightboxImg" src="" alt="Preview" />
|
|
383
|
+
</div>
|
|
384
|
+
|
|
343
385
|
<script>
|
|
344
386
|
const TOKEN = new URLSearchParams(location.search).get("token") || "";
|
|
345
387
|
const msgEl = document.getElementById("messages");
|
|
@@ -416,19 +458,50 @@
|
|
|
416
458
|
msgEl.scrollTop = msgEl.scrollHeight;
|
|
417
459
|
}
|
|
418
460
|
|
|
461
|
+
function isImageFile(name) {
|
|
462
|
+
return /\.(png|jpe?g|gif|webp)$/i.test(String(name || ""));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function openLightbox(src) {
|
|
466
|
+
document.getElementById("lightboxImg").src = src;
|
|
467
|
+
document.getElementById("lightbox").style.display = "flex";
|
|
468
|
+
}
|
|
469
|
+
|
|
419
470
|
function appendFileOffer(data) {
|
|
420
471
|
const div = document.createElement("div");
|
|
421
|
-
div.className = "msg";
|
|
472
|
+
div.className = "msg" + (data.own ? " own" : "");
|
|
422
473
|
const kb = (Number(data.size || 0) / 1024).toFixed(1);
|
|
474
|
+
const fid = encodeURIComponent(data.id);
|
|
475
|
+
const tok = encodeURIComponent(TOKEN);
|
|
476
|
+
const previewUrl = `/api/file/preview?id=${fid}&token=${tok}`;
|
|
477
|
+
const downloadUrl = `/api/file/preview?id=${fid}&download=1&token=${tok}`;
|
|
478
|
+
const saveBtn = data.own
|
|
479
|
+
? ""
|
|
480
|
+
: `<button class="btn" onclick="acceptFile('${escapeHtml(data.id)}', this)">Save to Downloads</button>`;
|
|
481
|
+
|
|
482
|
+
const bodyHtml = isImageFile(data.filename)
|
|
483
|
+
? `
|
|
484
|
+
<img class="chat-img" src="${previewUrl}" alt="${escapeHtml(data.filename)}" onclick="openLightbox('${previewUrl}')" />
|
|
485
|
+
<div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap; font-size:0.8rem;">
|
|
486
|
+
<span style="color:var(--muted);">${escapeHtml(data.filename)} (${kb} KB)</span>
|
|
487
|
+
<a class="btn-ghost" style="text-decoration:none;" href="${downloadUrl}" download="${escapeHtml(data.filename)}">⬇ Download</a>
|
|
488
|
+
${saveBtn}
|
|
489
|
+
</div>
|
|
490
|
+
`
|
|
491
|
+
: `
|
|
492
|
+
<div class="file-card">
|
|
493
|
+
<span>📄 <strong>${escapeHtml(data.filename)}</strong> <span style="color:var(--muted);">(${kb} KB)</span></span>
|
|
494
|
+
<a class="btn-ghost" style="text-decoration:none;" href="${downloadUrl}" download="${escapeHtml(data.filename)}">⬇ Download</a>
|
|
495
|
+
${saveBtn}
|
|
496
|
+
</div>
|
|
497
|
+
`;
|
|
498
|
+
|
|
423
499
|
div.innerHTML = `
|
|
424
500
|
<div class="msg-head">
|
|
425
|
-
<span class="msg-sender"
|
|
501
|
+
<span class="msg-sender">${escapeHtml(data.own ? data.sender + " (You)" : data.sender)}</span>
|
|
426
502
|
<span class="msg-time">${escapeHtml(data.time || "")}</span>
|
|
427
503
|
</div>
|
|
428
|
-
|
|
429
|
-
<span><strong>${escapeHtml(data.filename)}</strong> (${kb} KB)</span>
|
|
430
|
-
<button class="btn" onclick="acceptFile('${escapeHtml(data.id)}', this)">Accept & Save</button>
|
|
431
|
-
</div>
|
|
504
|
+
${bodyHtml}
|
|
432
505
|
`;
|
|
433
506
|
msgEl.appendChild(div);
|
|
434
507
|
msgEl.scrollTop = msgEl.scrollHeight;
|
|
@@ -69,3 +69,34 @@ def test_web_server_auth_and_api(tmp_path):
|
|
|
69
69
|
assert json.loads(resp.read().decode("utf-8"))["ok"] is True
|
|
70
70
|
finally:
|
|
71
71
|
server.stop()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_web_file_preview_inline_image_and_attachment():
|
|
75
|
+
chat = ChatClient("Alice", "img-room", "pw", store_chain=False)
|
|
76
|
+
chat.client = MagicMock()
|
|
77
|
+
chat.safe_print = MagicMock()
|
|
78
|
+
chat.write_to_log = MagicMock()
|
|
79
|
+
|
|
80
|
+
assert chat.send_file_bytes("photo.png", b"\x89PNG\r\n\x1a\nfake") is True
|
|
81
|
+
assert chat.send_file_bytes("script.html", b"<script>alert(1)</script>") is True
|
|
82
|
+
|
|
83
|
+
server = WebServer(chat, port=0)
|
|
84
|
+
url = server.start()
|
|
85
|
+
try:
|
|
86
|
+
base_url, token_part = url.split("/?token=")
|
|
87
|
+
|
|
88
|
+
# Safe PNG image is served inline with image/png and nosniff
|
|
89
|
+
with urllib.request.urlopen(f"{base_url}/api/file/preview?id=1&token={token_part}") as resp:
|
|
90
|
+
assert resp.status == 200
|
|
91
|
+
assert resp.headers["Content-Type"] == "image/png"
|
|
92
|
+
assert "inline;" in resp.headers["Content-Disposition"]
|
|
93
|
+
assert resp.headers["X-Content-Type-Options"] == "nosniff"
|
|
94
|
+
assert resp.read().startswith(b"\x89PNG")
|
|
95
|
+
|
|
96
|
+
# HTML/non-image file is forced as attachment (never inline XSS)
|
|
97
|
+
with urllib.request.urlopen(f"{base_url}/api/file/preview?id=2&token={token_part}") as resp:
|
|
98
|
+
assert resp.status == 200
|
|
99
|
+
assert resp.headers["Content-Type"] == "application/octet-stream"
|
|
100
|
+
assert "attachment;" in resp.headers["Content-Disposition"]
|
|
101
|
+
finally:
|
|
102
|
+
server.stop()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|