Safety could be an intimidating matter for internet builders. The vocabulary is wealthy and filled with acronyms. Tendencies evolve shortly as hackers and analysts play a perpetual cat-and-mouse sport. Vulnerabilities stem from little particulars we can’t afford to spend an excessive amount of time on throughout our day-to-day operations.
JavaScript builders have already got rather a lot to take with the emergence of a brand new wave of progressive architectures, resembling React Server Parts, Subsequent.js App Router, or Astro islands.
So, let’s have a targeted strategy. What we want is to have the ability to detect and palliate the commonest safety points. A high ten of the commonest vulnerabilities can be excellent.
Meet The OWASP Prime 10
Guess what: there occurs to be such a high ten of the commonest vulnerabilities, curated by specialists within the subject!
It’s offered by the OWASP Basis, and it’s a particularly invaluable useful resource for getting began with safety.
OWASP stands for “Open Worldwide Software Safety Mission.” It’s a nonprofit basis whose aim is to make software program safer globally. It helps many open-source initiatives and produces high-quality schooling assets, together with the OWASP high 10 vulnerabilities checklist.
We are going to dive by way of every merchandise of the OWASP high 10 to know how to acknowledge these vulnerabilities in a full-stack software.
Word: I’ll use Subsequent.js for instance, however this data applies to any related full-stack structure, even exterior of the JavaScript ecosystem.
Let’s begin our countdown in direction of a safer internet!
Quantity 10: Server-Facet Request Forgery (SSRF)
You might have heard about Server-Facet Rendering, aka SSR. Effectively, you may take into account SSRF to be its evil twin acronym.
Server-Facet Request Forgery could be summed up as letting an attacker hearth requests utilizing your backend server. Moreover internet hosting prices which will stand up, the principle drawback is that the attacker will profit out of your server’s degree of accreditation. In a posh structure, this implies having the ability to goal your inner non-public companies utilizing your personal corrupted server.

Right here is an instance. Our app lets a person enter a URL and summarizes the content material of the goal web page server-side utilizing an AI SDK. A mischievous person passes localhost:3000
because the URL as a substitute of an internet site they’d prefer to summarize. Your server will hearth a request towards itself or another service working on port 3000 in your backend infrastructure. It is a extreme SSRF vulnerability!
You’ll wish to watch out when firing requests based mostly on person inputs, particularly server-side.
Quantity 9: Safety Logging And Monitoring Failures
I want we might set up a telepathic reference to our beloved Node.js server working within the backend. As a substitute, the very best factor we have now to see what occurs within the cloud is a dreadful stream of unstructured items of textual content we identify “logs.”
But we must take care of that, not just for debugging or efficiency optimization but additionally as a result of logs are sometimes the one data you’ll get to find and remediate a safety subject.
As a starter, you may wish to concentrate on logging crucial transactions of your software precisely such as you would prioritize writing end-to-end checks. In most purposes, this implies login, signup, payouts, mail sending, and so forth. In an even bigger firm, a extra full telemetry answer is a must have, resembling Open Telemetry, Sentry, or Datadog.
If you’re utilizing React Server Parts, you might have to arrange a correct logging technique anyway because it’s not doable to debug them straight from the browser as we used to do for Consumer elements.
Quantity 8: Software program And Information Integrity Failures
The OWASP high 10 vulnerabilities are likely to have varied ranges of granularity, and this one is mostly a massive household. I’d prefer to concentrate on provide chain assaults, as they’ve gained a variety of reputation through the years.
You might have heard concerning the Log4J vulnerability. It was very publicized, very crucial, and really exploited by hackers. It’s an enormous provide chain assault.
Within the JavaScript ecosystem, you likely set up your dependencies utilizing NPM. Earlier than selecting dependencies, you may wish to craft your self a small checklist of well being indicators.
- Is the library maintained and examined with correct code?
- Does it play a crucial position in my software?
- Who’s the principle contributor?
- Did I spell it proper when putting in?
For extra critical enterprise, you may wish to take into account establishing a Provide Chain Evaluation (SCA) answer; GitHub’s Dependabot is a free one, and Snyk and Datadog are different well-known actors.
Quantity 7: Identification And Authentication Failures
Here’s a stereotypical vulnerability belonging to this class: your admin password is leaked. A hacker finds it. Increase, sport over.
Password administration procedures are past the scope of this text, however within the context of full-stack internet improvement, let’s dive deep into how we will stop brute pressure assaults utilizing Subsequent.js edge middlewares.
Middlewares are tiny proxies written in JavaScript. They course of requests in a manner that’s alleged to be very, very quick, sooner than a traditional Node.js endpoint, for instance. They’re a great match for dealing with low-level processing, like blocking malicious IPs or redirecting customers in direction of the proper translation of a web page.
One fascinating use case is price limiting. You may shortly enhance the safety of your purposes by limiting folks’s capability to spam your POST endpoints, particularly login and signup.
You could go even additional by establishing a Net Functions Firewall (WAF). A WAF lets builders implement elaborate safety guidelines. This isn’t one thing you’ll arrange straight in your software however slightly on the host degree. As an example, Vercel has launched its personal WAF in 2024.
Quantity 6: Weak And Outdated Parts
Now we have mentioned provide chain assaults earlier. Outdated elements are a variation of this vulnerability, the place you truly are the particular person responsible. Sorry about that.
Safety vulnerabilities are sometimes found forward of time by diligent safety analysts earlier than a imply attacker may even begin occupied with exploiting them. Thanks, analysts mates! When this occurs, they fill out a Frequent Vulnerabilities and Publicity and retailer that in a public database.
The treatment is similar as for provide chain assaults: arrange an SCA answer like Dependabot that can often verify for the usage of susceptible packages in your software.

