首页 > 解决方案 > 以 html 帖子形式存储重置密码令牌是否安全?

问题描述

我想在我的个人 express.js 应用程序中添加重置/忘记密码功能。我决定以与Django类似的方式实现它。

基本上,它会根据用户(id、哈希密码、电子邮件、上次登录时间和当前时间,所有这些都与 unqiue 密码和 salt 混合)生成唯一令牌。然后,用户在他的“重置密码链接”中收到该令牌。在stackoverflow的一个答案中,有人比我解释得更好。

这是Django类的源代码PasswordResetTokenGenerator

我将在底部发布我的 javascript 实现。如果您检查它是否存在可能的缺陷会很好,但这不是我的主要问题:)

因此,用户收到带有“重置密码链接”的电子邮件。链接看起来像这样https://example.com/reset-password/MQ/58ix7l-35858854f74c35d0c64a5a17bd127f71cd3ad1da,其中:

用户点击链接。服务器接收 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形式存储令牌是否安全?

我可以想到一种可能的威胁:邪恶的用户可以向某人发送链接<script>alert('boo!')</script>而不是令牌。但是,如果令牌之前经过验证和转义,这应该不是问题。还有其他可能的漏洞吗?

正如我之前所说,我发布了我generateTokencheckTokenjavascript 实现,以防万一......


生成更改密码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;

谢谢

标签: javascriptdjangoexpresssecurityforgot-password

解决方案


从安全的角度来看,将重置令牌存储在 URL(get 变量)或表单(作为 post 变量)中并没有太大区别。在这两种情况下,任何有权访问 URL 的人都将有权重置密码。

正如您所提到的,您需要注意 XSS 攻击(在令牌中嵌入 JavaScript,然后在页面中显示),并验证令牌是否只是字母数字应该可以解决该特定问题。您还需要注意 CORS 样式的攻击,大多数框架都可以为您处理。

对我来说,另外两件要考虑的事情是——

  1. 令牌在合理的时间内过期,因为它基本上是一个密码,可以用来接管一个帐户。

  2. 该通知会在任何密码请求后发送,因此如果用户没有故意重置自己的密码,他们可以知道该事件。


推荐阅读