Cookie规范

http协议之cookie标准RFC6265介绍

 

cookie是现代web系统开发中非常重要的一个技术,最近对cookie标准RFC6265进行了了解,从中选取了部分内容。

1.cookie的主要作用

 

因为HTTP协议是无状态的,对于一个浏览器发出的多次请求,WEB服务器无法区分是不是来源于同一个浏览器。所以,需要额外的数据用于维护会话。 Cookie 正是这样的一段随HTTP请求一起被传递的额外数据。

2.cookie的主要作用

 

除了name、value这两个必备属性外,还有下面几个可选属性(这些属性名都是大小写不敏感的,并且只要设置了浏览器是必须处理的),分别控制cookie的生存周期、可见性、安全性。

2.1) expires:绝对过期时间

 

如果这个属性的值不能被转换为日期,客户端会忽略该属性。当同一个cookie两次请求的expires值不相同时,新的可能 会替换旧的。

If the attribute-value failed to parse as acookie date, ignore the cookie-av.

If the expiry-time is later than the lastdate the user agent can represent, the user agent MAY replace the expiry-timewith the last representable date.

If the expiry-time is earlier than theearliest date the user agent can represent, the user agent MAY replace theexpiry-time with the earliest representable date

2.2)Max-Age:相对过期时间,以秒为单位。如果该属性的值不是数字,客户端将不做处理。

 

If the first character of theattribute-value is not a DIGIT or a "-" character, ignore thecookie-av.

If the remainder of attribute-value containsa non-DIGIT character, ignore the cookie-av.

If delta-seconds is less than or equal tozero (0), let expiry-time be the earliest representable date and time.Otherwise, let the expiry-time be the current date and time plus delta-secondsseconds.

 

Max-age和expires这两个属性控制cookie生命周期。 如果两个都设置了,以Max-Age为准。 默认情况下,cookie是暂时存在的,他们存储的值只在浏览器会话期间存在。当浏览器退出后,这些值也就丢失了.

If a cookie has neither the Max-Age nor theExpires attribute, the user agent will retain the cookie until "thecurrent session is over" (as defined by the user agent)。

2.3)path:指定了与cookie关联在一起的网页,默认情况下,cookie会和创建它的网页以及与这个网页处于同一个目录下的网页和处于该目录的子目录下的网页关联,同时不能用这个属性来确定安全性

 

The scope of each cookie is limited to aset of paths, controlled by the Path attribute. If the server omits the Pathattribute, the user agent will use the "directory" of therequest-uri’s path component as the default value.

The user agent will include the cookie inan HTTP request only if the path portion of the request-uri matches (or is asubdirectory of) the cookie’s Path attribute, where the %x2F ("/")character is interpreted as a directory separator.

Although seemingly useful for isolatingcookies between different paths within a given host,the Path attribute cannotbe relied upon for security

2.4)domain:如果没有设置cookie的domain值,该属性的默认值就是创建cookie的网页所在的服务器的主机名

 

If the server omits the Domain attribute,the user agent will return the cookie only to the origin server。但不能将一个cookie的域设置成服务器所在的域之外的域

The user agent will reject cookies unlessthe Domain attribute specifies a scope for the cookie that would include theorigin server. For example, the user agent will accept a cookie with a Domainattribute of "example.com" or of "foo.example.com" from foo.example.com,but the user agent will not accept a cookie with a Domain attribute of"bar.example.com" or of "baz.foo.example.com". NOTE: Forsecurity reasons, many user agents are configured to reject Domain attributesthat correspond to "public suffixes". For example, some user agentswill reject Domain attributes of "com" or "co.uk".

When a user agent receives a Set-Cookieheader field in an HTTP response, the user agent MAY ignore the Set-Cookieheader field in its entirety. For example, the user agent might wish to blockresponses to "third-party" requests from setting cookies。

2.5)secure:它指定了在网络上如何传输cookie值。默认情况下,cookie是不安全的,也就是说,他们是通过一个普通的、不安全的http链接传输的。但是如果将cookie标记为安全的,那么它将只在浏览器和服务器通过https或其他安全协议链接是才被传输。这个属性只能保证cookie是保密的。

 

