diff --git a/docs/milestone-audit-hardlink-initial-repro.json b/docs/milestone-audit-hardlink-initial-repro.json new file mode 100644 index 00000000..282ebc71 --- /dev/null +++ b/docs/milestone-audit-hardlink-initial-repro.json @@ -0,0 +1,9 @@ +{ + "at": "2026-10-30T15:14:23.779Z", + "directory": "D:\\Ai-project\\REA逆向\\Agent-Workbench\\.qa\\milestone-hardlink-gwRZXi", + "links": 2, + "modelFileReadContainsOwnedMarker": true, + "error": null, + "inspectionRulesContainOwnedMarker": true, + "realCredentials": "none; owned marker only" +} diff --git a/docs/milestone-audit-path-followup-tests.tap b/docs/milestone-audit-path-followup-tests.tap new file mode 100644 index 00000000..e362c17d --- /dev/null +++ b/docs/milestone-audit-path-followup-tests.tap @@ -0,0 +1,187 @@ +TAP version 13 +# warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it +# warning: in the working copy of 'src/two.mjs', LF will be replaced by CRLF the next time Git touches it +# Subtest: sealed batches preserve bytes until review, then apply and explicitly restore BOM/CRLF/new files +ok 1 - sealed batches preserve bytes until review, then apply and explicitly restore BOM/CRLF/new files + --- + duration_ms: 2041.7263 + type: 'test' + ... +# Switched to a new branch 'owner-branch' +# Subtest: a conflict in the last file or Git branch prevents writing every file in the batch +ok 2 - a conflict in the last file or Git branch prevents writing every file in the batch + --- + duration_ms: 2812.9693 + type: 'test' + ... +# Subtest: duplicate canonical paths, ancestor collisions, links, dependencies and Windows aliases never form a batch +ok 3 - duplicate canonical paths, ancestor collisions, links, dependencies and Windows aliases never form a batch + --- + duration_ms: 1009.9288 + type: 'test' + ... +# Subtest: fingerprints, changed proposals and corrupt snapshot artifacts cannot write or restore any target +ok 4 - fingerprints, changed proposals and corrupt snapshot artifacts cannot write or restore any target + --- + duration_ms: 2350.9921 + type: 'test' + ... +# Subtest: a failure after replacement restores only batch bytes and never retries an old application +ok 5 - a failure after replacement restores only batch bytes and never retries an old application + --- + duration_ms: 1833.9048 + type: 'test' + ... +# Subtest: rollback preserves owner edits, blocks other project writers, and explicit acknowledgement releases recovery +ok 6 - rollback preserves owner edits, blocks other project writers, and explicit acknowledgement releases recovery + --- + duration_ms: 2479.6809 + type: 'test' + ... +# Subtest: an unattempted owner-created file identical to a suggestion remains external and is never removed +ok 7 - an unattempted owner-created file identical to a suggestion remains external and is never removed + --- + duration_ms: 1837.4294 + type: 'test' + ... +# Subtest: active batch requests are serialized and cancellation leaves a verifiable ended batch +ok 8 - active batch requests are serialized and cancellation leaves a verifiable ended batch + --- + duration_ms: 1958.5244 + type: 'test' + ... +# Subtest: batch application and rollback preserve unrelated Git index and worktree bytes +ok 9 - batch application and rollback preserve unrelated Git index and worktree bytes + --- + duration_ms: 3201.692 + type: 'test' + ... +# Subtest: recovery reuses exact staged bytes but retains a conflicting temporary file for owner inspection +ok 10 - recovery reuses exact staged bytes but retains a conflicting temporary file for owner inspection + --- + duration_ms: 1784.9568 + type: 'test' + ... +# Subtest: one Task can seal pending proposals from earlier Runs while snapshots retain each source Run +ok 11 - one Task can seal pending proposals from earlier Runs while snapshots retain each source Run + --- + duration_ms: 1371.4633 + type: 'test' + ... +# Subtest: model code tools keep Task/Run ownership, index evidence and exact proposals across storage recovery +ok 12 - model code tools keep Task/Run ownership, index evidence and exact proposals across storage recovery + --- + duration_ms: 4195.5662 + type: 'test' + ... +# Subtest: bundled code APIs isolate project results, validate queries and create review-only patches with conflicts +ok 13 - bundled code APIs isolate project results, validate queries and create review-only patches with conflicts + --- + duration_ms: 1952.8462 + type: 'test' + ... +# Subtest: code tool arguments reject malformed ownership-independent inputs before side effects +ok 14 - code tool arguments reject malformed ownership-independent inputs before side effects + --- + duration_ms: 0.5327 + type: 'test' + ... +# Subtest: hard-link aliases cannot expose protected bytes through reads, proposals, inventory or automatic project rules +ok 15 - hard-link aliases cannot expose protected bytes through reads, proposals, inventory or automatic project rules + --- + duration_ms: 765.9297 + type: 'test' + ... +# Subtest: ambiguous project hard links are explicit and ordinary independent file copies remain readable +ok 16 - ambiguous project hard links are explicit and ordinary independent file copies remain readable + --- + duration_ms: 703.696 + type: 'test' + ... +# Subtest: proposals preserve project bytes until review; apply backs up the exact original bytes +ok 17 - proposals preserve project bytes until review; apply backs up the exact original bytes + --- + duration_ms: 933.708 + type: 'test' + ... +# Subtest: new nested files are created only on apply and unchanged content creates no proposal +ok 18 - new nested files are created only on apply and unchanged content creates no proposal + --- + duration_ms: 733.1599 + type: 'test' + ... +# Subtest: stale reads and changed baselines are rejected without overwriting external edits +ok 19 - stale reads and changed baselines are rejected without overwriting external edits + --- + duration_ms: 770.629 + type: 'test' + ... +# Subtest: file paths reject traversal, absolute paths, credentials and outside junctions +ok 20 - file paths reject traversal, absolute paths, credentials and outside junctions + --- + duration_ms: 831.413 + type: 'test' + ... +# Subtest: read and search expose bounded text scopes and reject non UTF-8 / oversized files +ok 21 - read and search expose bounded text scopes and reject non UTF-8 / oversized files + --- + duration_ms: 695.127 + type: 'test' + ... +# Subtest: project-local junctions cannot expose or create protected Git and credential files +ok 22 - project-local junctions cannot expose or create protected Git and credential files + --- + duration_ms: 957.424 + type: 'test' + ... +# Subtest: Windows trailing-dot and trailing-space path aliases cannot bypass protected paths +ok 23 - Windows trailing-dot and trailing-space path aliases cannot bypass protected paths + --- + duration_ms: 749.6176 + type: 'test' + ... +# Subtest: exact patches preserve BOM/CRLF, unicode and all unselected bytes and back up the original on apply +ok 24 - exact patches preserve BOM/CRLF, unicode and all unselected bytes and back up the original on apply + --- + duration_ms: 783.3927 + type: 'test' + ... +# Subtest: several patches are matched against one original snapshot in any input order and keep LF +ok 25 - several patches are matched against one original snapshot in any input order and keep LF + --- + duration_ms: 1116.5668 + type: 'test' + ... +# Subtest: mixed line endings require exact anchors and preserve untouched formatting +ok 26 - mixed line endings require exact anchors and preserve untouched formatting + --- + duration_ms: 844.7789 + type: 'test' + ... +# Subtest: ambiguous, overlapping, missing and malformed patch anchors never create a proposal or write +ok 27 - ambiguous, overlapping, missing and malformed patch anchors never create a proposal or write + --- + duration_ms: 717.7812 + type: 'test' + ... +# Subtest: stale original hashes and changes before apply preserve owner edits and mark conflicts +ok 28 - stale original hashes and changes before apply preserve owner edits and mark conflicts + --- + duration_ms: 896.25 + type: 'test' + ... +# Subtest: no-op BOM patches/full content produce no change and replacements cannot split Unicode bytes +ok 29 - no-op BOM patches/full content produce no change and replacements cannot split Unicode bytes + --- + duration_ms: 663.8407 + type: 'test' + ... +1..29 +# tests 29 +# suites 0 +# pass 29 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms 22801.3311 diff --git a/docs/milestone-audit-path-followup-validation.json b/docs/milestone-audit-path-followup-validation.json new file mode 100644 index 00000000..0f705757 --- /dev/null +++ b/docs/milestone-audit-path-followup-validation.json @@ -0,0 +1,29 @@ +{ + "success": true, + "version": "1.12.1", + "startedAt": "2026-10-30T15:26:30.086Z", + "finishedAt": "2026-10-30T15:26:52.939Z", + "directory": "D:\\Ai-project\\REA逆向\\Agent-Workbench\\.qa\\milestone-path-followup-4acaee3b-bfba-4dce-acfe-0d1bda2cba13", + "files": [ + "tests/file-boundaries.test.mjs", + "tests/files.test.mjs", + "tests/patch.test.mjs", + "tests/code-tools.test.mjs", + "tests/change-sets.test.mjs" + ], + "code": 0, + "tests": { + "tests": 29, + "pass": 29, + "fail": 0, + "cancelled": 0, + "skipped": 0, + "todo": 0 + }, + "log": { + "path": "D:\\Ai-project\\REA逆向\\Agent-Workbench\\.qa\\milestone-path-followup-4acaee3b-bfba-4dce-acfe-0d1bda2cba13\\tests.tap", + "bytes": 7708, + "sha256": "875bc7cebd4893db0468901b4af234b2c684c08a6d52cb5a9d0b53d30ea1176e" + }, + "realModelRequests": 0 +} diff --git a/src/server/files.mjs b/src/server/files.mjs index b4efd22a..6f727310 100644 --- a/src/server/files.mjs +++ b/src/server/files.mjs @@ -46,6 +46,10 @@ export function safePath(project, relative = '', allowMissing = false) { // A project-local link or Windows alias must not turn a public path into // access to protected Git metadata or credentials, including new files. if (restricted(path.relative(root, real))) throw new Error('实际路径指向受保护的项目目录或凭据。'); + const actual = fs.statSync(real); + // realpath cannot reveal the other names of a hard-linked file. Refuse + // ambiguous file scope before reading bytes or preparing a replacement. + if (actual.isFile() && actual.nlink !== 1) throw new Error('硬链接文件有多个路径,无法核对完整作用范围;请使用独立文件副本。'); return target; } @@ -65,8 +69,10 @@ export function listFiles(project, relative = '', depth = 4, limit = 700) { const relativePath = path.relative(project.root, absolute).replaceAll('\\', '/'); if (entry.isSymbolicLink()) continue; const directory = entry.isDirectory(); + const info = directory ? null : fs.statSync(absolute); + if (!directory && (!info.isFile() || info.nlink !== 1)) continue; files.push({ path: relativePath, name: entry.name, kind: directory ? 'directory' : 'file', - bytes: directory ? 0 : fs.statSync(absolute).size }); + bytes: directory ? 0 : info.size }); if (files.length >= limit) { truncated = true; break; } if (directory && current.depth < Math.min(12, Math.max(0, depth))) queue.push({ absolute, depth: current.depth + 1 }); } diff --git a/tests/change-sets.test.mjs b/tests/change-sets.test.mjs index 300e03b4..90322061 100644 --- a/tests/change-sets.test.mjs +++ b/tests/change-sets.test.mjs @@ -60,8 +60,13 @@ test('duplicate canonical paths, ancestor collisions, links, dependencies and Wi const dependency = proposeFile(f.store, f.session, { path: 'node_modules/owned.txt', content: 'dependency', reason: 'excluded' }); await assert.rejects(b.manager.prepare(f.session, [dependency.change_id]), /普通源码/); if (process.platform === 'win32') { - const windows = proposeFile(f.store, f.session, { path: 'src/one.mjs.', content: 'alias', reason: 'windows alias' }); - await assert.rejects(b.manager.prepare(f.session, [windows.change_id]), /路径别名/); + assert.throws(() => proposeFile(f.store, f.session, { path: 'src/one.mjs.', content: 'alias', reason: 'windows alias' }), /可读取的项目范围/); + // A proposal retained from an older host must still be refused by batch + // validation even though the current proposal API now rejects it earlier. + const retained = f.session.changes.find(value => value.id === alias.change_id), originalPath = retained.path; + retained.path = 'src/one.mjs.'; + try { await assert.rejects(b.manager.prepare(f.session, [retained.id]), /可读取的项目范围/); } + finally { retained.path = originalPath; } } assertBatchBytes(assert, f, b.before); }); diff --git a/tests/file-boundaries.test.mjs b/tests/file-boundaries.test.mjs new file mode 100644 index 00000000..f2af62c8 --- /dev/null +++ b/tests/file-boundaries.test.mjs @@ -0,0 +1,34 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fixture } from './helpers.mjs'; +import { readText, listFiles, proposeFile } from '../src/server/files.mjs'; +import { inspectProject } from '../src/core/project-inspector.mjs'; + +test('hard-link aliases cannot expose protected bytes through reads, proposals, inventory or automatic project rules', async t => { + const f = fixture(t); + fs.mkdirSync(path.join(f.projectRoot, '.git')); fs.mkdirSync(path.join(f.projectRoot, '.secrets')); + fs.writeFileSync(path.join(f.projectRoot, '.git/config'), '[core]\nrepositoryformatversion = 0\n'); + fs.writeFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'owned-protected-rule-marker'); + fs.linkSync(path.join(f.projectRoot, '.git/config'), path.join(f.projectRoot, 'public-config.txt')); + fs.linkSync(path.join(f.projectRoot, '.secrets/owned.txt'), path.join(f.projectRoot, 'AGENTS.md')); + for (const filename of ['public-config.txt', 'AGENTS.md']) { + assert.throws(() => readText(f.project, filename), /硬链接/); + assert.throws(() => proposeFile(f.store, f.session, { path: filename, content: 'new bytes' }), /硬链接/); + } + const inventory = listFiles(f.project); assert.ok(!inventory.files.some(value => ['public-config.txt', 'AGENTS.md'].includes(value.path))); + const inspection = await inspectProject(f.project); assert.equal(inspection.rules.length, 0); + assert.equal(JSON.stringify(inspection).includes('owned-protected-rule-marker'), false); + assert.equal(fs.readFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'utf8'), 'owned-protected-rule-marker'); +}); + +test('ambiguous project hard links are explicit and ordinary independent file copies remain readable', t => { + const f = fixture(t), original = path.join(f.projectRoot, 'README.md'), alias = path.join(f.projectRoot, 'alias.md'); + fs.linkSync(original, alias); + assert.throws(() => readText(f.project, 'README.md'), /独立文件副本/); assert.throws(() => readText(f.project, 'alias.md'), /硬链接/); + fs.unlinkSync(alias); fs.copyFileSync(original, alias); + assert.match(readText(f.project, 'README.md').content, /Fixture project/); + assert.match(readText(f.project, 'alias.md').content, /Fixture project/); + assert.ok(listFiles(f.project).files.some(value => value.path === 'alias.md')); +});