Skip to content

Commit

Permalink
prevent fork bombs
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Dec 3, 2022
1 parent 45513b8 commit c87a4d1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All you need is `tea`.
 


# tea/cli 0.15.0
# tea/cli 0.16.0

Open source is a treasure trove—yet those chests are sealed with gnarly locks.
tea is the key:
Expand Down
19 changes: 11 additions & 8 deletions src/app.X.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Args } from "hooks/useFlags.ts"
import { usePantry } from "hooks"
import { useCellar, usePantry } from "hooks"
import * as semver from "semver"
import exec from "./app.exec.ts"
import { TeaError, UsageError } from "utils"
import { prepare_exec_cmd } from "./app.exec.ts"
import { panic, run, TeaError, UsageError } from "utils"

export default async function X(opts: Args) {
const arg0 = opts.args[0]
if (!arg0) throw new UsageError()

let found: { project: string } | undefined | true
let found: { project: string } | undefined

const pantry = usePantry()
for await (const entry of pantry.ls()) {
Expand All @@ -25,9 +25,12 @@ export default async function X(opts: Args) {
}

opts.mode = 'exec'
if (found !== true && found) {
opts.pkgs.push({ ...found, constraint: new semver.Range('*') })
}
opts.pkgs.push({ ...found, constraint: new semver.Range('*') })

await exec(opts)
const { env, pkgs } = await prepare_exec_cmd(opts.pkgs, {env: opts.env ?? false})
const pkg = pkgs.find(x => x.project == found!.project) ?? panic()
const install = await useCellar().resolve(pkg)
const cmd = opts.args
cmd[0] = install.path.join('bin', arg0).string // force full path to avoid infinite recursion
await run({ cmd, env })
}
24 changes: 15 additions & 9 deletions src/app.exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,27 @@ async function exec(ass: RV1, pkgs: PackageSpecification[], opts: {env: boolean}
} break

case 'cmd': {
let blueprint: VirtualEnv | undefined
if (opts.env) {
blueprint = await useVirtualEnv()
pkgs.push(...blueprint.pkgs)
} else if (magic && (blueprint = await useVirtualEnv().swallow(/^not-found/))) {
pkgs.push(...blueprint.pkgs)
}
const { env } = await install(pkgs)
supp(env, blueprint)
const { env } = await prepare_exec_cmd(pkgs, opts)
await run({ cmd: ass.args, env })
}}
}

////

export async function prepare_exec_cmd(pkgs: PackageSpecification[], opts: {env: boolean}) {
const { magic } = useFlags()
let blueprint: VirtualEnv | undefined
if (opts.env) {
blueprint = await useVirtualEnv()
pkgs.push(...blueprint.pkgs)
} else if (magic && (blueprint = await useVirtualEnv().swallow(/^not-found/))) {
pkgs.push(...blueprint.pkgs)
}
const { env } = await install(pkgs)
supp(env, blueprint)
return { env, pkgs }
}

import {readLines} from "deno/io/buffer.ts"

async function extract_shebang(path: Path) {
Expand Down

0 comments on commit c87a4d1

Please sign in to comment.