The Secure attribute limits the scope ofthe cookie to "secure" channels (where "secure" is definedby the user agent). When a cookie has the Secure attribute, the user agent willinclude the cookie in an HTTP request only if the request is transmitted over asecure channel (typically HTTP over Transport Layer Security (TLS)

2.6)HttpOnly:设为true后,只能通过http访问,不能通过document.cookie获取设定为httponly的键值,防止xss读取cookie。

 

httpOnly属性和secure是独立的,一个cookie可以同时设置这两个属性。

The HttpOnly attribute limits the scope ofthe cookie to HTTP requests. In particular, the attribute instructs the useragent to omit the cookie when providing access to cookies via"non-HTTP" APIs (such as a web browser API that exposes cookies toscripts). Note that the HttpOnly attribute is independent of the Secure attribute:a cookie can have both the HttpOnly and the Secure attribute.

2.7)cookie属性其他相关内容

 

User agents ignore unrecognized cookieattributes (but not the entire cookie).

To maximize compatibility with user agents,servers that wish to store arbitrary data in a cookie-value SHOULD encode thatdata, for example, using Base64 [RFC4648].

To maximize compatibility with user agents,servers SHOULD NOT produce two attributes with the same name in the sameset-cookie-string.

If the user agent receives a new cookiewith the same cookie-name, domain-value, and path-value as a cookie that it hasalready stored, the existing cookie is evicted and replaced with the newcookie. Notice that servers can delete cookies by sending the user agent a newcookie with an Expires attribute with a value in the past.

3.cookie值在何处设置

 

通常cookie值是在服务端设置,但也可以通过js在客户端设置,另外

3.1)编码方式(Java中的httpclient包)的http请求可以直接在请求头上加入cookie;

3.2)iOS的UIWebview可以在loadRequest构造带cookie的reqeust;

3.3)Android的Webview可以通过CookieManager来设置cookie;

4.cookie如何传输及规则

4.1服务端—》客户端

 

通过http的response头,会将服务端设置的所有的cookie都发送到客户端,发送的内容是cookie的name、value及已设置的全部属性

4.2cookie属性其他相关内容

 

通过http的request头,浏览器也不是发送它所接收到的所有Cookie,它会检查当前要请求的域名以及目录,只要这二项目与Cookie对应的Domain和Path匹配,才会发送。对于Domain则是按照尾部匹配的原则进行的。发送的内容只有name和value,其他的属性是不发送的。

Each cookie-pair represents a cookie storedby the user agent. The cookie-pair contains the cookie-name and cookie-valuethe user agent received in the Set-Cookie header.

Notice that the cookie attributes are notreturned.

因而当客户端发送两个同名的cookie时,服务端是无法区分这两个cookie的归属。

Although cookies are serialized linearly inthe Cookie header, servers SHOULD NOT rely upon the serialization order. Inparticular, if the Cookie header contains two cookies with the same name (e.g.,that were set with different Path or Domain attributes), servers SHOULD NOTrely upon the order in which these cookies appear in the header.

5.cookie是否可以被截获

 

有两种方法可以截获他人的cookie,

5.1). 通过XSS脚本攻击, 获取他人的cookie

5.2.) 想办法获取别人电脑上保存的cookie文件(这个比较难)

6.cookie是否可以被非法修改

 

可以通过一些插件(如edit this cookie)或者其他技术手段进行修改。Secure属性也有其局限性。

Although seemingly useful for protectingcookies from active network attackers, the Secure attribute protects only thecookie’s confidentiality. An active network attacker can overwrite Securecookies from an insecure channel, disrupting their integrity

 

*****************************************************************************************************************************************************************

 

cookie操作在前端开发过程中经常遇到,当然如果只是用来存储一些简单用户数据,还是比较简单的,我们要做的可能只是设置cookie名,值,过期时间等,读取也只要根据cookie的名读取相应的cookie值就可以了。在复杂的应用中,光这些肯定就不够了。

 

cookie的属性

 

除了name(名)和value(值),cookie还有以下一些可选属性,用来控制cookie的有效期,作用域,安全性等:

 

