From ea71181692c027c661e9d18800c211217f6e83ed Mon Sep 17 00:00:00 2001 From: Just want to protect you <108321411+Ahaohaohao@users.noreply.github.com> Date: Mon, 16 Jun 2025 21:04:40 +0800 Subject: [PATCH] fix : js script escape error issue introduced #3751 (#3759) * refactoring fix js script is too strict * fix:format --------- Co-authored-by: Ahao <108321411+xuanyuan0408@users.noreply.github.com> --- src-tauri/src/enhance/script.rs | 102 +++++++------------------------- 1 file changed, 22 insertions(+), 80 deletions(-) diff --git a/src-tauri/src/enhance/script.rs b/src-tauri/src/enhance/script.rs index 40789f07a..d1ced5ed0 100644 --- a/src-tauri/src/enhance/script.rs +++ b/src-tauri/src/enhance/script.rs @@ -45,8 +45,8 @@ pub fn use_script( let config = use_lowercase(config.clone()); let config_str = serde_json::to_string(&config)?; - // 处理 name 参数中的特殊字符 - let safe_name = escape_js_string(&name); + // 仅处理 name 参数中的特殊字符 + let safe_name = escape_js_string_for_single_quote(&name); let code = format!( r#"try{{ @@ -64,18 +64,8 @@ pub fn use_script( let result = result.to_string(&mut context).unwrap(); let result = result.to_std_string().unwrap(); - // 处理 JS 执行结果中的特殊字符 - let unescaped_result = unescape_js_string(&result); - - if unescaped_result.starts_with("__error_flag__") { - anyhow::bail!(unescaped_result[15..].to_owned()); - } - if unescaped_result == "\"\"" { - anyhow::bail!("main function should return object"); - } - - // 安全地解析 JSON 结果 - let res: Result = parse_json_safely(&unescaped_result); + // 直接解析JSON结果,不做其他解析 + let res: Result = parse_json_safely(&result); let mut out = outputs.lock().unwrap(); match res { @@ -90,72 +80,25 @@ pub fn use_script( } } -// 解析 JSON 字符串,处理可能的转义字符 fn parse_json_safely(json_str: &str) -> Result { - // 移除可能的引号包裹 - let json_str = if json_str.starts_with('"') && json_str.ends_with('"') { - &json_str[1..json_str.len() - 1] + let json_str = strip_outer_quotes(json_str); + + Ok(serde_json::from_str::(json_str)?) +} + +// 移除字符串外层的引号 +fn strip_outer_quotes(s: &str) -> &str { + let s = s.trim(); + if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) { + &s[1..s.len() - 1] } else { - json_str - }; - - // 处理可能的 JSON 字符串中的转义字符 - let json_str = json_str.replace("\\\"", "\""); - - Ok(serde_json::from_str::(&json_str)?) + s + } } -// 转义 JS 字符串中的特殊字符 -fn escape_js_string(s: &str) -> String { - let mut result = String::with_capacity(s.len()); - for c in s.chars() { - match c { - '\'' => result.push_str("\\'"), - '"' => result.push_str("\\\""), - '\\' => result.push_str("\\\\"), - '\n' => result.push_str("\\n"), - '\r' => result.push_str("\\r"), - '\t' => result.push_str("\\t"), - '\0' => result.push_str("\\0"), - _ => result.push(c), - } - } - result -} - -// 反转义 JS 字符串中的特殊字符 -fn unescape_js_string(s: &str) -> String { - let mut result = String::with_capacity(s.len()); - let mut chars = s.chars(); - - while let Some(c) = chars.next() { - if c == '\\' { - match chars.next() { - Some('n') => result.push('\n'), - Some('r') => result.push('\r'), - Some('t') => result.push('\t'), - Some('0') => result.push('\0'), - Some('\\') => result.push('\\'), - Some('\'') => result.push('\''), - Some('"') => result.push('"'), - Some('u') => { - // 处理转义序列 - let hex = chars.by_ref().take(4).collect::(); - if let Ok(codepoint) = u32::from_str_radix(&hex, 16) { - if let Some(ch) = char::from_u32(codepoint) { - result.push(ch); - } - } - } - Some(other) => result.push(other), - None => break, - } - } else { - result.push(c); - } - } - - result +// 转义单引号和反斜杠,用于单引号包裹的JavaScript字符串 +fn escape_js_string_for_single_quote(s: &str) -> String { + s.replace('\\', "\\\\").replace('\'', "\\'") } #[test] @@ -197,10 +140,9 @@ fn test_script() { #[test] fn test_escape_unescape() { let test_string = r#"Hello "World"!\nThis is a test with \u00A9 copyright symbol."#; - let escaped = escape_js_string(test_string); - let unescaped = unescape_js_string(&escaped); - - assert_eq!(test_string, unescaped); + let escaped = escape_js_string_for_single_quote(test_string); + println!("Original: {}", test_string); + println!("Escaped: {}", escaped); let json_str = r#"{"key":"value","nested":{"key":"value"}}"#; let parsed = parse_json_safely(json_str).unwrap();