What are allowed characters in cookies?

前端 未结 13 1239
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:36

What are the allowed characters in both cookie name and value? Are they same as URL or some common subset?

Reason I\'m asking is that I\'ve recently hit some strange

13条回答
  •  甜味超标
    2020-11-22 04:27

    Here it is, in as few words as possible. Focus on characters that need no escaping:

    For cookies:

    abdefghijklmnqrstuvxyzABDEFGHIJKLMNQRSTUVXYZ0123456789!#$%&'()*+-./:<>?@[]^_`{|}~
    

    For urls

    abdefghijklmnqrstuvxyzABDEFGHIJKLMNQRSTUVXYZ0123456789.-_~!$&'()*+,;=:@
    

    For cookies and urls ( intersection )

    abdefghijklmnqrstuvxyzABDEFGHIJKLMNQRSTUVXYZ0123456789!$&'()*+-.:@_~
    

    That's how you answer.

    Note that for cookies, the = has been removed because it is usually used to set the cookie value.

    For urls this the = was kept. The intersection is obviously without.

    var chars = "abdefghijklmnqrstuvxyz"; chars += chars.toUpperCase() + "0123456789" + "!$&'()*+-.:@_~";
    

    Turns out escaping still occuring and unexpected happening, especially in a Java cookie environment where the cookie is wrapped with double quotes if it encounters the last characters.

    So to be safe, just use A-Za-z1-9. That's what I am going to do.

提交回复
热议问题