---
title: "Is a user's password sitting in your logs? What must stay out of them"
description: "Logs end up in central stores, archives and backups. The OWASP cheat sheet says passwords, tokens, session IDs and encryption keys should not go into them directly: remove, mask or hash them."
url: https://ihsanalapsi.dev/articles/logs-sensitive-data
lang: en
date: 2026-11-16
updated: 2026-11-16
tags: ["App security", "Privacy", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Is a user's password sitting in your logs? What must stay out of them

You write `console.log(req.body)` to find out why sign-in keeps failing, the bug gets fixed, and the line stays. From that day on, every password typed into the login form is saved in the log as plain text. In this article you find out what must not go into your logs, and how to hide it.

## Why is a log a risky place for passwords?

Because a log does not stay in one place, and every copy of it holds the password as plain text.

[The OWASP Logging cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html) says log data may be held on end devices, at intermediate points, in centralized repositories, and in archives and backups. A password that is hashed in the database and written in full in the log is no longer protected.

## What should never be written to your logs?

The same cheat sheet has a list of things that should not be written to the log directly, and should be removed, masked, sanitized, hashed or encrypted instead. The most important ones for a web app:

1. Passwords typed at sign-in.
2. Access tokens.
3. Session IDs. If you need to follow a session, the cheat sheet suggests a hashed value in place of the ID.
4. Database connection strings.
5. Encryption keys and other primary secrets.
6. Payment card and bank account data, and sensitive personal data.

## How do you hide these values before they reach the log?

The easiest way is to never write the whole request, and to pass everything through a function that covers the sensitive fields before it reaches the logger:

```javascript
const HIDDEN = ['password', 'token', 'authorization', 'cookie', 'secret'];

function forLog(data) {
  const out = {};
  for (const [key, value] of Object.entries(data)) {
    out[key] = HIDDEN.some((word) => key.toLowerCase().includes(word))
      ? '[hidden]'
      : value;
  }
  return out;
}

logger.info({ event: 'login_failed', body: forLog(req.body) });
```

The list here is an example. Search your code for the field names you actually use, and add them.

## How do you protect the log itself?

The cheat sheet gives three rules for the log after it is written:

- Clean every value that comes from the user of new line characters (CR and LF) and delimiters, so nobody can add a fake line by injection. The cheat sheet recommends a structured format such as JSON instead of plain text.
- Keep read access to the log limited, review it from time to time, and record every access to the log.
- Have a way to detect if a line was changed or deleted.

The same cheat sheet says sign-in attempts, successful and failed, should always be logged. So log that a failed attempt happened, but without the password that was typed.

## Where do you start now?

The lesson for you as a developer: a log is a second copy of your data, and it needs protection on the same level as the database.

1. Search your code for `console.log(req.body)`, `console.log(req.headers)` and any line that writes the whole request.
2. Pass those values through a function like `forLog` before they reach the log.
3. Search your existing logs for the word `password`. If you find any, delete them from the old copies and rotate any secrets that showed up.

If you find a token or a key in the log, [the article on rotating secrets after the Railway breach](/articles/railway-breach-env-vars-rotate-secrets) shows why you have to change it right away. For the passwords themselves: [the Argon2id article](/articles/password-hashing-argon2id). And if you want someone to test your whole platform, there is [Muhassan](/muhassan).

What is the last log line you wrote that holds the whole request?

## FAQ

**If I want to follow a user's session in the log, what do I write instead of the session ID?**

A hash of the session ID. That way you link the events of one session together, and nobody can take the ID from the log and use it.

**Should I log failed login attempts?**

Yes, both successful and failed ones. Failed ones are an early sign of a password attack. Log the event, not the password that was typed.

**Why is JSON better than a plain text line in the log?**

Because every value stays in its own field, so a new line inside a user's value does not create a fake event. The OWASP cheat sheet recommends a structured format such as JSON.
