mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:10:22 +08:00
fix: 收录 DSH NEXT 识别与单测(#174 #175)
Co-authored-by: Cursor <cursoragent@cursor.com> #171/#173 已在 1.1.80;#174 按官方 asar 路径认 DSH NEXT,不把任意 app.asar 当桌面。
This commit is contained in:
1 parent
fee23ac031
commit
5344d7e1cb
14 files changed
+322
-862
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
looksLikeDesktopName,
|
||||
isDesktopExecutable,
|
||||
isOfficialHarnessExecutable,
|
||||
} from "../lib/desktop.js";
|
||||
import { detectSurface, pathLooksDesktop, isCommunityElectronHost } from "../lib/surface.js";
|
||||
|
||||
test("DSH NEXT 路径和 exe 按桌面认", () => {
|
||||
assert.equal(looksLikeDesktopName("C:/Users/me/AppData/Local/Programs/DSH NEXT/DSH NEXT.exe"), true);
|
||||
assert.equal(isDesktopExecutable("C:/Users/me/AppData/Local/Programs/DSH NEXT/DSH NEXT.exe"), true);
|
||||
assert.equal(isOfficialHarnessExecutable("C:/Users/me/AppData/Local/Programs/DSH NEXT/DSH NEXT.exe"), true);
|
||||
assert.equal(pathLooksDesktop("C:/Users/me/AppData/Local/Programs/DSH NEXT/DSH NEXT.exe"), true);
|
||||
});
|
||||
|
||||
test("DSH NEXT 即使 DSH_SURFACE=web 也判桌面", () => {
|
||||
const input = {
|
||||
env: { DSH_SURFACE: "web", DSH_PROFILE: "dsh-next" },
|
||||
execPath: "C:\\\\Users\\\\me\\\\AppData\\\\Local\\\\Programs\\\\DSH NEXT\\\\DSH NEXT.exe",
|
||||
};
|
||||
assert.equal(isCommunityElectronHost(input), true);
|
||||
assert.equal(detectSurface(input), "desktop");
|
||||
});
|
||||
|
||||
test("普通路径不会因为带 app.asar 就当桌面", () => {
|
||||
assert.equal(pathLooksDesktop("/tmp/random-app/resources/app.asar"), false);
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
ipToInt, isIpv6, cidrOf, scopeOfIp, slugify, slugTarget, defaultPocFilename,
|
||||
} from "../lib/redteam/ip-utils.js";
|
||||
|
||||
test("ipToInt 把 IPv4 转成 32 位整数", () => {
|
||||
assert.equal(ipToInt("10.0.0.1"), 167772161);
|
||||
assert.equal(ipToInt("192.168.1.1"), 3232235777);
|
||||
assert.equal(ipToInt("0.0.0.0"), 0);
|
||||
});
|
||||
|
||||
test("ipToInt 对非 IPv4 返回 null", () => {
|
||||
assert.equal(ipToInt(""), null);
|
||||
assert.equal(ipToInt("abc"), null);
|
||||
assert.equal(ipToInt(null), null);
|
||||
assert.equal(ipToInt("1.2.3"), null);
|
||||
});
|
||||
|
||||
test("isIpv6 识别 IPv6 字面量", () => {
|
||||
assert.equal(isIpv6("2001:db8::1"), true);
|
||||
assert.equal(isIpv6("::1"), true);
|
||||
assert.equal(isIpv6("::ffff:192.0.2.1"), true);
|
||||
assert.equal(isIpv6("10.0.0.1"), false);
|
||||
assert.equal(isIpv6(""), false);
|
||||
});
|
||||
|
||||
test("cidrOf 归一到网段", () => {
|
||||
assert.equal(cidrOf("10.0.0.5"), "10.0.0.0/24");
|
||||
assert.equal(cidrOf("::1"), "0:0:0:0::/64");
|
||||
assert.equal(cidrOf("not-an-ip"), "not-an-ip");
|
||||
});
|
||||
|
||||
test("scopeOfIp 区分内网外网", () => {
|
||||
assert.equal(scopeOfIp("10.1.2.3"), "internal");
|
||||
assert.equal(scopeOfIp("192.168.1.1"), "internal");
|
||||
assert.equal(scopeOfIp("127.0.0.1"), "internal");
|
||||
assert.equal(scopeOfIp("172.16.0.1"), "internal");
|
||||
assert.equal(scopeOfIp("172.32.0.1"), "external");
|
||||
assert.equal(scopeOfIp("8.8.8.8"), "external");
|
||||
assert.equal(scopeOfIp("fc00::1"), "internal");
|
||||
assert.equal(scopeOfIp("2001:db8::1"), "external");
|
||||
});
|
||||
|
||||
test("slugify 归一化为 slug", () => {
|
||||
assert.equal(slugify("Hello World!"), "hello-world");
|
||||
assert.equal(slugify(" 中文 空格 "), "中文-空格");
|
||||
});
|
||||
|
||||
test("slugTarget 归一化目标", () => {
|
||||
assert.equal(slugTarget("10.0.0.5"), "10.0.0.5");
|
||||
assert.equal(slugTarget("10.0.0.5:8080"), "10.0.0.5");
|
||||
assert.equal(slugTarget("https://example.com/path"), "example.com");
|
||||
assert.equal(slugTarget(""), "unknown");
|
||||
});
|
||||
|
||||
test("defaultPocFilename 按语言取扩展名", () => {
|
||||
assert.equal(defaultPocFilename("t", "x", "python"), "poc.py");
|
||||
assert.equal(defaultPocFilename("t", "x", "go"), "poc.go");
|
||||
assert.equal(defaultPocFilename("t", "template", ""), "poc.yaml");
|
||||
assert.equal(defaultPocFilename("t", "x", ""), "poc.txt");
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
parseTargetPort, tunnelIsLegit, parseRowCount, applyScoreCaps, scoreServiceKey,
|
||||
} from "../lib/redteam/score-rules.js";
|
||||
|
||||
test("parseTargetPort 解析端口", () => {
|
||||
assert.equal(parseTargetPort("http://h:8080/x"), 8080);
|
||||
assert.equal(parseTargetPort("10.0.0.5:6379"), 6379);
|
||||
assert.equal(parseTargetPort("http://h/x"), null);
|
||||
assert.equal(parseTargetPort("10.0.0.5:99999"), null);
|
||||
assert.equal(parseTargetPort(""), null);
|
||||
});
|
||||
|
||||
test("tunnelIsLegit 判定隧道是否跨靶标边界", () => {
|
||||
assert.equal(tunnelIsLegit("self-only"), false);
|
||||
assert.equal(tunnelIsLegit("target-outbound"), true);
|
||||
assert.equal(tunnelIsLegit(""), null);
|
||||
assert.equal(tunnelIsLegit(null), null);
|
||||
});
|
||||
|
||||
test("parseRowCount 从证据解析数据量", () => {
|
||||
assert.equal(parseRowCount("120 万条"), 1200000);
|
||||
assert.equal(parseRowCount("1300万行"), 13000000);
|
||||
assert.equal(parseRowCount("2.5万行"), 25000);
|
||||
assert.equal(parseRowCount("1200000 records"), 1200000);
|
||||
assert.equal(parseRowCount(""), null);
|
||||
assert.equal(parseRowCount("密码是 123456"), null);
|
||||
});
|
||||
|
||||
test("scoreServiceKey 同一服务归一键", () => {
|
||||
assert.equal(scoreServiceKey({ target: "http://h:8080" }), "th:8080");
|
||||
assert.equal(scoreServiceKey({ target: "10.0.0.5:6379" }), "t10.0.0.5:6379");
|
||||
assert.equal(scoreServiceKey({}), null);
|
||||
});
|
||||
|
||||
test("applyScoreCaps 同一服务只计最高权限分", () => {
|
||||
const pointsByCode = new Map([
|
||||
["web-account-admin", { rule: 1, cap: 20, dedup_scope: "service", points: 20 }],
|
||||
["web-account-user", { rule: 1, cap: 20, dedup_scope: "service", points: 10 }],
|
||||
]);
|
||||
const rows = [
|
||||
{ id: 1, code: "web-account-user", points: 10, target: "http://h:8080" },
|
||||
{ id: 2, code: "web-account-admin", points: 20, target: "http://h:8080" },
|
||||
];
|
||||
const r = applyScoreCaps(rows, pointsByCode);
|
||||
assert.equal(r.capped, 1);
|
||||
const byId = Object.fromEntries(r.items.map((x) => [x.id, x]));
|
||||
assert.equal(byId[1].capped, true);
|
||||
assert.equal(byId[1].capped_reason_kind, "dedup");
|
||||
assert.equal(byId[1].capped_by_id, 2);
|
||||
assert.equal(byId[2].capped, false);
|
||||
});
|
||||
|
||||
test("applyScoreCaps 规则上限累计封顶", () => {
|
||||
const pointsByCode = new Map([
|
||||
["web-account-admin", { rule: 1, cap: 20, dedup_scope: "service", points: 20 }],
|
||||
]);
|
||||
const rows = [
|
||||
{ id: 1, code: "web-account-admin", points: 20, target: "http://h1:80" },
|
||||
{ id: 2, code: "web-account-admin", points: 20, target: "http://h2:80" },
|
||||
];
|
||||
const r = applyScoreCaps(rows, pointsByCode);
|
||||
assert.equal(r.capped, 1);
|
||||
const byId = Object.fromEntries(r.items.map((x) => [x.id, x]));
|
||||
assert.equal(byId[1].capped, false);
|
||||
assert.equal(byId[2].capped, true);
|
||||
assert.equal(byId[2].capped_reason_kind, "cap");
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
isInactiveContextError, settleDelayMs, settleMayRestart, settleMayReapply, applyShouldRestart,
|
||||
} from "../lib/settle-policy.js";
|
||||
|
||||
test("isInactiveContextError 识别失效上下文错误", () => {
|
||||
assert.equal(isInactiveContextError({ code: "INACTIVE_EFFECT" }), true);
|
||||
assert.equal(isInactiveContextError(new Error("inactive context")), true);
|
||||
assert.equal(isInactiveContextError(null), false);
|
||||
assert.equal(isInactiveContextError({}), false);
|
||||
});
|
||||
|
||||
test("settleDelayMs 桌面有延迟、网页无", () => {
|
||||
assert.equal(settleDelayMs("desktop"), 4000);
|
||||
assert.equal(settleDelayMs("web"), 0);
|
||||
});
|
||||
|
||||
test("settleMayRestart 桌面或密封包不重启", () => {
|
||||
assert.equal(settleMayRestart({ surface: "desktop" }), false);
|
||||
assert.equal(settleMayRestart({ surface: "web" }), true);
|
||||
assert.equal(settleMayRestart({ surface: "web", sealed: true }), false);
|
||||
});
|
||||
|
||||
test("settleMayReapply 只有桌面密封包禁止", () => {
|
||||
assert.equal(settleMayReapply({ surface: "desktop", sealed: true }), false);
|
||||
assert.equal(settleMayReapply({ surface: "desktop" }), true);
|
||||
assert.equal(settleMayReapply({ surface: "web" }), true);
|
||||
});
|
||||
|
||||
test("applyShouldRestart 应用后重启判定", () => {
|
||||
assert.equal(applyShouldRestart({ sealed: true }), true);
|
||||
assert.equal(applyShouldRestart({ complete: true }), true);
|
||||
assert.equal(applyShouldRestart({ promptReady: true }), true);
|
||||
assert.equal(applyShouldRestart({ appliedCount: 2 }), true);
|
||||
assert.equal(applyShouldRestart({}), false);
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import zlib from "node:zlib";
|
||||
import { extractZipBuffer } from "../lib/zip-extract.js";
|
||||
|
||||
function storedZip(entries) {
|
||||
const localParts = [];
|
||||
const centralParts = [];
|
||||
let offset = 0;
|
||||
for (const { name, data, dir } of entries) {
|
||||
const rawName = dir ? (name.endsWith("/") ? name : `${name}/`) : name;
|
||||
const body = dir ? Buffer.alloc(0) : Buffer.from(data);
|
||||
const crc = zlib.crc32(body) >>> 0;
|
||||
const nameBuf = Buffer.from(rawName, "utf8");
|
||||
const local = Buffer.alloc(30 + nameBuf.length);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt16LE(0, 8);
|
||||
local.writeUInt32LE(crc, 14);
|
||||
local.writeUInt32LE(body.length, 18);
|
||||
local.writeUInt32LE(body.length, 22);
|
||||
local.writeUInt16LE(nameBuf.length, 26);
|
||||
nameBuf.copy(local, 30);
|
||||
const localOff = offset;
|
||||
localParts.push(local, body);
|
||||
offset += local.length + body.length;
|
||||
|
||||
const central = Buffer.alloc(46 + nameBuf.length);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt16LE(0, 10);
|
||||
central.writeUInt32LE(crc, 16);
|
||||
central.writeUInt32LE(body.length, 20);
|
||||
central.writeUInt32LE(body.length, 24);
|
||||
central.writeUInt16LE(nameBuf.length, 28);
|
||||
central.writeUInt32LE(localOff, 42);
|
||||
nameBuf.copy(central, 46);
|
||||
centralParts.push(central);
|
||||
}
|
||||
const centralBuf = Buffer.concat(centralParts);
|
||||
const localBuf = Buffer.concat(localParts);
|
||||
const eocd = Buffer.alloc(22);
|
||||
eocd.writeUInt32LE(0x06054b50, 0);
|
||||
eocd.writeUInt16LE(entries.length, 8);
|
||||
eocd.writeUInt16LE(entries.length, 10);
|
||||
eocd.writeUInt32LE(centralBuf.length, 12);
|
||||
eocd.writeUInt32LE(localBuf.length, 16);
|
||||
return Buffer.concat([localBuf, centralBuf, eocd]);
|
||||
}
|
||||
|
||||
test("zip 目录条目建目录而不是空文件", () => {
|
||||
const dest = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-zip-"));
|
||||
try {
|
||||
extractZipBuffer(storedZip([
|
||||
{ name: "root/", dir: true },
|
||||
{ name: "root/nested/", dir: true },
|
||||
{ name: "root/nested/a.txt", data: "ok" },
|
||||
]), dest);
|
||||
assert.equal(fs.statSync(path.join(dest, "root")).isDirectory(), true);
|
||||
assert.equal(fs.statSync(path.join(dest, "root", "nested")).isDirectory(), true);
|
||||
assert.equal(fs.readFileSync(path.join(dest, "root", "nested", "a.txt"), "utf8"), "ok");
|
||||
} finally {
|
||||
fs.rmSync(dest, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user