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
10 changes: 4 additions & 6 deletions packages/core/src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeFileSystem } from "@effect/platform-node"
import { dirname, isAbsolute, join, relative, resolve as pathResolve } from "path"
import { dirname, isAbsolute, join, relative, resolve as pathResolve, sep } from "path"
import { realpathSync } from "fs"
import * as NFS from "fs/promises"
import { lookup } from "mime-types"
Expand Down Expand Up @@ -233,13 +233,11 @@ export namespace AppFileSystem {
}

export function overlaps(a: string, b: string) {
const relA = relative(a, b)
const relB = relative(b, a)
return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..")
return contains(a, b) || contains(b, a)
}

export function contains(parent: string, child: string) {
const rel = relative(parent, child)
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel))
const result = relative(parent, child)
return result === "" || (!isAbsolute(result) && result !== ".." && !result.startsWith(`..${sep}`))
}
}
8 changes: 8 additions & 0 deletions packages/core/test/filesystem/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,21 @@ describe("AppFileSystem", () => {

test("contains checks path containment", () => {
expect(AppFileSystem.contains("/a/b", "/a/b/c")).toBe(true)
expect(AppFileSystem.contains("/a/b", "/a/b")).toBe(true)
expect(AppFileSystem.contains("/a/b", "/a/b/..c")).toBe(true)
expect(AppFileSystem.contains("/a/b", "/a/c")).toBe(false)
expect(AppFileSystem.contains("/a/b", "/a")).toBe(false)
expect(AppFileSystem.contains("/a/b", "/a/bc")).toBe(false)
})

test("overlaps detects overlapping paths", () => {
expect(AppFileSystem.overlaps("/a/b", "/a/b/c")).toBe(true)
expect(AppFileSystem.overlaps("/a/b", "/a/b")).toBe(true)
expect(AppFileSystem.overlaps("/a/b", "/a/b/..c")).toBe(true)
expect(AppFileSystem.overlaps("/a/b/c", "/a/b")).toBe(true)
expect(AppFileSystem.overlaps("/a/b", "/a")).toBe(true)
expect(AppFileSystem.overlaps("/a", "/b")).toBe(false)
expect(AppFileSystem.overlaps("/a/b", "/a/bc")).toBe(false)
})
})
})
9 changes: 4 additions & 5 deletions packages/opencode/src/util/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { chmod, mkdir, readFile, stat as statFile, writeFile } from "fs/promises"
import { createWriteStream, existsSync, statSync } from "fs"
import { realpathSync } from "fs"
import { dirname, isAbsolute, join, relative, resolve as pathResolve, win32 } from "path"
import { dirname, isAbsolute, join, relative, resolve as pathResolve, sep, win32 } from "path"
import { Readable } from "stream"
import { pipeline } from "stream/promises"
import { Glob } from "@opencode-ai/core/util/glob"
Expand Down Expand Up @@ -163,13 +163,12 @@ export function windowsPath(p: string): string {
)
}
export function overlaps(a: string, b: string) {
const relA = relative(a, b)
const relB = relative(b, a)
return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..")
return contains(a, b) || contains(b, a)
}

export function contains(parent: string, child: string) {
return !relative(parent, child).startsWith("..")
const result = relative(parent, child)
return result === "" || (!isAbsolute(result) && result !== ".." && !result.startsWith(`..${sep}`))
}

export async function findUp(
Expand Down
25 changes: 25 additions & 0 deletions packages/opencode/test/file/path-traversal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Filesystem.contains", () => {
expect(Filesystem.contains("/project", "/project/src")).toBe(true)
expect(Filesystem.contains("/project", "/project/src/file.ts")).toBe(true)
expect(Filesystem.contains("/project", "/project")).toBe(true)
expect(Filesystem.contains("/project", "/project/..config")).toBe(true)
}),
)

Expand All @@ -33,6 +34,7 @@ describe("Filesystem.contains", () => {
expect(Filesystem.contains("/project", "/project/../etc")).toBe(false)
expect(Filesystem.contains("/project", "/project/src/../../etc")).toBe(false)
expect(Filesystem.contains("/project", "/etc/passwd")).toBe(false)
expect(Filesystem.contains("/project", "/")).toBe(false)
}),
)

Expand All @@ -52,6 +54,29 @@ describe("Filesystem.contains", () => {
)
})

describe("Filesystem.overlaps", () => {
it.effect("detects containment in either direction", () =>
Effect.sync(() => {
expect(Filesystem.overlaps("/project", "/project/src")).toBe(true)
expect(Filesystem.overlaps("/project/src", "/project")).toBe(true)
expect(Filesystem.overlaps("/project", "/project")).toBe(true)
}),
)

it.effect("keeps child names beginning with .. inside the parent", () =>
Effect.sync(() => {
expect(Filesystem.overlaps("/project", "/project/..config")).toBe(true)
}),
)

it.effect("rejects unrelated sibling and parent-only paths", () =>
Effect.sync(() => {
expect(Filesystem.overlaps("/project", "/project-other")).toBe(false)
expect(Filesystem.overlaps("/project/src", "/etc")).toBe(false)
}),
)
})

/*
* Integration tests for read() and list() path traversal protection.
*
Expand Down
Loading