Fix: stop web dialogs closing on text-selection drags

All modal overlays closed on a click whose target was the backdrop, but
browsers dispatch that click to the nearest common ancestor of mousedown
and mouseup: selecting text inside an input and releasing outside the card
closed the dialog. The overlays now ignore backdrop clicks and close only
from their own buttons (x / Cancel / confirm); app-dialog keeps Escape.

Review follow-ups in the same flow: a late set_device_remark/group reply
is applied only to the dialog still open for the same device (failure
replies now echo the device id, and the success path no longer fetches the
list twice), Save reports "not connected" instead of doing nothing while
the socket reconnects, and the remark/group menu icons get their colours.

Also escape client-reported card fields for HTML and attribute contexts
(screen, transfer/active-window titles, audit timestamp title), so a
modified host cannot inject markup into the admin page.

Verified: node --check on the inline script; go build ./... and go test
./web/ pass; the embedded asset copy is in sync.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-27 15:37:09 +01:00
1 parent 034b11fa6a
commit 4b96885016
2 files changed
+34 -17

No files matched your search

+6 -2
View File
@@ -1806,7 +1806,9 @@ func (h *wsHub) handleSetDeviceRemark(c *wsClient, raw []byte) {
"reason": reason,
"error": msg,
})
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "msg": msg}))
// id lets the page drop a late reply meant for a dialog it has since
// closed and replaced with another device's.
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "id": id, "msg": msg}))
}
// An unparsable payload (e.g. a non-string remark) must be rejected, not
// treated as an empty remark: empty clears the label.
@@ -1877,7 +1879,9 @@ func (h *wsHub) handleSetDeviceGroup(c *wsClient, raw []byte) {
"reason": reason,
"error": msg,
})
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "msg": msg}))
// id lets the page drop a late reply meant for a dialog it has since
// closed and replaced with another device's.
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "id": id, "msg": msg}))
}
if err := json.Unmarshal(raw, &in); err != nil {
fail("Invalid request", "invalid_request")
+28 -15
View File
@@ -725,6 +725,8 @@
margin-top: 1px;
}
.card-menu-item.term .card-menu-icon { color: #4caf50; }
.card-menu-item.edit .card-menu-icon { color: #4dabf7; }
.card-menu-item.group .card-menu-icon { color: #b197fc; }
.card-menu-item.bind .card-menu-icon { color: #e67e22; }
.card-menu-item.transfer .card-menu-icon { color: #d68910; }
.card-menu-item.del .card-menu-icon { color: #e74c3c; }
@@ -2193,7 +2195,7 @@
</div>
<!-- Logout Confirmation Modal -->
<div class="modal-overlay" id="logout-confirm-modal" onclick="if(event.target===this)cancelLogout()">
<div class="modal-overlay" id="logout-confirm-modal">
<div class="confirm-modal-content">
<div class="confirm-modal-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
@@ -2212,7 +2214,7 @@
</div>
<!-- Delete Device Confirmation Modal(离线=删除历史记录;在线=下发卸载主机) -->
<div class="modal-overlay" id="delete-device-modal" onclick="if(event.target===this)cancelDeleteDevice()">
<div class="modal-overlay" id="delete-device-modal">
<div class="confirm-modal-content">
<div class="confirm-modal-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
@@ -2232,7 +2234,7 @@
</div>
<!-- Transfer Host Modal(仅 Go 后端:device_list 带 transfer 字段时才有入口) -->
<div class="modal-overlay" id="transfer-device-modal" onclick="if(event.target===this)cancelTransferDevice()">
<div class="modal-overlay" id="transfer-device-modal">
<div class="modal-content" style="max-width:460px;">
<div class="modal-header">
<h3>Transfer Host</h3>
@@ -2262,7 +2264,7 @@
</div>
<!-- Device Binding Modal(仅内置 admin 超级管理员可见) -->
<div class="modal-overlay" id="bind-device-modal" onclick="if(event.target===this)closeBindModal()">
<div class="modal-overlay" id="bind-device-modal">
<div class="modal-content" style="max-width:440px;">
<div class="modal-header">
<h3>Device Binding</h3>
@@ -2290,7 +2292,7 @@
</div>
<!-- 修改备注(Go 后端):备注是卡片标题旁的显示标签 "hostname (备注)"。 -->
<div class="modal-overlay" id="remark-device-modal" onclick="if(event.target===this)closeRemarkModal()">
<div class="modal-overlay" id="remark-device-modal">
<div class="modal-content" style="max-width:440px;">
<div class="modal-header">
<h3>Edit Remark</h3>
@@ -2315,7 +2317,7 @@
</div>
<!-- 修改分组(Go 后端):覆盖主机上报的分组,仅影响本服务端的显示/过滤/MCP。 -->
<div class="modal-overlay" id="group-device-modal" onclick="if(event.target===this)closeGroupModal()">
<div class="modal-overlay" id="group-device-modal">
<div class="modal-content" style="max-width:440px;">
<div class="modal-header">
<h3>Edit Group</h3>
@@ -2344,7 +2346,7 @@
<!-- Generic alert / confirmation dialog. Replaces native alert()/confirm():
their button labels come from the browser locale (e.g. German on a
German system) and do not match the app. -->
<div class="modal-overlay" id="app-dialog" onclick="if(event.target===this)closeAppDialog(false)"
<div class="modal-overlay" id="app-dialog"
onkeydown="if(event.key==='Escape')closeAppDialog(false)">
<div class="confirm-modal-content" id="app-dialog-content" role="dialog" aria-modal="true"
aria-labelledby="app-dialog-title" aria-describedby="app-dialog-message">
@@ -3395,17 +3397,20 @@
}
break;
case 'set_device_remark_result':
// 只收尾"当前开着的那台设备"的对话框:请求在飞时管理员可能
// 已经取消并打开了另一台设备,迟到的回复不能把它关掉。列表
// 刷新由服务端的 devices_changed 广播负责,这里不重复拉取。
if (msg.id !== undefined && String(msg.id) !== pendingRemarkDeviceId) break;
if (msg.ok) {
closeRemarkModal();
getDevices();
} else {
appAlert(msg.msg || 'Failed to save remark', 'Edit Remark');
}
break;
case 'set_device_group_result':
if (msg.id !== undefined && String(msg.id) !== pendingGroupDeviceId) break;
if (msg.ok) {
closeGroupModal();
getDevices();
} else {
appAlert(msg.msg || 'Failed to save group', 'Edit Group');
}
@@ -5386,15 +5391,15 @@
? '<span class="meta-item offline-badge">Offline</span>'
: '<span class="meta-item rtt ' + getRttClass(rtt) + '">RTT: ' + escapeHtml(rtt) + '</span>') +
'<span class="meta-item">Ver: ' + escapeHtml(ver) + '</span>' +
'<span class="meta-item">' + screenInfo + '</span>' +
'<span class="meta-item">' + escapeHtml(screenInfo) + '</span>' +
'</div>' +
(offline ? '<div class="last-seen">Last seen: ' + escapeHtml(formatLastSeen(d.last_seen)) + '</div>' : '') +
(d.transferTo
? '<div class="transfer-badge" title="Transferred to ' + escapeHtml(d.transferTo) + '">' +
? '<div class="transfer-badge" title="Transferred to ' + escapeAttr(d.transferTo) + '">' +
'Transferred → ' + escapeHtml(d.transferTo) + ' · ' +
escapeHtml(formatTransferLeft(d.transferUntil)) + '</div>'
: '') +
(!offline && activeWin ? '<div class="active-window' + (isWindowBusy(activeWin) ? ' busy' : '') + '" title="' + escapeHtml(activeWin) + '">' + escapeHtml(activeWin) + '</div>' : '') +
(!offline && activeWin ? '<div class="active-window' + (isWindowBusy(activeWin) ? ' busy' : '') + '" title="' + escapeAttr(activeWin) + '">' + escapeHtml(activeWin) + '</div>' : '') +
'</div>';
}).join('');
}
@@ -5708,7 +5713,11 @@
}
function confirmSetRemark() {
if (!pendingRemarkDeviceId || !ws || ws.readyState !== WebSocket.OPEN) return;
if (!pendingRemarkDeviceId) return;
if (!ws || ws.readyState !== WebSocket.OPEN) {
appAlert('Not connected to the server. Try again in a moment.', 'Edit Remark');
return;
}
const remark = document.getElementById('remark-input').value.trim();
ws.send(JSON.stringify({
cmd: 'set_device_remark', token, id: pendingRemarkDeviceId, remark,
@@ -5747,7 +5756,11 @@
}
function confirmSetGroup() {
if (!pendingGroupDeviceId || !ws || ws.readyState !== WebSocket.OPEN) return;
if (!pendingGroupDeviceId) return;
if (!ws || ws.readyState !== WebSocket.OPEN) {
appAlert('Not connected to the server. Try again in a moment.', 'Edit Group');
return;
}
const group = document.getElementById('group-input').value.trim();
ws.send(JSON.stringify({
cmd: 'set_device_group', token, id: pendingGroupDeviceId, group,
@@ -6113,7 +6126,7 @@
tbody.innerHTML = events.map(rec => {
const level = rec.level === 'warn' ? 'warn' : 'info';
return '<tr>' +
'<td title="' + escapeHtml(rec.time || '') + '">' + formatAuditTime(rec.time) + '</td>' +
'<td title="' + escapeAttr(rec.time || '') + '">' + formatAuditTime(rec.time) + '</td>' +
'<td><span class="audit-level ' + level + '">' + level + '</span></td>' +
'<td>' + escapeHtml(rec.event || '') + '</td>' +
'<td>' + escapeHtml(rec.actor || '') + '</td>' +