Fail closed: check counts what it did not see, and any nobody wrote becomes unknown (no-ts-suppression, no-hidden-any, strict-lib) #108

Merged
sionzee merged 3 commits from fail-closed into main 2026-11-21 02:34:42 +01:00
Owner

An agent aims at "the checker passes", so whatever the checker doesn't see gets shipped. I ran about 120 small programs through safe-ts check --idiomatic, tsc 7 (strict) and oxlint with the preset to find what all three accept. Two of the leaks were in the foundations, and this PR closes them. It is the first of a series; the next ones cover calls written another way, receivers the checker can't resolve, and built-ins that throw.

1. The gate said "no problems" about code it never looked at

  • A file nothing imports was never checked, with exit 0 and no mention. safe-ts check <dir> was refused.
  • "rules": { "no-any": "off" } in package.json printed "✓ no problems" and was not counted anywhere.
  • @ts-nocheck, @ts-ignore and @ts-expect-error were neither reported nor counted.
  • An unresolved import was a warning that --deny-warnings let through.

Now check takes directories, names the source files its entries don't reach, and names every setting that checks less than the default. --deny-warnings fails on everything the run prints as a warning. A new rule, no-ts-suppression, reports @ts-nocheck, @ts-ignore and an @ts-expect-error without a reason. --write-baseline / --baseline ratchet the escape hatches, weakened settings, files not checked, unresolved imports and warnings per rule.

Two bugs found on the way are fixed too: whether a file counted as a test file depended on the checkout's path (a project under a directory named tests had test-only-helper off everywhere), and the directory walker followed symlink loops.

2. any that nobody wrote

no-any bans the keyword, but TypeScript's lib hands out any on its own. The worst case was our own advice: unsafe(() => JSON.parse(text)), the fix unhandled-failure recommends, was typed Result<any, unknown>, so after isErr the value could be read or assigned as anything. The packs' generated safe versions did the same (json(body) returned Result<any, unknown>).

  • unsafe / unsafeAsync give unknown for a function that returns any. No setup needed.
  • @evobug_com/safe-ts-std/strict-lib, an opt-in types entry, retypes JSON.parse, response.json(), Array.isArray, new Array(n), an untyped new Map(), Object.create, Object.fromEntries, Reflect.get and a promise's rejection reason. check warns when the tsconfig doesn't load it.
  • A new rule, no-hidden-any, covers what types can't retype: event.data, a bare MessageEvent or one with a claimed type argument, Function, history.state, Node's workerData and worker messages.
  • The packs' safe versions return unknown where lib says any.

What an adopter will notice

These can turn a green build red; the CHANGELOG's Changed section says what to do about each.

  • --deny-warnings now fails on unresolved imports, files not checked and setup warnings (a pack enabled but not installed, strict-lib not loaded, noUncheckedIndexedAccess off).
  • Code that read fields off unsafe(() => JSON.parse(...)) stops compiling: validate the value, or use parseJson<T>.
  • Two new rules are errors by default: no-ts-suppression and no-hidden-any.

How it was checked

  • The Rust suite (326 tests), test:std, typecheck, the docs examples (813 compile), test:vite and the docs site build pass.
  • 26 programs that must be stopped are stopped, and a set of legitimate forms (typed collections, Array.isArray on a union, a ws socket's typed message event, the project's own typed emitters) stays free of findings.
  • On an adopter's project (346 files) the findings are identical before and after, plus one new no-hidden-any.
  • The new test workflow passes on the runner. Its first run found one existing Node-pack test that assumed it was not root (process.initgroups with an unknown user succeeds as root); the third commit gives that test a trigger that fails as either user.

Not verified

  • What strict-lib does to a full adopter build under tsc is not measured; the checkout I had could not resolve its workspace packages.
  • The one new finding on the adopter is a false positive in effect: an untyped event.data passed straight to a parameter typed unknown through a contextually typed callback, which the checker can't see. Annotating the event clears it; the rule page lists this case.

