-
Notifications
You must be signed in to change notification settings - Fork 0
Require operator token for gRPC pause and resume #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
homerquan
merged 1 commit into
main
from
codex/propose-fix-for-grpc-vulnerability-371cem
May 18, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| defmodule MirrorNeuron.Grpc.Auth do | ||
| @moduledoc false | ||
|
|
||
| import Bitwise | ||
|
|
||
| @operator_token_env "MN_GRPC_OPERATOR_TOKEN" | ||
| @token_headers [ | ||
| "authorization", | ||
| "x-mirror-neuron-operator-token", | ||
| "mn-operator-token" | ||
| ] | ||
|
|
||
| def authorize_operator!(stream) do | ||
| case operator_token() do | ||
| {:ok, expected_token} -> | ||
| if authorized?(stream, expected_token) do | ||
| :ok | ||
| else | ||
| raise GRPC.RPCError, | ||
| status: GRPC.Status.unauthenticated(), | ||
| message: "operator token is required for this RPC" | ||
| end | ||
|
|
||
| :error -> | ||
| raise GRPC.RPCError, | ||
| status: GRPC.Status.unauthenticated(), | ||
| message: "#{@operator_token_env} must be set before operator control RPCs can be used" | ||
| end | ||
| end | ||
|
|
||
| def authorized?(stream, expected_token) when is_binary(expected_token) do | ||
| expected_token = String.trim(expected_token) | ||
|
|
||
| expected_token != "" and | ||
| stream | ||
| |> metadata() | ||
| |> token_candidates() | ||
| |> Enum.any?(&secure_compare(&1, expected_token)) | ||
| end | ||
|
|
||
| def authorized?(_stream, _expected_token), do: false | ||
|
|
||
| defp operator_token do | ||
| case System.get_env(@operator_token_env, "") |> String.trim() do | ||
| "" -> :error | ||
| token -> {:ok, token} | ||
| end | ||
| end | ||
|
|
||
| defp metadata(stream) when is_map(stream) do | ||
| stream_map = if Map.has_key?(stream, :__struct__), do: Map.from_struct(stream), else: stream | ||
|
|
||
| headers = | ||
| stream_map | ||
| |> adapter_headers() | ||
| |> Kernel.++( | ||
| [:http_request_headers, :headers, :metadata, :request_headers] | ||
| |> Enum.flat_map(&metadata_values(Map.get(stream_map, &1))) | ||
| ) | ||
|
|
||
| Enum.reduce(headers, %{}, fn {key, value}, acc -> Map.put(acc, normalize_key(key), value) end) | ||
| end | ||
|
|
||
| defp metadata(_stream), do: %{} | ||
|
|
||
| defp adapter_headers(%{adapter: adapter, payload: payload}) when is_atom(adapter) do | ||
| if match?({:module, ^adapter}, Code.ensure_loaded(adapter)) and | ||
| function_exported?(adapter, :get_headers, 1) do | ||
| adapter.get_headers(payload) |> metadata_values() | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| defp adapter_headers(_stream_map), do: [] | ||
|
|
||
| defp metadata_values(nil), do: [] | ||
| defp metadata_values(metadata) when is_map(metadata), do: Map.to_list(metadata) | ||
| defp metadata_values(metadata) when is_list(metadata), do: metadata | ||
| defp metadata_values(_metadata), do: [] | ||
|
|
||
| defp token_candidates(metadata) do | ||
| @token_headers | ||
| |> Enum.flat_map(fn header -> metadata |> Map.get(header) |> header_values() end) | ||
| |> Enum.flat_map(&authorization_values/1) | ||
| end | ||
|
|
||
| defp header_values(nil), do: [] | ||
| defp header_values(values) when is_list(values), do: values | ||
| defp header_values(value), do: [value] | ||
|
|
||
| defp authorization_values(value) when is_binary(value) do | ||
| value = String.trim(value) | ||
|
|
||
| case String.split(value, " ", parts: 2) do | ||
| [scheme, token] -> | ||
| if String.downcase(scheme) == "bearer", do: [String.trim(token)], else: [value] | ||
|
|
||
| _ -> | ||
| [value] | ||
| end | ||
| end | ||
|
|
||
| defp authorization_values(_value), do: [] | ||
|
|
||
| defp normalize_key(key) when is_atom(key), do: key |> Atom.to_string() |> String.downcase() | ||
| defp normalize_key(key) when is_binary(key), do: String.downcase(key) | ||
| defp normalize_key(key), do: key |> to_string() |> String.downcase() | ||
|
|
||
| defp secure_compare(left, right) when is_binary(left) and is_binary(right) do | ||
| byte_size(left) == byte_size(right) and compare_bytes(left, right) == 0 | ||
| end | ||
|
|
||
| defp secure_compare(_left, _right), do: false | ||
|
|
||
| defp compare_bytes(left, right), do: compare_bytes(left, right, 0) | ||
| defp compare_bytes(<<>>, <<>>, acc), do: acc | ||
|
|
||
| defp compare_bytes(<<left, left_rest::binary>>, <<right, right_rest::binary>>, acc) do | ||
| compare_bytes(left_rest, right_rest, bor(acc, bxor(left, right))) | ||
| end | ||
| end |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| defmodule MirrorNeuron.Grpc.AuthTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias MirrorNeuron.Grpc.Auth | ||
|
|
||
| defmodule Stream do | ||
| defstruct [:adapter, :payload, :http_request_headers, :headers, :metadata, :request_headers] | ||
| end | ||
|
|
||
| defmodule Adapter do | ||
| def get_headers(:payload), do: %{"authorization" => "Bearer secret-token"} | ||
| end | ||
|
|
||
| test "authorizes bearer tokens from request headers" do | ||
| stream = %Stream{headers: %{"authorization" => "Bearer secret-token"}} | ||
|
|
||
| assert Auth.authorized?(stream, "secret-token") | ||
| end | ||
|
|
||
| test "authorizes tokens from grpc adapter request headers" do | ||
| stream = %Stream{adapter: Adapter, payload: :payload} | ||
|
|
||
| assert Auth.authorized?(stream, "secret-token") | ||
| end | ||
|
|
||
| test "authorizes tokens from grpc http request headers" do | ||
| stream = %Stream{http_request_headers: %{"authorization" => "Bearer secret-token"}} | ||
|
|
||
| assert Auth.authorized?(stream, "secret-token") | ||
| end | ||
|
|
||
| test "authorizes explicit operator token metadata" do | ||
| stream = %Stream{metadata: [{"x-mirror-neuron-operator-token", "secret-token"}]} | ||
|
|
||
| assert Auth.authorized?(stream, "secret-token") | ||
| end | ||
|
|
||
| test "rejects missing, blank, and mismatched tokens" do | ||
| refute Auth.authorized?(%Stream{headers: %{}}, "secret-token") | ||
|
|
||
| refute Auth.authorized?( | ||
| %Stream{headers: %{"authorization" => "Bearer wrong"}}, | ||
| "secret-token" | ||
| ) | ||
|
|
||
| refute Auth.authorized?(%Stream{headers: %{"authorization" => "Bearer secret-token"}}, "") | ||
| end | ||
| end |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
MN_GRPC_OPERATOR_TOKENis set and the gRPC listener is reachable, this guard only protectsPauseJob/ResumeJob; in the same serverCancelJobandClearJobsstill callMirrorNeuron.cancel/1andMonitor.clear_jobs/0withoutauthorize_operator!/1, andClusterServer.remove_node/1is likewise unguarded. Those RPCs are at least as destructive as pause/resume, so the new token gives operators a false sense that control RPCs are protected while unauthenticated clients can still cancel or delete job state.Useful? React with 👍 / 👎.