Prototype Pollution in JavaScript

Rate this content
Bookmark

In 2018, a new attack vector against JavaScript codebases has been published: Prototype Pollution.
At first glance, it seemed pretty limited in impact: it would basically be a good way to crash some code. However, multiple cases of Remote Code Executions have happened based on this vector.
In this talk, we will clarify what are prototype pollutions, their real impact and of to prevent them from happening in your codebase.

FAQ

Prototype pollution in JavaScript occurs when an arbitrary payload handled by the code can overwrite properties or methods on the prototype chain of one or multiple objects. This usually happens through functions like merging, where properties on the prototype can be unintentionally modified, leading to unexpected behavior or security vulnerabilities.

Prototype pollution can lead to security vulnerabilities such as unauthorized access, remote code execution, and other unintended behaviors within an application. It manipulates the prototype of an object, which all instances of objects inherit methods and properties from, potentially allowing attackers to exploit this for malicious purposes.

To prevent prototype pollution, developers can sanitize and validate incoming data to ensure it does not contain malicious inputs, use Object.create(null) to create objects with no prototype, avoid using vulnerable versions of functions like merge or extend, and employ libraries or tools that filter out prototype properties like __proto__.

Yes, using maps instead of plain JavaScript objects can help prevent prototype pollution because maps do not have a prototype that can be polluted. Maps store keys and values in a form that doesn't interfere with the object's prototype chain.

The __proto__ property in JavaScript is used to assign the prototype of an object. In the context of prototype pollution, malicious payloads can manipulate __proto__ to overwrite properties on an object's prototype chain, leading to potential security risks.

Prototype pollution can significantly impact third-party libraries by altering their behavior if these libraries do not properly guard against modifications to their prototype. This can lead to widespread issues across applications that utilize the affected libraries, compromising application integrity and security.

Real-world consequences of prototype pollution include unauthorized data access, application crashes, and remote code execution. Historical incidents have shown that prototype pollution can lead to significant security breaches, affecting large-scale systems and leading to data loss or corruption.

Vladimir de Turckheim
Vladimir de Turckheim
27 min
14 Apr, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk discusses prototype production in JavaScript and focuses on the concept of prototype pollution. It explains the impact of prototype pollution and ways to avoid it. The Talk also highlights real-world examples of prototype pollution vulnerabilities in Kibana and MongoDB. It provides recommendations for preventing and mitigating prototype pollution, such as filtering out merge functions and using defensive objects. The Talk concludes with a discussion on tools like Semgrep for static analysis and the importance of sanitization and validation in preventing outside attacks.

1. Introduction

Short description:

We'll talk about prototype production in JavaScript, but before that, let's address something important. The speaker discusses their current situation, being unemployed and working on building a company.

Thanks a lot. So we'll talk about prototype production in JavaScript, but before we do that, let's talk about something very important, me. So the bio is a bit outdated and that's on me. I don't work at Datadog anymore, like since two weeks ago. And I'm working at a company named QueryTails that is not even a company I need to tell the lawyer to incorporate. And it's not even an association because it's basically myself. So if you want to feel sad for me, you have to know that I feel so lonely that I do my stand up meetings with Chad GPT in the morning. And speaking of the logo of the companies that's another AI that designed it. So that's basically me being unemployed on Twitter, French unemployment money, trying to build a company.

2. Understanding Prototype Pollution

Short description:

Let's talk about prototype pollution. First, let's learn what prototypes are. We'll also discuss the impact of prototype pollution and avoiding it in JavaScript. JavaScript is prototype-based and somewhat typed. We have the type of operator to check variable types. Objects in JavaScript have prototypes, and when a method or property is not found on an object, it's looked up on the prototype. We'll recursively check the prototype chain until we find the method or property. If it's nowhere in the prototype chain, it's undefined.

But let's talk about prototype pollution. So first of all, before we pollute prototypes, let's learn what prototypes are. Even if that's a JavaScript conference, it doesn't hurt to go back. Also, you can notice there is no design in my slides whatsoever because designing these slides consisted of removing the Datadog logo and color scheme late yesterday in the plane.

