What are allowed characters in cookies?

前端 未结 13 1234
佛祖请我去吃肉
佛祖请我去吃肉 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:33

    One more consideration. I recently implemented a scheme in which some sensitive data posted to a PHP script needed to convert and return it as an encrypted cookie, that used all base64 values I thought were guaranteed 'safe". So I dutifully encrypted the data items using RC4, ran the output through base64_encode, and happily returned the cookie to the site. Testing seemed to go well until a base64 encoded string contained a "+" symbol. The string was written to the page cookie with no trouble. Using the browser diagnostics I could also verify the cookies was written unchanged. Then when a subsequent page called my PHP and obtained the cookie via the $_COOKIE array, I was stammered to find the string was now missing the "+" sign. Every occurrence of that character was replaced with an ASCII space.

    Considering how many similar unresolved complaints I've read describing this scenario since then, often siting numerous references to using base64 to "safely" store arbitrary data in cookies, I thought I'd point out the problem and offer my admittedly kludgy solution.

    After you've done whatever encryption you want to do on a piece of data, and then used base64_encode to make it "cookie-safe", run the output string through this...

    // from browser to PHP. substitute troublesome chars with 
    // other cookie safe chars, or vis-versa.  
    
    function fix64($inp) {
        $out =$inp;
        for($i = 0; $i < strlen($inp); $i++) {
            $c = $inp[$i];
            switch ($c) {
                case '+':  $c = '*'; break; // definitly won't transfer!
                case '*':  $c = '+'; break;
    
                case '=':  $c = ':'; break; // = symbol seems like a bad idea
                case ':':  $c = '='; break;
    
                default: continue;
                }
            $out[$i] = $c;
            }
        return $out;
        }
    

    Here I'm simply substituting "+" (and I decided "=" as well) with other "cookie safe" characters, before returning the encoded value to the page, for use as a cookie. Note that the length of the string being processed doesn't change. When the same (or another page on the site) runs my PHP script again, I'll be able to recover this cookie without missing characters. I just have to remember to pass the cookie back through the same fix64() call I created, and from there I can decode it with the usual base64_decode(), followed by whatever other decryption in your scheme.

    There may be some setting I could make in PHP that allows base64 strings used in cookies to be transferred back to to PHP without corruption. In the mean time this works. The "+" may be a "legal" cookie value, but if you have any desire to be able to transmit such a string back to PHP (in my case via the $_COOKIE array), I'm suggesting re-processing to remove offending characters, and restore them after recovery. There are plenty of other "cookie safe" characters to choose from.

提交回复
热议问题