-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix allowlist rejecting paths under symlinked directories (#590) #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rahul188
wants to merge
3
commits into
wonderwhy-er:main
Choose a base branch
from
rahul188:fix/allowlist-symlink-realpath
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import assert from 'assert'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import fsp from 'fs/promises'; | ||
| import { validatePath } from '../dist/tools/filesystem.js'; | ||
| import { configManager } from '../dist/config-manager.js'; | ||
|
|
||
| /** | ||
| * Regression test for #590: a configured allowlist entry that points at a | ||
| * symlink was rejected for its own children. | ||
| * | ||
| * validatePath() canonicalizes the requested path with fs.realpath (so on macOS | ||
| * "/tmp/x" becomes "/private/tmp/x"), but isPathAllowed() used to compare that | ||
| * against the raw, unresolved allowlist entry. The two never matched and every | ||
| * access to the allowed directory failed. | ||
| * | ||
| * This reproduces the mismatch portably with an explicit symlink instead of the | ||
| * macOS /tmp alias: the allowlist entry is the symlink, the requested paths | ||
| * resolve to its target. If directory symlinks cannot be created (Windows | ||
| * without the privilege), the test skips rather than failing. | ||
| */ | ||
|
|
||
| let passed = 0; | ||
| const ok = (msg) => { passed++; console.log(`✓ ${msg}`); }; | ||
|
|
||
| async function run() { | ||
| const base = await fsp.realpath(await fsp.mkdtemp(path.join(os.tmpdir(), 'dc-allow-realpath-'))); | ||
| const target = path.join(base, 'real-target'); | ||
| const link = path.join(base, 'link'); | ||
| const outside = path.join(base, 'outside'); | ||
| await fsp.mkdir(target); | ||
| await fsp.mkdir(outside); | ||
|
|
||
| try { | ||
| await fsp.symlink(target, link, 'dir'); | ||
| } catch (e) { | ||
| // Only skip when the platform genuinely won't create the symlink; a real | ||
| // setup error should fail the test rather than masquerade as a skip. | ||
| const skippableCodes = new Set(['EPERM', 'EACCES', 'ENOTSUP', 'ENOSYS']); | ||
| if (!skippableCodes.has(e.code)) { | ||
| await fsp.rm(base, { recursive: true, force: true }); | ||
| throw e; | ||
| } | ||
| console.log(`SKIP: cannot create directory symlink on this platform (${e.code})`); | ||
| await fsp.rm(base, { recursive: true, force: true }); | ||
| return 'skipped'; | ||
| } | ||
|
|
||
| const original = await configManager.getConfig(); | ||
| const originalAllowed = original.allowedDirectories; | ||
| try { | ||
| // Allowlist the symlink itself, not its resolved target. | ||
| await configManager.setValue('allowedDirectories', [link]); | ||
|
|
||
| const resolvedTarget = await fsp.realpath(target); | ||
|
|
||
| // 1) The allowed directory itself validates and resolves to the target. | ||
| { | ||
| const validated = await validatePath(link); | ||
| assert.strictEqual(await fsp.realpath(validated), resolvedTarget, | ||
| 'allowed symlink directory should validate and resolve to its target'); | ||
| ok('allowlisted symlink directory is accepted'); | ||
| } | ||
|
|
||
| // 2) An existing child under the symlink validates (read/list/search path). | ||
| { | ||
| const childFile = path.join(link, 'child.txt'); | ||
| await fsp.writeFile(path.join(target, 'child.txt'), 'hello'); | ||
| const validated = await validatePath(childFile); | ||
| assert.strictEqual(await fsp.realpath(validated), path.join(resolvedTarget, 'child.txt'), | ||
| 'existing child under the allowed symlink should validate'); | ||
| ok('existing child under the allowlisted symlink is accepted'); | ||
| } | ||
|
|
||
| // 3) A not-yet-created child under the symlink validates (write path). | ||
| { | ||
| const newChild = path.join(link, 'new-file.txt'); | ||
| const validated = await validatePath(newChild); | ||
| assert.strictEqual(await fsp.realpath(path.dirname(validated)), resolvedTarget, | ||
| 'new child under the allowed symlink should validate inside the target itself'); | ||
| ok('not-yet-created child under the allowlisted symlink is accepted'); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // 4) A path outside the allowed directory is still rejected — the fix must | ||
| // not widen access beyond the configured entry. | ||
| { | ||
| await assert.rejects( | ||
| () => validatePath(path.join(outside, 'secret.txt')), | ||
| /Path not allowed/, | ||
| 'paths outside the allowed directory must still be rejected'); | ||
| ok('path outside the allowlisted directory is still rejected'); | ||
| } | ||
| } finally { | ||
| await configManager.setValue('allowedDirectories', originalAllowed); | ||
| await fsp.rm(base, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| run() | ||
| .then((result) => { | ||
| if (result === 'skipped') { console.log('\nSKIPPED'); process.exit(0); } | ||
| console.log(`\nPASS (${passed}/4)`); | ||
| process.exit(0); | ||
| }) | ||
| .catch((e) => { console.error(`\nFAIL: ${e.message}`); process.exit(1); }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.