-
Notifications
You must be signed in to change notification settings - Fork 697
[security] fix(proxy): require auth for node management #4579
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
Open
Hinotoi-agent
wants to merge
1
commit into
InternLM:main
Choose a base branch
from
Hinotoi-agent:fix/proxy-node-management-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−2
Open
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
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
59 changes: 59 additions & 0 deletions
59
tests/test_lmdeploy/serve/test_authentication_middleware.py
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,59 @@ | ||
| import importlib.util | ||
| from pathlib import Path | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.testclient import TestClient | ||
|
|
||
|
|
||
| def _load_authentication_middleware(): | ||
| repo_root = Path(__file__).resolve().parents[3] | ||
| module_path = repo_root / 'lmdeploy' / 'serve' / 'utils' / 'server_utils.py' | ||
| spec = importlib.util.spec_from_file_location('server_utils_for_test', module_path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| assert spec.loader is not None | ||
| spec.loader.exec_module(module) | ||
| return module.AuthenticationMiddleware | ||
|
|
||
|
|
||
| AuthenticationMiddleware = _load_authentication_middleware() | ||
|
|
||
|
|
||
| def _make_client(): | ||
| app = FastAPI() | ||
|
|
||
| @app.get('/health') | ||
| def health(): | ||
| return {'ok': True} | ||
|
|
||
| @app.post('/nodes/add') | ||
| def add_node(): | ||
| return {'added': True} | ||
|
|
||
| @app.get('/nodes/status') | ||
| def node_status(): | ||
| return {'nodes': []} | ||
|
|
||
| @app.post('/v1/chat/completions') | ||
| def chat_completions(): | ||
| return {'ok': True} | ||
|
|
||
| app.add_middleware(AuthenticationMiddleware, tokens=['secret']) | ||
| return TestClient(app) | ||
|
|
||
|
|
||
| def test_auth_middleware_protects_node_management_routes(): | ||
| client = _make_client() | ||
|
|
||
| assert client.post('/nodes/add').status_code == 401 | ||
| assert client.get('/nodes/status').status_code == 401 | ||
|
|
||
| headers = {'Authorization': 'Bearer secret'} | ||
| assert client.post('/nodes/add', headers=headers).status_code == 200 | ||
| assert client.get('/nodes/status', headers=headers).status_code == 200 | ||
|
|
||
|
|
||
| def test_auth_middleware_keeps_passive_health_public(): | ||
| client = _make_client() | ||
|
|
||
| assert client.get('/health').status_code == 200 | ||
| assert client.post('/v1/chat/completions').status_code == 401 |
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.
These node management endpoints are only intended for internal communication between the api_server and the proxy. In that context, do we really need to enforce authentication on them?
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.
Thanks for raising this. I agree these endpoints are intended for api_server ↔ proxy coordination, but today that is only an architectural convention, not an enforced boundary.
The proxy defaults to binding on
0.0.0.0, and whenapi_keysare configured the public inference routes are protected while/nodes/*was still skipped by auth. If the proxy port is reachable, an unauthenticated caller can:/nodes/statusto learn the current node URLs/models;/nodes/removeor/nodes/terminate*to disrupt serving; and/nodes/addwith a non-emptystatus.models, whichNodeManager.add()accepts directly without first querying the backend.That last case lets an attacker register their own backend for an existing model and potentially receive subsequent inference requests. Since the proxy forwards the original request headers to the selected backend, that can also expose client request metadata/authorization headers to the malicious node.
So the issue is exploitable whenever the proxy listener is reachable outside the trusted internal network. If deployments enforce isolation externally (localhost/private listener/firewall/service mesh), the risk is reduced, but when operators configure
api_keyson a network-reachable proxy, leaving/nodes/*unauthenticated creates a separate unauthenticated management plane.This patch keeps unauthenticated behavior for deployments without
api_keys, and for authenticated deployments it makes the existing api_server registration path send the configured bearer token so the intended internal flow still works.