Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/drogon/src/controllers/ForcedPasswordChangeSessionState.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "ForcedPasswordChangeSessionState.h"

#include <drogon/Session.h>

namespace fulla::drogon::controllers::detail
{
void clearForcedPasswordChangeSessionAndRotate(::drogon::Session &session)
{
session.erase("must_change_password");
session.erase("userId");
session.erase("sub");
session.erase("auth_time");
session.erase("amr");
session.erase("mfa_pending");
session.erase("mfa_code_challenge");
session.erase("mfa_code_challenge_method");
// Rotate once. The framework retains the old ID for in-flight requests.
session.changeSessionIdToClient();
}
} // namespace fulla::drogon::controllers::detail
12 changes: 12 additions & 0 deletions libs/drogon/src/controllers/ForcedPasswordChangeSessionState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

namespace drogon
{
class Session;
}

namespace fulla::drogon::controllers::detail
{
// Clear only the forced-change authentication state at the existing boundary.
void clearForcedPasswordChangeSessionAndRotate(::drogon::Session &session);
} // namespace fulla::drogon::controllers::detail
13 changes: 4 additions & 9 deletions libs/drogon/src/controllers/SessionController.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "PasswordLoginSessionState.h"
#include "ForcedPasswordChangeSessionState.h"

#include <mutex>
#include <fulla/common/observability/AuditActions.h>
Expand Down Expand Up @@ -2147,21 +2148,15 @@ void SessionController::changePasswordForced(
// marker was ever set for it).
if (req->session())
{
req->session()->erase("must_change_password");
req->session()->erase("userId");
req->session()->erase("sub");
req->session()->erase("auth_time");
req->session()->erase("amr");
req->session()->erase("mfa_pending");
req->session()->erase("mfa_code_challenge");
req->session()->erase("mfa_code_challenge_method");
// Rotate the session identifier too:
// the anonymous remnant must not be
// replayable against any future login
// on this browser (fixation defense;
// the old id stays valid 10s for
// in-flight requests only).
req->session()->changeSessionIdToClient();
detail::clearForcedPasswordChangeSessionAndRotate(
*req->session()
);
}
::fulla::drogon::adapters::DrogonAuditSink::logFromRequest(
::drogon::app().getPlugin<::OAuth2Plugin>()->getAuditSink(),
Expand Down
110 changes: 110 additions & 0 deletions libs/drogon/test/PasswordLoginSessionStateTest.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "../src/controllers/PasswordLoginSessionState.h"
#include "../src/controllers/ForcedPasswordChangeSessionState.h"

#include <drogon/drogon.h>
#include <gtest/gtest.h>

#include <atomic>
#include <array>
#include <chrono>
#include <cstdint>
#include <exception>
Expand Down Expand Up @@ -256,6 +258,114 @@ TEST(PasswordLoginSessionState, ClaimsKeepSubjectsAndFullSignedTimeRange)
EXPECT_NE(fixture.cookie(), cookie);
}
}
constexpr std::array<const char *, 8> kForcedChangeKeys{{
"must_change_password", "userId", "sub", "auth_time", "amr", "mfa_pending",
"mfa_code_challenge", "mfa_code_challenge_method"
}};

TEST(ForcedPasswordChangeSessionState, ClearsAllAuthenticationKeysAndRotates)
{
SessionFixture fixture;
const auto cookie = fixture.cookie();
const auto observed = fixture.observe([](::drogon::Session &session) {
session.insert("must_change_password", true);
session.insert("userId", std::string("41"));
session.insert("sub", std::string("public-user"));
session.insert("auth_time", int64_t{2147483799});
session.insert("amr", std::string("pwd mfa"));
session.insert("mfa_pending", true);
session.insert("mfa_code_challenge", std::string("old-challenge"));
session.insert("mfa_code_challenge_method", std::string("S256"));
state::clearForcedPasswordChangeSessionAndRotate(session);
});
EXPECT_EQ(observed, fixture.session);
for (const auto *key : kForcedChangeKeys)
EXPECT_FALSE(observed->find(key)) << key;
EXPECT_NE(fixture.cookie(), cookie);
}

