Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

## 2024-05-20 - Fix path traversal vulnerability in exportGherkin
**Vulnerability:** The `--output` argument in `exportGherkin` was not properly validated against path traversal (e.g. `../../etc/passwd`).
**Learning:** `join()` is vulnerable to path traversal if the user-controlled input contains relative path markers. Because this is a CLI executing locally, arbitrary file paths could allow file overwriting outside the working directory bounds.
**Prevention:** Construct absolute paths using `path.resolve()` and verify path boundaries using `const rel = path.relative(root, target)`. Check `rel === ".." || rel.startsWith(".." + path.sep) || path.isAbsolute(rel)` to strictly prevent traversal outside the project root.
11 changes: 9 additions & 2 deletions src/export/gherkin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { dirname, join, resolve, relative, isAbsolute, sep } from "node:path";
import { findUseCaseFile, readConfig, relativePath } from "../files.js";
import { parseUseCaseMarkdown } from "../format/parse.js";
import { VspecError } from "../errors.js";
import type { ParsedUseCase } from "../domain/types.js";

export function renderGherkin(useCase: ParsedUseCase): string {
Expand Down Expand Up @@ -39,7 +40,13 @@ export function exportGherkin(args: { key: string; output?: string; cwd?: string
if (!source) throw new Error("KEY_NOT_FOUND");
const text = renderGherkin(parseUseCaseMarkdown(readFileSync(source, "utf8")));
const output = args.output ?? join("tests", `${args.key}.feature`);
const outputPath = join(config.root, output);
const outputPath = resolve(config.root, output);

const rel = relative(config.root, outputPath);
if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
throw new VspecError("INVALID_ARGUMENT", `Path traversal detected: ${args.output} is outside project root.`);
}

mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, text);
return { key: args.key, text, path: relativePath(outputPath, config.root) };
Expand Down
13 changes: 11 additions & 2 deletions tests/gherkin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { renderGherkin } from "../src/export/gherkin.js";
import { describe, expect, it, vi } from "vitest";
import { renderGherkin, exportGherkin } from "../src/export/gherkin.js";
import { parseUseCaseMarkdown } from "../src/format/parse.js";

describe("gherkin export", () => {
Expand All @@ -10,4 +10,13 @@ describe("gherkin export", () => {
const expected = readFileSync(join(import.meta.dirname, "fixtures/export/VSPEC-010.feature"), "utf8");
expect(renderGherkin(useCase)).toBe(expected);
});

it("prevents path traversal outside project root in exportGherkin", () => {
const cwd = join(import.meta.dirname, "fixtures/export");

// We expect it to throw a VspecError with INVALID_ARGUMENT
expect(() => {
exportGherkin({ key: "VSPEC-010", output: "../../../../../../../../../etc/passwd", cwd });
}).toThrow(/Path traversal detected/);
});
});