Skip to content

Turn complex codebases into clear, navigable architecture diagrams with Claude Code.

fix: reject unsafe cloud pull paths (#6)

김승진0ad9a3a8cfc6
diff --git a/src/__tests__/cloud-paths.test.ts b/src/__tests__/cloud-paths.test.ts
new file mode 100644
index 0000000..d830484
--- /dev/null
+++ b/src/__tests__/cloud-paths.test.ts
@@ -0,0 +1,21 @@
+import path from 'node:path';
+import os from 'node:os';
+import { describe, expect, it } from 'vitest';
+import { resolvePullDestination } from '../lib/cloud-paths.js';
+
+describe('resolvePullDestination', () => {
+ const ommDir = path.join(os.tmpdir(), 'omm-path-test', '.omm');
+
+ it('allows safe paths inside the .omm directory', () => {
+ expect(resolvePullDestination(ommDir, 'config.yaml')).toBe(path.join(ommDir, 'config.yaml'));
+ expect(resolvePullDestination(ommDir, 'service/description.md')).toBe(
+ path.join(ommDir, 'service', 'description.md'),
+ );
+ });
+
+ it('rejects paths that escape the .omm directory', () => {
+ expect(() => resolvePullDestination(ommDir, '../secrets.txt')).toThrow(/unsafe file path/i);
+ expect(() => resolvePullDestination(ommDir, '/tmp/evil')).toThrow(/unsafe file path/i);
+ expect(() => resolvePullDestination(ommDir, 'service/../../evil')).toThrow(/unsafe file path/i);
+ });
+});
diff --git a/src/commands/pull.ts b/src/commands/pull.ts
index 338e843..97eddf1 100644
--- a/src/commands/pull.ts
+++ b/src/commands/pull.ts
@@ -3,6 +3,7 @@ import path from 'node:path';
import YAML from 'yaml';
import { getToken, getDefaultOrg, apiRequest } from '../lib/cloud.js';
import { ensureOmmForWrite, getOmmDir } from '../lib/store.js';
+import { resolvePullDestination } from '../lib/cloud-paths.js';
export async function commandPull(): Promise<void> {
ensureOmmForWrite();
@@ -48,7 +49,7 @@ export async function commandPull(): Promise<void> {
const files = data.files ?? [];
for (const file of files) {
- const dest = path.join(ommDir, file.path);
+ const dest = resolvePullDestination(ommDir, file.path);
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.writeFileSync(dest, file.content, 'utf-8');
}
diff --git a/src/lib/cloud-paths.ts b/src/lib/cloud-paths.ts
new file mode 100644
index 0000000..86d0108
--- /dev/null
+++ b/src/lib/cloud-paths.ts
@@ -0,0 +1,23 @@
+import path from 'node:path';
+
+export function resolvePullDestination(ommDir: string, filePath: string): string {
+ if (!filePath) {
+ throw new Error('Unsafe file path received from cloud');
+ }
+
+ const normalized = path.posix.normalize(filePath.replace(/\\/g, '/'));
+ if (!normalized || normalized === '.' || normalized.startsWith('../') || normalized.includes('/../')) {
+ throw new Error(`Unsafe file path received from cloud: ${filePath}`);
+ }
+ if (path.posix.isAbsolute(normalized)) {
+ throw new Error(`Unsafe file path received from cloud: ${filePath}`);
+ }
+
+ const destination = path.resolve(ommDir, normalized);
+ const root = path.resolve(ommDir) + path.sep;
+ if (destination !== path.resolve(ommDir, 'config.yaml') && !destination.startsWith(root)) {
+ throw new Error(`Unsafe file path received from cloud: ${filePath}`);
+ }
+
+ return destination;
+}