expires属性

  指定了cookie的生存期,默认情况下cookie是暂时存在的,他们存储的值只在浏览器会话期间存在,当用户退出浏览器后这些值也会丢失,如果想让cookie存在一段时间,就要为expires属性设置为未来的一个用毫秒数表示的过期日期或时间点,expires默认为设置的expires的当前时间。现在已经被max-age属性所取代,max-age用秒来设置cookie的生存期。

 

如果max-age属性为正数,则表示该cookie会在max-age秒之后自动失效。浏览器会将max-age为正数的cookie持久化,即写到对应的cookie文件中。无论客户关闭了浏览器还是电脑,只要还在max-age秒之前,登录网站时该cookie仍然有效。

 

如果max-age为负数,则表示该cookie仅在本浏览器窗口以及本窗口打开的子窗口内有效,关闭窗口后该cookie即失效。max-age为负数的Cookie,为临时性cookie,不会被持久化,不会被写到cookie文件中。cookie信息保存在浏览器内存中,因此关闭浏览器该cookie就消失了。cookie默认的max-age值为-1。

 

如果max-age为0,则表示删除该cookie。cookie机制没有提供删除cookie的方法,因此通过设置该cookie即时失效实现删除cookie的效果。失效的Cookie会被浏览器从cookie文件或者内存中删除。

 

如果不设置expires或者max-age这个cookie默认是Session的,也就是关闭浏览器该cookie就消失了。

 

这里要说明一下:Session的cookie在ie6下,如果用户实在网页上跳转打开页面或新开窗口(包括target="_blank",鼠标右键新开窗口),都是在同一个Session内。如果用户新开浏览器程序或者说是进程再打开当前的页面就不是同一个Session。其他浏览器只要你Session存在,还是同一个Session,cookie还能共享。在前段时间的项目中ie6下吃了很大一个亏。

 

domain属性

 domain属性可以使多个web服务器共享cookie。domain属性的默认值是创建cookie的网页所在服务器的主机名。不能将一个cookie的域设置成服务器所在的域之外的域。

  例如让位于a.sodao.com的服务器能够读取b.sodao.com设置的cookie值。如果b.sodao.com的页面创建的cookie把它的path属性设置为"/",把domain属性设置成".sodao.com",那么所有位于b.sodao.com的网页和所有位于a.sodao.com的网页,以及位于sodao.com域的其他服务器上的网页都可以访问这个cookie。

 

path属性

  它指定与cookie关联在一起的网页。在默认的情况下cookie会与创建它的网页,该网页处于同一目录下的网页以及与这个网页所在目录下的子目录下的网页关联

 

secure属性

  它是一个布尔值,指定在网络上如何传输cookie,默认是不安全的,通过一个普通的http连接传输;

 

HttpOnly属性

 HttpOnly 属性限制了 cookie 对 HTTP 请求的作用范围。特别的,该属性指示用户代理忽略那些通过"非 HTTP" 方式对 cookie 的访问(比如浏览器暴露给js的接口)。注意 HttpOnly 属性和 Secure 属性相互独立:一个 cookie 既可以是 HttpOnly 的也可以有 Secure 属性。

  在前段时间的项目中我就用js去读取一个cookie,结果怎么都取不到这个值,最后查证这个cookie是httpOnly的,花了近2个小时,悲剧了。

 

cookie的传输

 

浏览器将cookie信息以name-value对的形式存储于本地,每当请求新文档时,浏览器将发送Cookie,目的是让Server可以通过HTTP请求追踪客户。所以从WEB性能的角度来说我们要尽量的减小cookie,以达到传输性能的最大化。

 

cookie的编码和解码

 

由于cookie的名/值中的值不允许包含分号,逗号和空格符,为了最大化用户代理和服务器的兼容性,任何被存储为 cookie 值的数据都应该被编码,例如用我们前端熟知的js全局函数encodeURIComponent编码和decodeURIComponent解码。

 

cookie作为客户端存储

 

前面说了每当请求新文档时,浏览器将发送Cookie到服务器,导致WEB性能下降。所以不建议将cookie作为客户端存储一种实现方案,替代方案参见:JavaScript本地存储实践(html5的localStorage和ie的userData)等。

 

同名的 cookie

 

同名的 cookie,不同的 domain 或不同的 path,属不同的 cookie;同名的 cookie,相同的 domain 且相同的 path,不同的 expires,属同一个 cookie。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值