Skip to content

Fix session and login vulnerabilities #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Environment files
.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ YES, indeed it is powered by `Node.js v6` on `Raspberry PI 3`.



## 2. Reuqirements
## 2. Requirements
### 2.1 Equipment
* [**Raspberry PI 3 Model B**](https://www.raspberrypi.org/products/raspberry-pi-3-model-b/) with the latest **Raspbian OS** installed
* **Node.js v6.0**
Expand Down
31 changes: 31 additions & 0 deletions create_env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs');
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const prompt = require('prompt');

prompt.start();


prompt.get({
properties: {
username: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Username must be only letters, spaces, or dashes',
required: true
},
password: {
hidden: true
}
}
}, function (err, result) {
if (err) console.error('Failed to create a .env file');

const password = bcrypt.hashSync(result.password, 10)

fs.writeFile(
'.env',
'COOKIE_SECRET=' + crypto.randomBytes(40).toString('hex') + '\n' +
'USERNAME=' + result.username + '\n' +
'PASSWORD=' + password + '\n'
);
});
7 changes: 5 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const express = require("express");
const path = require("path");
const session = require("express-session");
const FileStore = require('session-file-store')(session);

const dotenv = require('dotenv');
var crypto = require("crypto");

/**
* Setup express app
*/
const app = express();
const passport = require("./passport");
dotenv.config();

/**
* Parsers
Expand All @@ -19,7 +21,8 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: "aedf1829f7",
secret: (process.env.COOKIE_SECRET && process.env.COOKIE_SECRET !== '') ?
process.env.COOKIE_SECRET : crypto.randomBytes(40).toString('hex'),
resave: false,
saveUninitialized: true,
cookie: { secure: false, maxAge: 1000 * 60 * 60 * 24 * 100 },
Expand Down
11 changes: 10 additions & 1 deletion lib/passport.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const LocalStrategy = require("passport-local").Strategy;
const md5 = require("md5");
const bcrypt = require('bcrypt');
const passport = require("passport");
const dotenv = require('dotenv');
dotenv.config();

passport.use("local", new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
},
(username, password, done) => {
if (username === "admin" && md5(password) === "a99e2bc0efaa6c17888f2946aedc6be8")
const serverUsername = (process.env.USERNAME && process.env.USERNAME !== '') ?
process.env.USERNAME : 'admin';
const serverPassword = (process.env.PASSWORD && process.env.PASSWORD !== '') ?
process.env.PASSWORD : '$2y$10$BiQ8hbUWvjnu4Yi59i4e/u0LKMcoOzAn/5oeZjh5JrzekAeVn4oX.';

if (!process.env.PASSWORD) password = md5(password)
if (username === serverUsername && bcrypt.compareSync(password, serverPassword))
{
return done(null, {
id: "admin"
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
},
"homepage": "https://github.com/MagicCube/rpi-man#readme",
"dependencies": {
"bcrypt": "3.0.6",
"body-parser": "^1.15.2",
"cookie-parser": "^1.4.3",
"dotenv": "8.0.0",
"express": "^4.14.0",
"express-session": "^1.14.0",
"md5": "^2.1.0",
"os-utils": "0.0.14",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"pty.js": "^0.3.1",
"prompt": "1.0.0",
"session-file-store": "^0.2.0",
"socket.io": "^1.4.8"
},
Expand Down
2 changes: 2 additions & 0 deletions rpi-man-get
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ install()
{
echo "Installing rpi-man..."
install_dependencies
echo "Generating .env file"
node create_env.js
echo "Make rpi-man-server as global command..."
ln -s $path/bin/rpi-man-server $bin_path
echo "Make rpi-man-server run at startup..."
Expand Down