Skip to content

documentCookies

Reports uses of document.cookie which can be error-prone and has security implications.

✅ This rule is included in the browser logical preset.

Direct use of document.cookie for reading and writing cookies is error-prone and has security implications. The document.cookie API requires manual string parsing and formatting, which is tedious and can lead to bugs with encoding, expiration dates, and security flags. Modern cookie management should be performed through dedicated libraries or the Cookie Store API, which provide better ergonomics and security.

const sessionId = document.cookie
.split("; ")
.find((row) => row.startsWith("session="))
?.split("=")[1];
document.cookie = "theme=dark";
document.cookie = `user=${userId}; expires=${expiryDate.toUTCString()}`;

This rule is not configurable.

If you need to support older browsers that do not have access to the Cookie Store API or cookie management libraries, you might need to use document.cookie directly. However, for most modern applications, prefer using proper cookie management abstractions.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.