Created with Claude Opus subagents (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.

An agent aims at "the checker passes", so whatever the checker doesn't see gets shipped. I ran about 120 small programs through `safe-ts check --idiomatic`, `tsc` 7 (strict) and oxlint with the preset to find what all three accept. Two of the leaks were in the foundations, and this PR closes them. It is the first of a series; the next ones cover calls written another way, receivers the checker can't resolve, and built-ins that throw. ## 1. The gate said "no problems" about code it never looked at - A file nothing imports was never checked, with exit 0 and no mention. `safe-ts check <dir>` was refused. - `"rules": { "no-any": "off" }` in package.json printed "✓ no problems" and was not counted anywhere. - `@ts-nocheck`, `@ts-ignore` and `@ts-expect-error` were neither reported nor counted. - An unresolved import was a warning that `--deny-warnings` let through. Now `check` takes directories, names the source files its entries don't reach, and names every setting that checks less than the default. `--deny-warnings` fails on everything the run prints as a warning. A new rule, `no-ts-suppression`, reports `@ts-nocheck`, `@ts-ignore` and an `@ts-expect-error` without a reason. `--write-baseline` / `--baseline` ratchet the escape hatches, weakened settings, files not checked, unresolved imports and warnings per rule. Two bugs found on the way are fixed too: whether a file counted as a test file depended on the checkout's path (a project under a directory named `tests` had `test-only-helper` off everywhere), and the directory walker followed symlink loops. ## 2. `any` that nobody wrote `no-any` bans the keyword, but TypeScript's lib hands out `any` on its own. The worst case was our own advice: `unsafe(() => JSON.parse(text))`, the fix `unhandled-failure` recommends, was typed `Result<any, unknown>`, so after `isErr` the value could be read or assigned as anything. The packs' generated safe versions did the same (`json(body)` returned `Result<any, unknown>`). - `unsafe` / `unsafeAsync` give `unknown` for a function that returns `any`. No setup needed. - `@evobug_com/safe-ts-std/strict-lib`, an opt-in types entry, retypes `JSON.parse`, `response.json()`, `Array.isArray`, `new Array(n)`, an untyped `new Map()`, `Object.create`, `Object.fromEntries`, `Reflect.get` and a promise's rejection reason. `check` warns when the tsconfig doesn't load it. - A new rule, `no-hidden-any`, covers what types can't retype: `event.data`, a bare `MessageEvent` or one with a claimed type argument, `Function`, `history.state`, Node's `workerData` and worker messages. - The packs' safe versions return `unknown` where lib says `any`. ## What an adopter will notice These can turn a green build red; the CHANGELOG's **Changed** section says what to do about each. - `--deny-warnings` now fails on unresolved imports, files not checked and setup warnings (a pack enabled but not installed, `strict-lib` not loaded, `noUncheckedIndexedAccess` off). - Code that read fields off `unsafe(() => JSON.parse(...))` stops compiling: validate the value, or use `parseJson<T>`. - Two new rules are errors by default: `no-ts-suppression` and `no-hidden-any`. ## How it was checked - The Rust suite (326 tests), `test:std`, `typecheck`, the docs examples (813 compile), `test:vite` and the docs site build pass. - 26 programs that must be stopped are stopped, and a set of legitimate forms (typed collections, `Array.isArray` on a union, a `ws` socket's typed `message` event, the project's own typed emitters) stays free of findings. - On an adopter's project (346 files) the findings are identical before and after, plus one new `no-hidden-any`. - The new test workflow passes on the runner. Its first run found one existing Node-pack test that assumed it was not root (`process.initgroups` with an unknown user succeeds as root); the third commit gives that test a trigger that fails as either user. ## Not verified - What `strict-lib` does to a full adopter build under `tsc` is not measured; the checkout I had could not resolve its workspace packages. - The one new finding on the adopter is a false positive in effect: an untyped `event.data` passed straight to a parameter typed `unknown` through a contextually typed callback, which the checker can't see. Annotating the event clears it; the rule page lists this case. Created with Claude Opus subagents (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
A run could say "no problems" about code it never looked at, or with its rules
turned off. `safe-ts check` now takes directories, names the source files its
entries don't reach, counts unresolved imports as warnings, and names every
setting in package.json that checks less than the default.

- New rule `no-ts-suppression`: `@ts-nocheck`, `@ts-ignore`, and an
  `@ts-expect-error` without a reason. All three are counted as escape hatches.
- `--write-baseline` / `--baseline`: a ratchet over the escape hatches, weakened
  settings, files not checked, unresolved imports and warnings per rule.
- Whether a file is a test file no longer depends on where the checkout lives.
- The directory walker follows a symlinked directory once.
- `.forgejo/workflows/test.yml` runs the test suite on pull requests.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
any nobody wrote becomes unknown: unsafe(), strict-lib, no-hidden-any, the packs' safe versions
Some checks failed
Benchmarks / bench (pull_request) Successful in 3m18s
Tests / test (pull_request) Failing after 6m34s
2498141da2
`no-any` bans the keyword, but TypeScript's lib hands out `any` on its own, and
the fix `unhandled-failure` recommends, `unsafe(() => JSON.parse(text))`, was
typed `Result<any, unknown>`.

- `unsafe` / `unsafeAsync` give `unknown` for a function that returns `any`.
- `@evobug_com/safe-ts-std/strict-lib`: types that retype `JSON.parse`,
  `response.json()`, `Array.isArray`, `new Array(n)`, an untyped `new Map()`,
  `Object.create`, `Object.fromEntries`, `Reflect.get` and a promise's rejection
  reason. `check` warns when the tsconfig doesn't load it.
- New rule `no-hidden-any`: what types can't retype (`event.data`, a bare or
  claimed `MessageEvent`, `Function`, `history.state`, `workerData`).
- The packs' generated safe versions return `unknown` where lib says `any`.
- `--deny-warnings` counts every warning the run prints.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Node pack test: make the initgroups trigger fail as root too
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m4s
Tests / test (pull_request) Successful in 6m10s
7ba1dc35eb
The new test workflow runs as root, where `process.initgroups("no-such-user", 0)`
succeeds: Node hands a user name straight to libc, which only fails on the
`setgroups` permission. The trigger passed locally through `EPERM`, not through
the unknown credential it was written for. It now names a group that doesn't
exist, which Node refuses with `ERR_UNKNOWN_CREDENTIAL` before any system call.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch fail-closed 2026-11-21 02:34:42 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!108
No description provided.