Description
Plaintext passwords and tokens can be read from files, backups, or browser storage. A script running on your site can read localStorage and sessionStorage.
Potential impact
A leaked token can grant access until it expires or is revoked. A reused password may expose accounts on other services.
Remediation
- Hash passwords with Argon2id, bcrypt, or scrypt. Keep reusable API keys and tokens in a server-side secret store when you must retain them. For encrypted storage, manage key access separately.
- Keep long-lived authentication tokens out of browser storage. For a session cookie, keep the value small and set
Secure,HttpOnly, and an appropriateSameSitepolicy. Treat the contents as readable even if the token is signed.
Examples
The bcrypt example stores only passwordHash through users, the application's storage layer. Check the cost and use the same input policy at sign-up and login. This library processes at most 72 bytes of a password.
Before
javascript
const fs = require("fs");
function save(req, res) {
fs.writeFileSync("/tmp/password.txt", req.body.password);
}
After
javascript
const bcrypt = require("bcrypt");
async function save(req, res) {
const passwordHash = await bcrypt.hash(req.body.password, 12);
await users.save({ passwordHash });
}