Repository navigation
Expand file tree
/
Copy pathcheck.test.ts
More file actions
423 lines (384 loc) · 13.7 KB
/
Copy pathcheck.test.ts
File metadata and controls
423 lines (384 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import { execFileSync, spawn } from 'node:child_process'
import {
linkSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
symlinkSync,
writeFileSync,
} from 'node:fs'
import { createServer, type RequestListener } from 'node:http'
import type { AddressInfo } from 'node:net'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { cliVersion } from '#sim-cli/version'
import { announceUpdateIfAvailable, type UpdateCheckOptions } from './check'
/** A global install, which is the only shape that gets advised at all. */
const INSTALLED = '/usr/local/lib/node_modules/sim/dist/index.js'
let configDir: string
let previousConfigDir: string | undefined
let notices: string[]
let fetched: URL[]
type RegistryRequest = NonNullable<UpdateCheckOptions['registryRequest']>
let inits: Parameters<RegistryRequest>[1][]
let registryRequest: RegistryRequest
/** Answers the dist-tags request the way the registry does. */
function stubRegistry(
tags: Record<string, unknown> | 'reject' | 'not-found' | 'html' | 'oversized'
): void {
registryRequest = async (input, init) => {
fetched.push(input)
inits.push(init)
if (tags === 'oversized') {
return `${JSON.stringify({ latest: '2.1.5' })}${' '.repeat(64 * 1024)}`
}
if (tags === 'reject') throw new Error('getaddrinfo ENOTFOUND')
if (tags === 'not-found') return null
if (tags === 'html') return '<html>nope</html>'
return JSON.stringify(tags)
}
}
async function run(overrides: Parameters<typeof announceUpdateIfAvailable>[0] = {}) {
await announceUpdateIfAvailable({
currentVersion: '2.1.2',
env: {},
isTty: true,
modulePath: INSTALLED,
registryRequest,
write: (message) => notices.push(message),
...overrides,
})
}
function cachePath(): string {
return join(configDir, 'update-check.json')
}
beforeEach(() => {
previousConfigDir = process.env.SIM_CONFIG_DIR
configDir = mkdtempSync(join(tmpdir(), 'sim-cli-update-'))
process.env.SIM_CONFIG_DIR = configDir
notices = []
fetched = []
inits = []
stubRegistry({ latest: '2.1.5' })
})
afterEach(() => {
if (previousConfigDir === undefined) Reflect.deleteProperty(process.env, 'SIM_CONFIG_DIR')
else process.env.SIM_CONFIG_DIR = previousConfigDir
rmSync(configDir, { recursive: true, force: true })
})
describe('announcing a newer release', () => {
it('names both versions and the command that closes the gap', async () => {
await run()
expect(notices.join('')).toBe('Update available: sim 2.1.2 → 2.1.5. Run: sim update\n')
})
it('sends only its own version and gives the request a one-second deadline', async () => {
await run()
const headers = inits[0]?.headers
expect(headers['user-agent']).toBe(`sim-cli/${cliVersion()}`)
expect(headers.accept).toBe('application/json')
expect(headers.authorization).toBeUndefined()
expect(inits[0]?.maxResponseBytes).toBe(64 * 1024)
expect(inits[0]?.timeoutMs).toBe(1000)
})
})
describe('when the notice is suppressed', () => {
it.each(['CI', 'GITHUB_ACTIONS', 'JENKINS_URL', 'TEAMCITY_VERSION', 'BUILDKITE'])(
'says nothing when %s is set, even where CI allocates a terminal',
async (variable) => {
await run({ env: { [variable]: 'true' } })
expect(fetched).toEqual([])
expect(notices).toEqual([])
}
)
it.each([
'/Users/x/.npm/_npx/a1b2/node_modules/sim/dist/index.js',
'C:\\Users\\x\\AppData\\Local\\npm-cache\\_npx\\a1b2\\node_modules\\sim\\dist\\index.js',
])('says nothing for an npx cache installation (%s)', async (modulePath) => {
await run({ modulePath })
expect(fetched).toEqual([])
expect(notices).toEqual([])
})
})
describe('the once-a-day cache', () => {
it('re-checks rather than trusting a truncated file', async () => {
writeFileSync(cachePath(), '{"version": 1, "checked')
await run()
expect(notices).toHaveLength(1)
})
it('replaces a hard-linked cache without modifying its other name', async () => {
const victimPath = join(configDir, 'victim')
writeFileSync(victimPath, 'do not overwrite')
linkSync(victimPath, cachePath())
await run()
expect(readFileSync(victimPath, 'utf8')).toBe('do not overwrite')
expect(JSON.parse(readFileSync(cachePath(), 'utf8'))).toMatchObject({
version: 1,
checkedAt: expect.any(String),
})
})
it('re-checks rather than following a cache symlink', async () => {
const victimPath = join(configDir, 'victim')
writeFileSync(victimPath, JSON.stringify({ version: 1, checkedAt: new Date().toISOString() }))
symlinkSync(victimPath, cachePath())
await run()
expect(fetched).toHaveLength(1)
expect(notices).toHaveLength(1)
expect(readFileSync(victimPath, 'utf8')).toContain('"version":1')
})
it('still runs the command when the cache cannot be written', async () => {
const wall = join(configDir, 'wall')
writeFileSync(wall, 'not a directory')
process.env.SIM_CONFIG_DIR = join(wall, 'sim')
await expect(run()).resolves.toBeUndefined()
expect(notices).toHaveLength(1)
})
})
describe('when the registry does not answer', () => {
it('refuses a body far larger than this endpoint could legitimately return', async () => {
stubRegistry('oversized')
await run()
expect(notices).toEqual([])
})
it('refuses registry URLs with username/password userinfo', async () => {
await run({ env: { npm_config_registry: 'https://user:secret@npm.internal/api/npm' } })
expect(fetched).toEqual([])
expect(notices).toEqual([])
})
})
describe('across processes', () => {
interface ChildResult {
code: number | null
elapsedMs: number
signal: NodeJS.Signals | null
stderr: string
stdout: string
}
interface CheckOutput {
elapsedMs: number
notices: string[]
}
interface RunChildOptions {
env?: NodeJS.ProcessEnv
nodeArgs?: string[]
useProcessEnv?: boolean
}
let entrypoint: string
let temporaryDir: string
function buildUpdateCheck(temporaryDir: string): string {
const entrypoint = join(temporaryDir, 'dist', 'check.mjs')
const sourcePath = fileURLToPath(new URL('./check.ts', import.meta.url))
mkdirSync(join(temporaryDir, 'dist'))
writeFileSync(join(temporaryDir, 'package.json'), JSON.stringify({ version: '2.1.2' }))
execFileSync(
'bun',
['build', sourcePath, '--target=node', '--format=esm', '--outfile', entrypoint],
{ stdio: 'pipe' }
)
return entrypoint
}
function runChild(
entrypoint: string,
registry: string,
configDir: string,
options: RunChildOptions = {}
): Promise<ChildResult> {
const requestEnvironment = options.useProcessEnv
? 'process.env'
: `{ npm_config_registry: ${JSON.stringify(registry)} }`
const source = `
import { announceUpdateIfAvailable } from ${JSON.stringify(pathToFileURL(entrypoint).href)}
const started = Date.now()
const notices = []
await announceUpdateIfAvailable({
currentVersion: '2.1.2',
env: ${requestEnvironment},
isTty: true,
modulePath: '/usr/local/lib/node_modules/sim/dist/index.js',
write: (message) => notices.push(message),
})
process.stdout.write(JSON.stringify({ elapsedMs: Date.now() - started, notices }))
`
const started = performance.now()
const child = spawn(
process.execPath,
[...(options.nodeArgs ?? []), '--input-type=module', '--eval', source],
{
env: {
...process.env,
BUILDKITE: '0',
CI: '0',
GITHUB_ACTIONS: '0',
JENKINS_URL: '0',
NODE_USE_ENV_PROXY: '0',
NO_PROXY: '127.0.0.1,localhost',
npm_command: '',
TEAMCITY_VERSION: '0',
...options.env,
SIM_CONFIG_DIR: configDir,
...(options.useProcessEnv ? { npm_config_registry: registry } : {}),
},
stdio: ['ignore', 'pipe', 'pipe'],
}
)
let stdout = ''
let stderr = ''
child.stdout.setEncoding('utf8').on('data', (chunk: string) => {
stdout += chunk
})
child.stderr.setEncoding('utf8').on('data', (chunk: string) => {
stderr += chunk
})
return new Promise((resolve, reject) => {
let timedOut = false
const deadline = setTimeout(() => {
timedOut = true
child.kill('SIGKILL')
}, 3500)
child.once('error', reject)
child.once('close', (code, signal) => {
clearTimeout(deadline)
if (timedOut) {
reject(new Error('Update-check child did not exit promptly'))
return
}
resolve({ code, elapsedMs: performance.now() - started, signal, stderr, stdout })
})
})
}
async function withServer(
listener: RequestListener,
run: (origin: string) => Promise<void>
): Promise<void> {
const server = createServer(listener)
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
const { port } = server.address() as AddressInfo
try {
await run(`http://127.0.0.1:${port}`)
} finally {
server.closeAllConnections()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
}
async function _withProxyServer<T>(
run: (origin: string) => Promise<T>
): Promise<{ requests: string[]; result: T }> {
const requests: string[] = []
const body = JSON.stringify({ latest: '2.1.5' })
const server = createServer((request, response) => {
requests.push(request.url ?? '')
response.setHeader('content-type', 'application/json')
response.end(body)
})
server.on('connect', (request, socket, head) => {
requests.push(`CONNECT ${request.url ?? ''}`)
socket.write('HTTP/1.1 200 Connection Established\r\n\r\n')
const respond = () => {
socket.end(
`HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ${Buffer.byteLength(body)}\r\nConnection: close\r\n\r\n${body}`
)
}
if (head.length > 0) respond()
else socket.once('data', respond)
})
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
const { port } = server.address() as AddressInfo
try {
const result = await run(`http://127.0.0.1:${port}`)
return { requests, result }
} finally {
server.closeAllConnections()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
}
beforeAll(() => {
temporaryDir = mkdtempSync(join(tmpdir(), 'sim-cli-update-process-'))
entrypoint = buildUpdateCheck(temporaryDir)
})
afterAll(() => {
rmSync(temporaryDir, { recursive: true, force: true })
})
it('keeps CLI credentials out of probe argv and environment', async () => {
const inspectionPath = join(temporaryDir, 'probe-inspection.json')
const preloadDir = join(temporaryDir, 'probe preload')
const preloadPath = join(preloadDir, 'inspect-probe.cjs')
const registrySentinel = 'registry-secret-sentinel'
const apiKeySentinel = 'api-key-secret-sentinel'
let requestPath: string | undefined
mkdirSync(preloadDir)
writeFileSync(
preloadPath,
`
const { writeFileSync } = require('node:fs')
if (process.execArgv.some((value) => value.includes('maxResponseBytes'))) {
writeFileSync(
process.env.PROBE_INSPECTION_PATH,
JSON.stringify({
argv: process.argv,
environmentValues: Object.values(process.env),
execArgv: process.execArgv,
})
)
}
`
)
await withServer(
(request, response) => {
requestPath = request.url
response.setHeader('content-type', 'application/json')
response.end(JSON.stringify({ latest: '2.1.5' }))
},
async (origin) => {
const registry = `${origin}?token=${registrySentinel}`
const result = await runChild(
entrypoint,
registry,
join(temporaryDir, 'config-credential'),
{
env: {
NODE_OPTIONS: `--require="${preloadPath}"`,
NPM_CONFIG_REGISTRY: registry,
PROBE_INSPECTION_PATH: inspectionPath,
SIM_API_KEY: apiKeySentinel,
},
useProcessEnv: true,
}
)
expect(result).toMatchObject({ code: 0, signal: null, stderr: '' })
expect(requestPath).toBe(`/-/package/sim/dist-tags?token=${registrySentinel}`)
const inspection = JSON.parse(readFileSync(inspectionPath, 'utf8')) as {
argv: string[]
environmentValues: string[]
execArgv: string[]
}
const serializedInspection = JSON.stringify(inspection)
expect(serializedInspection).not.toContain(registrySentinel)
expect(serializedInspection).not.toContain(apiKeySentinel)
}
)
}, 10_000)
it('refuses redirects without contacting their destination', async () => {
const paths: string[] = []
await withServer(
(request, response) => {
paths.push(request.url ?? '')
if (request.url === '/redirected') {
response.setHeader('content-type', 'application/json')
response.end(JSON.stringify({ latest: '2.1.5' }))
return
}
response.writeHead(302, { location: '/redirected' })
response.end()
},
async (origin) => {
const result = await runChild(entrypoint, origin, join(temporaryDir, 'config-redirect'))
expect(result).toMatchObject({ code: 0, signal: null, stderr: '' })
const output = JSON.parse(result.stdout) as CheckOutput
expect(output.notices).toEqual([])
expect(paths).toEqual(['/-/package/sim/dist-tags'])
}
)
}, 10_000)
})