-
Notifications
You must be signed in to change notification settings - Fork 28
Fix crash when Authorization header is missing for /v1/responses requests #29
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
hsperker
wants to merge
3
commits into
huggingface:main
Choose a base branch
from
hsperker:fix/missing-auth-crash
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,51 @@ | ||
| import { strict as assert } from "assert"; | ||
|
|
||
| const BASE_URL = process.env.RESPONSES_BASE_URL ?? "http://localhost:3000"; | ||
|
|
||
| describe("missing Authorization header", function () { | ||
| // Matches the existing test suite behavior: tests expect a running dev server. | ||
| before(async function () { | ||
| try { | ||
| const response = await fetch(`${BASE_URL}/`); | ||
| if (response.status !== 200) { | ||
| throw new Error(`Server returned status ${response.status}`); | ||
| } | ||
| } catch (error) { | ||
| console.error("❌ Server is not running. Please start the server with 'pnpm dev' before running the tests."); | ||
| throw error; | ||
| } | ||
| }); | ||
|
|
||
| it("streaming request returns 401 (does not start an SSE stream)", async function () { | ||
| const res = await fetch(`${BASE_URL}/v1/responses`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Accept: "text/event-stream", | ||
| }, | ||
| body: JSON.stringify({ model: "gpt-4.1-mini", input: "hi", stream: true }), | ||
| }); | ||
|
|
||
| assert.equal(res.status, 401); | ||
|
|
||
| const contentType = res.headers.get("content-type") ?? ""; | ||
| assert.ok(!contentType.includes("text/event-stream"), `Expected non-SSE response, got content-type: ${contentType}`); | ||
| }); | ||
|
|
||
| it("non-streaming request returns 401 and server remains healthy", async function () { | ||
| const res = await fetch(`${BASE_URL}/v1/responses`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ model: "gpt-4.1-mini", input: "hi" }), | ||
| }); | ||
|
|
||
| assert.equal(res.status, 401); | ||
|
|
||
| // Regression: the handler must not crash the whole process after responding. | ||
| // (The bug was triggering "Cannot set headers after they are sent to the client".) | ||
| const healthRes = await fetch(`${BASE_URL}/health`); | ||
| assert.equal(healthRes.status, 200); | ||
| }); | ||
| }); |
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.
I don't think we should raise a 401 not authorized if bearer token does not exist.
responses.jsis meant to be a lightweight proxy, meaning it does not handle authentication. One use case is to place the responses.js proxy in front of a local llama.cpp server. In this case, it doesn't make any sense to have authorizations at all.I think the only thing that the PR should solve is that if the authorization header is not present, and the backend server needs one, then the proxy should not crash (which was the initial issue)
Does that make sense to you?