Skip to content

Commit cc65162

Browse files
committed
Check unsafe values in production JavaScript.
1 parent 26871f0 commit cc65162

3 files changed

Lines changed: 55 additions & 34 deletions

File tree

‎packages/void/src/runtime/routing/error-pages/script.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
return;
1515
}
1616
const canvas = document.querySelector('.error-texture');
17+
if (!(canvas instanceof HTMLCanvasElement)) {
18+
return;
19+
}
1720
const ctx = canvas.getContext('2d');
1821
if (!ctx) {
1922
return;

‎platform/packages/api/container/build-agent.mjs‎

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ export function pipeStream(source, stream) {
618618
};
619619

620620
source.setEncoding('utf8');
621-
source.on('data', (chunk) => {
621+
source.on('data', (/** @type {string} */ chunk) => {
622622
const n = chunk.length;
623623
let i = 0;
624624
// A CR at a chunk seam is the one ambiguous case: wait for the next byte so
@@ -1236,7 +1236,7 @@ function formatRate(bytes, elapsedMs) {
12361236
*
12371237
* Exported so the retry loop above it is unit-testable without spawning `tar`.
12381238
*
1239-
* @param {unknown} body web ReadableStream from `fetch`
1239+
* @param {NonNullable<Response['body']>} body response body from `fetch`
12401240
* @param {{ onBytes?: (length: number) => void, signal?: AbortSignal, spawnImpl?: typeof spawn, killGraceMs?: number }} [options]
12411241
* @returns {Promise<void>}
12421242
*/
@@ -1360,8 +1360,10 @@ export function extractTarball(body, options = {}) {
13601360
// `activeChild` out from under the new attempt. Waiting for 'close' makes
13611361
// "attempt finished" mean the child is actually reaped.
13621362
pipeline(
1363-
Readable.fromWeb(/** @type {any} */ (body)),
1364-
async function* meter(source) {
1363+
Readable.fromWeb(
1364+
/** @type {import('node:stream/web').ReadableStream<Uint8Array<ArrayBuffer>>} */ (body),
1365+
),
1366+
async function* meter(/** @type {AsyncIterable<Uint8Array>} */ source) {
13651367
for await (const chunk of source) {
13661368
onBytes(chunk.length);
13671369
yield chunk;
@@ -1402,10 +1404,7 @@ export function extractTarball(body, options = {}) {
14021404
*
14031405
* @param {{
14041406
* fetchImpl?: typeof fetch,
1405-
* extract?: (
1406-
* body: unknown,
1407-
* options?: { onBytes?: (length: number) => void, signal?: AbortSignal },
1408-
* ) => Promise<void>,
1407+
* extract?: typeof extractTarball,
14091408
* resetWorkDir?: () => void | Promise<void>,
14101409
* sleep?: (ms: number) => Promise<void>,
14111410
* isCancelled?: () => boolean,
@@ -1515,10 +1514,7 @@ export async function downloadSource(options = {}) {
15151514
* tarballUrl: string,
15161515
* sourceToken: string,
15171516
* fetchImpl: typeof fetch,
1518-
* extract: (
1519-
* body: unknown,
1520-
* options?: { onBytes?: (length: number) => void, signal?: AbortSignal },
1521-
* ) => Promise<void>,
1517+
* extract: typeof extractTarball,
15221518
* isCancelled: () => boolean,
15231519
* stallTimeoutMs: number,
15241520
* phaseDeadline: number,
@@ -1649,7 +1645,7 @@ async function downloadSourceAttempt(args) {
16491645
}
16501646
// Anything already classified (an HTTP status, a non-zero `tar`) keeps its
16511647
// verdict.
1652-
if (err instanceof Error && typeof (/** @type {any} */ (err).retryable) === 'boolean') {
1648+
if (err instanceof Error && 'retryable' in err && typeof err.retryable === 'boolean') {
16531649
throw err;
16541650
}
16551651
// Everything else reaching here came off the wire: `fetch` rejecting with
@@ -1664,19 +1660,30 @@ async function downloadSourceAttempt(args) {
16641660

16651661
// --- dependency install ------------------------------------------------------
16661662

1663+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
1664+
function isRecord(value) {
1665+
return typeof value === 'object' && value !== null && !Array.isArray(value);
1666+
}
1667+
1668+
/** @param {string} path */
1669+
function readPackageManifest(path) {
1670+
try {
1671+
/** @type {unknown} */
1672+
const value = JSON.parse(readFileSync(path, 'utf8'));
1673+
return isRecord(value) ? value : null;
1674+
} catch {
1675+
return null;
1676+
}
1677+
}
1678+
16671679
/**
16681680
* Read the repo's `package.json#packageManager` pin (e.g. "pnpm@9.1.0"). Returns
1669-
* the raw string, or '' if the file is missing/unreadable/has no pin. Wrapped in
1670-
* try/catch so a malformed package.json can't crash install resolution.
1681+
* the raw string, or '' if the manifest can't be read or has no pin.
16711682
* @returns {string}
16721683
*/
16731684
function readPackageManagerPin() {
1674-
try {
1675-
const pkg = JSON.parse(readFileSync(`${WORK_DIR}/package.json`, 'utf8'));
1676-
return typeof pkg.packageManager === 'string' ? pkg.packageManager : '';
1677-
} catch {
1678-
return '';
1679-
}
1685+
const pkg = readPackageManifest(`${WORK_DIR}/package.json`);
1686+
return typeof pkg?.packageManager === 'string' ? pkg.packageManager : '';
16801687
}
16811688

16821689
/**
@@ -2043,13 +2050,12 @@ function redactUrl(raw) {
20432050
/** Resolve the CLI when deploying the Void source repository or a fork. */
20442051
export function resolveSourceVoidCli(root = WORK_DIR) {
20452052
const packageRoot = `${root}/packages/void`;
2046-
let manifest;
2047-
try {
2048-
manifest = JSON.parse(readFileSync(`${packageRoot}/package.json`, 'utf8'));
2049-
} catch {
2050-
return null;
2051-
}
2052-
if (manifest.name !== 'void' || typeof manifest.bin?.void !== 'string') {
2053+
const manifest = readPackageManifest(`${packageRoot}/package.json`);
2054+
if (
2055+
manifest?.name !== 'void' ||
2056+
!isRecord(manifest.bin) ||
2057+
typeof manifest.bin.void !== 'string'
2058+
) {
20532059
return null;
20542060
}
20552061
const cli = `${packageRoot}/${manifest.bin.void}`;
@@ -2065,12 +2071,11 @@ export function resolveSourceVoidCli(root = WORK_DIR) {
20652071
// locally resolvable" signal under PnP, where `node_modules/.bin/void` never
20662072
// exists. Falls back to `false` on any read/parse error (→ fetch the CLI).
20672073
const repoDeclaresVoid = () => {
2068-
try {
2069-
const pkg = JSON.parse(readFileSync(`${WORK_DIR}/package.json`, 'utf8'));
2070-
return Boolean(pkg.dependencies?.void || pkg.devDependencies?.void);
2071-
} catch {
2072-
return false;
2073-
}
2074+
const pkg = readPackageManifest(`${WORK_DIR}/package.json`);
2075+
return (
2076+
(isRecord(pkg?.dependencies) && Boolean(pkg.dependencies.void)) ||
2077+
(isRecord(pkg?.devDependencies) && Boolean(pkg.devDependencies.void))
2078+
);
20742079
};
20752080

20762081
/**

‎vite.config.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,19 @@ const config = defineConfig({
411411
'react-hooks-js/use-memo': 'error',
412412
},
413413
},
414+
{
415+
files: [
416+
'packages/void/src/runtime/routing/error-pages/script.js',
417+
'platform/packages/api/container/build-agent.mjs',
418+
],
419+
rules: {
420+
'typescript/no-unsafe-argument': 'error',
421+
'typescript/no-unsafe-assignment': 'error',
422+
'typescript/no-unsafe-call': 'error',
423+
'typescript/no-unsafe-member-access': 'error',
424+
'typescript/no-unsafe-return': 'error',
425+
},
426+
},
414427
// Enforce the staged type rules where the SDK and platform have typed contracts.
415428
{
416429
files: ['packages/**/*', 'platform/packages/{dashboard,email-gateway,tail}/**/*'],

0 commit comments

Comments
 (0)