面临的安全风险
- 密码安全风险:用户密码如果以明文形式存储,一旦数据库泄露,用户密码将直接暴露,导致严重安全问题。
- CSRF(跨站请求伪造)风险:攻击者利用用户已登录的状态,在用户不知情的情况下,以用户的名义发送恶意请求,执行非用户本意的操作,如转账、修改密码等。
- XSS(跨站脚本攻击)风险:如果应用程序对用户输入过滤不严格,攻击者可以注入恶意脚本,当其他用户访问页面时,这些脚本会在受害者浏览器中执行,可能窃取用户会话信息等。
- 中间人攻击风险:在用户与服务器通信过程中,攻击者可能拦截、篡改数据,比如修改认证信息等。
- 会话劫持风险:攻击者获取用户的会话标识(如 session ID),冒用用户身份进行操作。
防范措施
- 密码加密
- 使用 bcrypt、argon2 等强加密算法对用户密码进行哈希处理。例如在 Node.js 中使用 bcrypt 库:
const bcrypt = require('bcrypt');
const password = "userPassword";
bcrypt.hash(password, 10).then((hash) => {
// 存储 hash 到数据库
});
- 在用户登录验证时,使用 `bcrypt.compare` 方法对比用户输入密码和数据库中存储的哈希值:
bcrypt.compare("inputPassword", storedHash).then((result) => {
if (result) {
// 密码匹配,允许登录
} else {
// 密码不匹配,拒绝登录
}
});
- 防范 CSRF
- 使用 csurf 中间件。首先安装
csurf
库:npm install csurf
。
- 在 Express 应用中使用:
const express = require('express');
const csrf = require('csurf');
const app = express();
const csrfProtection = csrf({ cookie: true });
app.use(csrfProtection);
app.get('/form', csrfProtection, (req, res) => {
res.render('form', { csrfToken: req.csrfToken() });
});
app.post('/submit', csrfProtection, (req, res) => {
// 处理表单提交
});
- 在前端表单中包含 CSRF 令牌:
<form action="/submit" method="post">
<input type="hidden" name="_csrf" value="{{ csrfToken }}">
<!-- 其他表单字段 -->
<input type="submit" value="提交">
</form>
- 防范 XSS
- 对用户输入进行严格的过滤和转义。使用
DOMPurify
库可以有效地清理用户输入,防止恶意脚本注入。安装:npm install dompurify
。
- 在 Express 应用中使用:
const DOMPurify = require('dompurify');
app.post('/input', (req, res) => {
const cleanInput = DOMPurify.sanitize(req.body.input);
// 处理清理后的输入
});
- 防范中间人攻击
- 使用 HTTPS 协议。通过申请 SSL/TLS 证书,配置 Express 应用使用 HTTPS。可以使用
https
模块结合 fs
模块来实现:
const https = require('https');
const fs = require('fs');
const app = require('./app');
const options = {
key: fs.readFileSync('path/to/private.key'),
cert: fs.readFileSync('path/to/certificate.crt')
};
https.createServer(options, app).listen(443, () => {
console.log('Server running on HTTPS port 443');
});
- 防范会话劫持
- 对会话标识进行安全存储和传输。例如,设置
httpOnly
和 secure
属性。httpOnly
可以防止通过 JavaScript 访问 cookie,减少 XSS 攻击获取会话标识的风险;secure
确保 cookie 仅通过 HTTPS 传输。
- 在 Express 中设置 cookie:
app.use((req, res, next) => {
res.cookie('sessionId', req.sessionID, { httpOnly: true, secure: true });
next();
});
- 定期更新会话标识,当用户执行敏感操作(如修改密码、转账等)时,强制重新认证并更新会话标识。