Then we will talk about the impact of prototype pollution and avoiding prototype pollution in JavaScript. And this talk has been designed for twenty five minutes and I only had twenty of them. JavaScript is prototype based and somewhat typed. OK, what do I mean? Let's start with the type part because it's probably the most trolly. Well, you've got the type of operator and you can check the types of variables. So types of true is a boolean, type of null is an object that's called the billion dollar mystic, but that's not the topic of this talk. Type of 10 is number type of 10 n is bigint and so on and so on. We even have the undefined type for stuff that are undefined. JavaScript is so great. And pretty much everything else is an object. So objects are objects. String objects are objects. Regex are objects, null is an object and objects are objects. Obviously again and objects have methods like if you create the object Foo1 and call hasOnProperty on it, it we returned true. And if you create the object Foo1 and check hasOnProperty, hasOnProperty, it will return false because the method hasOnProperty does not belong to the Foo object.

So where does it belong? Let's use the best tool in history of programming, the debugger. And we can check our object and see that the method hasOnProperty exists on something that's called a prototype and the prototype can be accessed from the object directly. So what are prototypes? In JavaScript, objects have prototypes. When a method or property is not found on an object, it's looked up on the prototype. But prototypes are objects. So if a method or property is not found on a prototype, we check it on its prototype. But the prototypes are objects. So if a method or property is not found on the prototype, we can check in the prototype. And that's what we call the prototype chain. It means that we will recursively check the whole prototype chain until we get to null, to find a method or a property. And if it's nowhere in the prototype chain, it's undefined and undefined is not a function.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
Do you have a large product built by many teams? Are you struggling to release often? Did your frontend turn into a massive unmaintainable monolith? If, like me, you’ve answered yes to any of those questions, this talk is for you! I’ll show you exactly how you can build a micro frontend architecture with Remix to solve those challenges.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
Remix is a web framework that gives you the simple mental model of a Multi-Page App (MPA) but the power and capabilities of a Single-Page App (SPA). One of the big challenges of SPAs is network management resulting in a great deal of indirection and buggy code. This is especially noticeable in application state which Remix completely eliminates, but it's also an issue in individual components that communicate with a single-purpose backend endpoint (like a combobox search for example).
In this talk, Kent will demonstrate how Remix enables you to build complex UI components that are connected to a backend in the simplest and most powerful way you've ever seen. Leaving you time to chill with your family or whatever else you do for fun.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
JavaScript in the browser runs many times faster than it did two decades ago. And that happened because the browser vendors spent that time working on intensive performance optimizations in their JavaScript engines.Because of this optimization work, JavaScript is now running in many places besides the browser. But there are still some environments where the JS engines can’t apply those optimizations in the right way to make things fast.We’re working to solve this, beginning a whole new wave of JavaScript optimization work. We’re improving JavaScript performance for entirely different environments, where different rules apply. And this is possible because of WebAssembly. In this talk, I'll explain how this all works and what's coming next.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
Webpack in 5 Years?
JSNation 2022JSNation 2022
26 min
Webpack in 5 Years?
Top Content
What can we learn from the last 10 years for the next 5 years? Is there a future for Webpack? What do we need to do now?
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
You can check the slides for James' talk here.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.
Build a powerful DataGrid in few hours with Ag Grid
React Summit US 2023React Summit US 2023
96 min
Build a powerful DataGrid in few hours with Ag Grid
WorkshopFree
Mike Ryan
Mike Ryan
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
We all know that rolling our own grid solution is not easy, and let's be honest, is not something that we should be working on. We are focused on building a product and driving forward innovation. In this workshop, you'll see just how easy it is to get started with AG Grid.
Prerequisites: Basic React and JavaScript
Workshop level: Beginner
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Build a Powerful Datagrid With AG Grid
React Summit 2024React Summit 2024
168 min
Build a Powerful Datagrid With AG Grid
WorkshopFree
Brian Love
Brian Love
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
JavaScript-based full-text search with Orama everywhere
Node Congress 2023Node Congress 2023
49 min
JavaScript-based full-text search with Orama everywhere
Workshop
Michele Riva
Michele Riva
In this workshop, we will see how to adopt Orama, a powerful full-text search engine written entirely in JavaScript, to make search available wherever JavaScript runs. We will learn when, how, and why deploying it on a serverless function could be a great idea, and when it would be better to keep it directly on the browser. Forget APIs, complex configurations, etc: Orama will make it easy to integrate search on projects of any scale.