代码之家  ›  专栏  ›  技术社区  ›  antpaw

rails安全会话和cookies不能与cdn一起工作

  •  0
  • antpaw  · 技术社区  · 8 年前

    我的设置如下:仅使用https的cloudfront cdn和仅使用ec2 instnace或ign http的cloudfront cdn(所有对cloudfront的请求都是https,所有从cloudfront到ec2的请求都是http)

    如果我把安全设置为 真的 在我的申请中, 会话和cookie不再保存在任何浏览器中 .如果我设置为 它可以在大多数浏览器中工作,但是 在狩猎中不起作用 是的。

    Rails.application.config.session_store :cookie_store, key: '_K_session', secure: true
    

    我的目标是让会话在所有浏览器上都能正常工作。我真的不需要安全会话设置。

    以下是我的简化地形设置:

    resource "aws_cloudfront_distribution" "main_rails_app" {
      origin {
        domain_name = "${aws_elastic_beanstalk_environment.main_rails_app.cname}"
        origin_id   = "${var.cf_main_rails_app_origin_id}"
    
        custom_origin_config {
          http_port              = "80"
          https_port             = "443"
          origin_protocol_policy = "http-only"
          origin_ssl_protocols   = ["TLSv1.1"]
        }
      }
      default_cache_behavior {
        allowed_methods  = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
        cached_methods   = ["GET", "HEAD", "OPTIONS"]
        target_origin_id = "${var.cf_main_rails_app_origin_id}"
    
        forwarded_values {
          query_string = true
          headers      = ["*"]
    
          cookies {
            forward = "all"
          }
        }
    
        min_ttl                = 0
        default_ttl            = 0
        max_ttl                = 0
        compress               = true
        viewer_protocol_policy = "redirect-to-https"
      }
      viewer_certificate {
        # cloudfront_default_certificate = true
        acm_certificate_arn      = "${data.aws_acm_certificate.some_domain.arn}"
        minimum_protocol_version = "TLSv1.1_2016"
        ssl_support_method       = "sni-only"
      }
      restrictions {
        geo_restriction {
          restriction_type = "none"
        }
      }
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   strongjz    8 年前

    cookie存储正在做它应该做的事情,rails在一个未加密的会话中接收到一个cookie,cf to ec2是未加密的。

    你有几种选择。

    1. Encrypt traffic between ec2 and cf if you want to set to secure: true. And force-ssl redirect as well.
    2. 保持安全:false,因为您已经信任cf和ec2之间的连接。

    http://guides.rubyonrails.org/v5.0/security.html

    Secure session cookie is not set

        2
  •  1
  •   antpaw    7 年前

    事实证明,Safari确实发送了一个位置标头,即使表单是在同一个域上托管的,其他浏览器也没有。位置标头值(CDN URL)与应用程序URL(EC2 URL)不匹配,请求将被标记为Rails中无效。我有 protect_from_forgery 而不是 protect_from_forgery with: :exception 我花了很长时间才看到这个,因为我没有收到任何明显的错误。我的解决方案是禁用此设置:

    Rails.application.config.action_controller.forgery_protection_origin_check = false
    

    此设置的默认状态在Rails 5中翻转。