Applying defensive patterns in JavaScript and TypeScript
Applying defensive patterns in JavaScript and TypeScript connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding defensive patterns in JavaScript and TypeScript helps establish which security property the feature needs and where that property is actually enforced.
What does this involve?
A framework provides useful security mechanisms only when the application uses them consistently and deploys them with appropriate settings. Custom handlers, plugins and alternate execution paths can bypass protections that the development team assumes are automatic.
Never build code from strings. See MDN's eval guidance. eval and new Function execute arbitrary code with the current runtime's privileges, so any attacker-influenced input reaching them is code injection (CWE-94). In browsers, setTimeout and setInterval with a string argument compile it the same way through implied eval.
Nested quantifiers such as (a+)+, and repeated ambiguous alternatives such as ^(a|aa)*$, can make matching take exponential time on crafted input, consuming CPU and potentially blocking the executing thread or event loop (CWE-1333). Whether a pattern is actually exploitable depends on the surrounding expression and the failing input, not just the quantified group.
Why it matters
Consider a realistic failure: a JavaScript utility merges untrusted objects or uses dynamic execution in a security-sensitive path.
A configuration or integration mistake can affect many routes at once. The important question is which application data and operations become reachable, rather than whether a framework option merely differs from its default.
Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.
How penetration testers check for it
Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.
- Review supported runtime versions and release settings.
- Trace identity, input and output through real application paths.
- Inspect custom integrations and security overrides.
- Test important negative cases alongside ordinary functionality.
How to fix it
Use the appropriate maintained mechanisms and verify the result in the actual production configuration.
- Do not use the with statement. It makes scope unpredictable, is forbidden in strict mode, and blocks engine optimizations.
- Enforce this statically with the no-eval, no-implied-eval, and no-new-func lint rules.
- CSP is a browser-side control and does not apply to server-side runtimes, so the lint rules remain the primary enforcement everywhere.
- Keep patterns linear: avoid nested quantifiers and ambiguous alternation over the same characters.
- Avoid unnecessary custom replacements for trusted mechanisms.
- Retain regression checks for the application's actual trust boundaries.
A secure framework cannot determine every business permission or data ownership rule. Those requirements still need clear application design and enforcement.
Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.
Related issues
- Applying defensive Node.js runtime and application patterns
- Applying secure Ruby on Rails development patterns
- Applying secure Symfony configuration and development patterns
Read about our methodology or contact pentested.com to discuss secure development and code review.