Summary
@scalar/typebox has a code injection vulnerability in TypeCompiler: crafted schema strings can cause attacker-controlled JavaScript to run during compilation or checking. The risk arises when untrusted schema values reach these operations and are incorporated into generated or evaluated code without adequate safeguards.
Affected context
The issue affects applications using @scalar/typebox where schema strings are supplied by an untrusted or modifiable source and passed to TypeCompiler's Compile() or Check() operations. Risk depends on whether an attacker can control values such as string formats, Kind names, property keys, or Literal values that the compiler processes.
Exploit preconditions
- Attack surface: Application code that processes schemas with TypeCompiler
- Required access: Ability to provide or influence schema strings
- Trigger point: Affected Compile() or Check() processing of the crafted schema
- Why exploitation is possible: Certain schema values can break out of string contexts and be interpreted as JavaScript by the host process
Why this happens
The underlying issue is unsafe handling of schema-derived strings when TypeCompiler processes or generates code. If values such as format names, Kind names, property keys, or Literal values are embedded in executable JavaScript without context-appropriate encoding or safe construction, crafted input may escape its intended string context and inject code. This is a code injection flaw mapped to CWE-94: Improper Control of Generation of Code.
Potential impact
- Arbitrary JavaScript may execute in the host Node.js process
- Application data or secrets accessible to that process could be exposed
- The running application or its dependent services could be disrupted or altered
- An attacker may gain additional capabilities consistent with the process's permissions
General mitigation guidance
Mitigation generally focuses on preventing untrusted schema data from becoming executable code.
Common defensive patterns include:
- Prefer safe compiler designs that avoid generating or evaluating code from schema strings
- Use structured APIs and context-appropriate escaping or encoding for values used in generated code
- Validate schema inputs against a strict, documented schema before compilation
- Reduce trust in externally supplied schemas and restrict who can create or modify them
- Run Node.js workloads with least privilege and limit access to sensitive resources
How teams detect issues like this
High-level detection approaches include:
- Static analysis for schema-derived strings interpolated into generated code or evaluation contexts
- Code review for unsafe string construction involving formats, Kind names, property keys, or Literal values
- Tests using unusual schema strings to verify that compilation and checking treat them strictly as data
- Policy checks that restrict dynamic code generation and flag untrusted schemas reaching compiler operations
Automated code review tools may help identify risky data flows and code-generation patterns.

