Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(sketch): strengthen path traversal check with explicit realpath canonicalization

RinZ271b3d90d32765
diff --git a/sketch/agent-harness/src/builder.js b/sketch/agent-harness/src/builder.js
index 5eecf51b7..af8081163 100644
--- a/sketch/agent-harness/src/builder.js
+++ b/sketch/agent-harness/src/builder.js
@@ -66,11 +66,22 @@ function loadTokens(specTokensPath, cliTokensPath, specDir) {
const isSafePath = (p, base) => {
try {
+ // Canonicalize both paths to resolve symlinks and '..' segments.
+ // This ensures that even if a path looks like it's inside 'base',
+ // if it's a symlink pointing outside, we'll find the real outside path.
const realP = fs.existsSync(p) ? fs.realpathSync(p) : path.resolve(p);
const realBase = fs.realpathSync(base);
+
const relative = path.relative(realBase, realP);
+
+ // On Windows, relative might be an absolute path if drives differ
if (path.isAbsolute(relative)) return false;
- return !relative.startsWith('..' + path.sep) && relative !== '..';
+
+ // Ensure the path doesn't escape the base directory.
+ // We check for '../' prefix or if it's exactly '..'.
+ // This specifically allows directory names that happen to start with dots (like '..brand').
+ const isOutside = relative.startsWith('..' + path.sep) || relative === '..';
+ return !isOutside;
} catch (e) {
return false;
}
@@ -78,15 +89,21 @@ function loadTokens(specTokensPath, cliTokensPath, specDir) {
if (cliTokensPath) {
const resolved = path.resolve(cliTokensPath);
- // Allow if in tokens dir or local to specDir
- if (isSafePath(resolved, tokensDir) || (specDir && isSafePath(resolved, path.resolve(specDir)))) {
+ // Allow if in tokens dir, local to specDir, or in current working directory (CWD)
+ // CWD is allowed for explicit CLI overrides as documented.
+ if (
+ isSafePath(resolved, tokensDir) ||
+ (specDir && isSafePath(resolved, path.resolve(specDir))) ||
+ isSafePath(resolved, process.cwd())
+ ) {
tokensPath = resolved;
} else {
console.warn(`Unsafe tokens path ignored: ${cliTokensPath}`);
}
} else if (specTokensPath) {
const resolved = path.resolve(specDir, specTokensPath);
- if (isSafePath(resolved, path.resolve(specDir))) {
+ // Allow if in project tokens dir or local to specDir
+ if (isSafePath(resolved, tokensDir) || isSafePath(resolved, path.resolve(specDir))) {
tokensPath = resolved;
} else {
console.warn(`Unsafe spec tokens path ignored: ${specTokensPath}`);
diff --git a/sketch/agent-harness/tests/security.test.js b/sketch/agent-harness/tests/security.test.js
index 9c07b6582..55aa33997 100644
--- a/sketch/agent-harness/tests/security.test.js
+++ b/sketch/agent-harness/tests/security.test.js
@@ -102,4 +102,98 @@ describe('sketch-cli security', () => {
expect(result).toContain('Unsafe spec tokens path ignored');
});
+
+ test('allows symlinks pointing inside the spec directory', () => {
+ const specDir = path.join(OUTPUT_DIR, 'spec-symlink-safe');
+ fs.mkdirSync(specDir, { recursive: true });
+
+ const actualTokensPath = path.join(specDir, 'actual-tokens.json');
+ fs.writeFileSync(actualTokensPath, JSON.stringify({ colors: { primary: '#0000ff' } }));
+
+ const symlinkPath = path.join(specDir, 'safe-link.json');
+ try {
+ fs.symlinkSync(actualTokensPath, symlinkPath);
+ } catch (e) {
+ console.warn('Skipping safe symlink test: symlinks not supported/permitted');
+ return;
+ }
+
+ const inputPath = path.join(specDir, 'design.json');
+ const design = {
+ pages: [{ do_objectID: 'page1', layers: [], artboards: [] }],
+ tokens: './safe-link.json'
+ };
+ fs.writeFileSync(inputPath, JSON.stringify(design));
+
+ const outputPath = path.join(OUTPUT_DIR, 'out-safe-symlink.sketch');
+
+ const result = execSync(
+ `node "${CLI}" build --input "${inputPath}" --output "${outputPath}" 2>&1`,
+ { encoding: 'utf-8', cwd: ROOT }
+ );
+
+ expect(result).toContain('Done!');
+ expect(result).not.toContain('Unsafe spec tokens path ignored');
+ });
+
+ test('allows spec tokens from the global project tokens directory', () => {
+ const specDir = path.join(OUTPUT_DIR, 'spec-global-tokens');
+ fs.mkdirSync(specDir, { recursive: true });
+
+ // We expect it to reach the actual global tokens directory in CLI-Anything/sketch/agent-harness/tokens
+ // For this test, we can mock the behavior by pointing to the real tokens dir
+ const tokensDir = path.resolve(ROOT, 'tokens');
+ const brandTokensPath = path.join(tokensDir, 'brand-test.json');
+ fs.writeFileSync(brandTokensPath, JSON.stringify({ colors: { primary: '#666666' } }));
+
+ const inputPath = path.join(specDir, 'design.json');
+ const design = {
+ pages: [{ do_objectID: 'page1', layers: [], artboards: [] }],
+ tokens: '../../../tokens/brand-test.json' // Correct relative path to reach sketch/agent-harness/tokens
+ };
+ fs.writeFileSync(inputPath, JSON.stringify(design));
+
+ const outputPath = path.join(OUTPUT_DIR, 'out-global.sketch');
+
+ try {
+ const result = execSync(
+ `node "${CLI}" build --input "${inputPath}" --output "${outputPath}" 2>&1`,
+ { encoding: 'utf-8', cwd: ROOT }
+ );
+
+ expect(result).toContain('Done!');
+ expect(result).not.toContain('Unsafe spec tokens path ignored');
+ } finally {
+ if (fs.existsSync(brandTokensPath)) fs.unlinkSync(brandTokensPath);
+ }
+ });
+
+ test('allows CLI tokens from the current working directory (CWD)', () => {
+ // In this test, the ROOT of the harness is the CWD for the command
+ const cwdTokensPath = path.join(ROOT, 'cwd-tokens.json');
+ fs.writeFileSync(cwdTokensPath, JSON.stringify({ colors: { primary: '#333333' } }));
+
+ const specDir = path.join(OUTPUT_DIR, 'spec-cwd');
+ fs.mkdirSync(specDir, { recursive: true });
+
+ const inputPath = path.join(specDir, 'design.json');
+ const design = {
+ pages: [{ do_objectID: 'page1', layers: [], artboards: [] }]
+ };
+ fs.writeFileSync(inputPath, JSON.stringify(design));
+
+ const outputPath = path.join(OUTPUT_DIR, 'out-cwd.sketch');
+
+ try {
+ const result = execSync(
+ `node "${CLI}" build --input "${inputPath}" --output "${outputPath}" --tokens cwd-tokens.json 2>&1`,
+ { encoding: 'utf-8', cwd: ROOT }
+ );
+
+ expect(result).toContain('Done!');
+ expect(result).not.toContain('Unsafe tokens path ignored');
+ } finally {
+ if (fs.existsSync(cwdTokensPath)) fs.unlinkSync(cwdTokensPath);
+ }
+ });
});