【问题标题】:CORS configuration with Restify使用 Restify 进行 CORS 配置
【发布时间】:2013-09-22 10:26:13
【问题描述】:

我看到当我对启用 CORS 的 Restify 服务执行 HTTP GET 时,默认情况下 access-control-allow-origin 标头设置为通配符。我宁愿它与我发送的 Origin 相呼应,因为这是OWASP 的最佳实践。有什么建议我该怎么做?尝试了 API 文档中的默认标头、格式化程序等,但没有成功。

这是我的工作:

    var server = restify.createServer({
        name: 'People Data Service',
        version: '1.0.6'
    });        
        server.pre(wrapper(restify.pre.pause()));
        // Cleans up sloppy paths
        server.pre(wrapper(restify.pre.sanitizePath()));
        server.use(wrapper(restify.acceptParser(server.acceptable)));
        server.use(wrapper(restify.authorizationParser()));
        server.use(wrapper(restify.queryParser()));
        server.use(wrapper(restify.bodyParser()));
        server.use(wrapper(restify.CORS()));
    //    server.use(wrapper(restify.fullResponse()));

        // Needed this for OPTIONS preflight request: https://github.com/mcavage/node-restify/issues/284
        function unknownMethodHandler(req, res) {
            if (req.method.toUpperCase() === 'OPTIONS') {
                console.log('Received an options method request from: ' + req.headers.origin);
                var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With', 'Authorization'];

                if (res.methods.indexOf('OPTIONS') === -1) {
                    res.methods.push('OPTIONS');
                }

                res.header('Access-Control-Allow-Credentials', false);
                res.header('Access-Control-Expose-Headers', true);
                res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
                res.header('Access-Control-Allow-Methods', res.methods.join(', '));
                res.header('Access-Control-Allow-Origin', req.headers.origin);
                res.header('Access-Control-Max-Age', 1209600);

                return res.send(204);
            }
            else {
                return res.send(new restify.MethodNotAllowedError());
            }
        }
    server.on('MethodNotAllowed', wrapper(unknownMethodHandler));

【问题讨论】:

    标签: node.js cors restify


    【解决方案1】:

    我在我的 restify 基础应用上这样做:

        //setup cors
        restify.CORS.ALLOW_HEADERS.push('accept');
        restify.CORS.ALLOW_HEADERS.push('sid');
        restify.CORS.ALLOW_HEADERS.push('lang');
        restify.CORS.ALLOW_HEADERS.push('origin');
        restify.CORS.ALLOW_HEADERS.push('withcredentials');
        restify.CORS.ALLOW_HEADERS.push('x-requested-with');
        server.use(restify.CORS());
    

    您需要先使用restify.CORS.ALLOW_HEADERS.push 方法将您想要的标头推送到restify,然后使用CORS中间件启动CORS功能。

    【讨论】:

    • 只允许标题;这不会将 Access-control-allow-origin 设置为回显原始请求标头。我在 Github 上开了一个案例,看看是否可以添加。 github.com/mcavage/node-restify/issues/446
    • @occasl 似乎在调用 restify.CORS() 时默认设置为“*”
    • 是的,没错。您必须根据我的答案更新源才能设置原点。我向所有者提交了拉取请求。
    【解决方案2】:

    我找到了一种方法,方法是对 Restify 中的 cors.js 文件进行简单修改,如下所示:

    Index: node_modules/restify/lib/plugins/cors.js
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    --- node_modules/restify/lib/plugins/cors.js    (revision )
    +++ node_modules/restify/lib/plugins/cors.js    (revision )
    @@ -102,6 +102,7 @@
                                     res.setHeader(AC_ALLOW_ORIGIN, origin);
                                     res.setHeader(AC_ALLOW_CREDS, 'true');
                             } else {
    +                            origin = req.headers['origin'];
                                     res.setHeader(AC_ALLOW_ORIGIN, origin);
                             }
    

    即使凭据设置为 false,这也会将来源添加到响应标头中。此外,如果您想将您的来源列入白名单,您只需将其添加到您的配置中即可:

    server.use(restify.CORS({'origins': ['http://localhost', 'https://domain.my.com', 'https://anotherDomain.my.com']}));
    

    【讨论】:

    • 根据我看到的in the cors plugin filevar origins = opts.origins || ['*'];
    • 他们最近接受了我的拉取请求,所以我认为这应该是最新的了。
    【解决方案3】:

    其实很简单。

    你需要初始化一个服务器,然后才能设置它!

    var server = restify.createServer( ... );

    要配置 CORS,请按以下步骤操作:

    1.您需要指定 ALLOW_HEADERS

    这是用于设置 CORS 请求是否到达的标头数组。

    2。配置 CORS 插件

    server.use(restify.CORS({ 凭据:真 }));

    restif-CORS-plugin 将使用选项“凭据”来添加 Access-Control-Allow-Credentials 和 Access-Control-Allow-Origin。

    在客户端,在 xhr-transport 上使用 withCredentials = true 很重要。

    希望这些回答对你有所帮助。

    ...编写代码并玩得开心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 2012-12-29
      • 2019-06-09
      • 2014-09-05
      • 2017-02-15
      • 2018-11-05
      • 2021-05-03
      相关资源
      最近更新 更多