TypeScript: which security bugs does the compiler not catch?

Before your next release, parse every untrusted input with a runtime schema, make typescript-eslint's type-aware no-unsafe-* rules errors and check authorization in each handler, not only in middleware. Judge everything else by whether outside data can reach a shell, a query, a file path, a URL, an object merge, eval or raw HTML unchecked.

Reviewed
Next review
Version
1.1
Written for
Developers and tech leads writing production TypeScript for Node.js servers and React front ends, and the security people who review it

If you only do five things

  1. Parse every input at runtime.

    The compiler erases types and as casts1, and JSON.parse() and .json() return any2. Parse bodies, params, headers and messages with a schema such as Zod's z.strictObject(), which rejects unknown keys3.

  2. Make the compiler and linter refuse `any`.

    Keep strict, the default since TypeScript 6.04, add noUncheckedIndexedAccess6 and lint with typescript-eslint's strictTypeChecked7. Run tsc --noEmit in CI, because Node.js runs .ts files unchecked8.

  3. Authorize where the data is read.

    CVE-2025-29927 let attackers bypass authorization in Next.js middleware9, and five more bypasses followed in 202610. Check the user's right to each record in every handler and Server Action12.

  4. Keep input away from shells, SQL, `eval` and raw HTML.

    Cross-site scripting, SQL injection, OS command injection and code injection rank 1st, 2nd, 9th and 10th in the 2025 CWE Top 2513. Use parameterized queries and execFile with an argument array, never eval input, and render user text as JSX children.

  5. Patch the framework within days.

    CVE-2025-55182 (React2Shell), an unauthenticated remote code execution bug in React Server Components rated CVSS 10, was disclosed on 3 December 202514. CISA listed it as exploited two days later and now flags known ransomware use16. Patch within the patching sheet's deadlines and run Node.js 22, 24 or 2617.

Review a TypeScript pull request

  1. Where does outside data enter?

    Find the handlers, Server Actions, webhooks and queue consumers the change touches, including Next.js folders in brackets12, and check that each parses its input first.

  2. Did it add `any`, `as`, `!` or `@ts-expect-error`?

    Ask why for each one near outside data, then follow that data to the sinks in the traps below.

  3. Who may do this, to which record?

    Missing authorization ranks 4th in the CWE Top 2513. Check ownership of the record, not just the login. Server Actions can be called with a direct POST, not just from your UI, so each one checks for itself12.

  4. Does it run code it didn't write?

    Neither vm24 nor the Permission Model is a sandbox: the model is a seat belt for trusted code29, and October's release closed two more gaps in it22. Run untrusted code in its own container without secrets.

  5. What leaves the server?

    Return only the fields the client needs, and keep secrets out of NEXT_PUBLIC_ variables, which reach the browser12. Send new dependencies through the supply-chain sheet's checks.

TypeScript traps and what to use instead

  • JSON.parse(body) as Order. The cast checks nothing, so a missing field or a wrong type flows on1. Use Order.parse(JSON.parse(body)) and treat as on outside data as a bug.
  • exec("convert " + file). exec runs a shell, and Node.js says never to pass it unsanitized input18. Use execFile("convert", [file]), or spawn without shell: true, and reject a file that starts with -. A React Native CLI command injection is on CISA's exploited list16.
  • merge(target, req.body) and obj[a][b] = value. __proto__ or constructor.prototype keys in input can add properties to every object in the process. oRPC's deserializer polluted the prototype before Zod validation ran (CVE-2026-28794, critical)19. Use a Map or Object.create(null)20, and make merge code skip those keys.
  • path.join(root, req.query.name). Path traversal ranks 6th in the CWE Top 2513. Resolve the path with path.resolve or fs.realpath and reject it unless it starts with the root folder plus path.sep.
  • fetch(req.body.webhookUrl). It can reach internal services and the cloud metadata address 169.254.169.254. Allowlist hosts, validate the IP address you connect to and disable redirects21 with redirect: "error". WHATWG URL rules read 0127.0.0.1 as 87.0.0.1, some OS resolvers as 127.0.0.122.
  • new RegExp(input) and nested quantifiers like (a+)*. One crafted string can take exponential time and block the event loop. Cap the input length and use the re2 package for patterns users supply23.
  • eval, new Function and node:vm. vm is not a security mechanism24. Remote code execution through n8n's expression evaluation is on CISA's exploited list16. Run user code only in its own container. --disallow-code-generation-from-strings blocks eval and new Function25.
  • dangerouslySetInnerHTML and user-supplied URLs. React says raw HTML must come from a trusted source and be sanitized26. Accept only https: URLs, parsed with new URL(), and enforce Trusted Types with require-trusted-types-for 'script'27.

Tools that catch it

  • tsc. strict plus noUncheckedIndexedAccess, which strict omits6.
  • typescript-eslint. strictTypeChecked with parserOptions.projectService: true enables the no-unsafe-* rules, no-implied-eval and no-non-null-assertion. Add no-unsafe-type-assertion yourself7.
  • CodeQL. js/command-line-injection, js/path-injection, js/request-forgery, js/prototype-polluting-assignment and js/polynomial-redos cover TypeScript too28.
  • Node.js flags. --disable-proto=delete removes __proto__25 but not constructor.prototype20. --permission with --allow-fs-read limits what your code reads through node:fs29.

What you can skip

  • Hand-written type guards for request bodies. They drift from the types they guard. Derive types from the schema with z.infer.
  • eslint-plugin-security as your main check. Its README warns of many false positives30. Triage typed lint and CodeQL findings first.
  • Freezing built-in prototypes. --frozen-intrinsics is experimental25 and freezing can break libraries20. Fix the merges instead.
  • Escaping shell arguments yourself. Drop the shell instead.

What changed in 2026

  • React Server Components had a second critical bug. An unauthenticated remote code execution flaw, exploitable only in rare cases, was fixed on 14 October in 19.0.9, 19.1.10, 19.2.9 and 19.3.115.
  • Next.js shipped more middleware bypasses and critical fixes. Five middleware or proxy bypasses came in May and July, and five critical remote code execution fixes from August to October10. Run at least 16.4.1 or 15.5.2811.
  • TypeScript turned strict on by default. TypeScript 6.0 did so in March4. TypeScript 7.0, the native compiler, has no API until 7.1, so typescript-eslint needs TypeScript 6 alongside5.
  • Node.js 20 reached end of life on 30 April. Node.js 26 has been Active LTS since 28 October, and 22 ends on 30 April 202717.
  • Trusted Types work in every major browser. They became Baseline in February27, and React passes TrustedHTML to the DOM unchanged26.

What this looks like for your code

New project

  • Start on Node.js 26 with strict, noUncheckedIndexedAccess and strictTypeChecked as errors.
  • Write one schema per input and derive the types from it.
  • In Next.js, keep data access and secrets in a server-only layer that authorizes12.

Existing codebase

  • Turn on the no-unsafe-* rules as warnings and fix request handlers first.
  • Search for exec(, shell: true, eval(, dangerouslySetInnerHTML and as any.
  • Check authorization in handlers, not only middleware, and leave Node.js 20.

High-assurance or regulated

  • Make CodeQL's security-extended suite a required check; private code needs a paid GitHub Code Security license31.
  • Run services with --permission, --disable-proto=delete and minimal --allow-* grants.
  • Pen-test proxy.ts and route handlers regularly, as Next.js advises12.