Files
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

93 lines
3.8 KiB
TypeScript

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { isPathWithinDir } from '../../src/util/isPathWithinDir';
describe('isPathWithinDir', () => {
let testRoot: string;
let workspace: string;
let outsideDir: string;
beforeEach(async () => {
testRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'permissions-test-'));
workspace = path.join(testRoot, 'workspace');
outsideDir = path.join(testRoot, 'outside');
await fs.mkdir(workspace);
await fs.mkdir(outsideDir);
await fs.mkdir(path.join(workspace, 'subdir'));
await fs.writeFile(path.join(workspace, 'file.txt'), 'test');
await fs.writeFile(path.join(workspace, 'subdir', 'nested.txt'), 'test');
await fs.writeFile(path.join(outsideDir, 'external.txt'), 'test');
// Create a symlink that points outside the workspace (for symlink attack testing)
await fs.symlink(path.join(outsideDir, 'external.txt'), path.join(workspace, 'evil-symlink'));
});
afterEach(async () => {
await fs.rm(testRoot, { recursive: true, force: true });
});
it('should allow files within directory', async () => {
await expect(isPathWithinDir('file.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir('./file.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir('subdir/nested.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir('./subdir/nested.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir(path.join(workspace, 'file.txt'), workspace)).resolves.toBe(true);
});
it('should block files outside directory using relative paths', async () => {
await expect(isPathWithinDir('../outside/external.txt', workspace)).resolves.toBe(false);
await expect(isPathWithinDir('../../external.txt', workspace)).resolves.toBe(false);
});
it('should block files outside directory using absolute paths', async () => {
await expect(isPathWithinDir('/etc/hosts', workspace)).resolves.toBe(false);
await expect(isPathWithinDir(path.join(outsideDir, 'external.txt'), workspace)).resolves.toBe(
false,
);
});
it('should block access to parent directory', async () => {
await expect(isPathWithinDir('..', workspace)).resolves.toBe(false);
await expect(isPathWithinDir('../', workspace)).resolves.toBe(false);
});
it('should allow directory itself', async () => {
await expect(isPathWithinDir('.', workspace)).resolves.toBe(true);
await expect(isPathWithinDir(workspace, workspace)).resolves.toBe(true);
});
it('should handle path traversal attempts', async () => {
await expect(isPathWithinDir('subdir/../../outside/external.txt', workspace)).resolves.toBe(
false,
);
});
it('should block symlinks that point outside directory (symlink attack)', async () => {
await expect(isPathWithinDir('evil-symlink', workspace)).resolves.toBe(false);
});
it('should allow non-existent paths within directory (for Write tool)', async () => {
await expect(isPathWithinDir('nonexistent.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir('subdir/new-file.txt', workspace)).resolves.toBe(true);
await expect(isPathWithinDir('../nonexistent.txt', workspace)).resolves.toBe(false);
await expect(isPathWithinDir('../../nonexistent.txt', workspace)).resolves.toBe(false);
});
it('should throw error immediately if directory itself does not exist', async () => {
const nonExistentDir = path.join(testRoot, 'does-not-exist');
await expect(isPathWithinDir('file.txt', nonExistentDir)).rejects.toThrow(
'Directory does not exist or is inaccessible',
);
await expect(isPathWithinDir('subdir/file.txt', nonExistentDir)).rejects.toThrow(
'Directory does not exist or is inaccessible',
);
});
});