【问题标题】:How to set response headers in serverless.yml?如何在 serverless.yml 中设置响应头?
【发布时间】:2018-03-14 09:41:39
【问题描述】:

我有使用无服务器框架版本 1.25 的无服务器 API

出于安全原因,我想添加响应标头。请帮助我如何通过 serverless.yml 文件设置以下标题。出于安全原因,是否需要添加此标头?

• Content-Security-Policy:包括 default-src 'self'

• Strict-Transport-Security max-age=31536000;包括子域;预加载

• X-Content-Type-Options:nosniff

• X-XSS 保护:1

• 缓存控制:max-age=0;过期=-1 或过期:格林威治标准时间 1990 年 1 月 1 日星期五 00:00:00;无缓存,必须重新验证

下面是我的无服务器应用 serverless.yaml

service: myService
provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-west-1
  environment:
    REGION: ${self:provider.region}
    PROJECT_NAME: ${self:custom.projectName}
    SERVERLESS_STAGE: ${self:provider.stage}
    SERVERLESS_SERVICE: ${self:service}
    IP_ADDRESS: http://example.com
functions:
   getMyFunction:
     handler: handler.getMyFunction
     timeout: 30
     events:
      - http:
          method: get
          path: api/getMyFunction/v1
          integration: lambda
          cors: true
          authorizer:
            name: authorizerFunc
            identitySource: method.request.header.Token
            authorizationType: AWS_IAM

【问题讨论】:

    标签: javascript amazon-web-services http-headers httpresponse serverless-framework


    【解决方案1】:

    您可以使用Lambda Proxy Integration。根据文档,您需要创建一个函数,该函数将在有人访问您的 API 端点时运行。

    举个例子:

    module.exports.hello = function (event, context, callback) {
        console.log(event); // Contains incoming request data (e.g., query params, headers and more)
    
        const response = {
            statusCode: 200,
            headers: {
                "x-custom-header": "My Header Value"
            },
            body: JSON.stringify({ "message": "Hello World!" })
        };
    
        callback(null, response);
    };
    

    在你的 serverless.yml 中

    functions:
     index:
       handler: handler.hello
       events:
         - http: GET hello
    

    【讨论】:

      【解决方案2】:

      由于您使用 Lambda 集成,因此您必须将其放入您的 serverless.yml

      service: myService
      provider:
        name: aws
        runtime: nodejs6.10
        stage: dev
        region: eu-west-1
        environment:
          REGION: ${self:provider.region}
          PROJECT_NAME: ${self:custom.projectName}
          SERVERLESS_STAGE: ${self:provider.stage}
          SERVERLESS_SERVICE: ${self:service}
          IP_ADDRESS: http://example.com
      functions:
         getMyFunction:
           handler: handler.getMyFunction
           timeout: 30
           events:
            - http:
                method: get
                path: api/getMyFunction/v1
                integration: lambda
                cors: true
                authorizer:
                  name: authorizerFunc
                  identitySource: method.request.header.Token
                  authorizationType: AWS_IAM
                response:
                  headers:
                    Content-Security-Policy: "'Include default-src 'self''"
                    Strict-Transport-Security: "'max-age=31536000; includeSubDomains; preload'"
                    X-Content-Type-Options: "'nosniff'"
                    X-XSS-Protection: "'1'"
                    Cache-Control: "'max-age=0; Expires=-1 or Expires: Fri, 01 Jan 1990 00:00:00 GMT; no-cache, must-revalidate'"
      

      参考:https://serverless.com/framework/docs/providers/aws/events/apigateway#custom-response-headers

      【讨论】:

      • 你推送的代码是正确的,但是需要在值后面加上“''”。我正在发布这个答案。
      【解决方案3】:
      service: myService 
      provider:
        name: aws
        runtime: nodejs6.10
        stage: dev
        region: eu-west-1
        environment:
          REGION: ${self:provider.region}
          PROJECT_NAME: ${self:custom.projectName}
          SERVERLESS_STAGE: ${self:provider.stage}
          SERVERLESS_SERVICE: ${self:service}
          IP_ADDRESS: http://example.com
      functions:
        getMyFunction:
         handler: handler.getMyFunction
         timeout: 30
         events:
          - http:
            method: get
            path: api/getMyFunction/v1
            integration: lambda
            cors: true
            authorizer:
              name: authorizerFunc
              identitySource: method.request.header.Token
              authorizationType: AWS_IAM
            response:
              headers:
                Content-Security-Policy: "'Include default-src 'self''"
                Strict-Transport-Security: "'max-age=31536000; includeSubDomains; preload'"
                X-Content-Type-Options: "'nosniff'"
                X-XSS-Protection: "'1'"
                Cache-Control: "'max-age=0; Expires=-1 or Expires: Fri, 01 Jan 1990 00:00:00 GMT; no-cache, must-revalidate'"
      

      【讨论】:

      • 最新版本的无服务器不喜欢这样,无服务器:在'functions.login.events[1].httpApi':无法识别的属性'cors'无服务器:在'functions.login.events[1 ].httpApi': 无法识别的属性'response'
      猜你喜欢
      • 1970-01-01
      • 2014-11-09
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 2012-06-19
      • 1970-01-01
      相关资源
      最近更新 更多