【问题标题】:Remove csrf protecteion on API post calls删除 API post 调用上的 csrf 保护
【发布时间】:2014-04-13 06:11:24
【问题描述】:

我想从我的 Express 3.0 应用程序中删除 csrf,因为我不需要它。我使用 oauth 来验证客户端。使用 express.csrf() 时将 API url 列入白名单的中间件吗?

【问题讨论】:

  • 您仍然应该使用 CSRF 保护,因为 CSRF 攻击使用与有效客户端相同的授权。

标签: node.js api express oauth csrf


【解决方案1】:

您可以通过两种方式做到这一点。

1.)创建一个自己的小中间件,让白名单url模式不被csrf之类的拦截;

var express = require("express");
var expressCsrf = express.csrf();
var app = express.createServer();

var customCsrf = function (req, res, next) {
    // I assume exact match, but you can use regex match here
  var csrfEnabled = true;
  var whiteList = new Array("/pattern1/param1","/pattern2/param2","/pattern3/param3");
  if (whiteList.indexOf(req.path) != -1) {
    csrfEnabled = false;
  }

  if (csrfEnabled) {
    expressCsrf(req, res, next);
  } else {
    next();
  }
}

app.use(customCsrf);
app.listen(3000);

2.) 在您要启用的控制器上使用 csrf 中间件。例如,您想在配置文件保存控制器上使用 csrf 检查;

app.post("/profile/save", express.csrf(), function(req, res, next) {
    // put your code here
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2014-02-19
    • 2017-01-21
    • 2013-01-21
    • 2012-07-21
    相关资源
    最近更新 更多