I Run Code From the Internet!

Rate this content
Bookmark

Is it wise to run code from strangers? Well, we do it all the time and there's no backing out of it. Let's take a look at how a JavaScript project could get hacked and then defend itself from supply chain attacks. Limit access to globals for each package? Sure. Control if a package can access network or file system? Yup, that too. And no more install scripts or prototype pollution.

This talk has been presented at React Summit 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Nauchter's website, nauchter.pl, offers extensive resources on JavaScript, security, and other technical topics.

NPM packages can pose risks as they are essentially unsanitized inputs from the internet that could contain malicious code aimed at harming applications or users.

SocketDev is a tool that automates the detection of suspicious qualities in NPM packages, providing early warnings about potential security issues shortly after the packages are published.

Lavamote assumes that any dependency might be malicious and uses policies to control application behavior, effectively isolating dependencies and preventing unauthorized actions.

Hardened JavaScript enhances security by isolating each dependency in a separate realm or context, preventing prototype pollution and other common JavaScript vulnerabilities.

The Lavamote plugin for Webpack, codenamed Scoretrap, is still in development and aims to provide comprehensive security features compatible with Webpack's extensive features.

Lavamote can be installed and configured in projects to enforce security policies, and Nauchter offers assistance in rolling out Lavamote tooling for serious projects.

Lavamote may face compatibility issues with a minor portion of the NPM ecosystem, especially under lockdown conditions that restrict modifications to Object prototypes.

Zbyszek Tenerowicz
Zbyszek Tenerowicz
12 min
06 Jun, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Today's Talk covers the risks of running someone else's code, the use of supply chain security tools, and the automation of package analysis with SocketDev. It also introduces the concept of Hardened JavaScript and Lockdown for creating secure environments. The mention of LavaMode and its bundler, as well as the application example using Cookiemonster, showcases the practical applications of these tools. The speaker also offers assistance in implementing Lavamote for projects.
Available in Español: ¡Ejecuto código de Internet!

1. Running Someone Else's Code and Ensuring Security

Short description:

Today, we're going to look at running someone else's code and the potential risks associated with it. NPM packages, although widely used, can pose a threat as they are unsanitized input from the internet. Supply chain security tools can help identify malicious packages, but they are not foolproof. SocketDev automates the process of finding suspicious qualities in packages, providing a better feedback loop. At Lavamote, we take a proactive approach by assuming that one of the dependencies is already malicious. LavaMode allows you to generate a policy for your application and run it based on that policy, isolating each dependency using Hardened JavaScript.

Hello everyone! I'm Nauchter, you can find me online if you type Nauchter anyway, nauchter.pl Nauchter.pl is my website, it has a bunch of other material on JavaScript, security and other things.

And today, we're going to look at running someone else's code. Speaking of that, if I gave you a string of text and asked you to put it in your application and run it, would you do that? Would you put my code in production without looking at it much? Well, I guess the answer is going to be no. But would it help if I put it in a tar.gz file? Is it more appealing that way? Somehow it is, because that's what NPM packages are. And they're great, and everyone's using them, but they're actually unsanitized input from the internet, that we put in our applications and run. Okay? So yes, that's what we're doing, we're doing that all the time. I'm installing NPM packages all the time. What if some of the packages in there aren't great? And by that, I don't mean lousy packages. I published some lousy packages, and nothing bad happened. But I mean malicious packages, dangerous packages, that want to hurt your application, your users. What do we do about that? Well, there's tools for supply chain security, right? So, a tool might say, hey, some researchers found out that this package is not great, and they reported it. So, that package that you shipped to production a few weeks ago, now we know that it's not a good one. Please do something about it. Is that a great situation to be in? Not necessarily. Okay, there's SocketDev. You can use that and get a much better feedback loop, because SocketDev is automating finding suspicious qualities about packages. So, SocketDev, within a few hours of the package being published, is going to be able to tell you that there's possibly something wrong with it. But then you need to investigate it. At Lavamote we decided to be proactive instead of reactive, so we're running with the assumption that one of the dependencies in the dependency graph is already malicious when the application runs. So, how does it work? I do have a demo, if that was a longer talk, you can check that demo out, but let's look at the situation where you install a build tool for your project, and then someone puts this in your build tool's code somewhere. So it takes your GitHub token, while running in CI, and sends it somewhere. Is that great? Probably not. What LavaMode is doing, LavaMode is allowing you to generate a policy for your entire application, in this case a build process, and then run it based on that policy. The policy is automatically generated, it detects almost everything that is needed for the app to run, then you proceed to tweak the policy to the sideline, which things should be ok to allow and which shouldn't. If our analysis fails to find something, it doesn't show up in the policy, and is by default not allowed. In this case, the policy is generated with the HTTPS request and Process.env being there. What we can do is either remove those, or change them to false, which means they are not allowed, and running your build with lavamode is going to cause one of these errors to show up. Either this evil dependency requesting HTTPS package, which is not available, or failing to read env from process because process is undefined. How is that even possible? Well, it's thanks to Hardened JavaScript. We're using Hardened JavaScript behind the scenes to isolate each dependency within the same process.

