-
Notifications
You must be signed in to change notification settings - Fork 4
feat: sync modern opencode directories and portable extra paths #41
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,4 +11,5 @@ coverage/ | |
| .bun.lockb | ||
| .memory/ | ||
| opencode-plugin-template/ | ||
| opencode-docs-*/ | ||
| opencode-docs-*/ | ||
| .worktrees/ | ||
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,166 @@ | ||
| import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { syncRepoToLocal } from './apply.js'; | ||
| import type { SyncPlan } from './paths.js'; | ||
| import { normalizePath } from './paths.js'; | ||
|
|
||
| describe('syncRepoToLocal extra manifest repoPath validation', () => { | ||
| it('rejects absolute repoPath values from manifest', async () => { | ||
| const root = await mkdtemp(path.join(os.tmpdir(), 'opencode-sync-apply-')); | ||
| try { | ||
| const homeDir = path.join(root, 'home'); | ||
| const repoRoot = path.join(root, 'repo'); | ||
| const localPath = path.join(homeDir, 'target.txt'); | ||
| const manifestPath = path.join(repoRoot, 'config', 'extra-manifest.json'); | ||
|
|
||
| await mkdir(path.dirname(manifestPath), { recursive: true }); | ||
| await writeFile( | ||
| manifestPath, | ||
| JSON.stringify( | ||
| { | ||
| entries: [ | ||
| { | ||
| sourcePath: localPath, | ||
| repoPath: '/etc/passwd', | ||
| type: 'file', | ||
| }, | ||
| ], | ||
| }, | ||
| null, | ||
| 2 | ||
| ), | ||
| 'utf8' | ||
| ); | ||
|
|
||
| const plan: SyncPlan = { | ||
| items: [], | ||
| extraConfigs: { | ||
| allowlist: [normalizePath(localPath, homeDir, 'linux')], | ||
| manifestPath, | ||
| entries: [], | ||
| }, | ||
| extraSecrets: { | ||
| allowlist: [], | ||
| manifestPath: path.join(repoRoot, 'secrets', 'extra-manifest.json'), | ||
| entries: [], | ||
| }, | ||
| repoRoot, | ||
| homeDir, | ||
| platform: 'linux', | ||
| }; | ||
|
|
||
| await expect(syncRepoToLocal(plan, null)).rejects.toThrow(/absolute paths are not allowed/); | ||
| } finally { | ||
| await rm(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects traversal repoPath values from manifest', async () => { | ||
| const root = await mkdtemp(path.join(os.tmpdir(), 'opencode-sync-apply-')); | ||
| try { | ||
| const homeDir = path.join(root, 'home'); | ||
| const repoRoot = path.join(root, 'repo'); | ||
| const localPath = path.join(homeDir, 'target.txt'); | ||
| const manifestPath = path.join(repoRoot, 'config', 'extra-manifest.json'); | ||
|
|
||
| await mkdir(path.dirname(manifestPath), { recursive: true }); | ||
| await writeFile( | ||
| manifestPath, | ||
| JSON.stringify( | ||
| { | ||
| entries: [ | ||
| { | ||
| sourcePath: localPath, | ||
| repoPath: '../../etc/passwd', | ||
| type: 'file', | ||
| }, | ||
| ], | ||
| }, | ||
| null, | ||
| 2 | ||
| ), | ||
| 'utf8' | ||
| ); | ||
|
|
||
| const plan: SyncPlan = { | ||
| items: [], | ||
| extraConfigs: { | ||
| allowlist: [normalizePath(localPath, homeDir, 'linux')], | ||
| manifestPath, | ||
| entries: [], | ||
| }, | ||
| extraSecrets: { | ||
| allowlist: [], | ||
| manifestPath: path.join(repoRoot, 'secrets', 'extra-manifest.json'), | ||
| entries: [], | ||
| }, | ||
| repoRoot, | ||
| homeDir, | ||
| platform: 'linux', | ||
| }; | ||
|
|
||
| await expect(syncRepoToLocal(plan, null)).rejects.toThrow(/path escapes repository root/); | ||
| } finally { | ||
| await rm(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('accepts safe relative repoPath values from manifest', async () => { | ||
| const root = await mkdtemp(path.join(os.tmpdir(), 'opencode-sync-apply-')); | ||
| try { | ||
| const homeDir = path.join(root, 'home'); | ||
| const repoRoot = path.join(root, 'repo'); | ||
| const localPath = path.join(homeDir, 'target.txt'); | ||
| const manifestPath = path.join(repoRoot, 'config', 'extra-manifest.json'); | ||
| const repoSourcePath = path.join(repoRoot, 'config', 'extra', 'safe.txt'); | ||
|
|
||
| await mkdir(path.dirname(manifestPath), { recursive: true }); | ||
| await mkdir(path.dirname(repoSourcePath), { recursive: true }); | ||
| await writeFile(repoSourcePath, 'safe-data\n', 'utf8'); | ||
| await writeFile( | ||
| manifestPath, | ||
| JSON.stringify( | ||
| { | ||
| entries: [ | ||
| { | ||
| sourcePath: localPath, | ||
| repoPath: 'config/extra/safe.txt', | ||
| type: 'file', | ||
| }, | ||
| ], | ||
| }, | ||
| null, | ||
| 2 | ||
| ), | ||
| 'utf8' | ||
| ); | ||
|
|
||
| const plan: SyncPlan = { | ||
| items: [], | ||
| extraConfigs: { | ||
| allowlist: [normalizePath(localPath, homeDir, 'linux')], | ||
| manifestPath, | ||
| entries: [], | ||
| }, | ||
| extraSecrets: { | ||
| allowlist: [], | ||
| manifestPath: path.join(repoRoot, 'secrets', 'extra-manifest.json'), | ||
| entries: [], | ||
| }, | ||
| repoRoot, | ||
| homeDir, | ||
| platform: 'linux', | ||
| }; | ||
|
|
||
| await syncRepoToLocal(plan, null); | ||
|
|
||
| const output = await readFile(localPath, 'utf8'); | ||
| expect(output).toBe('safe-data\n'); | ||
| } finally { | ||
| await rm(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
applyExtraPathsfunction trusts therepoPathprovided in the manifest file from the repository. An attacker who controls the sync repository can modify theextra-manifest.jsonto setrepoPathto an absolute path or use path traversal (e.g.,../../etc/passwd). During asyncRepoToLocaloperation, this would cause the client to copy arbitrary files from the local system into the synced directories. If these directories are subsequently synced back to the repository (viasyncLocalToRepo), sensitive local files could be exfiltrated to the attacker-controlled repository.To remediate this, ensure that
repoPathis a relative path and that the resolved path remains within the repository root.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 95a77c0.
applyExtraPathsnow validates manifestrepoPathwithresolveManifestRepoPath: absolute paths are rejected and any path that escapesplan.repoRootis rejected. Added regression tests insrc/sync/apply.test.tsfor absolute and traversal cases.