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
2 changes: 1 addition & 1 deletion apps/cli-go/internal/functions/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func downloadFunction(ctx context.Context, projectRef, slug, extractScriptPath s

resBuf := bytes.NewReader(resp.Body)
funcDir := filepath.Join(utils.FunctionsDir, slug)
args := []string{"run", "-A", extractScriptPath, funcDir, *meta.EntrypointPath}
args := []string{"run", "-A", extractScriptPath, funcDir, *meta.EntrypointPath, utils.FunctionsDir}
cmd := exec.CommandContext(ctx, denoPath, args...)
var errBuf bytes.Buffer
cmd.Stdin = resBuf
Expand Down
28 changes: 26 additions & 2 deletions apps/cli-go/internal/utils/denos/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ async function loadEszip(bytes: Uint8Array) {
async function extractEszip(
destPath: string,
entrypointUrl: string,
rootPath: string,
parser: Parser,
specifiers: string[],
) {
const entrypointPath = path.fromFileUrl(entrypointUrl);
const basePath = path.dirname(entrypointPath);
// Containment is enforced against rootPath (the shared functions
// directory), not destPath (this function's own directory): a common
// layout imports a sibling like "../_shared/cors.ts", which legitimately
// resolves one level above destPath, but must never resolve outside
// rootPath entirely.
const resolvedRoot = path.resolve(rootPath);
for (const specifier of specifiers) {
// skip remote dependencies
if (!specifier.startsWith("file:")) {
Expand All @@ -32,16 +39,33 @@ async function extractEszip(
const absPath = path.fromFileUrl(specifier);
const relPath = path.relative(basePath, absPath);
const dest = path.join(destPath, relPath);

const resolvedDest = path.resolve(dest);
const relToRoot = path.relative(resolvedRoot, resolvedDest);
if (
relToRoot === ".." ||
relToRoot.startsWith(`..${path.SEP}`) ||
path.isAbsolute(relToRoot)
) {
Comment thread
staaldraad marked this conversation as resolved.
throw new Error(
`Refusing to extract "${specifier}" outside of ${rootPath}`,
);
}

console.info(path.resolve(dest));
await write(dest, module);
}
}

async function extractSource(destPath: string, entrypointUrl: string) {
async function extractSource(
destPath: string,
entrypointUrl: string,
rootPath: string,
) {
const buf = await readAll(Deno.stdin);

const { parser, specifiers } = await loadEszip(buf);
await extractEszip(destPath, entrypointUrl, parser, specifiers);
await extractEszip(destPath, entrypointUrl, rootPath, parser, specifiers);
}

extractSource(...Deno.args);
Loading