Skip to content

Fix hassio auth IndexError on Supervisor Unix socket requests#169911

Merged
agners merged 5 commits into
devfrom
fix-hassio-auth-unix-socket
May 6, 2026
Merged

Fix hassio auth IndexError on Supervisor Unix socket requests#169911
agners merged 5 commits into
devfrom
fix-hassio-auth-unix-socket

Conversation

@agners
Copy link
Copy Markdown
Member

@agners agners commented May 6, 2026

Breaking change

Proposed change

Requests over the Supervisor Unix socket (added in #163907) are authenticated by the http auth middleware as the Supervisor user, but have an empty peername on the transport. The hassio auth view's _check_access did a TCP-style peername[0] IP comparison and crashed with IndexError: string index out of range.

Skip the IP check when the request comes via the Supervisor Unix socket — the socket is the trust boundary and the existing user-ID check ensures only the Supervisor user can reach the endpoint. Also guard against a missing peername on TCP transports (reject rather than crash).

This is related to #163907.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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:

Requests over the Supervisor Unix socket (added in #163907) are
authenticated by the http auth middleware as the Supervisor user, but
have an empty `peername` on the transport. The hassio auth view's
`_check_access` did a TCP-style `peername[0]` IP comparison and
crashed with `IndexError: string index out of range`.

Skip the IP check when the request comes via the Supervisor Unix
socket — the socket is the trust boundary and the existing user-ID
check ensures only the Supervisor user can reach the endpoint. Also
guard against a missing peername on TCP transports (reject rather
than crash).
Copilot AI review requested due to automatic review settings May 6, 2026 12:56
@agners agners added the bugfix label May 6, 2026
@agners agners added this to the 2026.5.0 milestone May 6, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 6, 2026

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

Code owner commands

Code owners of hassio 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 hassio 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.

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 fixes an IndexError in the Hass.io auth view when requests arrive via the Supervisor Unix socket (where peername can be empty), by skipping the IP-based peer check for Unix-socket traffic and rejecting missing peername on non-Unix-socket requests.

Changes:

  • Skip the caller-IP validation for requests identified as coming over the Supervisor Unix socket.
  • Add a guard so missing/empty peername on non-Unix-socket requests is rejected instead of causing an index error.
  • Add regression tests covering Unix-socket (empty peername) and missing-peername scenarios.

Reviewed changes

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

File Description
homeassistant/components/hassio/auth.py Adjusts _check_access to bypass IP checks for Supervisor Unix socket requests and harden peername handling.
tests/components/hassio/test_auth.py Adds parametrized tests to ensure _check_access no longer crashes on empty/missing peername.

Comment thread homeassistant/components/hassio/auth.py
Comment on lines +50 to +56
if not is_supervisor_unix_socket_request(request):
hassio_ip = os.environ["SUPERVISOR"].split(":")[0]
assert request.transport
peername = request.transport.get_extra_info("peername")
if not peername or ip_address(peername[0]) != ip_address(hassio_ip):
_LOGGER.error("Invalid auth request from %s", request.remote)
raise HTTPUnauthorized
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if not is_supervisor_unix_socket_request(request):
hassio_ip = os.environ["SUPERVISOR"].split(":")[0]
assert request.transport
peername = request.transport.get_extra_info("peername")
if not peername or ip_address(peername[0]) != ip_address(hassio_ip):
_LOGGER.error("Invalid auth request from %s", request.remote)
raise HTTPUnauthorized
if not is_supervisor_unix_socket_request(request):
hassio_ip = os.environ["SUPERVISOR"].split(":")[0]
if (
not (transport := request.transport)
or not (peername := transport.get_extra_info("peername"))
or ip_address(peername[0]) != ip_address(hassio_ip)
):
_LOGGER.error("Invalid auth request from %s", request.remote)
raise HTTPUnauthorized

I'd overprefer a walrus operator here to patch out the assert. What do you think? :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that the assert is existing code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

True, merely spotted a potential improvement. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We use the same pattern (assert) also in other places in the hassio integration.

From what I understand that happens if the connection closed. I am not sure how realistic this is in this particular case, the close must have happened between the request coming in and processing here 🤔

I'd rather prefer to address all sites at once, if we decide to change how to handle request.transport being None.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That makes sense. I agree this does not need to block this PR, especially if the same pattern exists elsewhere.
If it's there purely to satisfy mypy, it should be fine. If we want to continuously check this in runtime, it would justify its own dedicated PR. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It got added with #74603 as a mypy defense, so I assume the latter.

@home-assistant home-assistant Bot marked this pull request as draft May 6, 2026 13:03
@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 6, 2026

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.

Comment thread tests/components/hassio/test_auth.py Outdated
Comment thread tests/components/hassio/test_auth.py Outdated

request = MagicMock()
request.transport.get_extra_info.return_value = peername
request.__getitem__.side_effect = lambda key: user if key is KEY_HASS_USER else None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider moving the side effect to a test parameter that is a lambda.

@frenck frenck modified the milestones: 2026.5.0, 2026.5.1 May 6, 2026
agners added 2 commits May 6, 2026 15:22
Address review feedback on the regression test:
- Use `@pytest.mark.usefixtures("hassio_stubs")` instead of taking it
  as an unused parameter (silences Pylance "not accessed").
- Split the parametrized test with an `if/else` branch into two
  separate tests — one per behavior — per CLAUDE.md guidance to
  avoid branching in tests.
- Extract the shared setup into a small helper.
Per @MartinHjelmare's review feedback:
- Merge the two split tests back into a single parametrized test
  using `contextlib.ExitStack as DefaultContext` for the success
  case and `pytest.raises(HTTPUnauthorized)` for the failure case,
  removing the conditional branch in the test body.
- Move the `__getitem__` side effect into a parameter (lambda).
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 1 comment.

Comment thread tests/components/hassio/test_auth.py Outdated
Per Copilot review feedback: short string identity comparisons rely
on CPython interning and aren't guaranteed across implementations.
@agners agners requested a review from MartinHjelmare May 6, 2026 13:40
@agners agners marked this pull request as ready for review May 6, 2026 13:40
Copilot AI review requested due to automatic review settings May 6, 2026 13:40
@home-assistant home-assistant Bot requested a review from erwindouna May 6, 2026 13:40
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 no new comments.

Comment thread tests/components/hassio/test_auth.py Outdated
(
None,
False,
lambda user, key: user if key == KEY_HASS_USER else None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought the condition in the lambda matched the parameter cases. Maybe I misunderstood that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the lambda is always the same we don't need to have it as a test parameter. But I was thinking we could remove the condition in the lambda and just return the correct thing depending on the test case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah we always check with Supervisor user. I guess we could argue testing with another user for completeness. But the lambda stays the same anyways, it's about which key returns the username.

But yeah lambda doesn't make sense in the parameter list, I'll remove it again.

Copy link
Copy Markdown
Member Author

@agners agners May 6, 2026

Choose a reason for hiding this comment

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

I guess we could argue testing with another user for completeness.

Hm, makes the test more complex, and it's (test) feature creep. This is about Supervisor user only, but covering Unix domain socket. So I left it at that.

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.

I'll approve preemptively here. The above question can be solved as needed or not.

The lambda was identical across both parameter rows, so it doesn't
need to be a parameter. `_check_access` only ever subscripts the
request with `KEY_HASS_USER`, so a single
`__getitem__.return_value = user` is sufficient and removes the
conditional from the mock side effect.
@agners agners dismissed erwindouna’s stale review May 6, 2026 15:12

Agreed on leave as is for this PR.

@agners
Copy link
Copy Markdown
Member Author

agners commented May 6, 2026

Test failure unrelated.

@agners agners merged commit 38634dd into dev May 6, 2026
43 of 44 checks passed
@agners agners deleted the fix-hassio-auth-unix-socket branch May 6, 2026 15:48
@github-actions github-actions Bot locked and limited conversation to collaborators May 7, 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.

5 participants