Watch out for setCookie and clearCookie opts, as clearCookie mutates opts instance
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- aws, javascript
Línea de trabajo
Comienza en lib/response.js, en las líneas 221-224, e inspecciona cómo clearCookie gestiona el objeto options proporcionado. Reproduce la secuencia de inicio de sesión, cierre de sesión e inicio de sesión con opciones de cookie compartidas y verifica después que borrar una cookie no afecte a las llamadas posteriores a setCookie; documenta el comportamiento si la implementación permanece intencionadamente sin cambios.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- JavaScript
- Estrellas
- 1.5k
- Forks
- 127
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de jeremydaly/lambda-api
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
jeremydaly/lambda-api#356 ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 30/100
jeremydaly/lambda-api#270 ·
-
Logger OutputQuizá libre de nuevo @naorpeled la tomó hace 905 días y no hay ningún pull request abierto. Abierto
jeremydaly/lambda-api#259 · 1 comentario · 1 asignado ·
-
Is it possible to disable versioning?Posiblemente ocupada @naorpeled la tomó hace 100 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 52/100
jeremydaly/lambda-api#257 · 5 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 38/100
jeremydaly/lambda-api#251 · 1 comentario ·
Todos los issues de jeremydaly/lambda-api
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 76/100
521xueweihan/HelloGitHub#3939 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
-
needs-website-changes
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
[Bug] critique requires the detector on native targets, but audit.native.md says it does not applyAbiertoneeds triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
pbakaus/impeccable#1017 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día