Watch out for setCookie and clearCookie opts, as clearCookie mutates opts instance
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- aws, javascript
Hướng nghiên cứu
Bắt đầu tại lib/response.js ở các dòng 221-224 và kiểm tra cách clearCookie xử lý đối tượng options được truyền vào. Tái hiện chuỗi đăng nhập, đăng xuất và đăng nhập với các tùy chọn cookie dùng chung, sau đó xác minh rằng việc xóa một cookie không ảnh hưởng đến các lệnh gọi setCookie về sau; ghi lại hành vi nếu việc triển khai được cố ý giữ nguyên.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hi!
Lambda-api is a great package and has been really helpful in our one single backend lambda architecture 🚀
Today, I faced a really hard to debug issue and I though passing the information here if it helps anyone else. I was running our app with SAM CLI and everything worked like a charm, but when I deployed to AWS lambda, it worked in really weird way.
The problem was that I had somewhat the following structure:
const cookieOptions: CookieOptions = {
sameSite: 'Lax',
secure: true,
httpOnly: true,
}
// And pseudo authentication
api.post('/login', async (req,res) => {
// Do login stuff
res.setCookie('authcookie', sessionId, cookieOpts).json({ message: "Authenticated" })
})
api.post('/logout', async (req,res) => {
// Do logout stuff
res.clearCookie('authcookie', cookieOpts).json({ message: "Logged out" })
})
It worked locally like a charm. But when it was deployed to AWS Lambda, it worked like this:
- Login - OK you get a cookie
- Logout - OK cookie gets evicted
- Login - FAILS and no cookie is set
- After some time, it starts to work again, for once 😂
After some weird debugging I found out that the third call gets correct cookie, but with expiration set to -1, so the browser thinks that yes, unset the cookie.
Obvious reason for this is that response.clearCookie call mutates the given opts instance:
After the first response.clearCookie call my opts instance was mutated to contain expiration: -1 field and calls to response.setCookie after that naturally included the expiration.
The reason why it works after some time in AWS lambda is that runtime spins up a new sandbox and then it works again, for once 😄, because opts instance is not mutated and no one has logged out. Locally SAM CLI fires a whole new sandbox for each request and thus it worked locally with no issues.
You can easily work around this for example by wrapping opts in function:
const cookieOptions = (): CookieOptions => ({
sameSite: 'Lax',
secure: true,
httpOnly: true,
})
It surely took me some time to figure out what is going on 😄 I don't think this is mentioned in documentation. I think I can work out a PR for this if this is something that should be fixed - in code or with documentation.
- Ngôn ngữ chính
- JavaScript
- Star
- 1.5k
- Fork
- 127
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của jeremydaly/lambda-api
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
jeremydaly/lambda-api#356 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
jeremydaly/lambda-api#270 ·
-
Logger OutputCó thể làm lại được @naorpeled đã nhận 906 ngày trước và không có pull request nào đang mở. Đang mở
jeremydaly/lambda-api#259 · 1 bình luận · 1 người được giao ·
-
Is it possible to disable versioning?Có thể đã có người làm @naorpeled đã nhận 101 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
jeremydaly/lambda-api#257 · 5 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
jeremydaly/lambda-api#251 · 1 bình luận ·
Tất cả issue của jeremydaly/lambda-api
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
jfromaniello/url-join#134 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
quarto-dev/quarto-cli#15042 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
extendr/extendr.github.io#134 ·