To prevent unauthorized access and potential misuse of session tokens.
defmodule VulnerableApp do
use Plug.Router
plug Plug.Session, store: :cookie
plug :match
plug :dispatch
get "/logout" do
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, "Logged out!")
end
end
This code sets up a session using Plug.Session. However, when a user logs out, their session is not properly invalidated, leaving it vulnerable to misuse.
defmodule SecureApp do
use Plug.Router
plug Plug.Session, store: :cookie
plug :match
plug :dispatch
get "/logout" do
conn
|> delete_session(:user)
|> put_resp_content_type("text/plain")
|> send_resp(200, "Logged out!")
end
end
This code correctly invalidates the session when the user logs out, preventing further use of their session token.