Skip to content
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

The problem of SessionMiddleware always overwriting cookies. #144

Open
takapi327 opened this issue Jan 7, 2023 · 0 comments
Open

The problem of SessionMiddleware always overwriting cookies. #144

takapi327 opened this issue Jan 7, 2023 · 0 comments

Comments

@takapi327
Copy link

Hi there 👋

If another cookie is set using methods such as addCookie while SessionMiddleware is in use, it will be overwritten by SessionMiddleware.

When tried with VaultSessionExample.

def app[F[_]: Sync](key: Key[PageViews]): HttpRoutes[F] = {
  val dsl = new Http4sDsl[F] {}; import dsl._
  HttpRoutes.of {
    ...
    case GET -> Root / "other" / "cookie" =>
      Ok("Burn Other Cookies")
        .map(_.addCookie("OtherCookie", "hogehoge"))
        .map(res => {
          println(res.headers.headers) // List(Content-Type: text/plain; charset=UTF-8, Content-Length: 18, Set-Cookie: OtherCookie=hogehoge)
          res
        })
    ...
  }
}

Check the value of the header inside the SessionMiddleware.

...
  sessionCookie(id).map(response.response.putHeaders(_)).map(res => {
    println(response.response.headers.headers) // List(Content-Type: text/plain; charset=UTF-8, Content-Length: 18, Set-Cookie: OtherCookie=hogehoge)
    println(res.headers.headers) // List(Content-Type: text/plain; charset=UTF-8, Content-Length: 18, Set-Cookie: id=QiTmr/STABek+iyx+Z4SB7j98GJygDupnTokqr1ioDs=; SameSite=Lax; HttpOnly)
    res
  })
...

OtherCookie is never burned into the browser's cookie, only the id generated by SessionMiddleware is burned into the browser's cookie.

This is probably due to the fact that a new Set-Cookie has been generated inside SessionMiddleware using the putHeaders method.

As a test, instead of generating a Set-Cookie and updating the header, I was able to update the ResponseCookie value inside SessionMiddleware using the addCookie method to process the update without overwriting other cookie information.

example

    def sessionCookie(id: SessionIdentifier): F[ResponseCookie] = {
      expiration match {
        case ExpirationManagement.Static(maxAge, expires) =>
          ResponseCookie(sessionIdentifierName,
                         id.value,
                         domain = domain,
                         httpOnly = httpOnly,
                         secure = secure,
                         path = path,
                         sameSite = sameSite.some,
                         maxAge = maxAge,
                         expires = expires
          ).pure[F]
        case e @ ExpirationManagement.Dynamic(fromNow) =>
          HttpDate.current[F](Functor[F], e.C).flatMap { now =>
            fromNow(now).map { case ExpirationManagement.Static(maxAge, expires) =>
                ResponseCookie(sessionIdentifierName,
                               id.value,
                               domain = domain,
                               httpOnly = httpOnly,
                               secure = secure,
                               path = path,
                               sameSite = sameSite.some,
                               maxAge = maxAge,
                               expires = expires
                )
            }
          }
      }
    }

...

  sessionCookie(id).map(response.response.addCookie(_)).map(res => {
    println(response.response.headers.headers) // List(Content-Type: text/plain; charset=UTF-8, Content-Length: 18, Set-Cookie: OtherCookie=hogehoge)
    println(res.headers.headers) // List(Content-Type: text/plain; charset=UTF-8, Content-Length: 18, Set-Cookie: OtherCookie=hogehoge, Set-Cookie: id=Z15hkKuTeO7xSPACYOYn2kq4Krqf0QYDWgYJ7fPKliE=; SameSite=Lax; HttpOnly)
    res
  })
...

If this solution is acceptable, I will create a pull request.
If I am wrong, I would appreciate it if you could let me know.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant