Skip to content

Commit 70de896

Browse files
os-zhuangclaude
andcommitted
fix(expr): resolve 2 CodeQL alerts in the ADR-0058 changes
- expression-conformance.test.ts (HIGH js/file-system-race): the ratchet's spec walk did statSync(p) then readFileSync(p) — a check-then-use TOCTOU window. Read the entry type from the single readdir syscall via withFileTypes instead. - cel-to-filter.ts coerceLiteral (warning, comparison between inconvertible types): `v !== null` is dead after the preceding line returns for null — drop it; the early return already guarantees non-null. No behavior change; cel-to-filter 52 + expression-conformance 7 still green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 626c445 commit 70de896

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

packages/dogfood/test/expression-conformance.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// #1887 class of declared-but-unwired predicate — breaks the build.
99

1010
import { describe, it, expect } from 'vitest';
11-
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
11+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
1212
import { fileURLToPath } from 'node:url';
1313
import { dirname, join, relative } from 'node:path';
1414
import { EXPRESSION_SURFACE, type ExprSurface } from './expression-conformance.ledger.js';
@@ -26,10 +26,12 @@ const DIALECTS = new Set(['cel', 'cron', 'template', 'js']);
2626
function discoverSurfaces(): Set<string> {
2727
const found = new Set<string>();
2828
const walk = (dir: string) => {
29-
for (const e of readdirSync(dir)) {
30-
const p = join(dir, e);
31-
if (statSync(p).isDirectory()) walk(p);
32-
else if (e.endsWith('.zod.ts')) {
29+
// `withFileTypes` reads the entry type from the single readdir syscall — no
30+
// stat-then-read window (avoids a file-system TOCTOU race; CodeQL).
31+
for (const ent of readdirSync(dir, { withFileTypes: true })) {
32+
const p = join(dir, ent.name);
33+
if (ent.isDirectory()) walk(p);
34+
else if (ent.isFile() && ent.name.endsWith('.zod.ts')) {
3335
const rel = relative(SPEC_SRC, p);
3436
for (const line of readFileSync(p, 'utf8').split('\n')) {
3537
const m = line.match(/^\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*ExpressionInputSchema\b/);

packages/formula/src/cel-to-filter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ function resolveValue(leaf: Leaf, ctx: Ctx): unknown {
387387
function coerceLiteral(v: unknown): unknown {
388388
if (typeof v === 'bigint') return Number(v);
389389
if (v === null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') return v;
390-
if (typeof v === 'object' && v !== null && typeof (v as { valueOf?: unknown }).valueOf === 'function') {
390+
// `v` is already non-null here (the line above returns for null), so a
391+
// further `v !== null` would be a dead comparison; rely on the early return.
392+
if (typeof v === 'object' && typeof (v as { valueOf?: unknown }).valueOf === 'function') {
391393
const prim = (v as { valueOf: () => unknown }).valueOf();
392394
if (typeof prim === 'bigint') return Number(prim);
393395
if (typeof prim === 'number' || typeof prim === 'string' || typeof prim === 'boolean') return prim;

0 commit comments

Comments
 (0)