【问题标题】:Cross-site Scritping跨站脚本
【发布时间】:2017-10-29 13:15:51
【问题描述】:

在我们正在开发的应用上,我们遇到了跨站点脚本问题。

我们的应用程序使用 Angular 2 编码,我们将其作为静态网站托管在 AWS s3 中。此应用程序连接到 AWS API 网关后面的后端。

我们的前端无法进行任何 api 调用,这是跨站点脚本的原因。

http://docs.aws.amazon.com/waf/latest/developerguide/web-acl-xss-conditions.html#web-acl-xss-conditions-values

我自己试过这个,但它对我不起作用。本地前端和后端可以通信,并且它们都可以完美地单独工作。

有没有很好的方法来处理这个问题?

提前致谢!

【问题讨论】:

  • 您在验证 URL 参数吗? XSS 检查器对此抱怨,因为它可能会尝试在 URL 中发送 JavaScript 标记。如果我们不通过去除危险标签来清理字符串,就有可能在这里注入恶意脚本。
  • @RaviChandra,我如何验证它们?通过 AWS?我对这个问题了解不多。

标签: angular amazon-s3 amazon-ec2 aws-lambda aws-api-gateway


【解决方案1】:

您是否在 API 网关上激活了 CORS? 我有完全相同的应用程序,在 API 网关中你需要激活 CORS: 在链接到 lambda 的资源上(在 POST 中,如果我没记错的话,这是强制性的)您添加 OPTION 方法。 以下是 Python 中如何以编程方式激活 CORS 的示例:

gateway = boto3.client('apigateway', region_name=self.conf.region)
gateway.put_method(
     restApiId=apiId,
     resourceId=resourceId,
     httpMethod="OPTIONS",
     authorizationType="NONE"
)

gateway.put_method_response(
 restApiId=apiId,
 resourceId=resourceId,
 httpMethod="OPTIONS",
 statusCode="200",
 responseParameters={
   'method.response.header.Access-Control-Allow-Headers': True,
   'method.response.header.Access-Control-Allow-Origin': True,
   'method.response.header.Access-Control-Allow-Methods': True 
},
responseModels={
  'application/json': 'Empty'
}
   )

gateway.put_integration(
    restApiId=api['id'],
    resourceId=apiResource['id'],
    httpMethod="OPTIONS",
    type="MOCK",
    requestTemplates={
        'application/json': '{"statusCode": 200}'
 }
)

gateway.put_integration_response(
    restApiId=api['id'],
    resourceId=apiResource['id'],
    httpMethod="OPTIONS",
    statusCode="200",
    selectionPattern=".*",
    responseParameters={
     "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
     "method.response.header.Access-Control-Allow-Methods": "'*'",
     "method.response.header.Access-Control-Allow-Origin": "'*'"
    },
    responseTemplates={
     'application/json': ''
    }
)  

gateway.put_method_response(
  restApiId=apiId,
  resourceId=resourceId,
  httpMethod="POST",
  statusCode=200,
  responseParameters={'method.response.header.Access-Control-Allow-Origin': True},
  responseModels={'application/json': 'Empty'}

【讨论】:

    【解决方案2】:

    您还可以使用 AWS 控制台轻松激活 CORS。有关more details on enabling CORS,请参阅本指南。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2014-10-14
      • 2013-03-23
      • 2014-05-25
      • 2015-08-04
      相关资源
      最近更新 更多