Fix _is_inside_docker() false positive on systemd cgroups v2 hosts (part of #886)#945
Open
4gust wants to merge 1 commit into
Open
Fix _is_inside_docker() false positive on systemd cgroups v2 hosts (part of #886)#9454gust wants to merge 1 commit into
_is_inside_docker() false positive on systemd cgroups v2 hosts (part of #886)#9454gust wants to merge 1 commit into
Conversation
On modern systemd hosts using cgroups v2, /proc/1/cgroup reports PID 1 in "/init.scope" rather than "/". The previous heuristic treated any non-"/" cgroup path as "inside docker", so the auth-code callback server bound to 0.0.0.0 (all interfaces) instead of 127.0.0.1 loopback on ordinary Linux hosts, contrary to RFC 8252 section 8.3. Detect only genuine container markers (/.dockerenv, /run/.containerenv, or docker/containerd/kubepods/lxc/libpod in the PID 1 cgroup path). Real Docker/Podman/Kubernetes containers are still detected, so the docker port-forwarding scenario is preserved. Adds unit tests for _is_inside_docker(). Part of #886. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens the Linux container-detection heuristic used by msal.oauth2cli.authcode.AuthCodeReceiver to choose the callback listener bind address, fixing a cgroups v2 / systemd false-positive that could cause the local OAuth2 redirect listener to bind to 0.0.0.0 on non-container hosts (exposing the loopback callback on the LAN).
Changes:
- Update
_is_inside_docker()to treat only runtime marker files (/.dockerenv,/run/.containerenv) or known container cgroup tokens as “inside a container”, avoiding cgroups v2/init.scopefalse positives. - Add unit tests for
_is_inside_docker()covering cgroups v1/v2 host cases, marker files, known container cgroup paths, and non-Linux behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
msal/oauth2cli/authcode.py |
Refines _is_inside_docker() to reduce false positives on systemd cgroups v2 hosts while preserving real container detection signals. |
tests/test_authcode.py |
Adds focused unit tests for the updated container-detection behavior, including regression coverage for the cgroups v2 /init.scope case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the container-detection false positive described in #886.
msal/oauth2cli/authcode.py::_is_inside_docker()decides whether the OAuth2 authorization-code callback server binds to0.0.0.0(all interfaces, when inside a container sodocker run -pcan reach it) or127.0.0.1(loopback only, on a normal host).The previous heuristic treated any
/proc/1/cgrouppath other than"/"as "inside docker". On modern systemd hosts using cgroups v2, PID 1 lives in/init.scope(not/), so ordinary Linux desktops/servers were misdetected as containers and the auth-code callback server was bound to0.0.0.0— listening on all network interfaces. That exposes the localhost OAuth callback (which receives authorization codes) to the local network, contrary to RFC 8252 §8.3.Change
_is_inside_docker()now returnsTrueonly for genuine container markers:/.dockerenv(Docker, incl. Docker Desktop on Mac) or/run/.containerenv(Podman), ordocker/containerd/kubepods/lxc/libpodtoken in PID 1's cgroup path.Real Docker/Podman/Kubernetes containers are still detected, so the legitimate
docker run -p 127.0.0.1:{port}:{port}port-forwarding scenario is preserved. Non-container hosts now correctly bind to loopback.Tests
Adds
TestIsInsideDockerintests/test_authcode.pycovering: systemd cgroups v2 host, cgroups v1 host,/.dockerenv,/run/.containerenv, docker cgroup path, kubepods cgroup path, and non-Linux. All 15 tests in the file pass.Scope / compatibility
Not included (deferred — needs maintainer input)
Issue #886 also requests IPv6 support for the callback listener (so
localhost→::1browsers succeed). That part involves a cross-platform design decision (bind::1vs. dual-stack::withIPV6_V6ONLY=0vs. running two servers) and is intentionally left out of this low-risk PR.Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com