import { h as __exportAll } from "./pi-embedded-utils-Dx4_ysu7.js";
import path from "node:path";
import fs from "node:fs";

//#region src/security/scan-paths.ts
var scan_paths_exports = /* @__PURE__ */ __exportAll({
	isPathInside: () => isPathInside,
	isRealPathInside: () => isRealPathInside
});
function isPathInside(basePath, candidatePath) {
	const base = path.resolve(basePath);
	const candidate = path.resolve(candidatePath);
	const rel = path.relative(base, candidate);
	return rel === "" || !rel.startsWith(`..${path.sep}`) && rel !== ".." && !path.isAbsolute(rel);
}
/**
* Symlink-safe path containment check.
*
* Resolves symlinks via `fs.realpathSync` before checking that `candidatePath`
* is inside `basePath`. Prevents workspace escape via symlinks pointing outside
* the workspace root.
*
* Falls back to `path.resolve()` (no symlink resolution) if the path doesn't
* exist yet — this allows creating new files inside the workspace.
*
* Inspired by PicoClaw v0.1.2 security patch (filepath.EvalSymlinks).
*/
function isRealPathInside(basePath, candidatePath) {
	const base = realpathSafe(path.resolve(basePath));
	const candidate = realpathSafe(path.resolve(basePath, candidatePath));
	const rel = path.relative(base, candidate);
	return rel === "" || !rel.startsWith(`..${path.sep}`) && rel !== ".." && !path.isAbsolute(rel);
}
/**
* Resolve the real path (following symlinks), falling back to `path.resolve()`
* if the target doesn't exist (e.g. new file creation).
*/
function realpathSafe(p) {
	try {
		return fs.realpathSync(p);
	} catch {
		return path.resolve(p);
	}
}

//#endregion
export { scan_paths_exports as n, isPathInside as t };