feat(cursor): 改进机器ID替换逻辑并添加注入校验

- 使用正则捕获组动态匹配函数名和参数名,避免硬编码
- 替换为动态引用捕获的函数名和参数名,提高代码灵活性
- 添加注入一致性校验机制,防止重复注入导致语法错误
- 检测__cursor_patched__标记数量,超过1个时抛出异常
```
This commit is contained in:
煎饼果子卷鲨鱼辣椒
2026-01-14 14:28:52 +08:00
parent a763ba63c1
commit a1d7546d39

View File

@@ -519,8 +519,8 @@ function Modify-CursorJSFiles {
# 说明b6(t) 是 machineId 的核心生成函数t=true 返回原始值t=false 返回哈希
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 b6(t){return t?'$machineGuid':'$machineId';}"
$b6Pattern = '(?s)async function (\w+)\((\w+)\)\{.*?createHash\("sha256"\).*?return \w+\?\w+:\w+\}'
$b6Replacement = "async function `$1(`$2){return `$2?'$machineGuid':'$machineId';}"
$b6Regex = [regex]::new($b6Pattern)
if ($b6Regex.IsMatch($content)) {
$content = $b6Regex.Replace($content, $b6Replacement, 1)
@@ -575,6 +575,12 @@ try {
Write-Host " $GREEN$NC [方案C] Loader Stub 已注入(文件开头)"
}
# 注入一致性校验:避免重复注入导致语法损坏
$patchedCount = ([regex]::Matches($content, "__cursor_patched__")).Count
if ($patchedCount -gt 1) {
throw "检测到重复注入标记:$patchedCount"
}
# 写入修改后的内容
Set-Content -Path $file -Value $content -Encoding UTF8 -NoNewline