Skip to content

[cherry-pick][2.60.0][core][sandbox] Require the API token on the gRPC facade (#66675) - #66897

Merged
sai-miduthuri merged 1 commit into
releases/2.60.0from
sai-miduthuri/cherry-pick-66675-2.60.0
Oct 9, 2026
Merged

sai-miduthuri merged 1 commit into
releases/2.60.0from
sai-miduthuri/cherry-pick-66675-2.60.0

Conversation

@sai-miduthuri

@sai-miduthuri sai-miduthuri commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Cherry-pick of #66675 (master commit 4f967ab) onto releases/2.60.0.

Why

Cherry-pick details

Applied cleanly with git cherry-pick -x. The diff is byte-identical to the master commit: the same 10 files, 726 insertions and 29 deletions, with identical hunks, context lines and offsets.

No prerequisite commits are missing:

  • No commit on master between the branch cut and [core][sandbox] Require the API token on the gRPC facade #66675 touched python/ray/experimental/sandbox/ or doc/source/ray-core/sandboxes.md. Neither did any commit on this branch after the cut.
  • After the pick, the whole python/ray/experimental/sandbox/ package and doc/source/ray-core/sandboxes.md are identical to master at 4f967ab.
  • Outside the sandbox package, the picked code imports only ray.util.annotations, which is identical on both branches.

Not a duplicate: no existing PR against releases/2.60.0 references #66675.

Testing

test_grpc_facade.py and test_modal_client.py skip in CI because grpclib and modal aren't in the CI image, so I ran them locally.

Environment: Linux, Python 3.10, grpclib 0.4.9 and modal 1.5.5 (the versions #66675 used). Other test dependencies were pinned from python/requirements_compiled.txt. Ray was the CI-built ray-2.60.0 wheel for this PR's parent commit (b7a4529), with the wheel's ray/experimental/sandbox/ replaced by this branch's copy.

Command Code under test Result
pytest .../http/tests/test_grpc_facade.py .../http/tests/test_modal_client.py this PR 58 passed (52 + 6), 0 skipped
pytest .../http/tests this PR 136 passed, 2 skipped (test_http_integration.py, TEST_SANDBOX unset)
Same two files, the branch's tests before this pick releases/2.60.0 24 passed
This PR's two test files releases/2.60.0 (before this pick) 30 failed, 28 passed: every token/auth test fails, as expected

test_image_utils.py::test_seeding_stays_inside_the_tree and the 2 skipped test_http_integration.py cases run in CI's :ray: core: sandbox tests job, which sets TEST_SANDBOX=1.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with Claude Code

## Description

The Ray Sandbox gRPC facade
(`ray.experimental.sandbox.http.grpc_facade`) served sandbox create,
exec, and file read/write to any client that could reach its port.
Unlike the REST app, it never read `RAY_SANDBOX_API_TOKEN`, so even an
operator who wanted to lock it down couldn't, and the documented
quickstart binds `--host 0.0.0.0`.

With this PR:

- **The token is required on every RPC.** When `RAY_SANDBOX_API_TOKEN`
is set, a call that doesn't present it fails with `UNAUTHENTICATED`
before its handler reads the request.
- A client presents the token the way the client SDK already sends
credentials: as its token secret (`x-modal-token-secret`, from
`MODAL_TOKEN_SECRET`), or as `authorization: Bearer <token>`, the REST
app's form, which `MODAL_OVERRIDE_HEADERS` can also send.
- `TaskGetCommandRouterAccess` hands out the token as the command
router's credential. The SDK then sends it as a bearer token on every
exec and file call, so an unmodified client only needs
`MODAL_TOKEN_SECRET` set.
- A bearer-only check wouldn't work with the SDK: its control-plane
calls carry the token secret, and its router calls carry only the
credential that `TaskGetCommandRouterAccess` returned.
- **The check is bound to the servicers, not to one server.**
`_TokenGate` wraps the handler table (`__mapping__`) that
`grpclib.server.Server` dispatches through. So every server built from
`build_servicers` is gated, including RPCs added later, such as #66359's
`TaskSetNetworkAccess`. Without a token the table is returned unchanged.
- **The CLI fails closed.** Without a token, `python -m
ray.experimental.sandbox.http.grpc_facade` refuses a non-loopback
`--host` unless `--allow-unauthenticated` is passed.
- Keeping the facade on a private network isn't enough on its own. The
facade's sandboxes get open egress by default and can reach any address
their node can, including the facade's. So a tokenless facade on a
network address would also serve the code running inside them.
- Loopback binds stay tokenless, since `--disable-host-loopback` keeps
sandboxes off the node's loopback.
- **Docs.** The gRPC section now covers:
  - the token;
- the trust model: one token controls every sandbox in the namespace, as
with the REST token, with no per-client isolation;
  - the TLS requirement;
- a fix for the stale network bullet: allowlists are refused, not
widened.
- **REST.** `require_bearer_token` compares bytes, so a non-ASCII
`Authorization` header gets a 401 instead of a `TypeError` and a 500.

**Behavior change:** a tokenless facade started with a non-loopback
`--host`, as the old quickstart did, now exits with an error that names
`RAY_SANDBOX_API_TOKEN` and `--allow-unauthenticated`.

**This PR also fixes the failing `:ray: core: sandbox tests` job** (for
example premerge build 74278 and microcheck build 55110).
- `test_image_utils.py::test_seeding_stays_inside_the_tree` linked the
fake image's `/tmp` to an absolute path under pytest's `tmp_path`, which
is itself under `/tmp` in CI.
- From the image's point of view that's a symlink loop.
`_resolve_in_root` correctly reports it as `ELOOP`, so nothing was
seeded.
- The test now gives `tmp` its own tree with a relative link. The
product code is unchanged.
- The test only passed on macOS because `tmp_path` lives under
`/private/var/folders` there.

## Related issues

Related to #65839 (the facade) and #65352 (sandboxing umbrella).

Not a duplicate: no open PR or issue covers facade authentication. I
searched open PRs and issues for `grpc_facade`, `RAY_SANDBOX_API_TOKEN`,
and sandbox authentication.

## Additional information

**Tests run.** Environment: macOS, Python 3.12, the ray 2.58 wheel with
this branch's sandbox package overlaid, grpclib 0.4.9.

| Command | Result |
| --- | --- |
| `pytest --import-mode=importlib
python/ray/experimental/sandbox/http/tests` | 130 passed, 8 skipped
(modal not installed, `TEST_SANDBOX` unset) |
| The same, with `RAY_SANDBOX_API_TOKEN=ambient` exported | 130 passed,
8 skipped |
| `uv run --with modal==1.5.5 pytest
.../http/tests/test_modal_client.py` | 6 passed |
| `uv run --with modal==1.5.1 pytest
.../http/tests/test_modal_client.py` | 5 passed, 1 skipped (1.5.1 can't
send custom headers) |
| The new auth tests against `origin/master`'s `grpc_facade.py` | 29
failed, as expected |
| `test_image_utils.py::test_seeding_stays_inside_the_tree`, with the
default `tmp_path` and with `tmp_path` under `/tmp` | Passed both ways
(the old test fails with `tmp_path` under `/tmp`) |
| `python -m ray.experimental.sandbox.http.grpc_facade --host 0.0.0.0`
without a token | Exit 2, before connecting to the cluster |
| Unauthenticated create path replayed against a local Ray cluster |
Anonymous and wrong-token `AppGetOrCreate`, `ImageGetOrCreate`, and
`SandboxCreate` return `UNAUTHENTICATED` and create no actor. With the
token, a `SandboxHost` actor is created and then terminated. |
| `pre-commit` (ruff, pydoclint, docstyle, import order), black 22.10.0,
API param coverage check | Pass |

`test_grpc_facade.py` and `test_modal_client.py` skip in CI because
grpclib and modal aren't in the CI image, so the auth tests above ran
locally only.

AI assistance was used to write this change.

Signed-off-by: xyuzh <[email protected]>
Co-authored-by: Philipp Moritz <[email protected]>
(cherry picked from commit 4f967ab)
Signed-off-by: Sai Miduthuri <[email protected]>
@sai-miduthuri
sai-miduthuri requested review from a team as code owners October 9, 2026 18:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces token-based authentication for the Ray Sandbox gRPC facade, aligning its security model with the REST API. When the RAY_SANDBOX_API_TOKEN environment variable is set, all RPCs on both the control and exec planes require the token, which can be presented as a token secret or a bearer token. By default, the CLI refuses to serve non-loopback addresses without a token unless the new --allow-unauthenticated flag is passed. Additionally, the REST API's token comparison was updated to handle non-ASCII headers gracefully by comparing them as bytes. Corresponding unit and integration tests have been added to verify these authentication mechanisms and prevent environment variable leaks. There are no review comments, so I have no feedback to provide.

@sai-miduthuri sai-miduthuri added the go add ONLY when ready to merge, run all tests label Oct 9, 2026
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core release-test release test labels Oct 9, 2026
@xyuzh

xyuzh commented Oct 9, 2026

Copy link
Copy Markdown
Member

LGTM

@sai-miduthuri
sai-miduthuri merged commit 4fc1830 into releases/2.60.0 Oct 9, 2026
6 checks passed
@sai-miduthuri
sai-miduthuri deleted the sai-miduthuri/cherry-pick-66675-2.60.0 branch October 9, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests release-test release test sandbox

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants