【问题标题】:Understanding RFC6265 domain-matching conditions了解 RFC6265 域匹配条件
【发布时间】:2014-03-13 18:32:56
【问题描述】:

我正在寻找一种简单的方法来检查给定的 cookie 域域是否与给定的主机名匹配。

为此,我将实现section 5.1.3 of RFC 6265 中定义的域匹配条件。

定义的两个匹配条件中的第二个是一个多部分条件,其中应用了三个子条件:

以下所有条件都成立:

  • 域字符串是字符串的后缀。
  • 未包含在域字符串中的字符串的最后一个字符是 %x2E (".") 字符。
  • 字符串是主机名(即,不是 IP 地址)。

为清楚起见,当上面引用的文本指的是“字符串”时,它指的是 cookie 的域值,而当上面引用的文本指的是“域名”时,它指的是主机的域名cookie 可能被发送到的对象。

在这三个子条件中,第一个和第三个是很清楚的。第二个的措辞让我感到困惑。

我知道“example.com”的 cookie 域仅匹配“example.com”,“.example.com”的 cookie 域匹配“.example.com”。如果提到这个广泛的子域匹配概念,我最好的猜测是上面的第二个子条件,但是鉴于措辞我无法确定。

有人能把第二个子条件翻译成简单的技术英语吗?

【问题讨论】:

    标签: cookies domain-name rfc6265


    【解决方案1】:

    在试图理解这种特殊情况时,我也遇到了很多困难。第十五次阅读后,我注意到我没有注意关键词。引用(并强调):

    • 域字符串中未包含字符串的最后一个字符是 %x2E(“.”)字符。

    所以我相信以下 Python 3 实现将符合 RFC:

    import ipaddress
    
    def domain_matches (string: str, domain_string: str) -> bool:
        string = string.lower()
        domain_string = domain_string.lower()
        try:
            ipaddress.ip_address(string)
            is_host = False
        except ValueError:
            is_host = True
        return (
            string == domain_string
            or
            (string.endswith(domain_string)
             and
             string[-(len(domain_string) + 1)] == "."
             and
             is_host)
        )
    

    ipaddress 位是完全迂腐的。但是,对于我的用例,我省略了它。

    【讨论】:

      【解决方案2】:

      根据section 4.1.2.3 of RFC 6265

      例如,如果 Domain 属性的值为“example.com”,当向 example.com、www.example.comwww.corp.example.com 发出 HTTP 请求时,用户代理将在 Cookie 标头中包含 cookie。 (请注意,如果存在前导 %x2E(“.”),即使该字符是不允许的,也会被忽略,但如果存在尾随 %x2E(“.”),将导致用户代理忽略该属性。 )

      所以逻辑是:

      1. check targetString endsWith domain
      2. check lastChar(targetString) != "."
      3. check targetString is host name
      

      【讨论】:

        猜你喜欢
        • 2013-09-11
        • 2014-04-15
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        相关资源
        最近更新 更多