Midway break
I simply wish to point out at this level how a lot progress we have now made because the starting of this text. To sum it up:
- We all know learn how to acknowledge an SSRF. It is a nasty vulnerability, and it’s simple to by chance introduce whereas crafting a brilliant cool characteristic.
- Now we have recognized monitoring and dependency evaluation options as vital items of “help” software program for securing purposes.
- Now we have found out a great use case for Subsequent.js edge middlewares: price limiting our authentication endpoints to forestall brute pressure assaults.
It’s a great time to go seize a tea or espresso. However after that, come again with us as a result of we’re going to uncover the 5 most typical vulnerabilities affecting internet purposes!
Quantity 5: Safety Misconfiguration
There are such a lot of configurations that we will mismanage. However let’s concentrate on probably the most insightful ones for an online developer studying about safety: HTTP headers.
You need to use HTTP response headers to go on a variety of data to the person’s browser about what’s doable or not in your web site.
For instance, by narrowing down the “Permissions-Coverage” headers, you may declare that your web site won’t ever require entry to the person’s digital camera. That is a particularly highly effective safety mechanism in case of a script injection assault (XSS). Even when the hacker manages to run a malicious script within the sufferer’s browser, the latter won’t enable the script to entry the digital camera.
I invite you to watch the safety configuration of any template or boilerplate that you simply use to craft your personal web sites. Do you perceive them correctly? Are you able to enhance them? Answering these questions will inevitably lead you to vastly improve the protection of your web sites!
Quantity 4: Insecure Design
I discover this one humorous, though a bit insulting for us builders.
Design is definitely not nearly code however about the best way we use our programming instruments to supply software program artifacts.

Within the context of full-stack JavaScript frameworks, I might suggest studying learn how to use them idiomatically, the identical manner you’d wish to be taught a overseas language. It’s not nearly translating what you already know word-by-word. It’s essential get a grasp of how a local speaker would phrase their ideas.
Studying idiomatic Subsequent.js is actually, actually laborious. Belief me, I educate this framework to internet builders. Subsequent is all about consumer and server logic hybridization, and a few patterns might not even switch to competing frameworks with a special structure like Astro.js or Remix.
Hopefully, the Subsequent.js core workforce has produced many free studying assets, together with articles and documentation particularly specializing in safety.
I like to recommend studying Sebastian Markbåge’s well-known article “Find out how to Suppose About Safety in Subsequent.js” as a place to begin. For those who use Subsequent.js in an expert setting, take into account organizing correct coaching periods earlier than you begin engaged on high-stakes initiatives.
Quantity 3: Injection
Injections are the epitome of vulnerabilities, the quintessence of breaches, and the paragon of safety points. SQL injections are sometimes very well-known, however JavaScript injections are additionally fairly widespread. Regardless of being well-known vulnerabilities, injections are nonetheless within the high 3 within the OWASP rating!
React doesn’t need you to incorporate person enter that might comprise a malicious script.
The screenshot beneath is an indication of an injection utilizing photographs. It might goal a message board, as an example. The attacker misused the picture posting system. They handed a URL that factors in direction of an API GET endpoint as a substitute of an precise picture. Every time your web site’s customers see this put up of their browser, an authenticated request is fired towards your backend, triggering a fee!
As a bonus, having a GET endpoint that triggers side-effects resembling fee additionally constitutes a danger of Cross-Web site Request Forgery (CSRF, which occurs to be SSRF client-side cousin).

