hazbox 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of hazbox might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +139 -2
- package/bin/hazbox.mjs +67 -0
- package/package.json +34 -4
- package/src/check.mjs +266 -0
- package/src/prune.mjs +62 -0
- package/src/run.mjs +623 -0
- package/src/seccomp.mjs +74 -0
- package/src/x.mjs +102 -0
package/src/seccomp.mjs
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// A small seccomp filter for bwrap --seccomp. It refuses, with EPERM:
|
|
2
|
+
// - keyctl, add_key, request_key: the kernel session keyring is shared with
|
|
3
|
+
// the parent shell, and tools keep tickets and keys there.
|
|
4
|
+
// - ioctl TIOCSTI and TIOCLINUX: push keystrokes into the terminal, which
|
|
5
|
+
// the shell outside reads after the box exits.
|
|
6
|
+
// Everything else is allowed. Returns null on CPUs it doesn't know.
|
|
7
|
+
|
|
8
|
+
const ERRNO_EPERM = 0x00050001
|
|
9
|
+
const ALLOW = 0x7fff0000
|
|
10
|
+
const TIOCSTI = 0x5412
|
|
11
|
+
const TIOCLINUX = 0x541c
|
|
12
|
+
|
|
13
|
+
// AUDIT_ARCH value -> syscall numbers. The 32-bit ABIs are included because a
|
|
14
|
+
// 64-bit kernel accepts them too.
|
|
15
|
+
const ARCHES = {
|
|
16
|
+
x64: [
|
|
17
|
+
{ arch: 0xc000003e, keyctl: 250, add_key: 248, request_key: 249, ioctl: 16, x32: true },
|
|
18
|
+
{ arch: 0x40000003, keyctl: 288, add_key: 286, request_key: 287, ioctl: 54 }, // i386
|
|
19
|
+
],
|
|
20
|
+
arm64: [
|
|
21
|
+
{ arch: 0xc00000b7, keyctl: 219, add_key: 217, request_key: 218, ioctl: 29 },
|
|
22
|
+
{ arch: 0x40000028, keyctl: 311, add_key: 309, request_key: 310, ioctl: 54 }, // arm
|
|
23
|
+
],
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// seccomp_data: int nr; u32 arch; u64 ip; u64 args[6]. The kernel reads
|
|
27
|
+
// ioctl's cmd as 32 bits, so compare only the low half of args[1].
|
|
28
|
+
const NR = 0, ARCH = 4, ARG1_LO = 24
|
|
29
|
+
|
|
30
|
+
export function seccompFilter(cpu = process.arch) {
|
|
31
|
+
const arches = ARCHES[cpu]
|
|
32
|
+
if (!arches) return null
|
|
33
|
+
const prog = [] // [code, jt, jf, k] with jt/jf as labels or 0
|
|
34
|
+
const LD = k => prog.push([0x20, 0, 0, k])
|
|
35
|
+
const JEQ = (k, jt, jf = 0) => prog.push([0x15, jt, jf, k])
|
|
36
|
+
const JGE = (k, jt, jf = 0) => prog.push([0x35, jt, jf, k])
|
|
37
|
+
const RET = k => prog.push([0x06, 0, 0, k])
|
|
38
|
+
const labels = {}
|
|
39
|
+
const label = name => { labels[name] = prog.length }
|
|
40
|
+
|
|
41
|
+
LD(ARCH)
|
|
42
|
+
arches.forEach((a, i) => JEQ(a.arch, `arch${i}`))
|
|
43
|
+
RET(ERRNO_EPERM) // an ABI this filter doesn't cover
|
|
44
|
+
arches.forEach((a, i) => {
|
|
45
|
+
label(`arch${i}`)
|
|
46
|
+
LD(NR)
|
|
47
|
+
if (a.x32) JGE(0x40000000, 'deny') // x32 ABI on x86-64
|
|
48
|
+
for (const n of [a.keyctl, a.add_key, a.request_key]) JEQ(n, 'deny')
|
|
49
|
+
JEQ(a.ioctl, 'ioctl')
|
|
50
|
+
RET(ALLOW)
|
|
51
|
+
})
|
|
52
|
+
label('ioctl')
|
|
53
|
+
LD(ARG1_LO)
|
|
54
|
+
JEQ(TIOCSTI, 'deny')
|
|
55
|
+
JEQ(TIOCLINUX, 'deny')
|
|
56
|
+
RET(ALLOW)
|
|
57
|
+
label('deny')
|
|
58
|
+
RET(ERRNO_EPERM)
|
|
59
|
+
|
|
60
|
+
const buf = Buffer.alloc(prog.length * 8)
|
|
61
|
+
prog.forEach(([code, jt, jf, k], pc) => {
|
|
62
|
+
const off = t => {
|
|
63
|
+
if (t === 0) return 0
|
|
64
|
+
const d = labels[t] - pc - 1
|
|
65
|
+
if (!(d >= 0 && d < 256)) throw new Error(`seccomp: bad jump to ${t}`)
|
|
66
|
+
return d
|
|
67
|
+
}
|
|
68
|
+
buf.writeUInt16LE(code, pc * 8)
|
|
69
|
+
buf.writeUInt8(off(jt), pc * 8 + 2)
|
|
70
|
+
buf.writeUInt8(off(jf), pc * 8 + 3)
|
|
71
|
+
buf.writeUInt32LE(k >>> 0, pc * 8 + 4)
|
|
72
|
+
})
|
|
73
|
+
return buf
|
|
74
|
+
}
|
package/src/x.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// hazbox x: npx with a card. Shows who published what you're about to run,
|
|
2
|
+
// then runs that exact version inside the box.
|
|
3
|
+
import readline from 'node:readline/promises'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import { run, findProject, HazboxError } from './run.mjs'
|
|
6
|
+
|
|
7
|
+
const DAY = 86400e3
|
|
8
|
+
|
|
9
|
+
export async function card(spec) {
|
|
10
|
+
const at = spec.lastIndexOf('@')
|
|
11
|
+
const name = at > 0 ? spec.slice(0, at) : spec
|
|
12
|
+
const want = at > 0 ? spec.slice(at + 1) : 'latest'
|
|
13
|
+
const res = await fetch(`https://registry.npmjs.org/${name.replace('/', '%2f')}`, { signal: AbortSignal.timeout(15000) })
|
|
14
|
+
if (!res.ok) throw new HazboxError(`${name} not found on npm (HTTP ${res.status})`)
|
|
15
|
+
const doc = await res.json()
|
|
16
|
+
const version = doc['dist-tags']?.[want] ?? want
|
|
17
|
+
const v = doc.versions?.[version]
|
|
18
|
+
if (!v) throw new HazboxError(`${name}@${want} not found`)
|
|
19
|
+
|
|
20
|
+
const now = Date.now()
|
|
21
|
+
const ordered = Object.keys(doc.versions).filter(k => doc.time?.[k]).sort((a, b) => new Date(doc.time[a]) - new Date(doc.time[b]))
|
|
22
|
+
const prev = doc.versions[ordered[ordered.indexOf(version) - 1]]
|
|
23
|
+
const scripts = ['preinstall', 'install', 'postinstall'].filter(s => v.scripts?.[s])
|
|
24
|
+
const dl = await fetch(`https://api.npmjs.org/downloads/point/last-week/${name}`, { signal: AbortSignal.timeout(10000) })
|
|
25
|
+
.then(r => r.json()).catch(() => ({}))
|
|
26
|
+
|
|
27
|
+
const c = {
|
|
28
|
+
package: name, version,
|
|
29
|
+
published: doc.time?.[version]?.slice(0, 10) ?? null,
|
|
30
|
+
ageDays: doc.time?.[version] ? Math.floor((now - new Date(doc.time[version])) / DAY) : null,
|
|
31
|
+
packageAgeDays: doc.time?.created ? Math.floor((now - new Date(doc.time.created)) / DAY) : null,
|
|
32
|
+
publisher: v._npmUser?.name ?? null,
|
|
33
|
+
previousPublisher: prev?._npmUser?.name ?? null,
|
|
34
|
+
weeklyDownloads: dl.downloads ?? null,
|
|
35
|
+
unpackedSize: v.dist?.unpackedSize ?? null,
|
|
36
|
+
dependencies: Object.keys(v.dependencies ?? {}).length,
|
|
37
|
+
installScripts: scripts,
|
|
38
|
+
provenance: Boolean(v.dist?.attestations?.provenance),
|
|
39
|
+
trustedPublisher: v._npmUser?.trustedPublisher?.id ?? null,
|
|
40
|
+
deprecated: v.deprecated ?? null,
|
|
41
|
+
repository: typeof v.repository === 'string' ? v.repository : v.repository?.url ?? null,
|
|
42
|
+
}
|
|
43
|
+
const w = []
|
|
44
|
+
if (c.ageDays !== null && c.ageDays < 7) w.push(`this version is ${c.ageDays} day(s) old; brand-new releases carry the most risk`)
|
|
45
|
+
if (c.packageAgeDays !== null && c.packageAgeDays < 30) w.push(`the package itself is only ${c.packageAgeDays} day(s) old`)
|
|
46
|
+
if (c.previousPublisher && c.publisher && c.previousPublisher !== c.publisher) w.push(`published by ${c.publisher}; the previous version was published by ${c.previousPublisher}`)
|
|
47
|
+
if (scripts.length) w.push(`runs install scripts: ${scripts.join(', ')}`)
|
|
48
|
+
if (!c.provenance && !c.trustedPublisher) w.push('published from a personal machine or token (no provenance, no trusted publishing)')
|
|
49
|
+
if (c.weeklyDownloads !== null && c.weeklyDownloads < 1000) w.push(`low usage: ${c.weeklyDownloads} downloads last week`)
|
|
50
|
+
if (c.deprecated) w.push(`deprecated: ${c.deprecated}`)
|
|
51
|
+
c.warnings = w
|
|
52
|
+
return c
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Every string here comes from the registry, where anyone can publish
|
|
56
|
+
// anything. Strip control characters so text can't move the cursor and
|
|
57
|
+
// overwrite the warnings above the prompt.
|
|
58
|
+
export const clean = v => typeof v === 'string' ? v.replace(/[\x00-\x1f\x7f-\x9f]/g, ' ')
|
|
59
|
+
: Array.isArray(v) ? v.map(clean)
|
|
60
|
+
: v && typeof v === 'object' ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, clean(x)]))
|
|
61
|
+
: v
|
|
62
|
+
|
|
63
|
+
const size = n => n == null ? '?' : n > 1e6 ? `${(n / 1e6).toFixed(1)} MB` : `${Math.round(n / 1e3)} kB`
|
|
64
|
+
|
|
65
|
+
export async function main(argv) {
|
|
66
|
+
const f = { yes: false, json: false, offline: false, here: false }
|
|
67
|
+
while (argv[0]?.startsWith('-')) {
|
|
68
|
+
const a = argv.shift()
|
|
69
|
+
if (a === '-y' || a === '--yes') f.yes = true
|
|
70
|
+
else if (a === '--json') f.json = true
|
|
71
|
+
else if (a === '--offline') f.offline = true
|
|
72
|
+
else if (a === '--here') f.here = true
|
|
73
|
+
else throw new HazboxError(`unknown flag ${a}`)
|
|
74
|
+
}
|
|
75
|
+
const spec = argv.shift()
|
|
76
|
+
if (!spec) throw new HazboxError('usage: hazbox x [-y] [--json] [--offline] [--here] <package>[@version] [args...]')
|
|
77
|
+
// Check the folder before fetching anything, so a refusal comes first.
|
|
78
|
+
if (!f.json && process.platform === 'linux') findProject(process.cwd(), os.homedir(), f.here)
|
|
79
|
+
const c = clean(await card(spec))
|
|
80
|
+
if (f.json) { console.log(JSON.stringify(c, null, 2)); return 0 }
|
|
81
|
+
|
|
82
|
+
const src = c.provenance ? 'provenance ✓' : c.trustedPublisher ? `trusted publishing ✓ (${c.trustedPublisher})` : 'published by hand'
|
|
83
|
+
const linux = process.platform === 'linux'
|
|
84
|
+
process.stderr.write(`
|
|
85
|
+
${c.package}@${c.version}
|
|
86
|
+
published ${c.published ?? '?'}${c.ageDays !== null ? ` (${c.ageDays}d ago)` : ''} by ${c.publisher ?? '?'}
|
|
87
|
+
usage ${c.weeklyDownloads?.toLocaleString('en-US') ?? '?'} downloads/week · ${size(c.unpackedSize)} · ${c.dependencies} deps
|
|
88
|
+
source ${src} · ${c.repository ?? 'no repo listed'}
|
|
89
|
+
it gets ${linux ? `this project only · no secrets · network: ${f.offline ? 'none' : 'yes'}` : 'NOT SANDBOXED (hazbox run is Linux-only for now)'}
|
|
90
|
+
${c.warnings.length ? c.warnings.map(x => ` ⚠ ${x}`).join('\n') : ' no warnings'}
|
|
91
|
+
|
|
92
|
+
`)
|
|
93
|
+
if (!linux) return 0
|
|
94
|
+
if (!f.yes) {
|
|
95
|
+
if (!process.stdin.isTTY) throw new HazboxError('not a terminal; pass -y to run')
|
|
96
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stderr })
|
|
97
|
+
const ans = (await rl.question(' run it? [y/N] ')).trim().toLowerCase()
|
|
98
|
+
rl.close()
|
|
99
|
+
if (ans !== 'y' && ans !== 'yes') return 1
|
|
100
|
+
}
|
|
101
|
+
return run(['npx', '--yes', `${c.package}@${c.version}`, ...argv], { offline: f.offline, here: f.here })
|
|
102
|
+
}
|