Giter VIP home page Giter VIP logo

nodejs-goof's Introduction

Goof - Snyk's vulnerable demo app

Known Vulnerabilities

A vulnerable Node.js demo application, based on the Dreamers Lab tutorial.

Features

This vulnerable app includes the following capabilities to experiment with:

Running

mongod &

git clone https://github.com/snyk-labs/nodejs-goof
npm install
npm start

This will run Goof locally, using a local mongo on the default port and listening on port 3001 (http://localhost:3001)

Note: You have to use an old version of MongoDB version due to some of these old libraries' database server APIs. MongoDB 3 is known to work ok.

You can also run the MongoDB server individually via Docker, such as:

docker run --rm -p 27017:27017 mongo:3

Running with docker-compose

docker-compose up --build
docker-compose down

Heroku usage

Goof requires attaching a MongoLab service to be deployed as a Heroku app. That sets up the MONGOLAB_URI env var so everything after should just work.

CloudFoundry usage

Goof requires attaching a MongoLab service and naming it "goof-mongo" to be deployed on CloudFoundry. The code explicitly looks for credentials to that service.

Cleanup

To bulk delete the current list of TODO items from the DB run:

npm run cleanup

Exploiting the vulnerabilities

This app uses npm dependencies holding known vulnerabilities, as well as insecure code that introduces code-level vulnerabilities.

The exploits/ directory includes a series of steps to demonstrate each one.

Vulnerabilities in open source dependencies

Here are the exploitable vulnerable packages:

Vulnerabilities in code

  • Open Redirect
  • NoSQL Injection
  • Code Injection
  • Command execution
  • Cross-site Scripting (XSS)
  • Information exposure via Hardcoded values in code
  • Security misconfiguration exposes server information
  • Insecure protocol (HTTP) communication

Code injection

The page at /account_details is rendered as an Handlebars view.

The same view is used for both the GET request which shows the account details, as well as the form itself for a POST request which updates the account details. A so-called Server-side Rendering.

The form is completely functional. The way it works is, it receives the profile information from the req.body and passes it, as-is to the template. This however means, that the attacker is able to control a variable that flows directly from the request into the view template library.

You'd think that what's the worst that can happen because we use a validation to confirm the expected input, however the validation doesn't take into account a new field that can be added to the object, such as layout, which when passed to a template language, could lead to Local File Inclusion (Path Traversal) vulnerabilities. Here is a proof-of-concept showing it:

curl -X 'POST' --cookie c.txt --cookie-jar c.txt -H 'Content-Type: application/json' --data-binary '{"username": "[email protected]", "password": "SuperSecretPassword"}' 'http://localhost:3001/login'
curl -X 'POST' --cookie c.txt --cookie-jar c.txt -H 'Content-Type: application/json' --data-binary '{"email": "[email protected]", "firstname": "admin", "lastname": "admin", "country": "IL", "phone": "+972551234123",  "layout": "./../package.json"}' 'http://localhost:3001/account_details'

Actually, there's even another vulnerability in this code. The validator library that we use has several known regular expression denial of service vulnerabilities. One of them, is associated with the email regex, which if validated with the {allow_display_name: true} option then we can trigger a denial of service for this route:

curl -X 'POST' -H 'Content-Type: application/json' --data-binary "{\"email\": \"`seq -s "" -f "<" 100000`\"}" 'http://localhost:3001/account_details'

The validator.rtrim() sanitizer is also vulnerable, and we can use this to create a similar denial of service attack:

curl -X 'POST' -H 'Content-Type: application/json' --data-binary "{\"email\": \"[email protected]\", \"country\": \"nop\", \"phone\": \"0501234123\", \"lastname\": \"nop\", \"firstname\": \"`node -e 'console.log(" ".repeat(100000) + "!")'`\"}" 'http://localhost:3001/account_details'

NoSQL injection

A POST request to /login will allow for authentication and signing-in to the system as an administrator user. It works by exposing loginHandler as a controller in routes/index.js and uses a MongoDB database and the User.find() query to look up the user's details (email as a username and password). One issue is that it indeed stores passwords in plaintext and not hashing them. However, there are other issues in play here.

We can send a request with an incorrect password to see that we get a failed attempt

echo '{"username":"[email protected]", "password":"WrongPassword"}' | http --json $GOOF_HOST/login -v

And another request, as denoted with the following JSON request to sign-in as the admin user works as expected:

echo '{"username":"[email protected]", "password":"SuperSecretPassword"}' | http --json $GOOF_HOST/login -v

However, what if the password wasn't a string? what if it was an object? Why would an object be harmful or even considered an issue? Consider the following request:

echo '{"username": "[email protected]", "password": {"$gt": ""}}' | http --json $GOOF_HOST/login -v

We know the username, and we pass on what seems to be an object of some sort. That object structure is passed as-is to the password property and has a specific meaning to MongoDB - it uses the $gt operation which stands for greater than. So, we in essence tell MongoDB to match that username with any record that has a password that is greater than empty string which is bound to hit a record. This introduces the NoSQL Injection vector.

Open redirect

The /admin view introduces a redirectPage query path, as follows in the admin view:

<input type="hidden" name="redirectPage" value="<%- redirectPage %>" />

One fault here is that the redirectPage is rendered as raw HTML and not properly escaped, because it uses <%- > instead of <%= >. That itself, introduces a Cross-site Scripting (XSS) vulnerability via:

http://localhost:3001/login?redirectPage="><script>alert(1)</script>

To exploit the open redirect, simply provide a URL such as redirectPage=https://google.com which exploits the fact that the code doesn't enforce local URLs in index.js:72.

Hardcoded values - session information

The application initializes a cookie-based session on app.js:40 as follows:

app.use(session({
  secret: 'keyboard cat',
  name: 'connect.sid',
  cookie: { secure: true }
}))

As you can see, the session secret used to sign the session is a hardcoded sensitive information inside the code.

First attempt to fix it, can be to move it out to a config file such as:

module.exports = {
    cookieSecret: `keyboard cat`
}

And then require the configuration file and use it to initialize the session. However, that still maintains the secret information inside another file, and Snyk Code will warn you about it.

Another case we can discuss here in session management, is that the cookie setting is initialized with secure: true which means it will only be transmitted over HTTPS connections. However, there's no httpOnly flag set to true, which means that the default false value of it makes the cookie accessible via JavaScript. Snyk Code highlights this potential security misconfiguration so we can fix it. We can note that Snyk Code shows this as a quality information, and not as a security error.

Snyk Code will also find hardcoded secrets in source code that isn't part of the application logic, such as tests/ or examples/ folders. We have a case of that in this application with the tests/authentication.component.spec.js file. In the finding, Snyk Code will tag it as InTest, Tests, or Mock, which help us easily triage it and indeed ignore this finding as it isn't actually a case of information exposure.

Docker Image Scanning

The Dockerfile makes use of a base image (node:6-stretch) that is known to have system libraries with vulnerabilities.

To scan the image for vulnerabilities, run:

snyk test --docker node:6-stretch --file=Dockerfile

To monitor this image and receive alerts with Snyk:

snyk monitor --docker node:6-stretch

Runtime Alerts

Snyk provides the ability to monitor application runtime behavior and detect an invocation of a function is known to be vulnerable and used within open source dependencies that the application makes use of.

The agent is installed and initialized in app.js.

For the agent to report back to your snyk account on the vulnerabilities it detected it needs to know which project on Snyk to associate with the monitoring. Due to that, we need to provide it with the project id through an environment variable SNYK_PROJECT_ID

To run the Node.js app with runtime monitoring:

SNYK_PROJECT_ID=<PROJECT_ID> npm start

** The app will continue to work normally even if it's not provided a project id

Fixing the issues

To find these flaws in this application (and in your own apps), run:

npm install -g snyk
snyk wizard

In this application, the default snyk wizard answers will fix all the issues. When the wizard is done, restart the application and run the exploits again to confirm they are fixed.

nodejs-goof's People

Contributors

ah7 avatar artursnyk avatar bmvermeer avatar clarkio avatar developersteve avatar erichusband avatar ericsmalling avatar fauxfaux avatar grnd avatar guypod avatar karniwl avatar kirill89 avatar lirantal avatar michaelaquilina avatar sebsnyk avatar sonyamoisset avatar tomersnyk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nodejs-goof's Issues

Build fails with SyntaxError: Unexpected token

goof | {"app":{},"services":{},"isLocal":true,"name":"goof","port":6001,"bind":"localhost","urls":["http://localhost:6001"],"url":"http://localhost:6001"}
goof | Using Mongo URI mongodb://goof-mongo/express-todo
goof | /usr/src/goof/node_modules/cli-highlight/node_modules/chalk/source/index.js:103
goof | ...styles,
goof | ^^^
goof |
goof | SyntaxError: Unexpected token ...
goof | at createScript (vm.js:56:10)
goof | at Object.runInThisContext (vm.js:97:10)
goof | at Module._compile (module.js:549:28)
goof | at Object.Module._extensions..js (module.js:586:10)
goof | at Module.load (module.js:494:32)
goof | at tryModuleLoad (module.js:453:12)
goof | at Function.Module._load (module.js:445:3)
goof | at Module.require (module.js:504:17)
goof | at require (internal/module.js:20:19)
goof | at Object. (/usr/src/goof/node_modules/cli-highlight/dist/theme.js:10:28)
goof | at Module._compile (module.js:577:32)
goof | at Object.Module._extensions..js (module.js:586:10)
goof | at Module.load (module.js:494:32)
goof | at tryModuleLoad (module.js:453:12)
goof | at Function.Module._load (module.js:445:3)
goof | at Module.require (module.js:504:17)

Broken dependencies

I found that the project has incompatible dependencies. It requires node "6.14.1" and dependencies require node ">=8".

I guess it because the project is old and needs to be updated. You could lock dependencies, so they don't require a higher node version.

Intel based MAC OS 12.2.1 and Node v19.1 - docker build fails

It bombs right here:

goof-mongo  | {"t":{"$date":"2022-11-30T01:11:58.349+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
goof        | 
goof        | > [email protected] start /usr/src/goof
goof        | > NODE_OPTIONS=--openssl-legacy-provider node app.js
goof        | 
goof        | node: --openssl-legacy-provider is not allowed in NODE_OPTIONS
goof        | npm ERR! code ELIFECYCLE
goof        | npm ERR! errno 9
goof        | npm ERR! [email protected] start: `NODE_OPTIONS=--openssl-legacy-provider node app.js`
goof        | npm ERR! Exit status 9
goof        | npm ERR! 
goof        | npm ERR! Failed at the [email protected] start script.
goof        | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
goof        | 
goof        | npm ERR! A complete log of this run can be found in:
goof        | npm ERR!     /root/.npm/_logs/2022-11-30T01_11_58_558Z-debug.log
goof exited with code 9

License is missing

The project/repo does not currently seem to have a license - is this intentional?

Docker deployment doesn't work

I faced two main issues while trying to run this project locally using docker compose during a workshop. The two issues where:

  • goof would fail to start complaining about not being able to connect to mysql at localhost
  • I wasn't able to create a new todo, it would fail with the following error: TypeError: Busboy is not a constructor
    • I have modified the Dockerfile not to run npm update anymore.

I have opened this #1254 to fix both.

[BUG]: goof docker build fails.

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

goof docker build fails and the container is exited.

santa@roli:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7a8788d5e4d nodejs-goof_goof "npm start" 7 minutes ago Exited (9) 7 minutes ago goof
86ffaea201b0 mysql:5 "docker-entrypoint.sā€¦" 7 minutes ago Up 7 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp goof-mysql
b6ca72bb5cfe mongo "docker-entrypoint.sā€¦" 7 minutes ago Up 7 minutes 0.0.0.0:27017->27017/tcp goof-mongo

santa@roli:~$ docker logs goof

[email protected] start /usr/src/goof
NODE_OPTIONS=--openssl-legacy-provider node app.js

node: --openssl-legacy-provider is not allowed in NODE_OPTIONS
npm ERR! code ELIFECYCLE
npm ERR! errno 9
npm ERR! [email protected] start: NODE_OPTIONS=--openssl-legacy-provider node app.js
npm ERR! Exit status 9
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2023-01-03T06_00_22_312Z-debug.log
santa@roli:~$ docker logs /root/.npm/_logs/2023-01-03T06_00_22_312Z-debug.log
Error: No such container: /root/.npm/_logs/2023-01-03T06_00_22_312Z-debug.log

Steps To Reproduce

git clone https://github.com/snyk-labs/nodejs-goof.git
cd nodejs-goof
docker-compose up --build

Additional Information

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.