202 lines
5.8 KiB
C++
202 lines
5.8 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
// SecurityExtra framework skeleton for the living example (namespace
|
||
|
|
// molo::example::fw). Mirrors names and behavior of src/security/{guard,
|
||
|
|
// crypto,auth,secret,upload} and src/cli/csp in small, readable form.
|
||
|
|
// Every container has a hard cap. Crypto here is real SHA-256/HMAC but the
|
||
|
|
// password hasher uses DEMO iteration counts: not for production use.
|
||
|
|
|
||
|
|
#include <chrono>
|
||
|
|
#include <cstddef>
|
||
|
|
#include <cstdint>
|
||
|
|
#include <map>
|
||
|
|
#include <mutex>
|
||
|
|
#include <set>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "Http.hpp"
|
||
|
|
|
||
|
|
namespace molo::example::fw {
|
||
|
|
|
||
|
|
namespace secaps {
|
||
|
|
inline constexpr std::size_t kMaxDenials = 100;
|
||
|
|
inline constexpr std::size_t kMaxRateKeys = 1024;
|
||
|
|
inline constexpr std::size_t kMaxCspReports = 50;
|
||
|
|
inline constexpr std::size_t kMaxRandomBytes = 64;
|
||
|
|
inline constexpr std::size_t kMaxHashInput = 4096;
|
||
|
|
inline constexpr std::size_t kMaxUploadHex = 4096;
|
||
|
|
inline constexpr std::size_t kMaxInputLen = 512;
|
||
|
|
inline constexpr int kDemoPbkdf2Rounds = 2000; // real: 600000+
|
||
|
|
} // namespace secaps
|
||
|
|
|
||
|
|
std::string to_hex(const std::string& raw);
|
||
|
|
bool from_hex(const std::string& hex, std::string& raw);
|
||
|
|
bool constant_time_equal(const std::string& a, const std::string& b);
|
||
|
|
|
||
|
|
// Mirrors src/security/crypto/Sha256.h and Hmac.h.
|
||
|
|
class Sha256 {
|
||
|
|
public:
|
||
|
|
static std::string digest(const std::string& data); // 32 raw bytes
|
||
|
|
static std::string hex(const std::string& data);
|
||
|
|
};
|
||
|
|
|
||
|
|
class Hmac {
|
||
|
|
public:
|
||
|
|
static std::string sha256(const std::string& key, const std::string& data);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors SecureRandom.h: /dev/urandom, capped length.
|
||
|
|
class SecureRandom {
|
||
|
|
public:
|
||
|
|
static std::string bytes(std::size_t n);
|
||
|
|
static std::string token_hex(std::size_t n);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors PasswordHasher.h with PBKDF2-HMAC-SHA256 at demo rounds.
|
||
|
|
class PasswordHasher {
|
||
|
|
public:
|
||
|
|
static std::string hash(const std::string& password); // "pbkdf2$r$salt$dk"
|
||
|
|
static bool verify(const std::string& password, const std::string& stored);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/DenialSink.h: capped ring of why requests were refused.
|
||
|
|
struct Denial {
|
||
|
|
std::string guard;
|
||
|
|
std::string reason;
|
||
|
|
};
|
||
|
|
class DenialSink {
|
||
|
|
public:
|
||
|
|
void record(const std::string& guard, const std::string& reason);
|
||
|
|
std::vector<Denial> list() const;
|
||
|
|
std::size_t total() const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
mutable std::mutex mu_;
|
||
|
|
std::vector<Denial> ring_;
|
||
|
|
std::size_t total_ = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/CsrfGuard.h: stateless signed token "nonce.mac".
|
||
|
|
class CsrfGuard {
|
||
|
|
public:
|
||
|
|
explicit CsrfGuard(std::string secret) : secret_(std::move(secret)) {}
|
||
|
|
std::string issue() const;
|
||
|
|
bool verify(const std::string& token) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::string secret_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/CorsPolicy.h: exact-origin allowlist, no wildcard with creds.
|
||
|
|
class CorsPolicy {
|
||
|
|
public:
|
||
|
|
explicit CorsPolicy(std::set<std::string> origins)
|
||
|
|
: origins_(std::move(origins)) {}
|
||
|
|
bool allowed(const std::string& origin) const;
|
||
|
|
// Fills Access-Control-* headers when the origin is allowed.
|
||
|
|
bool apply_preflight(const Request& req, Response& res) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::set<std::string> origins_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/RateLimiter.h: fixed window per key, capped key table.
|
||
|
|
class RateLimiter {
|
||
|
|
public:
|
||
|
|
RateLimiter(int limit, int window_seconds)
|
||
|
|
: limit_(limit), window_(window_seconds) {}
|
||
|
|
// Returns true when allowed; else sets retry_after (seconds, >= 1).
|
||
|
|
bool hit(const std::string& key, int& retry_after, int& remaining);
|
||
|
|
int limit() const { return limit_; }
|
||
|
|
int window() const { return window_; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
struct Slot {
|
||
|
|
std::chrono::steady_clock::time_point start;
|
||
|
|
int count = 0;
|
||
|
|
};
|
||
|
|
std::mutex mu_;
|
||
|
|
std::map<std::string, Slot> slots_;
|
||
|
|
int limit_;
|
||
|
|
int window_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/SecurityHeaders.h.
|
||
|
|
class SecurityHeaders {
|
||
|
|
public:
|
||
|
|
static std::vector<std::pair<std::string, std::string>> defaults();
|
||
|
|
static void apply(Response& res);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors src/cli/csp: builder, per-request nonce, capped report store.
|
||
|
|
class CspBuilder {
|
||
|
|
public:
|
||
|
|
CspBuilder();
|
||
|
|
CspBuilder& script_nonce(const std::string& nonce);
|
||
|
|
CspBuilder& report_uri(const std::string& uri);
|
||
|
|
CspBuilder& enforce();
|
||
|
|
std::string header_name() const;
|
||
|
|
std::string build() const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::map<std::string, std::string> directives_;
|
||
|
|
bool report_only_ = true;
|
||
|
|
};
|
||
|
|
class CspReportStore {
|
||
|
|
public:
|
||
|
|
bool add(const std::string& body); // false when body is empty/too large
|
||
|
|
std::vector<std::string> list() const;
|
||
|
|
std::size_t total() const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
mutable std::mutex mu_;
|
||
|
|
std::vector<std::string> reports_;
|
||
|
|
std::size_t total_ = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors auth/Permission.h + Guard.h: token -> role -> permission set.
|
||
|
|
class PermissionGuard {
|
||
|
|
public:
|
||
|
|
PermissionGuard();
|
||
|
|
enum class Verdict { Allow, Unauthenticated, Forbidden };
|
||
|
|
Verdict check(const std::string& authorization,
|
||
|
|
const std::string& permission, std::string& role) const;
|
||
|
|
std::vector<std::string> permissions(const std::string& role) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::map<std::string, std::string> token_role_;
|
||
|
|
std::map<std::string, std::set<std::string>> role_perms_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors secret/SecretsProvider.h; values never leave except masked.
|
||
|
|
class SecretsProvider {
|
||
|
|
public:
|
||
|
|
SecretsProvider();
|
||
|
|
std::vector<std::string> names() const;
|
||
|
|
bool masked(const std::string& name, std::string& out) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::map<std::string, std::string> values_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors guard/SqlSafety.h and PathGuard.h: returns "" when safe, else reason.
|
||
|
|
class SqlSafety {
|
||
|
|
public:
|
||
|
|
static std::string check(const std::string& value);
|
||
|
|
};
|
||
|
|
class PathGuard {
|
||
|
|
public:
|
||
|
|
static std::string check(const std::string& value);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Mirrors upload/UploadValidator.h: ext allowlist, size cap, magic bytes.
|
||
|
|
class UploadValidator {
|
||
|
|
public:
|
||
|
|
static constexpr std::size_t kMaxFileBytes = 1024;
|
||
|
|
static std::string check(const std::string& filename,
|
||
|
|
const std::string& bytes); // "" when allowed
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::example::fw
|