Skip to content

Redact sensitive api creds before logging message in websocket api#169326

Merged
MartinHjelmare merged 7 commits intohome-assistant:devfrom
ch604:issue-169272
Apr 29, 2026
Merged

Redact sensitive api creds before logging message in websocket api#169326
MartinHjelmare merged 7 commits intohome-assistant:devfrom
ch604:issue-169272

Conversation

@ch604
Copy link
Copy Markdown
Contributor

@ch604 ch604 commented Apr 27, 2026

Breaking change

Proposed change

redact specific dict of keys from messages posted to error log by websocket_api

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings April 27, 2026 18:44
@ch604 ch604 requested a review from a team as a code owner April 27, 2026 18:44
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (websocket_api) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of websocket_api can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign websocket_api Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@ch604
Copy link
Copy Markdown
Contributor Author

ch604 commented Apr 27, 2026

perfect use case for the existing helper:

>>> REDACT_KEYS = {"access_token", "password", "api_password", "refresh_token", "token", "auth_token"}
>>> from homeassistant.helpers.redact import async_redact_data
>>> msg = {'type': 'auth', 'access_token': '1234567'}
>>> msg = async_redact_data(msg, REDACT_KEYS)
>>> msg
{'type': 'auth', 'access_token': '**REDACTED**'}

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent credential leakage by redacting sensitive keys from WebSocket API messages before they are written to logs (fixes #169272).

Changes:

  • Add a REDACT_KEYS set for common credential fields (e.g., access_token, refresh_token, password).
  • Redact incoming invalid commands before logging them.
  • Attempt to redact data logged when sending on a closed connection and when logging handler exceptions.

Comment thread homeassistant/components/websocket_api/connection.py
Comment thread homeassistant/components/websocket_api/connection.py
Copilot AI review requested due to automatic review settings April 27, 2026 19:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread tests/components/websocket_api/test_connection.py Outdated
Comment thread tests/components/websocket_api/test_connection.py Outdated
Copilot AI review requested due to automatic review settings April 27, 2026 19:47
@MartinHjelmare MartinHjelmare changed the title redact sensitive api creds before posting message Redact sensitive api creds before logging message in websocket api Apr 28, 2026
Comment thread tests/components/websocket_api/test_connection.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft April 28, 2026 02:50
@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@ch604 ch604 marked this pull request as ready for review April 28, 2026 13:41
@home-assistant home-assistant Bot requested a review from MartinHjelmare April 28, 2026 13:41
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MartinHjelmare MartinHjelmare merged commit 278f25e into home-assistant:dev Apr 29, 2026
48 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

websocket_api: "Received invalid command" log line includes access_token in plain text

4 participants