?A Security Guide For JavaScript Dev
?A Security Guide For JavaScript Dev
JavaScript Developers
I
N Overview...................................................................1
D
The Most Common Security Issues in JavaScript
Projects.................................................................. 2
E
The Last Word in JavaScript Security.....................10
X
e w
Over v i
JavaScript developers have seen multiple issues,
threats, and trends over the past year. Software
security is a top priority across all industries since so
much can go wrong. Programming errors and insecure
user inputs, as well as a multitude of cyberattacks, can
all sink a deployment if the developers do not employ
strict security processes, penetration testing, and awareness of security issues involving JavaScript.
Regular, automated security scans can identify and remediate some vulnerabilities before damage
occurs, but building in security as you go and keeping your applications updated goes a lot further in
maintaining a shield between your code and malicious actors. The old adage, “An ounce of prevention is
worth a pound of cure,” applies.
This post looks at the elements that make up the JavaScript security footprint, highlighting some of the
most common security issues in JavaScript projects. We talk about the JavaScript packages
developers use and the impact of those packages on security, concentrating on the most vulnerable
packages in JavaScript containers and the accompanying risks. Then we provide targeted
recommendations to mitigate JavaScript security risks and produce secure code.
Package.json is so popular because it can list dependencies, specify the version of a package your
project can use with semantic versioning rules, and help you develop a reproducible, highly shareable
build.
You can visualize your dependencies and try deduplication to simplify the dependency tree.
JavaScript is everywhere, in client-side scripting, server-side scripting, and device programming. It has
many moving parts, including indirect dependencies that can hide vulnerabilities at a slight distance.
Dependencies pile up quickly. For example, using Gatsby.js to build a blog adds 190,000 extra
dependencies, and there’s no way to scan them all without an automated tool.
Attackers use these weaknesses to readily access security cameras, microphones, phone calls, and
searches. Building in risk mitigation instead of layering it on after the fact helps immensely in developing
a secure interactive web application.
SQL Injections
In SQL injection, data enters an application via an untrustworthy source and is
used to construct a SQL query dynamically. It can result in data loss or corruption,
denial of access, lack of accountability, and a complete host takeover.
Never trust user input provided in an SQL query. And don’t assign values from the
front-end to the database query without the appropriate care.
Inserting or enabling coded scripting allows hackers to break out of text fields
(like on forms) and provide additional browser-side codes that the hackers can
trigger.
All third-party libraries and tools are vulnerable to JavaScript exploits. Some
libraries and tools are created and monitored by large corporations that
remediate issues and follow best practices for JavaScript security. However,
libraries developed and maintained by independent parties, such as open-source
code, don’t have the same level of monitoring and maintenance due to a lack of resources for auditing
and updating publicly available code.
• GitHub is a repository with a version control system. It’s more like an online backup or host
for your code.
• NPM is a package manager and package repository for JavaScript code. You use a
command-line interface to install, update, and remove packages.
As a package manager, NPM is often implicated in cybersecurity events. Hackers use it to launch attacks
by placing malicious packages into the repository, hundreds of which are found and traced back to NPM
monthly. These packages stole credentials and crypto and ran botnets and reconnaissance.
One popular package was found in hundreds of JavaScript projects now at risk.
• jQuery UI
• Moment.js, Angular.js
• Handlebars
• Mustache, YUI 3
• jQuery mobile
• Knockout
• React
For example, a vulnerability was discovered in the NPM package ua-parser-js that could allow hackers
to execute remote code once installed on a target system. They use the vulnerability to detect browser,
engine, OS, CPU, and device type and model information from user-agent data. Industries report the
vulnerability as being actively exploited for malicious purposes.
• Programmatic errors
• Code smells (characteristics in the source code that may indicate a deeper problem)
• Stylistic errors
• Known security exploits
The best-known linters are JSLint, JSHint, and ESLint. Alternatively, you can use the pluggable J
avaScript linting functionality of Visual Code Studio and Atom.
Package managers allow you to audit your packages for common JavaScript security issues.
Add the hash value generated for the external JavaScript file to the integrity attribute of the <script> or
<link> element. Also, add the crossorigin=anonymous attributes to send a cross-origin request without
credentials.
A content security policy (CSP) adds a security layer to the communication pathways between client and
server. Add content security rules to your HTTP response header according to your CSP. Without inline
scripts in your code, it’s easier to set up an effective CSP, using script-src and default-src directives to
block those scripts entirely. If malicious scripts attempt to execute, they automatically fail.
The pattern HTML attribute validates the value of an input using a regular expression, another technique
to validate user input.
Also, most browsers support a Constraint Validation API so that you can perform custom input validation.
The API extends the JavaScript interfaces that belong to various HTML form elements, like HTML
InputElement, HTMLButtonElement, and HTMLSelectElement, all providing properties and techniques
for comparing input validity against multiple constraints.
Don’t rely only on client-side validation. Malicious actors use some tools that bypass those validations
and allow attacks directly on the server.
• Encoding adds an extra character before a potentially risky character, like \ before
quotation marks.
• Escaping converts a character into a safe, equivalent format like > places into the >
HTML string.
Some free escaping or encoding tools include JavaScript String Escaper and URL Encoder/Decoder.
Don’t use JavaScript methods and properties, like innerHTML, that return unescaped strings. Use the
textContent property instead.
Also, use the ;samesite flag to control cookie transmission in cross-site requests.
Obfuscation is a series of code transformations that turns simple JavaScript code into a modified ver-
sion that is difficult to understand and reverse-engineer. In particular, you want to obfuscate any data
in your code, so use obfuscation to hide variables, objects, and strings to make it hard for a hacker to
understand the type of data that might be within the code.
Beyond concealing data, you can use obfuscation to hide layout and program control flow and include
optimization methods to target the following:
The most common obfuscation techniques for JavaScript include encoding, reordering, renaming, logic
concealing, and splitting.
Use caution, however. Obfuscation alone is not a secure practice. Use it in addition, not instead of good
security practices. The more layers you have to your security, the less likely an attacker can defeat it.
Your DevSecOps team should use a multi-pronged approach to prevent security issues in their source
code. Auditing, testing, and dynamic scanning are all required to keep hackers at bay.
The OWASP Top 10 Security Risks for 2022 shows 94% of applications were tested for a form of broken
access control, the top issue on this year’s list, followed by cryptographic failures and injection. Security
misconfigurations, insecure design, identification and authentication failures, server-side request
forgery, and vulnerable and outdated components also made the list.
Before sending out your latest JavaScript application, provide protection using the techniques above.
Also, allow Kiuwan to help your DevSecOps team mitigate code security risks, improving your application
security using our DevOps tools during software development. Your team can manage every aspect of the
development process, from code analysis to governance, without exposing your JavaScript code to
vulnerabilities or attacks.
Kick your authentication practices and security testing up a notch with Kiuwan’s Code Security (SAST) and
Insights (SCA) to mitigate security incidents and comply with a constantly expanding regulatory
environment.
GET IN TOUCH: