【问题标题】:Configure POST endpoint to respond only to cron.yaml requests将 POST 端点配置为仅响应 cron.yaml 请求
【发布时间】:2017-06-14 05:01:35
【问题描述】:

我有一个在 Elastic Beanstalk 上运行的 Django 应用程序,并且想每晚运行一项工作(搜索引擎的重新索引)。根据AWS docs,我可以使用cron.yaml在正确的时间自动向我的应用发出POST请求,并配置我的应用以做出适当的响应。

如何确保我的应用只响应自动生成的请求,而不是对同一 URL 的随机请求?具体来说,我不希望恶意用户发布并导致应用执行某些操作。

【问题讨论】:

  • 你肯定可以在视图中检查请求是 POST 吗?
  • @DanielRoseman 我也想阻止恶意用户发帖。

标签: django amazon-web-services cron amazon-elastic-beanstalk amazon-sqs


【解决方案1】:

您可以检查几项以确保请求来自cron.yaml...

1) 请求将来自localhost

2) User-Agent 将包括 aws-sqsd

3) 请求只能由您的工作实例处理。

我使用before_action 过滤器在我的控制器中强制执行这些。代码如下所示:

##
# Protect against abuse - requests must have the correct "User-Agent" header,
# come from localhost, and will only be handled by worker instances.
before_action :correct_user_agent
before_action :correct_source
before_action :worker_instance

...controller code here...

private

##
# Confirms the correct User-Agent header was sent.
def correct_user_agent
  return if request.headers.env["HTTP_USER_AGENT"].include?("aws-sqsd")
  head(:forbidden)
end

##
# Confirms the request is coming from localhost.
def correct_source
  return if request.local? || Rails.env.development? || Rails.env.test?
  head(:forbidden)
end

##
# Don't allow requests to be processed in the production web environment, since
# it has a worker instance associated with it.
def worker_instance
  return unless Rails.env.production?
  head(:forbidden)
end

【讨论】:

  • 谢谢。 2 可以轻易伪造; 1 可能由于 TCP 握手而不能,但似乎有风险(不确定 Django 如何处理 X-FORWARDED-FOR 等); 3 依赖 ELB 不向工作实例发送流量(我可以依赖它吗?)
  • 如果您正确配置了负载均衡器,它应该只知道 Web 实例。
猜你喜欢
  • 2020-09-30
  • 1970-01-01
  • 2014-07-25
  • 2020-11-17
  • 2017-09-25
  • 2016-02-14
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多