-
Notifications
You must be signed in to change notification settings - Fork 0
correct origin cors middleware #2
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
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,9 +14,24 @@ const port = process.env.PORT || 3000; | |||||||||||||
|
||||||||||||||
app.use(express.json()); | ||||||||||||||
app.use(cookieParser()); | ||||||||||||||
const allowlist = [ | ||||||||||||||
"http://localhost:3000", | ||||||||||||||
"https://posts-seven-red.vercel.app", | ||||||||||||||
]; | ||||||||||||||
|
||||||||||||||
app.use( | ||||||||||||||
cors({ | ||||||||||||||
origin: "http://localhost:5173", // Autoriser uniquement votre frontend | ||||||||||||||
origin: (origin, callback) => { | ||||||||||||||
if ( | ||||||||||||||
!origin || process.env.NODE_ENV === "production" | ||||||||||||||
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. 🚨 question (security): Reconsider allowing requests with no Origin header Requests without an Origin header are currently permitted, which could allow non-browser clients to bypass CORS. Confirm if this behavior is intended. |
||||||||||||||
? allowlist.includes(origin) | ||||||||||||||
: origin === "http://localhost:5173" | ||||||||||||||
) { | ||||||||||||||
callback(null, true); | ||||||||||||||
} else { | ||||||||||||||
callback(new Error("Not allowed by CORS")); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+31
to
+33
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. suggestion (bug_risk): Use Calling the callback with an error causes a 500 response. Use
Suggested change
|
||||||||||||||
}, | ||||||||||||||
credentials: true, | ||||||||||||||
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"], | ||||||||||||||
exposedHeaders: ["Content-Length", "X-Total-Count"], | ||||||||||||||
|
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.
suggestion: Clarify ternary operator precedence
Consider adding parentheses around the production check or extracting the condition into a named variable to improve readability and prevent misinterpretation.