5.5.10

HTTPOnly cookie flag

HTTPOnly เป็นวิธีหนึ่งที่ใช้ป้องกัน client side script เข้าถึง cookie โดยที่เราไม่อนุญาตเพื่อป้องกันจุดอ่อนของ web application ที่ลดความเสี่ยงเรื่องของ XSS

เริ่มต้นจาก blog ของนาย Jordan Wiens, "No cookie for you!" (ตอนนี้ไปเป็น CTO ของ white hat security) แล้วถูกนำไป implement ครั้งแรกในปี 2002 ใน browser ของ microsoft internet explorer
ถ้าพูดตามหลักแล้ว HttpOnly เป็นเพียงการเพิ่ม flag ลงใน Set-Cookie HTTP response header แต่ browser จะต้องรองรับ flag นี้ด้วยถึงจะเข้าใจความหมาย

ถ้า HttpOnly flag เพิ่มลงใน Http response header, cookie จะไม่สามารถเข้าถึงได้ผ่าน client side script ผลก็คือถ้าเกิด cross-site scripting (XSS) คือผู้ใช้เผลอกด link ที่ไปเข้าหน้าที่เกิด XSS ตัว browser จะกัน client side script ไม่ให้เข้าถึง cookie ได้ แล้วจะ return เป็น empty string กลับคืนมาให้
ถ้า browser ไม่รองรับ HttpOnly และ website ได้มีการกำหนดค่า HttpOnly ตัว client side script ก็จะสามารถเข้าถึง cookie ได้

ตอนนี้มี browser ที่รองรับได้แก่
  • Internet Explorer 6 sp1+ http://www.petefreitag.com/item/644.cfm
  • Firefox 2.0.0.5+ http://www.petefreitag.com/item/644.cfm
  • Opera since 9.5+ (Prevents Read) http://manicode.blogspot.com/2008/06/opera-95-httponly-read-prevention.html
  • Netscape 9.0b3+

ตัวอย่างการใช้งาน
String sessionId= request.getSession().getId();response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionId + "; HttpOnly");
ใน tomcat 6 เราสามารถกำหนด useHttpOnly=true ใน context.xml ได้

สำหรับ servlet 3.0 รองรับ HTTPOnly เช่นกันผ่าน method
void setHttpOnly(boolean isHttpOnly)boolean isHttpOnly()

Reference:
http://msdn.microsoft.com/en-us/library/ms533046.aspx

No comments:

Post a Comment