【发布时间】:2020-05-29 17:40:45
【问题描述】:
我想在我的个人 express.js 应用中添加重置/忘记密码功能。我决定以与Django 类似的方式实现它。
基本上,它会根据用户(id、哈希密码、电子邮件、上次登录时间和当前时间,所有这些都与 unqiue 密码和 salt 混合)生成唯一令牌。然后,用户在他的“重置密码链接”中收到该令牌。有人在one of stackoverflow answers 中解释得比我好。
这里是source code of Django PasswordResetTokenGenerator class
我将在底部发布我的 javascript 实现。如果您检查它是否存在可能的缺陷会很好,但这不是我的主要问题:)
因此,用户收到带有“重置密码链接”的电子邮件。链接看起来像这样https://example.com/reset-password/MQ/58ix7l-35858854f74c35d0c64a5a17bd127f71cd3ad1da,其中:
-
MQ是 base64 编码的用户 ID(本例中为 1) -
58ix7l是 base36 编码的时间戳 -
35858...是实际令牌
用户点击链接。服务器接收 GET 请求 -> 服务器检查具有该 ID 的用户是否存在 -> 然后服务器检查令牌的正确性。 如果一切正常,服务器会向用户发送带有“设置新密码”表单的 html 响应。
到目前为止,一切几乎与 django 的操作方式完全相同(几乎没有细微差别)。但现在我想做一些不同的事情。 Django(收到 GET 请求后)建立匿名会话,将令牌存储在会话中,并重定向(302)以重置密码表单。客户端没有任何令牌迹象。用户填写表格,POST 请求使用新密码发送到服务器。服务器再次检查令牌(存储在会话中)。如果一切正常 - 密码已更改。
出于某种原因(这会使我的应用程序变得更加复杂:)),我不想添加匿名会话,我不想在会话中存储令牌。
我只想从req.params 获取令牌 -> 转义它 -> 检查它是否有效 -> 并使用表单发送给用户,如下所示:
<form action="/reset-password" method="POST">
<label for="new-password">New password</label><input id="new-password" type="password" name="new-password" />
<label for="repeat-new-password">Repeat new password</label><input id="repeat-new-password" type="password" name="repeat-new-password" />
<input name="token" type="hidden" value="58ix7l-35858854f74c35d0c64a5a17bd127f71cd3ad1da">
<input type="submit" value="Set new password" />
</form>
用户发送表单,服务器再次检查令牌,然后更改密码。
所以在文字墙之后,我的问题是:
像这样以html形式存储token安全吗?
我能想到一种可能的威胁:邪恶的用户可以向某人发送带有<script>alert('boo!')</script> 的链接而不是令牌。但是,如果令牌之前经过验证和转义,这应该不是问题。还有其他可能的漏洞吗?
正如我之前所说,我发布了我的 generateToken、checkToken javascript 实现,以防万一......
generate-change-password-token.js
const { differenceInSeconds } = require('date-fns');
const makeTokenWithTimestamp = require('../crypto/make-token-with-timestamp');
function generateChangePasswordToken(user) {
const timestamp = differenceInSeconds(new Date(), new Date(2010, 1, 1));
const token = makeTokenWithTimestamp(user, timestamp);
return token;
}
module.exports = generateChangePasswordToken;
验证更改密码-token.js
const crypto = require('crypto');
const { differenceInSeconds } = require('date-fns');
const makeTokenWithTimestamp = require('../crypto/make-token-with-timestamp');
function verifyChangePasswordToken(user, token) {
const timestamp = parseInt(token.split('-')[0], 36);
const difference = differenceInSeconds(new Date(), new Date(2010, 1, 1)) - timestamp;
if (difference > 60 * 60 * 24) {
return false;
}
const newToken = makeTokenWithTimestamp(user, timestamp);
const valid = crypto.timingSafeEqual(Buffer.from(token), Buffer.from(newToken));
if (valid === true) {
return true;
}
return false;
}
module.exports = verifyChangePasswordToken;
make-token-with-timestamp.js
const crypto = require('crypto');
function saltedHmac(keySalt, value, secret) {
const hash = crypto.createHash('sha1').update(keySalt + secret).digest('hex');
const hmac = crypto.createHmac('sha1', hash).update(value).digest('hex');
return hmac;
}
function makeHashValue(user, timestamp) {
const { last_login: lastLogin, id, password } = user;
const loginTimestamp = lastLogin ? lastLogin.getTime() : '';
return String(id) + password + String(loginTimestamp) + String(timestamp);
}
function makeTokenWithTimestamp(user, timestamp) {
const timestamp36 = timestamp.toString(36);
const hashValue = makeHashValue(user, timestamp);
const keySalt = process.env.KEY_SALT;
const secret = process.env.SECRET_KEY;
if (!(keySalt && secret)) {
throw new Error('You need to set KEY_SALT and SECRET_KEY in env variables');
}
const hashString = saltedHmac(keySalt, hashValue, secret);
return `${timestamp36}-${hashString}`;
}
module.exports = makeTokenWithTimestamp;
谢谢
【问题讨论】:
-
如果您要将令牌存储在客户端上,我会更倾向于将其存储为
HttpOnlycookie,javascript 无法访问。 -
用户已经可以通过邮件中的链接获取token,不再危险。我建议您将所有这些字段都放在隐藏输入中,并在提交表单时进行相同的验证。
标签: javascript django express security forgot-password