Browse Source

```

fix(cursor-hook): 修复Cursor ID修改器的正则匹配安全问题

- 修改PowerShell脚本中的正则表达式,从泛匹配改为精确匹配b6函数名,
  避免跨函数误替换导致main.js语法破坏
- 添加匹配长度检查,当匹配结果超过5000字符时跳过替换以防止文件损坏
- 更新警告信息,明确标注未找到目标函数时的行为
- 调整JavaScript Hook文件为纯CommonJS写法,避免ESM语法导致Cursor启动失败
```
煎饼果子卷鲨鱼辣椒 2 weeks ago
parent
commit
b6e34580bb
  1. 21
      scripts/hook/cursor_hook.js
  2. 16
      scripts/run/cursor_win_id_modifier.ps1

21
scripts/hook/cursor_hook.js

@ -84,23 +84,12 @@ var __cursor_hook_config__ = {
}; };
// 加载或生成 ID 配置 // 加载或生成 ID 配置
// 注意:使用 createRequire 来支持 ES Module 环境
// 注意:该 Hook 由脚本注入的 Loader 通过 CommonJS(require) 方式加载,
// 为避免出现 import.meta 等仅 ESM 支持的语法导致 Cursor 启动期解析失败,这里保持纯 CommonJS 写法。
const loadOrGenerateIds = () => { const loadOrGenerateIds = () => {
// 在 ES Module 环境中,需要使用 createRequire 来加载 CommonJS 模块
let fs, path, os;
try {
// 尝试使用 Node.js 内置模块
const { createRequire } = require('module');
const require2 = createRequire(import.meta?.url || __filename);
fs = require2('fs');
path = require2('path');
os = require2('os');
} catch (e) {
// 回退到直接 require
fs = require('fs');
path = require('path');
os = require('os');
}
const fs = require('fs');
const path = require('path');
const os = require('os');
const configPath = path.join(os.homedir(), __cursor_hook_config__.configFileName); const configPath = path.join(os.homedir(), __cursor_hook_config__.configFileName);

16
scripts/run/cursor_win_id_modifier.ps1

@ -518,16 +518,22 @@ function Modify-CursorJSFiles {
# ========== 方法B: b6 定点重写(机器码源函数,仅 main.js) ========== # ========== 方法B: b6 定点重写(机器码源函数,仅 main.js) ==========
# 说明:b6(t) 是 machineId 的核心生成函数,t=true 返回原始值,t=false 返回哈希 # 说明:b6(t) 是 machineId 的核心生成函数,t=true 返回原始值,t=false 返回哈希
if ((Split-Path $file -Leaf) -eq "main.js") { if ((Split-Path $file -Leaf) -eq "main.js") {
# 使用特征锚点定位(createHash("sha256") + return t?e:i),避免依赖函数名
$b6Pattern = '(?s)async function (\w+)\((\w+)\)\{.*?createHash\("sha256"\).*?return \w+\?\w+:\w+\}'
$b6Replacement = "async function `$1(`$2){return `$2?'$machineGuid':'$machineId';}"
# ⚠️ 安全修复:旧版“泛匹配 async function + .*?”会在部分版本中跨越大段代码误替换,直接破坏 main.js 语法。
# 这里改为仅锚定 b6(...) 的函数定义;若版本不存在 b6,则跳过方案B,避免误伤。
$b6Pattern = '(?s)async function b6\((\w+)\)\{.*?createHash\(["'']sha256["'']\).*?return \1\?\w+:\w+\}'
$b6Replacement = "async function b6(`$1){return `$1?'$machineGuid':'$machineId';}"
$b6Regex = [regex]::new($b6Pattern) $b6Regex = [regex]::new($b6Pattern)
if ($b6Regex.IsMatch($content)) {
$b6Match = $b6Regex.Match($content)
if ($b6Match.Success) {
if ($b6Match.Length -gt 5000) {
Write-Host " $YELLOW⚠️ $NC [方案B] b6 匹配长度异常($($b6Match.Length)),为避免破坏文件已跳过"
} else {
$content = $b6Regex.Replace($content, $b6Replacement, 1) $content = $b6Regex.Replace($content, $b6Replacement, 1)
Write-Host " $GREEN✓$NC [方案B] 已重写 b6(t) 返回值" Write-Host " $GREEN✓$NC [方案B] 已重写 b6(t) 返回值"
$replacedB6 = $true $replacedB6 = $true
}
} else { } else {
Write-Host " $YELLOW⚠️ $NC [方案B] 未定位到 b6(t) 目标函数"
Write-Host " $YELLOW⚠️ $NC [方案B] 未找到 b6(t) 目标函数,已跳过"
} }
} }

Loading…
Cancel
Save