2. Hardened JavaScript and Lockdown

Short description:

Hardened JavaScript provides isolation by creating a compartment where code can run with controlled globals. Lockdown ensures a secure environment by preventing malicious manipulation of global prototypes. An early proposal for compartments is being considered by TC39, with input from experienced developers. Implementation is underway, offering promising results.

There's no threads, workers, or iframes involved. This is all happening in one realm or context, and we can still get good isolation. Hardened JavaScript is the compartment constructor which creates a box where you can run code in and decide what globals are going to be available and hook into it importing other things.

And then there's lockdown which makes sure that the entire environment around it, like global prototypes of object, array, and function, is put in a state where it's impossible to maliciously fiddle with them. So, no prototype pollution anymore.

And the good news is, this is becoming part of language. Well, not yet! There's an early proposal for introducing compartments in TC39, and some of the people working on it are the people responsible for giving us used strict object frees or promises. So, I'm hoping this gets in. It's going to take a while, but we already have an implementation that works.

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

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
The State of Passwordless Auth on the Web
JSNation 2023JSNation 2023
30 min
The State of Passwordless Auth on the Web
Passwords are terrible and easily hacked, with most people not using password managers. The credential management API and autocomplete attribute can improve user experience and security. Two-factor authentication enhances security but regresses user experience. Passkeys offer a seamless and secure login experience, but browser support may be limited. Recommendations include detecting Passkey support and offering fallbacks to passwords and two-factor authentication.
5 Ways You Could Have Hacked Node.js
JSNation 2023JSNation 2023
22 min
5 Ways You Could Have Hacked Node.js
Top Content
The Node.js security team is responsible for addressing vulnerabilities and receives reports through HackerOne. The Talk discusses various hacking techniques, including DLL injections and DNS rebinding attacks. It also highlights Node.js security vulnerabilities such as HTTP request smuggling and certification validation. The importance of using HTTP proxy tunneling and the experimental permission model in Node.js 20 is emphasized. NearForm, a company specializing in Node.js, offers services for scaling and improving security.
Content Security Policy with Next.js: Leveling Up your Website's Security
React Summit US 2023React Summit US 2023
9 min
Content Security Policy with Next.js: Leveling Up your Website's Security
Top Content
Watch video: Content Security Policy with Next.js: Leveling Up your Website's Security
Lucas Estevão, a Principal UI Engineer and Technical Manager at Avenue Code, discusses how to implement Content Security Policy (CSP) with Next.js to enhance website security. He explains that CSP is a security layer that protects against cross-site scripting and data injection attacks by restricting browser functionality. The talk covers adding CSP to an XJS application using meta tags or headers, and demonstrates the use of the 'nonce' attribute for allowing inline scripts securely. Estevão also highlights the importance of using content security reports to identify and improve application security.
Let Me Show You How React Applications Get Hacked in the Real-World
React Advanced 2021React Advanced 2021
22 min
Let Me Show You How React Applications Get Hacked in the Real-World
Top Content
React's default security against XSS vulnerabilities, exploring and fixing XSS vulnerabilities in React, exploring control characters and security issues, exploring an alternative solution for JSON parsing, and exploring JSON input and third-party dependencies.
How React Applications Get Hacked in the Real-World
React Summit 2022React Summit 2022
7 min
How React Applications Get Hacked in the Real-World
How to hack a RealWorld live React application in seven minutes. Tips, best practices, and pitfalls when writing React code. XSS and cross-site scripting in React. React's secure by default, but not always. The first thing to discover: adding a link to a React application. React code vulnerability: cross-site scripting with Twitter link. React doesn't sanitize or output H ref attributes. Fix attempts: detect JavaScript, use dummy hashtag, transition to lowercase. Control corrector exploit. Best practices: avoid denialist approach, sanitize user inputs. React's lack of sanitization and output encoding for user inputs. Exploring XSS vulnerabilities and the need to pretty print JSON. The React JSON pretty package and its potential XSS risks. The importance of context encoding and secure coding practices.

