Write JavaScript and TypeScript so untrusted data is never treated as executable code or HTML. Use type checks, safe DOM APIs, dependency review, and strict linting to reduce risky patterns such as dynamic evaluation and unsafe string handling. Protect secrets in build pipelines and review client-side packages, because the browser receives everything shipped to it.
What matters most when securing JavaScript and TypeScript?
The core issue is trust boundary control: code that handles user input, browser state, third-party packages, and build output must assume data is hostile until proven otherwise. TypeScript helps catch classes of mistakes earlier, but it does not make runtime data safe. Security depends on disciplined input handling, explicit typing, conservative APIs, and package hygiene.
For teams building modern front ends or Node services, the practical priority is to prevent accidental code execution and data exposure. The biggest failures usually come from unsafe string-to-code patterns, insecure DOM updates, and dependency risk that enters through the ecosystem rather than the application logic itself. Secure Coding Guide is the most useful starting point when you want secure coding patterns that generalise across JavaScript and TypeScript.
How do you avoid unsafe execution and DOM injection?
Do not pass untrusted input into eval, Function, string-based timers, template assembly that becomes executable code, or DOM sinks that interpret HTML. Prefer text insertion APIs, vetted sanitisation where HTML is genuinely required, and explicit parsing or validation before data reaches a sensitive sink. TypeScript types help document intent, but runtime checks still decide what the browser or server actually does.
Pay close attention to code paths that convert plain data into markup, script, or configuration. In practice that means using safe DOM methods, avoiding ad hoc string concatenation, and treating URLs, selectors, attributes, and JSON as separate validation problems rather than one generic “input handling” task. The browser will execute whatever you hand it, so safe API choice matters more than naming conventions or folder structure.
Type narrowing is useful when it reduces ambiguity before a dangerous operation. However, it only improves safety when the narrowed value is also used through a safe sink. Strong typing should reduce accidental misuse, not be used as proof that attacker-controlled content is harmless.
Why do dependencies and build pipelines need the same scrutiny as application code?
JavaScript security is heavily shaped by package risk, because even small apps can import large dependency trees. Review direct and transitive packages, pin where appropriate, remove unused modules, and watch for packages that can run install-time or build-time logic. Supply-chain compromise often appears as a dependency update, a malicious package, or a compromised maintainer account rather than a flaw in your own source.
Protecting secrets in build and deployment flows is equally important. Anything placed in client-side bundles, build logs, CI variables, or artifact metadata should be assumed discoverable. Keep secrets server-side when possible, minimise exposure in CI/CD, and review whether build tooling, source maps, and environment files reveal more than intended. The Shai Hulud npm malware campaign is a useful reminder that package compromise can quickly turn into secret exposure.
Risk and Threat Considerations
JavaScript and TypeScript applications are exposed to both injection-style failures and ecosystem-driven compromise. Unsafe DOM handling can turn untrusted content into script execution, while a poisoned package or leaked build secret can give an attacker persistence, data access, or the ability to alter shipped code.
Failure mechanism: The failure usually happens when developers treat data as trusted at the point it crosses into execution, rendering, dependency installation, or build automation. A single unsafe sink, compromised package, or exposed token can expand from a local coding mistake into application compromise or supply-chain abuse.
Impact: The result can include account takeover, browser-side code execution, secret theft, malicious updates, data exfiltration, and hard-to-detect compromise of release pipelines. The risk grows sharply when the same package set, secrets, or unsafe patterns are reused across multiple apps or environments.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
MITRE ATT&CK addresses the attack and risk surface, while OWASP ASVS and CIS Controls v8 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP ASVS | V1 — Encoding and Sanitization | JavaScript security hinges on safe handling of untrusted input before rendering or execution. |
| V13 — Configuration | Secure defaults and tooling settings shape unsafe DOM, eval, and build-time behaviour. | |
| V15 — Secure Coding and Architecture | The topic is fundamentally about avoiding unsafe coding patterns and structuring trusted data flow. | |
| Recommendation — Use V1 controls to encode, sanitise, and validate data before it reaches dangerous sinks. Harden framework and tool settings to block risky dynamic behaviour and unsafe defaults. Design code paths so untrusted data never becomes executable code or privileged browser action. | ||
| CIS Controls v8 | CIS-16 — Application Software Security | This topic covers secure coding, dependency review, and application-level risk reduction. |
| CIS-4 — Secure Configuration of Enterprise Assets and Software | Safe defaults, dependency settings, and build hardening are central to this question. | |
| CIS-5 — Account Management | Secrets, tokens, and service credentials in pipelines are an access-control risk addressed by account hygiene. | |
| Recommendation — Build secure code review and application testing into the release process for JavaScript and TypeScript. Enforce secure configuration baselines for runtimes, build tools, and package managers. Restrict and regularly review accounts and credentials used by build and deployment systems. | ||
| MITRE ATT&CK | T1190 — Exploit Public-Facing Application | Unsafe input handling and exposed web surfaces can let attackers reach code execution paths. |
| T1552 — Unsecured Credentials | The answer addresses secret exposure in code, build output, and client-side artefacts. | |
| Recommendation — Map exposed web entry points to T1190 and hunt for injection-prone request paths. Hunt for exposed tokens, keys, and secrets in repositories, bundles, and CI logs. | ||
Practitioner Guidance
What to prioritise: Start with the places where untrusted input reaches execution or markup, then move to dependency governance and secret handling. Those are the highest-leverage controls because they address the most common and most damaging failure paths in JavaScript ecosystems.
What to verify: Confirm that high-risk APIs are absent or tightly constrained, that HTML insertion points are intentional and sanitised, that package changes are reviewed, and that secrets never appear in client bundles or build artefacts. If you cannot explain why a sink is safe, treat it as unsafe until proven otherwise.
Common mistake: Teams often assume TypeScript alone provides security because it reduces coding errors. It does not, by itself, prevent runtime injection, malicious dependencies, or accidental secret exposure.
Practitioner takeaway: The safest JavaScript and TypeScript code is not the code with the most types, it is the code that keeps untrusted data out of executable paths, limits package trust, and makes secret exposure hard to create and easy to spot.
Related resources from NHI Mgmt Group
- How should security teams make NHI best practices usable across the business?
- How should cloud teams enforce AWS Foundational Security Best Practices across Infrastructure as Code?
- What are the best practices for building a data security program around AI agents that can access sensitive systems?
- How should security teams enforce AWS security best practices in CI/CD pipelines?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on October 19, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org