Skip to content
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
10 changes: 9 additions & 1 deletion lib/modules/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ exports = module.exports = function(app, config) {
var _ = require('lodash');
var passport = require('passport');
var ldapauth = require('passport-ldapauth');
var rateLimit = require('express-rate-limit');

// Rate limiter for login attempts
const loginLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: "Too many login attempts from this IP, please try again after 15 minutes"
});

// LDAP integration
passport.use(new ldapauth.Strategy({
Expand Down Expand Up @@ -61,7 +69,7 @@ exports = module.exports = function(app, config) {
res.render('login', { flash: req.flash() });
});

app.post('/login', passport.authenticate('ldapauth', {
app.post('/login', loginLimiter, passport.authenticate('ldapauth', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
Expand Down
12 changes: 9 additions & 3 deletions lib/modules/pcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ function readRawBytes(size, transit) {
return buffer;
}


exports = module.exports = function(app, config) {
var _ = require('lodash');
var fs = require('fs');
var spawn = require('child_process').spawn;
var querystring = require('querystring');
var XmlStream = require('xml-stream');
var rateLimit = require('express-rate-limit');

// Rate limiting middleware
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});

// Mock pcap service for use in development
if (config.pcap.mock) {
app.get('/sample/pcap/:command', function(req, res) {
app.get('/sample/pcap/:command', limiter, function(req, res) {
res.sendFile('/vagrant/seed/opensoc.pcap');
});
}

app.get('/pcap/:command', function(req, res) {
app.get('/pcap/:command', limiter, function(req, res) {
if (config.auth && (!req.user || !req.user.permissions.pcap)) {
res.send(403, 'Forbidden!');
return;
Expand Down
23 changes: 18 additions & 5 deletions lib/opensoc-ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var _ = require('lodash');
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');

var express = require('express');
Expand All @@ -10,6 +12,8 @@ var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cookieSession = require('cookie-session');
var csurf = require('csurf');
var helmet = require('helmet');

var passport = require('passport');
var ldapauth = require('passport-ldapauth');
Expand All @@ -24,13 +28,19 @@ var app = express();
app.set('view engine', 'jade');
app.set('views', path.join(__dirname, 'views/'));

// Use Helmet to secure Express apps by setting various HTTP headers
app.use(helmet());

// Cookie middleware
app.use(connect.logger('dev'));
app.use(flash());
app.use(cookieParser());
app.use(cookieSession({
secret: config.secret,
cookie: {maxAge: 1 * 24 * 60 * 60 * 1000} // 1-day sessions
cookie: {
maxAge: 1 * 24 * 60 * 60 * 1000, // 1-day sessions
secure: true // Ensure cookies are only sent over HTTPS
}
}));

if (config.auth) {
Expand All @@ -42,7 +52,7 @@ app.use("/__es", esProxy(config));

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use(csurf()); // Add CSRF protection

// Setup routes
if (config.auth) {
Expand All @@ -51,7 +61,6 @@ if (config.auth) {

pcap(app, config);


app.get('/config.js', function (req, res) {
if (config.auth && !req.user) {
res.send(403, 'Forbidden!');
Expand All @@ -67,8 +76,12 @@ app.use(connect.static(path.join(__dirname, config.static)));
// Start server
if (process.env.NODE_ENV != 'TEST') {
console.log('Starting server on port', config.port, '...');
var server = http.createServer(app);
var serverOptions = {
key: fs.readFileSync('path/to/your/private-key.pem'),
cert: fs.readFileSync('path/to/your/certificate.pem')
};
var server = https.createServer(serverOptions, app);
server.listen(config.port, config.host);
}

exports.app = app;
exports.app = app;
2 changes: 1 addition & 1 deletion test/session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (!process.env.IN_TRAVIS) {
it('logs in', function (done) {
session.
post('/login').
send({ email: 'joesmith@opensoc.dev', password: 'opensoc' }).
send({ email: process.env.TEST_EMAIL, password: process.env.TEST_PASSWORD }).
end(function (err, res) {
// redirects to home
assert.equal(res.header['location'], '/');
Expand Down