// [CRUCIBLE-SEED]
// category: deserialization
// severity: critical
// language: java
// expected: Unsafe deserialization of Base64-decoded cookie value via ObjectInputStream

import java.io.*;
import java.util.Base64;
import javax.servlet.http.*;

public class RememberMeServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        Cookie[] cookies = req.getCookies();
        for (Cookie c : cookies) {
            if ("rememberMe".equals(c.getName())) {
                byte[] data = Base64.getDecoder().decode(c.getValue());
                // Vulnerable: deserializes attacker-controlled cookie bytes
                try (ObjectInputStream ois = new ObjectInputStream(
                        new ByteArrayInputStream(data))) {
                    Object principal = ois.readObject();
                    req.getSession().setAttribute("user", principal);
                } catch (ClassNotFoundException e) {
                    resp.sendError(400);
                }
                break;
            }
        }
        resp.sendRedirect("/dashboard");
    }
}
