feat(security): add SecureBuffer, VSEC envelopes, authorization, and SecurityAsync Authenticator
This commit is contained in:
1 parent
eb42228604
commit
713d2dc656
20 files changed
+4070
-43
No files matched your search
@@ -0,0 +1,439 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/Security/Authenticator.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Security;
|
||||
using namespace Void::SecurityAsync;
|
||||
|
||||
Void::Coro::Task<void> run_login(
|
||||
Authenticator& auth, LoginInput input,
|
||||
Void::Async::OperationOptions opts,
|
||||
std::optional<Void::Result::Result<SessionGrant>>& out,
|
||||
std::atomic<int>* done = nullptr)
|
||||
{
|
||||
out = co_await auth.login(std::move(input), std::move(opts));
|
||||
if (done != nullptr) {
|
||||
done->fetch_add(1, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Coro::Task<void> run_authenticate(
|
||||
Authenticator& auth, const BearerToken& token,
|
||||
Void::Async::OperationOptions opts,
|
||||
std::optional<Void::Result::Result<Principal>>& out)
|
||||
{
|
||||
out = co_await auth.authenticate(token, std::move(opts));
|
||||
}
|
||||
|
||||
Void::Coro::Task<void> run_revoke(
|
||||
Authenticator& auth, std::string session_id,
|
||||
Void::Async::OperationOptions opts,
|
||||
std::optional<Void::Result::Result<void>>& out)
|
||||
{
|
||||
out = co_await auth.revoke(std::move(session_id), std::move(opts));
|
||||
}
|
||||
|
||||
void pump_until(Void::Async::ManualExecutor& exec,
|
||||
const std::atomic<int>& counter, int target)
|
||||
{
|
||||
const auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::seconds{15};
|
||||
while (counter.load(std::memory_order_acquire) < target &&
|
||||
std::chrono::steady_clock::now() < deadline) {
|
||||
if (exec.drain() == 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{1});
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test security_async_login_auth_revoke_expiry_and_clock_rollback()
|
||||
{
|
||||
auto store = std::make_shared<InMemoryAccountSessionStore>();
|
||||
auto audit = std::make_shared<InMemoryAuditSink>(64);
|
||||
Void::Async::ManualExecutor exec;
|
||||
Void::Time::ManualClock mono_clock;
|
||||
Void::Time::ManualCivilClock civil_clock{
|
||||
std::chrono::system_clock::time_point{std::chrono::seconds{1000000}}};
|
||||
|
||||
auto alice_hash =
|
||||
hash_password("correct-pw", PasswordHashStrength::Interactive);
|
||||
Void::Testing::require(alice_hash.has_value());
|
||||
Void::Testing::require(
|
||||
store
|
||||
->upsert_account(AccountRecord{
|
||||
.subject = "sub-alice",
|
||||
.normalized_username = "alice",
|
||||
.password_hash = *alice_hash,
|
||||
.roles = {"user"},
|
||||
.credential_version = 1,
|
||||
.revocation_generation = 1,
|
||||
.policy_generation = 3,
|
||||
.enabled = true,
|
||||
})
|
||||
.has_value());
|
||||
|
||||
AuthenticatorOptions opts{};
|
||||
opts.dummy_hash_strength = PasswordHashStrength::Interactive;
|
||||
opts.precomputed_dummy_hash = *alice_hash;
|
||||
opts.session.ttl = std::chrono::seconds{3600};
|
||||
opts.session.max_total_sessions = 4;
|
||||
opts.session.max_per_subject = 2;
|
||||
opts.session.clock_rollback_tolerance = std::chrono::seconds{60};
|
||||
|
||||
auto auth = Authenticator::create(store, exec, opts, audit, mono_clock,
|
||||
civil_clock);
|
||||
Void::Testing::require(auth.has_value());
|
||||
|
||||
// 1. Unknown account and wrong password produce identical public denial but
|
||||
// distinct internal audit reasons.
|
||||
{
|
||||
std::optional<Void::Result::Result<SessionGrant>> unknown_res;
|
||||
std::optional<Void::Result::Result<SessionGrant>> wrong_pw_res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"ghost", "any"}, {}, unknown_res,
|
||||
&done))
|
||||
.has_value());
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"ALICE", "wrong-pw"}, {},
|
||||
wrong_pw_res, &done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 2);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
|
||||
Void::Testing::require(unknown_res.has_value() &&
|
||||
!unknown_res->has_value());
|
||||
Void::Testing::require(wrong_pw_res.has_value() &&
|
||||
!wrong_pw_res->has_value());
|
||||
Void::Testing::expect_eq(unknown_res->error().code().value,
|
||||
wrong_pw_res->error().code().value);
|
||||
Void::Testing::expect_eq(unknown_res->error().message(),
|
||||
wrong_pw_res->error().message());
|
||||
|
||||
const auto events = audit->snapshot();
|
||||
bool saw_not_found = false;
|
||||
bool saw_invalid_pw = false;
|
||||
for (const auto& ev : events) {
|
||||
if (ev.internal_reason == "account_not_found") {
|
||||
saw_not_found = true;
|
||||
}
|
||||
if (ev.internal_reason == "invalid_password") {
|
||||
saw_invalid_pw = true;
|
||||
}
|
||||
}
|
||||
Void::Testing::expect(saw_not_found);
|
||||
Void::Testing::expect(saw_invalid_pw);
|
||||
}
|
||||
|
||||
// 2. Successful login (case-insensitive username) + authoritative
|
||||
// authenticate + immediate revocation (zero positive cache).
|
||||
std::optional<SessionGrant> grant1;
|
||||
{
|
||||
std::optional<Void::Result::Result<SessionGrant>> res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {" Alice ", "correct-pw"}, {}, res,
|
||||
&done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(res.has_value() && res->has_value());
|
||||
grant1.emplace(std::move(**res));
|
||||
}
|
||||
|
||||
{
|
||||
std::optional<Void::Result::Result<Principal>> principal;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_authenticate(**auth, grant1->bearer_token, {},
|
||||
principal))
|
||||
.has_value());
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(principal.has_value() && principal->has_value());
|
||||
Void::Testing::expect_eq((*principal)->subject,
|
||||
std::string{"sub-alice"});
|
||||
Void::Testing::expect_eq((*principal)->policy_generation, 3ULL);
|
||||
}
|
||||
|
||||
// Revoke takes effect immediately on next authenticate call.
|
||||
{
|
||||
std::optional<Void::Result::Result<void>> rev_res;
|
||||
std::optional<Void::Result::Result<Principal>> after_rev;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_revoke(**auth, grant1->session_id, {}, rev_res))
|
||||
.has_value());
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(rev_res.has_value() && rev_res->has_value());
|
||||
|
||||
Void::Async::TaskScope scope2{exec};
|
||||
Void::Testing::require(
|
||||
scope2
|
||||
.spawn(run_authenticate(**auth, grant1->bearer_token, {},
|
||||
after_rev))
|
||||
.has_value());
|
||||
Void::Testing::require(scope2.join_pumping(exec).has_value());
|
||||
Void::Testing::require(after_rev.has_value() && !after_rev->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
after_rev->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::session_revoked));
|
||||
}
|
||||
|
||||
// 3. Per-subject session capacity (max_per_subject = 2) enforced, and
|
||||
// expiry + bounded cleanup reclaims slots.
|
||||
std::optional<SessionGrant> g2;
|
||||
std::optional<SessionGrant> g3;
|
||||
for (auto* target : {&g2, &g3}) {
|
||||
std::optional<Void::Result::Result<SessionGrant>> res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"alice", "correct-pw"}, {}, res, &done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(res.has_value() && res->has_value());
|
||||
target->emplace(std::move(**res));
|
||||
}
|
||||
{
|
||||
std::optional<Void::Result::Result<SessionGrant>> over_cap;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"alice", "correct-pw"}, {}, over_cap,
|
||||
&done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(over_cap.has_value() && !over_cap->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
over_cap->error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
SecurityErrorCode::session_capacity_exceeded));
|
||||
}
|
||||
|
||||
// Advance civil clock past TTL -> authenticate denies expired, cleanup
|
||||
// removes sessions.
|
||||
Void::Testing::require(
|
||||
civil_clock.advance(std::chrono::seconds{3601}).has_value());
|
||||
{
|
||||
std::optional<Void::Result::Result<Principal>> expired;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_authenticate(**auth, g2->bearer_token, {}, expired))
|
||||
.has_value());
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(expired.has_value() && !expired->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
expired->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::session_expired));
|
||||
}
|
||||
auto cleaned = (*auth)->cleanup_expired_step();
|
||||
Void::Testing::require(cleaned.has_value());
|
||||
Void::Testing::expect_eq(*cleaned, 3U);
|
||||
|
||||
// 4. Civil-clock rollback beyond 60s tolerance halts admission until
|
||||
// explicitly reconciled.
|
||||
civil_clock.set(civil_clock.now() - std::chrono::seconds{120});
|
||||
{
|
||||
std::optional<Void::Result::Result<SessionGrant>> halted;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"alice", "correct-pw"}, {}, halted))
|
||||
.has_value());
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(halted.has_value() && !halted->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
halted->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::clock_rollback));
|
||||
}
|
||||
(*auth)->reconcile_clock(civil_clock.now());
|
||||
|
||||
// 5. Store outage fails closed.
|
||||
store->set_available(false);
|
||||
{
|
||||
std::optional<Void::Result::Result<SessionGrant>> outage;
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"alice", "correct-pw"}, {}, outage))
|
||||
.has_value());
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(outage.has_value() && !outage->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
outage->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::store_unavailable));
|
||||
}
|
||||
store->set_available(true);
|
||||
|
||||
Void::Testing::require((*auth)->close_and_join().has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test security_async_version_race_hash_stop_and_mandatory_audit()
|
||||
{
|
||||
auto store = std::make_shared<InMemoryAccountSessionStore>();
|
||||
auto audit = std::make_shared<InMemoryAuditSink>(1); // capacity 1!
|
||||
Void::Async::ManualExecutor exec;
|
||||
Void::Time::ManualClock mono_clock;
|
||||
Void::Time::ManualCivilClock civil_clock{
|
||||
std::chrono::system_clock::time_point{std::chrono::seconds{2000000}}};
|
||||
|
||||
auto pw_hash =
|
||||
hash_password("hunter2", PasswordHashStrength::Interactive);
|
||||
Void::Testing::require(pw_hash.has_value());
|
||||
Void::Testing::require(
|
||||
store
|
||||
->upsert_account(AccountRecord{
|
||||
.subject = "sub-bob",
|
||||
.normalized_username = "bob",
|
||||
.password_hash = *pw_hash,
|
||||
.roles = {"member"},
|
||||
.credential_version = 1,
|
||||
.revocation_generation = 1,
|
||||
.policy_generation = 1,
|
||||
.enabled = true,
|
||||
})
|
||||
.has_value());
|
||||
|
||||
// 1. Stop during in-flight password hash: worker drains to completion,
|
||||
// releases memory budget, discards outcome, and creates zero sessions.
|
||||
{
|
||||
std::promise<void> entered_hash;
|
||||
std::promise<void> release_hash;
|
||||
auto entered_fut = entered_hash.get_future();
|
||||
auto release_fut = release_hash.get_future().share();
|
||||
|
||||
AuthenticatorOptions opts{};
|
||||
opts.dummy_hash_strength = PasswordHashStrength::Interactive;
|
||||
opts.precomputed_dummy_hash = *pw_hash;
|
||||
opts.before_password_verify_hook = [&]() {
|
||||
entered_hash.set_value();
|
||||
release_fut.wait();
|
||||
};
|
||||
|
||||
auto auth = Authenticator::create(store, exec, opts, nullptr,
|
||||
mono_clock, civil_clock);
|
||||
Void::Testing::require(auth.has_value());
|
||||
|
||||
std::stop_source ss;
|
||||
std::optional<Void::Result::Result<SessionGrant>> res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"bob", "hunter2"},
|
||||
Void::Async::OperationOptions{
|
||||
.stop = ss.get_token()},
|
||||
res, &done))
|
||||
.has_value());
|
||||
(void)exec.drain();
|
||||
|
||||
// Wait until worker actually holds password job budget.
|
||||
entered_fut.wait();
|
||||
Void::Testing::expect_eq((*auth)->stats().active_password_jobs, 1U);
|
||||
Void::Testing::expect((*auth)->stats().reserved_password_memory_bytes >
|
||||
0U);
|
||||
|
||||
ss.request_stop();
|
||||
release_hash.set_value();
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
|
||||
Void::Testing::require(res.has_value() && !res->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
res->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::cancelled));
|
||||
Void::Testing::expect_eq(store->session_count(), 0U);
|
||||
Void::Testing::expect_eq((*auth)->stats().active_password_jobs, 0U);
|
||||
Void::Testing::expect_eq(
|
||||
(*auth)->stats().reserved_password_memory_bytes, 0U);
|
||||
Void::Testing::require((*auth)->close_and_join().has_value());
|
||||
}
|
||||
|
||||
// 2. Credential version change racing with login invalidates session write.
|
||||
{
|
||||
AuthenticatorOptions opts{};
|
||||
opts.dummy_hash_strength = PasswordHashStrength::Interactive;
|
||||
opts.precomputed_dummy_hash = *pw_hash;
|
||||
opts.before_password_verify_hook = [&]() {
|
||||
(void)store->bump_credential_version("sub-bob");
|
||||
};
|
||||
|
||||
auto auth = Authenticator::create(store, exec, opts, nullptr,
|
||||
mono_clock, civil_clock);
|
||||
Void::Testing::require(auth.has_value());
|
||||
|
||||
std::optional<Void::Result::Result<SessionGrant>> res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"bob", "hunter2"}, {}, res, &done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(res.has_value() && !res->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
res->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::conflict));
|
||||
Void::Testing::expect_eq(store->session_count(), 0U);
|
||||
Void::Testing::require((*auth)->close_and_join().has_value());
|
||||
}
|
||||
|
||||
// 3. Mandatory fail-closed audit queue full revokes session and fails login.
|
||||
{
|
||||
// Pre-fill the 1-slot audit sink.
|
||||
Void::Testing::require(
|
||||
audit->record(AuditEvent{.event_type = "fill"}).has_value());
|
||||
|
||||
AuthenticatorOptions opts{};
|
||||
opts.dummy_hash_strength = PasswordHashStrength::Interactive;
|
||||
opts.precomputed_dummy_hash = *pw_hash;
|
||||
opts.audit_policy = AuditFailurePolicy::FailClosed;
|
||||
|
||||
auto auth = Authenticator::create(store, exec, opts, audit, mono_clock,
|
||||
civil_clock);
|
||||
Void::Testing::require(auth.has_value());
|
||||
|
||||
std::optional<Void::Result::Result<SessionGrant>> res;
|
||||
std::atomic<int> done{0};
|
||||
Void::Async::TaskScope scope{exec};
|
||||
Void::Testing::require(
|
||||
scope
|
||||
.spawn(run_login(**auth, {"bob", "hunter2"}, {}, res, &done))
|
||||
.has_value());
|
||||
pump_until(exec, done, 1);
|
||||
Void::Testing::require(scope.join_pumping(exec).has_value());
|
||||
Void::Testing::require(res.has_value() && !res->has_value());
|
||||
Void::Testing::expect_eq(
|
||||
res->error().code().value,
|
||||
static_cast<std::int32_t>(SecurityErrorCode::audit_failed));
|
||||
Void::Testing::require((*auth)->close_and_join().has_value());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user