SailsJS CSRF不匹配错误自定义

时间:2014-11-07 06:59:57

标签: sails.js

我需要自定义当有人没有使用POST请求发送CSRF代码时出现的错误。

因此,没有人会知道错误发生了什么,他们甚至不会尝试入侵CSRF机制。

希望这很清楚

1 个答案:

答案 0 :(得分:1)

现在Sails.js CSRF钩子使用res.forbidden()函数来处理错误的CSRF令牌。 它将此消息与此消息一起使用:

return res.forbidden("CSRF mismatch");

因此,您可以通过将名为forbidden.js的新文件放入/ api / responses来重写此响应 实际上你要复制这个:https://github.com/balderdashy/sails/blob/master/lib/hooks/responses/defaults/forbidden.js

在生产模式检查之前添加检查数据的条件:

...
else sails.log.verbose('Sending 403 ("Forbidden") response');

if (data == 'CSRF mismatch') {
    //Return another response for example:
    return res.jsonx(500, {/* data here */});
}
// Only include errors in response if application environment
// is not set to 'production'.  In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production') {
...

无论如何,只要您将风帆用于开发模式。当从帆中获得500或任何其他错误时,您将看到所有错误。但在生产模式下,所有错误消息都将被隐藏。并且您的用户不会收到任何错误详细信息。除了错误代码。

因此,在没有任何更改的生产模式下,您将只获得HTTP 403代码。