Cookie路径方案
如果一个恶意的cookie设置到一个具体的路径,这个路径不是根路径(例如,/notifications),当用户访问github.com/notifications时,浏览器会发送那个cookie,当我们在根路径上清除这个cookie时,我们的header不会起作用.
document.cookie = "_session=EVIL_SESSION_TOKEN; Path=/notifications; Domain=.github.com"
GET /notifications HTTP/1.1
Host: github.com Cookie: logged_in=yes; _session=EVIL_SESSION_TOKEN;
_session=THIS_IS_A_SESSION_TOKEN;
HTTP/1.1 302 Found
Location: /notifications Content-Type: text/html # This header has no
effect; the _session cookie was set # with `Path=/notifications` and
won't be cleared by this, # causing an infinite redirect loop
Set-Cookie: _session=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/;
Domain=.github.com;
这个方案非常直截了当,虽然不太雅:对于任何指定的请求URL,如果其路径部分匹配请求的URL,浏览器将只会发送一个恶意的JavaScript cookie.所以我们只需要在每个路径的元素上放弃这个cookie就可以了.
HTTP/1.1 302 Found
Location: /libgit2/libgit2/pull/1457 Content-Type: text/html Set-Cookie:
_session=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/;
Domain=.github.com; Set-Cookie: _session=; Expires=Thu, 01-Jan-1970
00:00:01 GMT; Path=/libgit2; Domain=.github.com; Set-Cookie: _session=;
Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/libgit2/libgit2;
Domain=.github.com; Set-Cookie: _session=; Expires=Thu, 01-Jan-1970
00:00:01 GMT; Path=/libgit2/libgit2/pull; Domain=.github.com;
Set-Cookie: _session=; Expires=Thu, 01-Jan-1970 00:00:01 GMT;
Path=/libgit2/libgit2/pull/1457; Domain=.github.com;
当谈到cookie时,我们需要在服务端做关联.我们唯一目的是用这个强力方式清楚那些cookie,这种方式虽然暴力,但完成github.io的迁移后,效果非常好.