您的位置:首页 > 理论基础 > 计算机网络

常用Web开发语言HttpOnly设置详解

2015-07-03 13:06 555 查看
http://www.2cto.com/kf/200905/38080.html

by:Neeao

Http://www.neeao.com

2009-05-11

———————————–

关于HttpOnly对于防范XSS来获取 cookies信息的分析,请参考剑心同学写的:利用httponly提升应用程序安全性,这里仅把常用的一些Web开发语言的设置方法整理总结下,备用。

javaEE中的设置:

API中没有提供具体的操作方法或者函数属性来设置,不知道后续版本是否会提供,下面为变通设置方法:

————————————————————————————–

response.setHeader(”Set-Cookie”, “cookiename=value;

Path=/;Domain=neeao.com;Max-Age=seconds;HTTPOnly”);

————————————————————————————–

ASP.NET中的设置

.net2.0以上版本支持在Web.config文件中来配置全局的httponly,设置如下,在web.config中添加一个节点即可:

------------------------------------------------------------------

<httpCookies httpOnlyCookies="true" />

------------------------------------------------------------------

.net2.0以上版本cookie对象中,直接有一个HttpOnly的参数供调用,使用方法如下:

C#代码:

------------------------------------------------------------------

HttpCookie myCookie = new HttpCookie("myCookie");

myCookie.HttpOnly = true;

Response.AppendCookie(myCookie);

-------------------------------------------------------------------

vb.net代码

-------------------------------------------------------------------

Dim myCookie As HttpCookie = new HttpCookie("myCookie")

myCookie.HttpOnly = True

Response.AppendCookie(myCookie)

-------------------------------------------------------------------

在asp.net1.1中也可以设置全局的cookies HttpOnly,在全局文件Global.asax的application节点的 Application_EndRequest事件中添加:

-------------------------------------------------------------------

protected void Application_EndRequest(Object sender, EventArgs e)

{

string authCookie = FormsAuthentication.FormsCookieName;

foreach (string sCookie in Response.Cookies)

{

if (sCookie.Equals(authCookie))

{

Response.Cookies[sCookie].Path += ";HttpOnly";

}

}

}

-------------------------------------------------------------------

在代码中写的话,就需要这样来添加了:

--------------------------------------------

Response.Cookies[cookie].Path += ";HTTPOnly";

---------------------------------------------

PHP中的设置

PHP5.2以上版本已支持HttpOnly参数的设置,同样也支持全局的HttpOnly的设置,在php.ini中

-----------------------------------------------------

session.cookie_httponly =

-----------------------------------------------------

设置其值为1或者TRUE,来开启全局的Cookie的HttpOnly属性,当然也支持在代码中来开启:

-----------------------------------------------------

<?php

ini_set("session.cookie_httponly", 1);

// or

session_set_cookie_params(0, NULL, NULL, NULL, TRUE);

?>

-----------------------------------------------------

Cookie操作函数setcookie函数和setrawcookie函数也专门添加了第7个参数来做为HttpOnly的选项,开启方法为:

-------------------------------------------------------

setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE);

setrawcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE);

-------------------------------------------------------

对于PHP5.1以前版本以及PHP4版本的话,则需要通过header函数来变通下了:

-------------------------------------------------------------

<?php

header("Set-Cookie: hidden=value; httpOnly");

?>

-------------------------------------------------------------

Asp中的设置

asp的内置对象中没有提供相关方法,只能变通来实现了:

-----------------------------------------------------<%

‘**************************************************

‘ASP 中输出httponly cookie IE6.0以上浏览器支持

‘WDFrog

‘2009-04-15

‘<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>

‘**************************************************

‘———-SetHttpOnlyCookie—————————————-

‘功能:设置HttpOnly Cookie

‘参数:expDate 为保到期, 0表示不设置,设置为过去某一时间表示清除

‘参数:domain 为空(string.Empty)表示不设置

‘——————————————————————-

Function SetHttpOnlyCookie(cookieName,cookieValue,domain,path,expDate)

Dim cookie

cookie=cookieName & “=” & Server.URLEncode(cookieValue) & “; path=” & path

If expDate <> 0 Then

cookie=cookie & “; expires=” & DateToGMT(expDate)

End If

If domain <> “” Then

cookie=cookie & “; domain=” & domain

End If

cookie=cookie & “; HttpOnly”

Call Response.AddHeader (”Set-Cookie”, cookie)

End Function

‘————-getGMTTime————

‘参数: sDate 需要转换成GMT的时间

‘———————————

Function DateToGMT(sDate)

Dim dWeek,dMonth

Dim strZero,strZone

strZero=”00″

strZone=”+0800″

dWeek=Array(”Sun”,”Mon”,”Tue”,”Wes”,”Thu”,”Fri”,”Sat”)

dMonth=Array(”Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”)

DateToGMT = dWeek(WeekDay(sDate)-1)&”, “&Right(strZero&Day(sDate),2)&” “&dMonth(Month(sDate)-1)&” “&Year(sDate)&” “&Right(strZero&Hour(sDate),2)&”:”&Right(strZero&Minute(sDate),2)&”:”&Right(strZero&Second(sDate),2)&” “&strZone

End Function

‘参考

‘Call SetHttpOnlyCookie(”cookieOnly1″,”onlyValue”,”.gyzs.com”,”/”,0)

%>

----------------------------------------------------

参考文献:

1.http://www.owasp.org/index.php/HTTPOnly

2.http://blogs.msdn.com/dansellers/archive/2006/03/13/550947.aspx

3.http://ilia.ws/archives/121-httpOnly-cookie-flag-support-in-PHP-5.2.html

4.http://www.asp101.com/tips/index.asp?id=160

5.http://www.cnblogs.com/wdfrog/archive/2009/04/15/1436493.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: