-
Notifications
You must be signed in to change notification settings - Fork 59.8k
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
Compatible for getting auth token from client #5904
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,7 +25,8 @@ function parseApiKey(bearToken: string) { | |||||||||
} | ||||||||||
|
||||||||||
export function auth(req: NextRequest, modelProvider: ModelProvider) { | ||||||||||
const authToken = req.headers.get("Authorization") ?? ""; | ||||||||||
const authToken = | ||||||||||
req.headers.get("Authorization") ?? req.headers.get("x-api-key") ?? ""; | ||||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider reversing the header precedence order The current implementation prioritizes the 'Authorization' header over 'x-api-key'. While this maintains backward compatibility, it could potentially allow an attacker to bypass intended 'x-api-key' authentication by providing an 'Authorization' header. Consider reversing the order if 'x-api-key' should take precedence for certain clients (e.g., Anthropic services). - const authToken =
- req.headers.get("Authorization") ?? req.headers.get("x-api-key") ?? "";
+ const authToken =
+ req.headers.get("x-api-key") ?? req.headers.get("Authorization") ?? ""; 📝 Committable suggestion
Suggested change
|
||||||||||
|
||||||||||
// check if it is openai api key or user token | ||||||||||
const { accessCode, apiKey } = parseApiKey(authToken); | ||||||||||
|
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.
Align authentication header precedence with auth.ts
The authentication header precedence here differs from auth.ts, which could lead to inconsistent behavior. Additionally, the Bearer token stripping is only applied to the 'Authorization' header, not to 'x-api-key'.
📝 Committable suggestion