Skip to content
Closed
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
20 changes: 17 additions & 3 deletions core/dev/bench-preflight.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
//
// Exit 0 when the bench can start, 1 with the remedy on stderr when it cannot, 2 on bad usage.
import { spawnSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { existsSync, realpathSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { fileURLToPath } from 'node:url'

// The addon loader picks the directory from Node's report, not from the ELF interpreter, so the
// preflight must read the same signal to name the file the loader would try.
Expand Down Expand Up @@ -81,6 +81,20 @@ export function main(argv = process.argv.slice(2)) {
return 1
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// `import.meta.url` holds the resolved module path, while argv[1] is whatever the shell typed.
// A checkout reached through a symlink (a worktree linked into place, or a path crossing one)
// therefore never matched, and the CLI exited 0 without running the check at all — the gate was
// silently skipped (#3363). Compare real paths, and treat an unreadable argv[1] as "not the
// entry point" rather than throwing.
export function isEntryPoint(argv1, moduleUrl = import.meta.url) {
if (!argv1) return false
try {
return realpathSync(argv1) === fileURLToPath(moduleUrl)
} catch {
return false
}
}

if (isEntryPoint(process.argv[1])) {
process.exit(main())
}
71 changes: 71 additions & 0 deletions core/test/bench-preflight-cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// The preflight's CLI entry point, reached the way a symlinked checkout reaches it (#3363):
// `import.meta.url` is the resolved module path while argv[1] is the path the shell typed, so a
// symlinked path never matched the old guard and the process exited 0 without running a check.
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { after, test } from 'node:test'
import { fileURLToPath } from 'node:url'

const here = dirname(fileURLToPath(import.meta.url))
const roots = []

after(() => {
for (const root of roots) rmSync(root, { recursive: true, force: true })
})

// A shell fixture has to execute `node`, and a tmpdir can sit on a noexec mount (it does on the
// shared box). node_modules is inside the checkout, ignored by git, and always executable.
function tree() {
const base = join(here, '..', 'node_modules', '.cache')
mkdirSync(base, { recursive: true })
const root = mkdtempSync(join(base, 'flybot-bench-preflight-cli-'))
roots.push(root)
return root
}

// A checkout-shaped tree whose script is reached through a symlinked path, plus a bin of only
// what runs before the preflight: node, a `dirname` stand-in, and coreutils without pnpm.
function checkout({ linkedDir = false } = {}) {
const root = tree()
const dev = join(root, 'core', 'dev')
mkdirSync(dev, { recursive: true })
copyFileSync(join(here, '../dev/bench.sh'), join(dev, 'bench.sh'))
copyFileSync(join(here, '../dev/bench-preflight.mjs'), join(dev, 'bench-preflight.mjs'))
const bin = join(root, 'bin')
mkdirSync(bin)
symlinkSync(process.execPath, join(bin, 'node'))
writeFileSync(join(bin, 'dirname'), '#!/bin/sh\nprintf "%s\\n" "${1%/*}"\n')
chmodSync(join(bin, 'dirname'), 0o755)
for (const tool of ['mkdir', 'cat', 'grep', 'rm', 'sleep', 'setsid', 'tail']) {
const found = ['/usr/bin', '/bin'].map((dir) => join(dir, tool)).find((file) => existsSync(file))
if (found) symlinkSync(found, join(bin, tool))
}
const harness = join(root, 'harness')
mkdirSync(harness)
const linked = join(root, 'linked')
symlinkSync(linkedDir ? root : dev, linked)
return { root, bin, harness, linked }
}

test('refuses through a symlinked entry path instead of exiting 0', () => {
const { root, bin, harness, linked } = checkout({ linkedDir: true })
const entry = join(linked, 'linked-preflight.mjs')
symlinkSync(join(linked, 'core', 'dev', 'bench-preflight.mjs'), entry)
const result = spawnSync(process.execPath, [entry, harness], { encoding: 'utf8', env: { PATH: bin, HOME: root } })
if (result.error) assert.fail(`could not run the preflight: ${result.error.message}`)
assert.equal(result.status, 1)
assert.match(result.stderr, /pnpm is not on PATH/)
})

test('bench.sh through a symlinked checkout refuses before making the bench home', { skip: process.platform === 'win32' }, () => {
const { root, bin, harness, linked } = checkout({ linkedDir: true })
const result = spawnSync('/bin/sh', [join(linked, 'core', 'dev', 'bench.sh'), 'smoke', '3097', '8797', 'start'], {
encoding: 'utf8',
env: { PATH: bin, DSH_HARNESS: harness, HOME: linked },
})
if (result.error) assert.fail(`could not run bench.sh: ${result.error.message}`)
assert.notEqual(result.status, 0)
assert.equal(existsSync(join(root, '.dsh-test-smoke')), false)
})
20 changes: 17 additions & 3 deletions patches/ds-bot/files/dev/bench-preflight.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
//
// Exit 0 when the bench can start, 1 with the remedy on stderr when it cannot, 2 on bad usage.
import { spawnSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { existsSync, realpathSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { fileURLToPath } from 'node:url'

// The addon loader picks the directory from Node's report, not from the ELF interpreter, so the
// preflight must read the same signal to name the file the loader would try.
Expand Down Expand Up @@ -81,6 +81,20 @@ export function main(argv = process.argv.slice(2)) {
return 1
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
// `import.meta.url` holds the resolved module path, while argv[1] is whatever the shell typed.
// A checkout reached through a symlink (a worktree linked into place, or a path crossing one)
// therefore never matched, and the CLI exited 0 without running the check at all — the gate was
// silently skipped (#3363). Compare real paths, and treat an unreadable argv[1] as "not the
// entry point" rather than throwing.
export function isEntryPoint(argv1, moduleUrl = import.meta.url) {
if (!argv1) return false
try {
return realpathSync(argv1) === fileURLToPath(moduleUrl)
} catch {
return false
}
}

if (isEntryPoint(process.argv[1])) {
process.exit(main())
}
71 changes: 71 additions & 0 deletions patches/ds-bot/files/test/bench-preflight-cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// The preflight's CLI entry point, reached the way a symlinked checkout reaches it (#3363):
// `import.meta.url` is the resolved module path while argv[1] is the path the shell typed, so a
// symlinked path never matched the old guard and the process exited 0 without running a check.
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { after, test } from 'node:test'
import { fileURLToPath } from 'node:url'

const here = dirname(fileURLToPath(import.meta.url))
const roots = []

after(() => {
for (const root of roots) rmSync(root, { recursive: true, force: true })
})

// A shell fixture has to execute `node`, and a tmpdir can sit on a noexec mount (it does on the
// shared box). node_modules is inside the checkout, ignored by git, and always executable.
function tree() {
const base = join(here, '..', 'node_modules', '.cache')
mkdirSync(base, { recursive: true })
const root = mkdtempSync(join(base, 'flybot-bench-preflight-cli-'))
roots.push(root)
return root
}

// A checkout-shaped tree whose script is reached through a symlinked path, plus a bin of only
// what runs before the preflight: node, a `dirname` stand-in, and coreutils without pnpm.
function checkout({ linkedDir = false } = {}) {
const root = tree()
const dev = join(root, 'core', 'dev')
mkdirSync(dev, { recursive: true })
copyFileSync(join(here, '../dev/bench.sh'), join(dev, 'bench.sh'))
copyFileSync(join(here, '../dev/bench-preflight.mjs'), join(dev, 'bench-preflight.mjs'))
const bin = join(root, 'bin')
mkdirSync(bin)
symlinkSync(process.execPath, join(bin, 'node'))
writeFileSync(join(bin, 'dirname'), '#!/bin/sh\nprintf "%s\\n" "${1%/*}"\n')
chmodSync(join(bin, 'dirname'), 0o755)
for (const tool of ['mkdir', 'cat', 'grep', 'rm', 'sleep', 'setsid', 'tail']) {
const found = ['/usr/bin', '/bin'].map((dir) => join(dir, tool)).find((file) => existsSync(file))
if (found) symlinkSync(found, join(bin, tool))
}
const harness = join(root, 'harness')
mkdirSync(harness)
const linked = join(root, 'linked')
symlinkSync(linkedDir ? root : dev, linked)
return { root, bin, harness, linked }
}

test('refuses through a symlinked entry path instead of exiting 0', () => {
const { root, bin, harness, linked } = checkout({ linkedDir: true })
const entry = join(linked, 'linked-preflight.mjs')
symlinkSync(join(linked, 'core', 'dev', 'bench-preflight.mjs'), entry)
const result = spawnSync(process.execPath, [entry, harness], { encoding: 'utf8', env: { PATH: bin, HOME: root } })
if (result.error) assert.fail(`could not run the preflight: ${result.error.message}`)
assert.equal(result.status, 1)
assert.match(result.stderr, /pnpm is not on PATH/)
})

test('bench.sh through a symlinked checkout refuses before making the bench home', { skip: process.platform === 'win32' }, () => {
const { root, bin, harness, linked } = checkout({ linkedDir: true })
const result = spawnSync('/bin/sh', [join(linked, 'core', 'dev', 'bench.sh'), 'smoke', '3097', '8797', 'start'], {
encoding: 'utf8',
env: { PATH: bin, DSH_HARNESS: harness, HOME: linked },
})
if (result.error) assert.fail(`could not run bench.sh: ${result.error.message}`)
assert.notEqual(result.status, 0)
assert.equal(existsSync(join(root, '.dsh-test-smoke')), false)
})
Loading