-
Notifications
You must be signed in to change notification settings - Fork 36
fix(llm-gateway): reject priority header #341
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
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
44 changes: 44 additions & 0 deletions
44
src/invocation-plane-services/llm-api-gateway/api/priority_guard.go
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,44 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| echo "github.com/labstack/echo/v4" | ||
| ) | ||
|
|
||
| const headerPriority = "X-Priority" | ||
|
|
||
| // rejectClientSuppliedPriority rejects requests that arrive with a | ||
| // client-supplied X-Priority header. | ||
| // Registered on the LLM route group, not globally. The check fires on header presence | ||
| // rather than value so a present-but-empty X-Priority (which Header.Get cannot | ||
| // distinguish from absent) is rejected too. | ||
| func rejectClientSuppliedPriority(next echo.HandlerFunc) echo.HandlerFunc { | ||
| return func(ec echo.Context) error { | ||
| if len(ec.Request().Header.Values(headerPriority)) > 0 { | ||
| return echo.NewHTTPError( | ||
| http.StatusBadRequest, | ||
| fmt.Sprintf("%s is a reserved header and cannot be set by the client", headerPriority), | ||
| ) | ||
| } | ||
| return next(ec) | ||
| } | ||
| } |
179 changes: 179 additions & 0 deletions
179
src/invocation-plane-services/llm-api-gateway/api/priority_guard_test.go
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,179 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| echo "github.com/labstack/echo/v4" | ||
|
|
||
| "github.com/NVIDIA/nvcf/src/invocation-plane-services/llm-gateway/config" | ||
| "github.com/NVIDIA/nvcf/src/invocation-plane-services/llm-gateway/nvcf" | ||
| ) | ||
|
|
||
| // newPriorityGuardTestServer wires the production route registration so these | ||
| // tests fail if the guard is ever dropped from the LLM route group. | ||
| func newPriorityGuardTestServer(authClient InvocationAuthClient) *echo.Echo { | ||
| cfg := config.Default() | ||
| e := echo.New() | ||
| e.Use(NewContextMiddleware(cfg)) | ||
| e.Use(NewNVCFAuthMiddleware(authClient)) | ||
| RegisterRoutes(e, NewHandlers(cfg, nil, nil)) | ||
| return e | ||
| } | ||
|
|
||
| func TestLLMRoutesRejectClientSuppliedPriority(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| paths := []string{"/v1/chat/completions", "/v1/responses", "/v1/embeddings"} | ||
| values := []struct { | ||
| name string | ||
| priority string | ||
| }{ | ||
| {name: "explicit value", priority: "0"}, | ||
| {name: "empty value", priority: ""}, | ||
| } | ||
|
|
||
| for _, path := range paths { | ||
| for _, value := range values { | ||
| t.Run(path+" "+value.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| authClient := &stubInvocationAuthClient{ | ||
| authResponse: &nvcf.InvocationAuthResponse{ | ||
| RoutingKey: "fn-chat", | ||
| ClientAuthID: "subject-123", | ||
| AuthContext: map[string]string{"ncaId": "nca-456"}, | ||
| RateLimitKey: "nca-456", | ||
| }, | ||
| } | ||
|
|
||
| e := newPriorityGuardTestServer(authClient) | ||
|
|
||
| req := httptest.NewRequest( | ||
| http.MethodPost, | ||
| path, | ||
| strings.NewReader(`{"model":"fn-chat/company-name/model-name","messages":[{"role":"user","content":"hello"}]}`), | ||
| ) | ||
| req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
| req.Header.Set(echo.HeaderAuthorization, "Bearer sk-live") | ||
| req.Header.Set(headerPriority, value.priority) | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| e.ServeHTTP(rec, req) | ||
|
|
||
| if rec.Code != http.StatusBadRequest { | ||
| t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusBadRequest, rec.Body.String()) | ||
| } | ||
| if !strings.Contains(rec.Body.String(), "reserved header") { | ||
| t.Fatalf("body = %q, want the reserved-header rejection message", rec.Body.String()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The guard is route middleware on the LLM group, so it runs after the | ||
| // global auth middleware: the reject does not skip authentication. | ||
| t.Run("auth runs before the reject", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| authClient := &stubInvocationAuthClient{ | ||
| authResponse: &nvcf.InvocationAuthResponse{ | ||
| RoutingKey: "fn-chat", | ||
| ClientAuthID: "subject-123", | ||
| AuthContext: map[string]string{"ncaId": "nca-456"}, | ||
| RateLimitKey: "nca-456", | ||
| }, | ||
| } | ||
|
|
||
| e := newPriorityGuardTestServer(authClient) | ||
|
|
||
| req := httptest.NewRequest( | ||
| http.MethodPost, | ||
| "/v1/chat/completions", | ||
| strings.NewReader(`{"model":"fn-chat/company-name/model-name","messages":[{"role":"user","content":"hello"}]}`), | ||
| ) | ||
| req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
| req.Header.Set(echo.HeaderAuthorization, "Bearer sk-live") | ||
| req.Header.Set(headerPriority, "0") | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| e.ServeHTTP(rec, req) | ||
|
|
||
| if rec.Code != http.StatusBadRequest { | ||
| t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusBadRequest, rec.Body.String()) | ||
| } | ||
| if !strings.Contains(rec.Body.String(), "reserved header") { | ||
| t.Fatalf("body = %q, want the reserved-header rejection message", rec.Body.String()) | ||
| } | ||
| if authClient.authorizeCalls != 1 { | ||
| t.Fatalf("authorize calls = %d, want 1", authClient.authorizeCalls) | ||
| } | ||
| }) | ||
|
|
||
| // The guard is scoped to the LLM route group; non-proxied routes such as | ||
| // the health endpoints accept requests regardless of X-Priority. | ||
| t.Run("healthz not rejected", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| e := newPriorityGuardTestServer(&stubInvocationAuthClient{}) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/healthz", nil) | ||
| req.Header.Set(headerPriority, "1") | ||
| rec := httptest.NewRecorder() | ||
|
|
||
| e.ServeHTTP(rec, req) | ||
|
|
||
| if rec.Code != http.StatusOK { | ||
| t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusOK, rec.Body.String()) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // A request without X-Priority must pass the guard untouched and reach the next | ||
| // handler. Positive control: without this, a guard that rejected unconditionally | ||
| // would still satisfy every rejection case above while breaking all legitimate | ||
| // LLM traffic. The guard runs after authentication in the wired server, so its | ||
| // only remaining job on a clean request is to forward it, which this asserts | ||
| // directly rather than driving the full provider stack behind the LLM routes. | ||
| func TestRejectClientSuppliedPriorityForwardsRequestWithoutHeader(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| forwarded := false | ||
| next := func(c echo.Context) error { | ||
| forwarded = true | ||
| return c.NoContent(http.StatusNoContent) | ||
| } | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) | ||
| rec := httptest.NewRecorder() | ||
| c := echo.New().NewContext(req, rec) | ||
|
|
||
| if err := rejectClientSuppliedPriority(next)(c); err != nil { | ||
| t.Fatalf("guard returned error for a request without X-Priority: %v", err) | ||
| } | ||
| if !forwarded { | ||
| t.Fatal("guard did not forward a request without X-Priority to the next handler") | ||
| } | ||
| if rec.Code != http.StatusNoContent { | ||
| t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent) | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.