# Secrets

A secret is anything that lets someone act as you or as your users, such as a token, a password or a private key. Keep it in the user's config entry or in GitHub's encrypted secrets, and out of the code, the logs and diagnostics downloads.

Source: https://ha-barkley.github.io/security/controls/secrets/

From HASC, the Home Assistant Security Controls for integrations, cards and apps. A community catalog, not an official Home Assistant or Open Home Foundation project.

Each control has an ID, cited as HASC-CI-4, and a level from 1 to 3. "Level marker" means anyone can check the control from outside the project, and levels are graded on those controls only.

> **From the 2026 sweep.** Gitleaks found something that looks like a key in 907 apps, mostly in tests and docs. 58 have something that looks like a key in the code they ship, and most of them (41 of 58) are a vendor's own client key or certificate, the same for every user. The [Marketplace report](https://ha-barkley.github.io/marketplace/2026/keys-in-the-code/) has more on this.

## Controls

### SEC-1: Ask users for credentials in the config flow

*Level 1 · Applies to integrations*

**Ask users for credentials in the config flow,** with a password field, and read them from the config entry. Don't hard-code your own account's keys into the app.

`config_flow.py`

```
from homeassistant.helpers.selector import (
    TextSelector, TextSelectorConfig, TextSelectorType,
)

STEP_USER = vol.Schema({
    vol.Required(CONF_USERNAME): str,
    vol.Required(CONF_PASSWORD): TextSelector(
        TextSelectorConfig(type=TextSelectorType.PASSWORD)
    ),
})
```

### SEC-2: Redact diagnostics and keep secrets out of logs

*Level 1 · Applies to integrations*

Users paste diagnostics into public issues.

`diagnostics.py`

```
from homeassistant.components.diagnostics import async_redact_data

TO_REDACT = {CONF_PASSWORD, CONF_API_KEY, CONF_TOKEN, "serial_number"}

async def async_get_config_entry_diagnostics(hass, entry):
    return {"entry": async_redact_data(entry.as_dict(), TO_REDACT)}
```

### SEC-3: Ignore local secret files

*Withdrawn*

Withdrawn on November 27, 2026, because two other controls cover it: [REPO-3](https://ha-barkley.github.io/security/controls/repository/#repo-3) ignores secret files in `.gitignore`, and [CI-3](https://ha-barkley.github.io/security/controls/ci/#ci-3) turns on push protection.

### SEC-4: In CI, prefer no secret at all

*Level 2*

Use the built-in `GITHUB_TOKEN` with the narrowest permissions, and trusted publishing (OpenID Connect) to publish to PyPI or npm instead of a long-lived token.

### SEC-5: If a secret leaks, revoke it first

*Level 1*

Removing it from the history doesn't help, because clones, forks and caches already have a copy. Issue a new key, then clean up the history.

### Check it worked

Download your integration's diagnostics with a real config entry and search the file for your password and token. Neither should appear.