Even skilled builders could be caught off-guard. Are you conscious that dynamic route parameters are person inputs? As an example, (language)/web page.jsx
in a Subsequent.js or Astro app. I usually see clumsy assault makes an attempt when logging them, like “language” being changed by a path traversal like ../../../../passwords.txt
.
Zod is a very fashionable library for working server-side information validation of person inputs. You may add a remodel step to sanitize inputs included in database queries, or that might land in locations the place they find yourself being executed as code.
Quantity 2: Cryptographic Failures
A typical dialogue between two builders which can be in deep, deep hassle:
— Now we have leaked our database and encryption key. What algorithm was used to encrypt the password once more? AES-128 or SHA-512?
— I don’t know, aren’t they the identical factor? They remodel passwords into gibberish, proper?
— Alright. We’re in deep, deep hassle.
This vulnerability principally considerations backend builders who need to take care of delicate private identifiers (PII) or passwords.
To be trustworthy, I don’t know a lot about these algorithms; I studied pc science manner too way back.
The one factor I keep in mind is that you simply want non-reversible algorithms to encrypt passwords, aka hashing algorithms. The purpose is that if the encrypted passwords are leaked, and the encryption key can also be leaked, it can nonetheless be tremendous laborious to hack an account (you may’t simply reverse the encryption).
Within the State of JavaScript survey, we use passwordless authentication with an e mail magic hyperlink and one-way hash emails, so at the same time as admins, we can’t guess a person’s e mail in our database.

And number one is…
Such suspense! We’re about to find that the highest 1 vulnerability on the earth of internet improvement is…
Damaged Entry Management! Tada.
Yeah, the identify isn’t tremendous insightful, so let me rephrase it. It’s about folks having the ability to entry different folks’s accounts or folks having the ability to entry assets they aren’t allowed to. That’s extra spectacular when put this manner.
Some time in the past, I wrote an article about the truth that checking authorization inside a structure might go away web page content material unprotected in Subsequent.js. It’s not a flaw within the framework’s design however a consequence of how React Server Parts have a special mannequin than their consumer counterparts, which then impacts how the structure works in Subsequent.
Here’s a demo of how one can implement a paywall in Subsequent.js that doesn’t shield something.
// app/structure.jsx
// Utilizing cookie-based authentication as regular
async perform checkPaid() {
const token = cookies.get("auth_token");
return await db.hasPayments(token);
}
// Working the fee verify in a structure to use it to all pages
// Sadly, this isn't how Subsequent.js works!
export default async perform Structure() {
// ❌ this would possibly not work as anticipated!!
const hasPaid = await checkPaid();
if (!hasPaid) redirect("/subscribe");
// then render the underlying web page
return {kids}
;
}
// ❌ this may be accessed straight
// by including “RSC=1” to the request that fetches it!
export default perform Web page() {
return PAID CONTENT
}
What We Have Realized From The Prime 5 Vulnerabilities
Most typical vulnerabilities are tightly associated to software design points:
- Copy-pasting configuration with out actually understanding it.
- Having an improper understanding of the framework we use in interior working. Subsequent.js is a posh beast and doesn’t make our life simpler on this level!
- Choosing an algorithm that’s not suited to a given process.
These vulnerabilities are robust ones as a result of they confront us to our personal limits as internet builders. No person is ideal, and probably the most skilled builders will inevitably write susceptible code sooner or later of their lives with out even noticing.
Find out how to stop that? By not staying alone! When doubtful, ask round fellow builders; there are nice possibilities that somebody has confronted the identical points and may lead you to the best options.
The place To Head Now?
First, I need to insist that you’ve got already accomplished an important job of enhancing the safety of your purposes by studying this text. Congratulations!
Most hackers depend on a quantity technique and are usually not notably expert, so they’re actually in ache when confronted with educated builders who can spot and repair the commonest vulnerabilities.

From there, I can recommend a couple of instructions to get even higher at securing your internet purposes:
- Attempt to apply the OWASP high 10 to an software you realize properly, both a private undertaking, your organization’s codebase, or an open-source answer.
- Give a shot at some third-party safety instruments. They have a tendency to overflow builders with an excessive amount of data however take into account that most actors within the subject of safety are conscious of this subject and work actively to offer extra targeted vulnerability alerts.
- I’ve added my favourite security-related assets on the finish of the article, so that you’ll have a lot to learn!
Thanks for studying, and keep safe!
Assets For Additional Studying
This text is impressed by my discuss at React Superior London 2024, “Securing Server-Rendered Functions: Subsequent.js case,” which is offered to look at as a replay on-line.

(yk)