TEST(ForcedPasswordChangeSessionState, PreservesUnownedValuesAndTypes)
{
SessionFixture fixture;
const auto observed = fixture.observe([](::drogon::Session &session) {
for (const auto *key : kForcedChangeKeys)
session.insert(key, std::string("old-authentication"));
session.insert("code_challenge", std::string("oauth-pkce"));
session.insert("code_challenge_method", std::string("S256"));
session.insert("pending_consent_csrf_slots", std::string("consent-state"));
session.insert("pending_org_context_slots", std::string("org-state"));
session.insert("webauthn_auth_challenge", std::string("webauthn-state"));
session.insert("unknown-number", int64_t{2147483799});
session.insert("unknown-flag", false);
state::clearForcedPasswordChangeSessionAndRotate(session);
});
EXPECT_EQ(observed, fixture.session);
for (const auto *key : kForcedChangeKeys)
EXPECT_FALSE(observed->find(key)) << key;
EXPECT_EQ(observed->getOptional<std::string>("code_challenge"), "oauth-pkce");
EXPECT_EQ(observed->getOptional<std::string>("code_challenge_method"), "S256");
EXPECT_EQ(observed->getOptional<std::string>("pending_consent_csrf_slots"), "consent-state");
EXPECT_EQ(observed->getOptional<std::string>("pending_org_context_slots"), "org-state");
EXPECT_EQ(observed->getOptional<std::string>("webauthn_auth_challenge"), "webauthn-state");
EXPECT_EQ(observed->getOptional<int64_t>("unknown-number"), int64_t{2147483799});
EXPECT_EQ(observed->getOptional<bool>("unknown-flag"), false);
size_t count = 0;
observed->modify([&count](const ::drogon::Session::SessionMap &values) {
count = values.size();
});
EXPECT_EQ(count, size_t{7});
}

TEST(ForcedPasswordChangeSessionState, HandlesEmptyPartialAndDifferentlyTypedKeys)
{
for (int scenario = 0; scenario < 3; ++scenario)
{
SessionFixture fixture;
const auto cookie = fixture.cookie();
const auto observed = fixture.observe([scenario](::drogon::Session &session) {
if (scenario == 1)
{
session.insert("userId", false);
session.insert("must_change_password", int64_t{3});
session.insert("mfa_code_challenge_method", std::string("partial"));
}
else if (scenario == 2)
{
for (const auto *key : kForcedChangeKeys)
session.insert(key, int64_t{9});
}
state::clearForcedPasswordChangeSessionAndRotate(session);
});
EXPECT_EQ(observed, fixture.session);
for (const auto *key : kForcedChangeKeys)
EXPECT_FALSE(observed->find(key)) << key;
size_t count = 0;
observed->modify([&count](const ::drogon::Session::SessionMap &values) {
count = values.size();
});
EXPECT_EQ(count, size_t{0});
EXPECT_NE(fixture.cookie(), cookie);
}
}

TEST(ForcedPasswordChangeSessionState, RepeatedRequestsRemainAnonymousAndRotateEachTime)
{
SessionFixture fixture;
auto cookie = fixture.cookie();
for (int request = 0; request < 2; ++request)
{
const auto observed = fixture.observe([](::drogon::Session &session) {
session.insert("unknown-number", int64_t{73});
state::clearForcedPasswordChangeSessionAndRotate(session);
});
EXPECT_EQ(observed, fixture.session);
for (const auto *key : kForcedChangeKeys)
EXPECT_FALSE(observed->find(key)) << key;
EXPECT_EQ(observed->getOptional<int64_t>("unknown-number"), int64_t{73});
EXPECT_NE(fixture.cookie(), cookie);
cookie = fixture.cookie();
}
}
} // namespace

int main(int argc, char **argv)
Expand Down
Loading
Loading