104 lines
6.9 KiB
JavaScript
104 lines
6.9 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { safePath, readText, readLines, listFiles, searchFiles, proposeFile, applyChange, hash } from '../src/server/files.mjs';
|
|
import { fixture } from './helpers.mjs';
|
|
|
|
test('proposals preserve project bytes until review; apply backs up the exact original bytes', t => {
|
|
const { store, session, projectRoot, project, data } = fixture(t);
|
|
const original = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from('原文\r\n第二行\r\n')]);
|
|
fs.writeFileSync(path.join(projectRoot, 'notes.md'), original);
|
|
const file = readText(project, 'notes.md');
|
|
const proposed = proposeFile(store, session, { path: 'notes.md', content: '修改后\n第二行\n', reason: 'Test review', base_sha256: file.sha256 });
|
|
assert.equal(proposed.status, 'awaiting_review');
|
|
assert.deepEqual(fs.readFileSync(path.join(projectRoot, 'notes.md')), original);
|
|
session.status = 'needs_review';
|
|
const applied = applyChange(store, session, proposed.change_id);
|
|
assert.equal(applied.status, 'applied');
|
|
assert.equal(session.status, 'completed');
|
|
assert.equal(fs.readFileSync(path.join(projectRoot, 'notes.md'), 'utf8'), '修改后\n第二行\n');
|
|
assert.deepEqual(fs.readFileSync(path.join(data, 'backups', session.id, proposed.change_id + '.txt')), original);
|
|
assert.throws(() => applyChange(store, session, proposed.change_id));
|
|
});
|
|
|
|
test('new nested files are created only on apply and unchanged content creates no proposal', t => {
|
|
const { store, session, projectRoot, project } = fixture(t);
|
|
const current = readText(project, 'README.md');
|
|
assert.equal(proposeFile(store, session, { path: current.path, content: current.content }).status, 'unchanged');
|
|
const proposed = proposeFile(store, session, { path: 'docs/new.md', content: '# New\n', base_sha256: null });
|
|
assert.equal(fs.existsSync(path.join(projectRoot, 'docs')), false);
|
|
applyChange(store, session, proposed.change_id);
|
|
assert.equal(fs.readFileSync(path.join(projectRoot, 'docs', 'new.md'), 'utf8'), '# New\n');
|
|
});
|
|
|
|
test('stale reads and changed baselines are rejected without overwriting external edits', t => {
|
|
const { store, session, projectRoot, project } = fixture(t);
|
|
const original = readText(project, 'README.md');
|
|
const change = proposeFile(store, session, { path: 'README.md', content: '# Suggestion\n', base_sha256: original.sha256 });
|
|
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# External change\n');
|
|
assert.throws(() => proposeFile(store, session, { path: 'README.md', content: 'replacement', base_sha256: original.sha256 }), { status: 409 });
|
|
assert.throws(() => applyChange(store, session, change.change_id), { status: 409 });
|
|
assert.equal(session.changes[0].status, 'conflict');
|
|
assert.equal(fs.readFileSync(path.join(projectRoot, 'README.md'), 'utf8'), '# External change\n');
|
|
});
|
|
|
|
test('file paths reject traversal, absolute paths, credentials and outside junctions', t => {
|
|
const { store, session, project, projectRoot, root } = fixture(t);
|
|
for (const target of ['../outside.txt', 'a/../../outside.txt', 'C:\\Windows\\win.ini', '/etc/passwd', '.env', '.env.local', '.GiT/config', 'data:stream', 'a\0b']) {
|
|
assert.throws(() => safePath(project, target, true), target);
|
|
}
|
|
const outside = path.join(root, 'outside'); fs.mkdirSync(outside);
|
|
fs.writeFileSync(path.join(outside, 'outside.txt'), 'outside');
|
|
fs.symlinkSync(outside, path.join(projectRoot, 'link'), process.platform === 'win32' ? 'junction' : 'dir');
|
|
assert.throws(() => readText(project, 'link/outside.txt'));
|
|
assert.throws(() => proposeFile(store, session, { path: 'link/new.txt', content: 'must not write' }));
|
|
assert.ok(!listFiles(project).files.some(file => file.path.startsWith('link')));
|
|
assert.equal(fs.readFileSync(path.join(outside, 'outside.txt'), 'utf8'), 'outside');
|
|
});
|
|
|
|
test('read and search expose bounded text scopes and reject non UTF-8 / oversized files', t => {
|
|
const { projectRoot, project } = fixture(t);
|
|
fs.writeFileSync(path.join(projectRoot, 'lines.txt'), Array.from({ length: 500 }, (_, index) => 'TODO line ' + (index + 1)).join('\n'));
|
|
const lines = readLines(project, { path: 'lines.txt', start_line: 2, end_line: 500 });
|
|
assert.equal(lines.start_line, 2); assert.equal(lines.end_line, 301); assert.equal(lines.truncated, true);
|
|
const results = searchFiles(project, { query: 'TODO' });
|
|
assert.equal(results.matches.length, 80); assert.equal(results.truncated, true);
|
|
fs.writeFileSync(path.join(projectRoot, 'binary.bin'), Buffer.from([0, 1]));
|
|
fs.writeFileSync(path.join(projectRoot, 'invalid.txt'), Buffer.from([0xff, 0xfe]));
|
|
fs.writeFileSync(path.join(projectRoot, 'large.txt'), 'a'.repeat(1024 * 1024 + 1));
|
|
for (const file of ['binary.bin', 'invalid.txt', 'large.txt']) assert.throws(() => readText(project, file));
|
|
assert.equal(hash(Buffer.from('abc')), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
|
|
});
|
|
|
|
test('project-local junctions cannot expose or create protected Git and credential files', t => {
|
|
const { store, session, project, projectRoot } = fixture(t);
|
|
for (const name of ['.git', 'credentials', '.secrets']) {
|
|
fs.mkdirSync(path.join(projectRoot, name));
|
|
fs.writeFileSync(path.join(projectRoot, name, 'config.txt'), 'protected fixture bytes');
|
|
const alias = name.replaceAll('.', '') + '-alias';
|
|
fs.symlinkSync(path.join(projectRoot, name), path.join(projectRoot, alias), process.platform === 'win32' ? 'junction' : 'dir');
|
|
for (const relative of [name + '/config.txt', alias + '/config.txt']) {
|
|
assert.throws(() => readText(project, relative), relative);
|
|
assert.throws(() => listFiles(project, relative.split('/')[0]), relative);
|
|
}
|
|
assert.throws(() => proposeFile(store, session, { path: alias + '/new.txt', content: 'forbidden' }));
|
|
assert.equal(fs.existsSync(path.join(projectRoot, name, 'new.txt')), false);
|
|
assert.equal(fs.readFileSync(path.join(projectRoot, name, 'config.txt'), 'utf8'), 'protected fixture bytes');
|
|
}
|
|
assert.equal(session.changes.length, 0);
|
|
assert.ok(!listFiles(project).files.some(item => /alias|credentials|secrets|\.git/.test(item.path)));
|
|
fs.mkdirSync(path.join(projectRoot, 'public-docs'));
|
|
fs.writeFileSync(path.join(projectRoot, 'public-docs', 'info.txt'), 'public fixture');
|
|
fs.symlinkSync(path.join(projectRoot, 'public-docs'), path.join(projectRoot, 'docs-alias'), process.platform === 'win32' ? 'junction' : 'dir');
|
|
assert.equal(readText(project, 'docs-alias/info.txt').content, 'public fixture');
|
|
});
|
|
|
|
test('Windows trailing-dot and trailing-space path aliases cannot bypass protected paths', { skip: process.platform !== 'win32' }, t => {
|
|
const { project } = fixture(t);
|
|
assert.equal(safePath(project, '.'), project.root);
|
|
for (const relative of ['.git./config', '.git /config', '.env ', 'credentials /config', '.secrets./new.txt']) {
|
|
assert.throws(() => safePath(project, relative, true), relative);
|
|
}
|
|
});
|