Commit 711e5003 authored by Jan Koester's avatar Jan Koester
Browse files

login: nach der Anmeldung auf die angeforderte Seite (?next=)

loginPage nimmt next aus Query oder verstecktem Formularfeld und leitet nach
erfolgreicher Anmeldung dorthin statt auf die Startseite; ist man schon
angemeldet, direkt. Nur lokale Pfade (beginnt mit /, nicht // oder /\).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent fb11bc5e
Loading
Loading
Loading
Loading
+46 −4
Changes for src/blogi.cpp: 46 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -1079,21 +1079,61 @@ void blogi::Blogi::api(libhttppp::HttpRequest &curreq, const int tid, DomainCont
    json_object_put(jrequest);
}

// Target after login (?next=/path or hidden form field): local absolute paths
// only, so the parameter cannot redirect to another site ("//host", "/\\host").
static std::string loginNextTarget(const std::string &next)
{
    if (next.empty() || next.size() > 2000 || next[0] != '/')
        return "";
    if (next.size() > 1 && (next[1] == '/' || next[1] == '\\'))
        return "";
    for (unsigned char c : next)
        if (c < 0x20 || c == 0x7f || c == '\\' || c == '"' || c == '<' || c == '>')
            return "";
    return next;
}

// hidden form field carrying the login target; value is checked by loginNextTarget
static std::string loginNextField(const std::string &next)
{
    if (next.empty())
        return "";
    std::string v;
    for (char c : next)
        v += (c == '&') ? std::string("&amp;") : std::string(1, c);
    return "<input type=\"hidden\" name=\"next\" value=\"" + v + "\">";
}

void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq, const int tid, const std::string &sessionid, libhtmlpp::HtmlElement &index, DomainContext &ctx)
{
    auto &PlgArgs = ctx.plgArgs;
    auto &BlogiPlg = ctx.plugins;
    libhttppp::HTTPException excep;

    libhttppp::HttpForm curform;
    curform.parse(curreq);

    std::string next;
    for (const auto &cururlform : curform.urlData())
        if (cururlform.key == "next")
            next = loginNextTarget(cururlform.value);

    if (PlgArgs->auth->isLoggedIn(tid, curreq, sessionid))
    {
        if (!next.empty())
        {
            libhttppp::HttpResponse curres;
            curres.setState(HTTP303);
            curres.setHeaderData("location")->push_back(next);
            curres.setContentType("text/html");
            curres.send(curreq, "", 0);
            return;
        }
        libhttppp::HTTPException err;
        err[libhttppp::HTTPException::Error] << "you already authenticated please logoff before you login again!";
        throw err;
    }

    libhttppp::HttpForm curform;
    curform.parse(curreq);

    std::string username;
    std::string password;
    std::string authToken;
@@ -1128,6 +1168,7 @@ void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq, const int tid, cons
        condat << "<div id=\"content\">"
               << "<span>" << blogi::tr(_lang, "Login") << "</span>"
               << "<form action=\"" << ctx.buildurl("login") << "\" method=\"post\">"
               << loginNextField(next)
               << blogi::tr(_lang, "username:") << "<br> <input type=\"text\" name=\"username\" value=\"\" placeholder=\"user@domain\"><br>"
               << blogi::tr(_lang, "password:") << "<br> <input type=\"password\" name=\"password\" value=\"\"><br>"
               << "<button type=\"submit\">" << blogi::tr(_lang, "Submit") << "</button>"
@@ -1204,7 +1245,7 @@ void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq, const int tid, cons
        authid.setcookie(curres, "authid", sessid.c_str(), "", cookieDomain(curreq).c_str(), (1000 * 60 * 60 * 1), "/", false, "1", "Lax", false);

        curres.setState(HTTP303);
        curres.setHeaderData("location")->push_back(ctx.startPage);
        curres.setHeaderData("location")->push_back(next.empty() ? ctx.startPage : next);
        curres.setContentType("text/html");
        curres.send(curreq, "", 0);
    }
@@ -1221,6 +1262,7 @@ void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq, const int tid, cons
               << "<span>" << blogi::tr(_lang, "Wrong Username or Password!") << "</span>"
               << "<span>" << blogi::tr(_lang, "Login") << "</span>"
               << "<form action=\"" << ctx.buildurl("login") << "\" method=\"post\">"
               << loginNextField(next)
               << blogi::tr(_lang, "username:") << "<br> <input type=\"text\" name=\"username\" value=\"\"><br>"
               << blogi::tr(_lang, "password:") << "<br> <input type=\"password\" name=\"password\" value=\"\"><br>"
               << "<button type=\"submit\">" << blogi::tr(_lang, "Submit") << "</button>"