Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If we're running in a mode that should bypass auth, set up a mock user
if (process.env.BYPASS_AUTH === "true") {
console.warn("Auth is being bypassed!");
app.use(setMockUser);
}
app.use(require("./routes"));
mongoose.connect(process.env.DB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
// Silence deprecation warnings
mongoose.set("useCreateIndex", true);
app.use(errors());
app.use(errorHandler);
module.exports = app;
// The error handler must be before any other error middleware
app.use(raven.ravenModule.middleware.express.errorHandler(raven.ravenMiddleware));
// log any joi validation errors
app.use((err, req, res, next) => {
if (err && err.isJoi) {
logger.error('validation.error', {
error: err.details.map(details => details.message).join(','),
path: req.path,
});
}
next(err);
});
// convert joi validation errors to a JSON response
app.use(celebrate.errors());
// server error!
app.use(serverErrorHandler);
return app;
}
// log any joi validation errors
app.use((err, req, res, next) => {
if (err && err.isJoi) {
logger.error('validation.error', {
error: err.details.map(details => details.message).join(','),
path: req.path,
});
// capture validation errors
raven.ravenModule.captureException(err);
}
next(err);
});
// convert joi validation errors to a JSON response
app.use(celebrate.errors());
// server error!
app.use(serverErrorHandler);
return app;
}
if (route.preProcess) {
routeHandlers.push(route.preProcess);
}
if (route.validate) {
routeHandlers.push(celebrate(route.validate, {
// silently drop any unknown fields within objects on the ground.
stripUnknown: { arrays: false, objects: true }
}));
}
routeHandlers.push(route.process);
app[route.method].apply(app, [route.path].concat(routeHandlers));
});
const defaultErrorHandler = celebrateErrors();
app.use((err, req, res, next) => {
if (err && isCelebrate(err)) {
logger.error('validation.failed', { err, method: req.method, path: req.url });
}
defaultErrorHandler(err, req, res, next);
// capture validation errors
raven.ravenModule.captureException(err);
});
};
};
setUpMiddleWare() {
app.use('/api/builder', new APIBuilder(this.state).setupRoutes());
app.use('/api/admin', new APIAdmin(this.state).setupRoutes());
app.set('json spaces', 2);
app.use(celebrate.errors());
app.use(whitelist(this.whiteListed));
app.use((err, req, res, next) => {
if (err.name == "WhitelistIpError") {
Logger.log(`[WEB]: Forbidden request: ${req.ip}`);
res.status(403).send('Forbidden');
}
});
}
}
const trackedScansRouter = require('../routes/tracked_scans')(envConfig);
app.use('/api/v1.0/', trackedScansRouter);
const utilitiesRouter = require('../routes/utilities')(envConfig);
app.use('/api/v1.0/', utilitiesRouter);
const virustotalRouter = require('../routes/virustotal')(envConfig);
app.use('/api/v1.0/', virustotalRouter);
const whoisRouter = require('../routes/whois_db')(envConfig);
app.use('/api/v1.0/', whoisRouter);
const zoneRouter = require('../routes/zones')(envConfig);
app.use('/api/v1.0/', zoneRouter);
app.use(errors());
};
app.use(bodyParser.json());
app.get('/healthcheck', (req, res) => {
try {
res.send({
uptime: Math.round(process.uptime()),
message: 'OK',
timestamp: Date.now(),
mongodb: mongo.isConnected()
});
} catch (e) {
res.status(503).end();
}
});
app.use(authorization);
app.use('/v1', routes);
app.use(errors());
app.use(logger);
if (NODE_ENV !== 'test') {
(async () => {
await mongo.connectWithRetry();
app.listen(PORT, () => {
winston.info(`Server listening on http://localhost:${PORT}`);
});
})();
}
module.exports = app;