Workshops on related topic

0 to Auth in an hour with ReactJS
React Summit 2023React Summit 2023
56 min
0 to Auth in an hour with ReactJS
WorkshopFree
Kevin Gao
Kevin Gao
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool. There are multiple alternatives that are much better than passwords to identify and authenticate your users - including SSO, SAML, OAuth, Magic Links, One-Time Passwords, and Authenticator Apps.
While addressing security aspects and avoiding common pitfalls, 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 securely for subsequent client requests, validating / refreshing sessions- Basic Authorization - extracting and validating claims from the session token JWT and handling authorization in backend flows
At the end of the workshop, we will also touch other approaches of authentication implementation with Descope - using frontend or backend SDKs.
OWASP Top Ten Security Vulnerabilities in Node.js
JSNation 2024JSNation 2024
97 min
OWASP Top Ten Security Vulnerabilities in Node.js
Workshop
Marco Ippolito
Marco Ippolito
In this workshop, we'll cover the top 10 most common vulnerabilities and critical security risks identified by OWASP, which is a trusted authority in Web Application Security.During the workshop, you will learn how to prevent these vulnerabilities and develop the ability to recognize them in web applications.The workshop includes 10 code challenges that represent each of the OWASP's most common vulnerabilities. There will be given hints to help solve the vulnerabilities and pass the tests.The trainer will also provide detailed explanations, slides, and real-life examples in Node.js to help understand the problems better. Additionally, you'll gain insights from a Node.js Maintainer who will share how they manage security within a large project.It's suitable for Node.js Developers of all skill levels, from beginners to experts, it requires a general knowledge of web application and JavaScript.
Table of contents:- Broken Access Control- Cryptographic Failures- Injection- Insecure Design- Security Misconfiguration- Vulnerable and Outdated Components- Identification and Authentication Failures- Software and Data Integrity Failures- Security Logging and Monitoring Failures- Server-Side Request Forgery
How to Build Front-End Access Control with NFTs
JSNation 2024JSNation 2024
88 min
How to Build Front-End Access Control with NFTs
WorkshopFree
Solange Gueiros
Solange Gueiros
Understand the fundamentals of NFT technology and its application in bolstering web security. Through practical demonstrations and hands-on exercises, attendees will learn how to seamlessly integrate NFT-based access control mechanisms into their front-end development projects.
Finding, Hacking and fixing your NodeJS Vulnerabilities with Snyk
JSNation 2022JSNation 2022
99 min
Finding, Hacking and fixing your NodeJS Vulnerabilities with Snyk
WorkshopFree
Matthew Salmon
Matthew Salmon
npm and security, how much do you know about your dependencies?Hack-along, live hacking of a vulnerable Node app https://github.com/snyk-labs/nodejs-goof, Vulnerabilities from both Open source and written code. Encouraged to download the application and hack along with us.Fixing the issues and an introduction to Snyk with a demo.Open questions.
Bring Code Quality and Security to your CI/CD pipeline
DevOps.js Conf 2022DevOps.js Conf 2022
76 min
Bring Code Quality and Security to your CI/CD pipeline
WorkshopFree
Elena Vilchik
Elena Vilchik
In this workshop we will go through all the aspects and stages when integrating your project into Code Quality and Security Ecosystem. We will take a simple web-application as a starting point and create a CI pipeline triggering code quality monitoring for it. We will do a full development cycle starting from coding in the IDE and opening a Pull Request and I will show you how you can control the quality at those stages. At the end of the workshop you will be ready to enable such integration for your own projects.
Passwordless Auth to Servers: hands on with ASA
DevOps.js Conf 2022DevOps.js Conf 2022
32 min
Passwordless Auth to Servers: hands on with ASA
WorkshopFree
E. Dunham
E. Dunham
These days, you don't need a separate password for every website you log into. Yet thanks to tech debt and tradition, many DevOps professionals are still wrangling a host of SSH keys to access the servers where we sometimes need to be. With modern OAuth, a single login and second factor to prove your identity are enough to securely get you into every service that you're authorized to access. What if SSHing into servers was that easy? In this workshop, we'll use Okta's Advanced Server Access tool (formerly ScaleFT) to experience one way that the dream of sending SSH keys the way of the password has been realized.
- we'll discuss how ASA works and when it's the right tool for the job- we'll walk through setting up a free trial Okta account to use ASA from, and configuring the ASA gateway and server on Linux servers- we'll then SSH into our hosts with the ASA clients without needing to supply an SSH key from our laptops- we'll review the audit logs of our SSH sessions to examine what commands were run