feat(security): add SecureBuffer, VSEC envelopes, authorization, and SecurityAsync Authenticator

This commit is contained in:
faumaray committed 2026-11-25 20:44:31 +03:00
1 parent eb42228604
commit 713d2dc656
20 files changed
+4070 -43

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
#include <Void/Security/Authorization.hpp>
#include <algorithm>
namespace Void::Security
{
namespace
{
[[nodiscard]] Void::Result::Result<void>
validate_predicate(const Predicate& pred, std::size_t depth)
{
if (depth > SecurityLimits::kMaxPredicateDepth) {
return std::unexpected{make_security_error(
SecurityErrorCode::policy_invalid,
"Security::PolicySnapshot: predicate nesting exceeds max "
"depth 16")};
}
switch (pred.kind) {
case Predicate::Kind::AlwaysTrue:
case Predicate::Kind::AlwaysFalse:
case Predicate::Kind::HasRole:
case Predicate::Kind::SubjectEquals:
case Predicate::Kind::ResourceTypeEquals:
case Predicate::Kind::ResourceIdEquals:
case Predicate::Kind::ResourceAttrEquals:
case Predicate::Kind::PrincipalAttrEquals:
case Predicate::Kind::ResourceOwnerMatchesSubject:
return {};
case Predicate::Kind::AllOf:
case Predicate::Kind::AnyOf:
for (const auto& child : pred.children) {
if (auto res = validate_predicate(child, depth + 1U);
!res.has_value()) {
return res;
}
}
return {};
case Predicate::Kind::Not:
if (pred.children.size() != 1U) {
return std::unexpected{make_security_error(
SecurityErrorCode::policy_invalid,
"Security::PolicySnapshot: Not predicate requires "
"exactly 1 child")};
}
return validate_predicate(pred.children.front(),
depth + 1U);
}
return std::unexpected{make_security_error(
SecurityErrorCode::policy_invalid,
"Security::PolicySnapshot: unknown predicate kind")};
}
[[nodiscard]] bool eval_predicate(const Predicate& pred,
const Principal& principal,
const ResourceAttributes& resource,
std::size_t depth)
{
if (depth > SecurityLimits::kMaxPredicateDepth) {
return false;
}
switch (pred.kind) {
case Predicate::Kind::AlwaysTrue:
return true;
case Predicate::Kind::AlwaysFalse:
return false;
case Predicate::Kind::HasRole:
return std::find(principal.roles.begin(),
principal.roles.end(),
pred.value) != principal.roles.end();
case Predicate::Kind::SubjectEquals:
return !principal.subject.empty() &&
principal.subject == pred.value;
case Predicate::Kind::ResourceTypeEquals:
return resource.resource_type == pred.value;
case Predicate::Kind::ResourceIdEquals:
return resource.resource_id == pred.value;
case Predicate::Kind::ResourceAttrEquals:
{
auto it = resource.attributes.find(pred.key);
return it != resource.attributes.end() &&
it->second == pred.value;
}
case Predicate::Kind::PrincipalAttrEquals:
{
auto it = principal.attributes.find(pred.key);
return it != principal.attributes.end() &&
it->second == pred.value;
}
case Predicate::Kind::ResourceOwnerMatchesSubject:
{
if (principal.subject.empty()) {
return false;
}
const std::string_view attr_key =
pred.key.empty() ? std::string_view{"owner"}
: std::string_view{pred.key};
auto it = resource.attributes.find(attr_key);
return it != resource.attributes.end() &&
it->second == principal.subject;
}
case Predicate::Kind::AllOf:
if (pred.children.empty()) {
return false;
}
for (const auto& child : pred.children) {
if (!eval_predicate(child, principal, resource,
depth + 1U)) {
return false;
}
}
return true;
case Predicate::Kind::AnyOf:
for (const auto& child : pred.children) {
if (eval_predicate(child, principal, resource,
depth + 1U)) {
return true;
}
}
return false;
case Predicate::Kind::Not:
if (pred.children.size() != 1U) {
return false;
}
return !eval_predicate(pred.children.front(), principal,
resource, depth + 1U);
}
return false;
}
} // namespace
Void::Result::Result<PolicySnapshot>
PolicySnapshot::create(std::uint64_t generation,
std::vector<PolicyRule> rules)
{
if (rules.size() > SecurityLimits::kMaxPolicyRules) {
return std::unexpected{make_security_error(
SecurityErrorCode::policy_invalid,
"Security::PolicySnapshot: rule count exceeds 4096 cap")};
}
for (const auto& rule : rules) {
if (rule.action.empty()) {
return std::unexpected{make_security_error(
SecurityErrorCode::policy_invalid,
"Security::PolicySnapshot: rule action must be non-empty")};
}
if (auto res = validate_predicate(rule.condition, 1U);
!res.has_value()) {
return std::unexpected{std::move(res.error())};
}
}
return PolicySnapshot{generation, std::move(rules)};
}
Decision PolicySnapshot::authorize(const Principal& principal,
std::string_view action,
const ResourceAttributes& resource) const
{
if (action.empty()) {
return Decision{.allowed = false,
.reason = "empty action denied",
.policy_generation = m_generation};
}
if (principal.roles.size() > SecurityLimits::kMaxRolesPerPrincipal) {
return Decision{.allowed = false,
.reason = "principal exceeds 64-role cap",
.policy_generation = m_generation};
}
std::string allow_rule_id;
bool matched_allow = false;
for (const auto& rule : m_rules) {
if (rule.action != "*" && rule.action != action) {
continue;
}
if (!eval_predicate(rule.condition, principal, resource, 1U)) {
continue;
}
if (rule.effect == Effect::Deny) {
return Decision{.allowed = false,
.matched_rule_id = rule.id,
.reason = "explicit deny",
.policy_generation = m_generation};
}
if (!matched_allow) {
matched_allow = true;
allow_rule_id = rule.id;
}
}
if (matched_allow) {
return Decision{.allowed = true,
.matched_rule_id = std::move(allow_rule_id),
.reason = "explicit allow",
.policy_generation = m_generation};
}
return Decision{.allowed = false,
.reason = "default deny",
.policy_generation = m_generation};
}
} // namespace Void::Security