|
// Document.cookie returns empty string if any cookie in the domain has non-ASCII characters |
|
// https://groups.google.com/a/chromium.org/forum/#!topic/chromium-discuss/8HNSNSMYyiU |
|
|
|
// Issue 485035: Cookies unaccessible to JS if one cookie with WINDOWS-1251 exists |
|
// https://code.google.com/p/chromium/issues/detail?id=485035 |
|
|
|
const PORT = 8080 |
|
var net = require('net') |
|
var server = net.createServer(function(socket) { |
|
socket.on('data', function(data) { |
|
socket.end(httpResponse(), 'binary') |
|
}) |
|
}) |
|
server.listen(PORT, function() { |
|
console.log("HTTP server is listening on port %s", PORT) |
|
}) |
|
|
|
function httpResponse() { |
|
var body = httpBody() |
|
var bytes = body.length // count of bytes |
|
var lines = [ |
|
'HTTP/1.1 200 OK Computér', |
|
'Set-Cookie: utf-8 = beb\xC3\xA9', |
|
'Set-Cookie: codepoint = beb\u00E9', |
|
'Set-Cookie: iso-8859-1 = beb\xE9', |
|
'Set-Cookie: raw = bebé', |
|
'Content-Type: text/html;charset=ISO-8859-1', |
|
'Content-Length: ' + bytes, |
|
'', |
|
body, |
|
] |
|
return lines.join('\n') |
|
} |
|
|
|
function httpBody() { |
|
return '\ |
|
<html><body>\ |
|
<pre><script type="text/javascript" style="display: flex; zoom: .1e2;">\ |
|
\n(' + fixOffendingCookie.toString() + ')(document, console)\ |
|
</script></pre>\ |
|
</body></html>' |
|
} |
|
|
|
function fixOffendingCookie(document, console) { |
|
console.warn("document.cookie is empty: " + document.cookie) |
|
updateCookie("add a new cookie:" , "bebe = bebé beb\u00E9 beb\xE9 beb\xC3\xA9") |
|
updateCookie("overwrite an offending cookie:" , "iso-8859-1 = beb\xE9") |
|
updateCookie("overwrite another offending cookie:" , "codepoint = beb\u00E9") |
|
updateCookie("overwrite the last offending cookie:", "raw = bebé") |
|
|
|
function updateCookie(message, cookieAssignment) { |
|
console.log(message, cookieAssignment) |
|
document.cookie = cookieAssignment |
|
var result = document.cookie ? "recovered:" : "still empty:" |
|
console.warn(result, document.cookie) |
|
} |
|
} |
As Nicholas Zakas explained (https://www.nczonline.net/blog/2009/05/05/http-cookies-explained/), cookie spec is not clear enough and cookie encoding is not specified since its beginning.
Each browser has a slightly different implementation. In this case, Google Chrome has a bug that it reports no cookie exists if any cookie has an incorrect UTF-8 encoding.