安全檢查補兩個防護:圖表的檢查也擋換行、Tab、斜線或引號後面接的 on…= 屬性(加測試);手機拖曳 Mac 的路徑最多 200 個點。

已驗證: cargo test 884 個全過(含新測試 a_drawn_chart_runs_nothing),clippy 沒有新警告。
未驗證: 手機實際拖曳 Mac(要實機 Mac 和手機,會動到使用者正在用的螢幕)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
王性驊 2026-11-22 12:44:35 +08:00
parent 2bae2eb2ba
commit 2d663152ef
1 changed files with 28 additions and 5 deletions

View File

@ -1333,11 +1333,12 @@ fn svg_is_plain(svg: &str) -> bool {
if l.contains("<script") || l.contains("<foreignobject") || l.contains("javascript:") {
return false;
}
// (An attribute named on…: ` onload=`, ` onclick =`.)
!l.match_indices(" on").any(|(i, _)| {
let rest = &l[i + 3..];
// (An attribute named on…: ` onload=`, `"onclick =`, `<svg/onload=`, one after a tab or a new line.)
!l.match_indices("on").any(|(i, _)| {
let before = l[..i].chars().next_back();
let rest = &l[i + 2..];
let name: String = rest.chars().take_while(|c| c.is_ascii_alphabetic()).collect();
!name.is_empty() && rest[name.len()..].trim_start().starts_with('=')
before.is_some_and(|c| c.is_whitespace() || matches!(c, '/' | '"' | '\'')) && !name.is_empty() && rest[name.len()..].trim_start().starts_with('=')
})
}
@ -1800,6 +1801,9 @@ fn screen(which: &str, s: &Settings) -> Resp {
}
}
/// The most points one drag from the phone may have.
const MAX_DRAG_POINTS: usize = 200;
/// The phone taps, types, scrolls, moves the pointer or drags on this Mac's screen, as the
/// person, in the pixels of the picture it was sent (shot_size). The bots' computer takes the
/// person's input through VNC.
@ -1816,7 +1820,8 @@ fn screen_input(which: &str, v: &Value, s: &Settings) -> Resp {
"click" => ("computer_use_click", json!({"x": x, "y": y, "button": v["button"].as_str().unwrap_or("left"), "count": v["count"].as_u64().unwrap_or(1).clamp(1, 3), "modifier_keys": mods(v)})),
// 虛擬觸控板: the pointer moves (no click), and a drag goes along the finger's path.
"move" => ("computer_use_mouse_move", json!({"x": x, "y": y})),
"drag" => ("computer_use_drag", json!({"path": v["path"], "modifier_keys": mods(v)})),
// (A finger's path is a few dozen points; a longer one is cut, not walked for minutes.)
"drag" => ("computer_use_drag", json!({"path": v["path"].as_array().map(|a| a.iter().take(MAX_DRAG_POINTS).cloned().collect::<Vec<_>>()).unwrap_or_default(), "modifier_keys": mods(v)})),
"type" => ("computer_use_typing", json!({"value": v["text"]})),
// (A combination too: "cmd+tab", from the key bar.)
"key" => ("computer_use_press_key", json!({"key": v["key"]})),
@ -3696,6 +3701,24 @@ mod tests {
use super::*;
/// A chart goes to the phone only as shapes and words: whatever a bot (or a page it read)
/// writes in a diagram's labels, links and styles, nothing in the drawing can run.
#[test]
fn a_drawn_chart_runs_nothing() {
for bad in ["<svg onload=alert(1)>", "<svg\nonload=alert(1)>", "<svg\tonclick = \"x\">", "<svg/onload=x>", "<g id=\"a\"onmouseover=x>", "<SCRIPT>x</SCRIPT>", "<foreignObject>", "<a href=\"JavaScript:x\">"] {
assert!(!svg_is_plain(bad), "{bad}");
}
for fine in ["<svg><text>turn on = yes, onward</text></svg>", "<svg font-family=\"sans\"><path d=\"M0 0\"/></svg>", "<text>conclusion=1</text>"] {
assert!(svg_is_plain(fine), "{fine}");
}
for src in ["graph TD\n A[\"<img src=x onerror=alert(1)>\"] --> B[\"<script>alert(1)</script>\"]", "graph TD\n A --> B\n click A href \"javascript:alert(1)\"", "graph TD\n A[\"x\\\" onload=\\\"alert(1)\"] --> B"] {
if let Ok(svg) = crate::diagram::svg(src, false) {
let l = svg.to_ascii_lowercase();
assert!(!svg_is_plain(&svg) || (!l.contains("<script") && !l.contains("<img") && !l.contains("javascript:")), "{src}\n{svg}");
}
}
}
/// A page holding a state is told only what changed: the keys that did, a bot that didn't as
/// {id, same}; one holding none (or an older page) all of it, with the fingerprint.
#[test]