resolveUnderRoot already blocked path-layer traversal but explicitly did NOT follow symlinks — a repo symlink config -> /etc/shadow passed the containment check because the link path was under root. clide would then read the target. Add resolveUnderRootFollowingSymlinks: resolves any symlinks at the target and re-verifies containment against the resolved real root. The split keeps pure path math testable without filesystem access. files.read and files.ls now route through it. Tests cover: plain non-symlink passthrough, non-existent target (returns path-layer result so caller surfaces not-found cleanly), single-hop and chained symlinks whose targets escape the workspace, and tolerance of symlinks in the root path itself (macOS /tmp). Also adds the T-101 CHANGELOG entry that the docs commit missed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
184 lines
6.8 KiB
Dart
184 lines
6.8 KiB
Dart
/// Tests for the `files.*` command handlers.
|
|
library;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:clide/clide.dart';
|
|
import 'package:clide/src/daemon/files_commands.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
late Directory sandbox;
|
|
late DaemonDispatcher dispatcher;
|
|
late FilesService files;
|
|
|
|
setUp(() async {
|
|
sandbox = await Directory.systemTemp.createTemp('clide-files-test-');
|
|
// Two files + a subdir + an ignored dir.
|
|
File('${sandbox.path}/README.md').writeAsStringSync('hi');
|
|
File('${sandbox.path}/pubspec.yaml').writeAsStringSync('name: fake');
|
|
Directory('${sandbox.path}/lib').createSync();
|
|
File('${sandbox.path}/lib/main.dart').writeAsStringSync('void main(){}');
|
|
Directory('${sandbox.path}/.dart_tool').createSync();
|
|
File('${sandbox.path}/.dart_tool/hidden').writeAsStringSync('x');
|
|
|
|
final sink = RecordingEventSink();
|
|
files = FilesService(
|
|
root: sandbox,
|
|
events: sink,
|
|
// builtin ignore set hides .dart_tool/, which is what we want.
|
|
ignore: IgnoreSet.builtin(),
|
|
);
|
|
dispatcher = DaemonDispatcher();
|
|
registerFilesCommands(dispatcher, files);
|
|
});
|
|
|
|
tearDown(() async {
|
|
await files.shutdown();
|
|
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
|
});
|
|
|
|
Future<IpcResponse> call(String cmd, Map<String, Object?> args) {
|
|
return dispatcher.dispatch(IpcRequest(id: '1', cmd: cmd, args: args));
|
|
}
|
|
|
|
test('files.root returns the configured root path', () async {
|
|
final r = await call('files.root', const {});
|
|
expect(r.ok, isTrue);
|
|
expect(r.data['path'], sandbox.absolute.path);
|
|
expect(r.data['ignorePatterns'], greaterThan(0));
|
|
});
|
|
|
|
test('files.ls lists the top-level directory', () async {
|
|
final r = await call('files.ls', const {'path': ''});
|
|
expect(r.ok, isTrue);
|
|
final entries = (r.data['entries'] as List).cast<Map>();
|
|
final names = entries.map((e) => e['name']).toList();
|
|
expect(names, containsAll(['lib', 'README.md', 'pubspec.yaml']));
|
|
expect(names, isNot(contains('.dart_tool')));
|
|
});
|
|
|
|
test('files.ls sorts directories first', () async {
|
|
final r = await call('files.ls', const {'path': ''});
|
|
final entries = (r.data['entries'] as List).cast<Map>();
|
|
expect(entries.first['isDirectory'], isTrue);
|
|
});
|
|
|
|
test('files.ls into a subdirectory returns its contents', () async {
|
|
final r = await call('files.ls', const {'path': 'lib'});
|
|
expect(r.ok, isTrue);
|
|
final names = [
|
|
for (final e in (r.data['entries'] as List).cast<Map>()) e['name'],
|
|
];
|
|
expect(names, ['main.dart']);
|
|
});
|
|
|
|
test('files.watch acks subscription', () async {
|
|
final r = await call('files.watch', const {});
|
|
expect(r.ok, isTrue);
|
|
expect(r.data['subscribed'], isTrue);
|
|
});
|
|
|
|
test('files.read returns the file content', () async {
|
|
final r = await call('files.read', const {'path': 'README.md'});
|
|
expect(r.ok, isTrue);
|
|
expect(r.data['content'], 'hi');
|
|
expect(r.data['path'], 'README.md');
|
|
});
|
|
|
|
test('files.read without a path returns toolError', () async {
|
|
final r = await call('files.read', const {});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.kind, IpcErrorKind.toolError);
|
|
expect(r.error!.message, contains('path'));
|
|
});
|
|
|
|
test('files.read with an empty string path returns toolError', () async {
|
|
final r = await call('files.read', const {'path': ''});
|
|
expect(r.ok, isFalse);
|
|
});
|
|
|
|
test('files.read with a path outside the root is rejected', () async {
|
|
final r = await call('files.read', const {'path': '../escape.txt'});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.message, contains('outside workspace'));
|
|
});
|
|
|
|
test('files.read returns toolError for a missing file', () async {
|
|
final r = await call('files.read', const {'path': 'does-not-exist.md'});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.message, contains('not found'));
|
|
});
|
|
|
|
test('files.ls with a path outside the root is rejected', () async {
|
|
final r = await call('files.ls', const {'path': '../escape'});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.message, contains('outside workspace'));
|
|
});
|
|
|
|
test('files.read rejects a symlink whose target is outside the workspace (T-102)', () async {
|
|
final outside = await Directory.systemTemp.createTemp('clide_t102_read_');
|
|
addTearDown(() async {
|
|
if (await outside.exists()) await outside.delete(recursive: true);
|
|
});
|
|
File('${outside.path}/secret.txt').writeAsStringSync('payload');
|
|
Link('${sandbox.path}/leak').createSync('${outside.path}/secret.txt');
|
|
|
|
final r = await call('files.read', const {'path': 'leak'});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.message, contains('outside workspace'));
|
|
});
|
|
|
|
test('files.ls rejects a symlinked subdir whose target is outside (T-102)', () async {
|
|
final outside = await Directory.systemTemp.createTemp('clide_t102_ls_');
|
|
addTearDown(() async {
|
|
if (await outside.exists()) await outside.delete(recursive: true);
|
|
});
|
|
Link('${sandbox.path}/leak-dir').createSync(outside.path);
|
|
|
|
final r = await call('files.ls', const {'path': 'leak-dir'});
|
|
expect(r.ok, isFalse);
|
|
expect(r.error!.message, contains('outside workspace'));
|
|
});
|
|
|
|
test('files.watch is idempotent: a second call still acks subscription', () async {
|
|
final r1 = await call('files.watch', const {});
|
|
final r2 = await call('files.watch', const {});
|
|
expect(r1.ok, isTrue);
|
|
expect(r2.ok, isTrue);
|
|
});
|
|
|
|
test('FilesService.atCwd resolves a workspace root', () {
|
|
final svc = FilesService.atCwd(events: RecordingEventSink());
|
|
expect(svc.root.existsSync(), isTrue);
|
|
addTearDown(svc.shutdown);
|
|
});
|
|
|
|
test('files.watch emits files.changed when a file is created under root', () async {
|
|
final ack = await call('files.watch', const {});
|
|
expect(ack.ok, isTrue);
|
|
await Future<void>.delayed(const Duration(milliseconds: 50));
|
|
await File('${sandbox.path}/created.txt').writeAsString('x');
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
// FilesService.startWatching wires watcher.stream → events.emit;
|
|
// exercising the emit branch is the goal — the consumer-side
|
|
// assertion is covered in test/files/watcher_test.dart.
|
|
});
|
|
|
|
test('FilesService.atCwd walks parent dirs looking for .git, falls back to CWD if none', () async {
|
|
final deepNoGit = await Directory.systemTemp.createTemp('clide-no-git-');
|
|
addTearDown(() => deepNoGit.deleteSync(recursive: true));
|
|
final nested = Directory('${deepNoGit.path}/a/b/c')..createSync(recursive: true);
|
|
final saved = Directory.current;
|
|
try {
|
|
Directory.current = nested;
|
|
final svc = FilesService.atCwd(events: RecordingEventSink());
|
|
// No .git anywhere on the walk → root falls back to CWD.
|
|
expect(svc.root.absolute.path, nested.absolute.path);
|
|
await svc.shutdown();
|
|
} finally {
|
|
Directory.current = saved;
|
|
}
|
|
});
|
|
}
|