>在2016年,当黑客通过利用私人github存储库中的暴露凭据访问其aws s3服务器时,uber面临严重的安全漏洞。该服务器包含5700万用户和60万驱动程序的敏感数据。违规发生是由于不良的访问控制和凭证管理在其node.js应用程序中。如果像uber这样的技术巨头可能会发生这种情况,那么您的应用程序呢?如果您要建立初创公司或管理企业,该如何保护自己?
本文将探讨确保node.js applications的最佳实践。无论您是初学者还是您已经开发了多年,采用这些工具和策略都可以保护您的应用程序免受违规的影响。到最后,您将知道如何保护您的应用程序并避免损失数百万美元的错误。
漏洞和安全风险
> valivator.js来删除任何额外的空格,然后使用
// ... const validator = require('validator'); const joi = require('joi'); app.post('/submit', (req, res) => { // sanitize the email by trimming extra spaces const sanitizedemail = validator.trim(req.body.email); // validate the sanitized email const schema = joi.object({ email: joi.string().email().required().label("email") }); const { error } = schema.validate({ email: sanitizedemail }); if (error) return res.status(400).json({ message: error.details[0].message }); // ... });
>
>> oauth 2.0:使用> passport.js
的oauth 2.0协议实现安全身份验证。 passport提供了广泛的策略,包括通过像google这样值得信赖的提供商登录。首先,只需安装
在此示例中,我们正在使用
> passport-google-oauth20策略来验证用户:
14926428773
hash密码:
const bcrypt = require('bcrypt'); // ... const saltrounds = 10; const hashedpassword = await bcrypt.hash(password, saltrounds); // ...
和 secure。这些措施大大降低了攻击者劫持用户会议的风险。>
const session = require('cookie-session') // ... app.use( session({ name: 'custom-cookie-name', secret: 'your-secret-key', cookie: { httponly: true, secure: true, // use true in production with https maxage: 60 * 60 * 1000, // 1 hour }, }) ); // ...
express-rate-limit库来限制登录尝试在15分钟的窗口中的每个ip地址。
const ratelimit = require('express-rate-limit'); const limiter = ratelimit({ windowms: 15 * 60 * 1000, // 15 minutes max: 5, // limit each ip to 5 requests per window message: 'too many login attempts, please try again later.', }); app.use('/login', limiter);
文件,然后删除任何未使用的软件包以维护您的应用程序的安全性。
$ npm audit fix
。它提供了一个cli和github集成,可针对snyk的开源数据库扫描您的应用程序,以检测依赖关系中的任何已知漏洞。入门很简单,只需安装snyk,导航到您的项目目录,然后运行
snyk test
$ npm install -g snyk $ cd your-app $ snyk test
错误处理和记录
避免将内部错误暴露于用户。相反,使用 winston>或
bunyan
const winston = require('winston');
// configure winston logging
const logger = winston.createlogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.console(),
new winston.transports.file({ filename: 'logs/app.log' })
]
});
// middleware for error handling
app.use((err, req, res, next) => {
// log the error, without exposing sensitive details to the user
logger.error(`error occurred: ${err.message}`);
// send a generic error response to the user
res.status(500).send('something went wrong!');
next();
});
库,这是一个设置安全http标头的中间件。它易于实现,只需安装软件包,在项目中初始化它,然后将其设置为设置。
// ... const helmet = require('helmet'); const app = express(); app.use(helmet()); // ...
头盔有助于防止诸如插锁,跨站点脚本和其他常见漏洞之类的威胁。虽然添加http标头似乎是一小步,但它为潜在的攻击提供了有力的防御。通过添加此额外的安全层,您可以使您的应用程序更难利用。> 结论
如果您发现这篇文章有帮助,请不要在这里停止!查看我有关每个开发人员应该知道的 git命令的文章,我介绍了如何安全地撤消更改,探索您的项目历史记录,像专业人士一样,并保持分支机构清洁和井井有条。关注更多编码技巧和技巧,以提高您的技能。继续探索和愉快的编码!
以上就是您是在犯这些Nodejs安全错误吗?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号