feat: implement MoloApp plans 01-15 with integration

Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
This commit is contained in:
2026-10-23 18:20:04 +02:00
parent 54068f1953
commit 3c4334cec9
1068 changed files with 100899 additions and 2 deletions
+256
View File
@@ -0,0 +1,256 @@
// Guard test: CSRF, CORS, headers, rate limit, body limits, paths, SQL.
#include <cstdint>
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#include "BodyLimits.h"
#include "CorsPolicy.h"
#include "CsrfGuard.h"
#include "DenialSink.h"
#include "HttpMessageView.h"
#include "PathGuard.h"
#include "RateLimiter.h"
#include "SecurityHeaders.h"
#include "Session.h"
#include "SqlSafety.h"
using namespace molo::security::guard;
using molo::security::session::Session;
static int failures = 0;
static void check(bool ok, const char* name) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", name);
if (!ok) ++failures;
}
static HttpRequestView post(const std::string& path) {
HttpRequestView r;
r.method = "POST";
r.path = path;
r.peer_ip = "10.0.0.1";
r.now_unix_sec = 1000;
return r;
}
static Session mk_session() {
Session s;
s.id = "AAAAAAAAAAAAAAAAAAAAAA";
s.csrf_token = "tok-123";
return s;
}
int main() {
NullSink sink;
// CSRF: synchronizer via header and form field.
{
CsrfGuard g(CsrfGuard::Policy(), "hmac-key-for-tests", sink);
Session s = mk_session();
auto r = post("/submit");
HttpResponseView res;
check(!g.enforce(r, res, s) && res.status == 403, "csrf missing token 403");
auto r2 = post("/submit");
r2.headers.emplace_back("x-csrf-token", "tok-123");
check(g.enforce(r2, res, s), "csrf header token passes");
auto r3 = post("/submit");
check(g.check(r3, s, "tok-123") == CsrfGuard::Verdict::Allow, "csrf form field passes");
Session other = mk_session();
other.id = "BBBBBBBBBBBBBBBBBBBBBB";
other.csrf_token = "tok-999";
auto r4 = post("/submit");
r4.headers.emplace_back("x-csrf-token", "tok-123");
check(g.check(r4, other) == CsrfGuard::Verdict::Deny, "csrf foreign token fails");
HttpRequestView safe;
safe.method = "GET";
check(g.check(safe, s) == CsrfGuard::Verdict::Allow, "csrf safe method passes");
}
// CSRF: signed double-submit fallback.
{
CsrfGuard g(CsrfGuard::Policy(), "hmac-key-for-tests", sink);
Session s = mk_session();
std::string nonce = "n-1";
std::string cookie = nonce + "." + g.fallback_token(s.id, nonce);
auto r = post("/submit");
r.headers.emplace_back("cookie", "molo_csrf=" + cookie);
r.headers.emplace_back("x-csrf-token", cookie);
check(g.check(r, s) == CsrfGuard::Verdict::Allow, "csrf fallback passes");
auto r2 = post("/submit");
r2.headers.emplace_back("cookie", "molo_csrf=" + cookie);
r2.headers.emplace_back("x-csrf-token", "tampered");
check(g.check(r2, s) == CsrfGuard::Verdict::Deny, "csrf fallback echo fails");
}
// CSRF: Fetch Metadata block and exemptions.
{
CsrfGuard::Policy p;
p.exempt_paths = {"/hooks/stripe"};
CsrfGuard g(p, "k", sink);
Session s = mk_session();
auto r = post("/submit");
r.headers.emplace_back("sec-fetch-site", "cross-site");
r.headers.emplace_back("x-csrf-token", "tok-123");
check(g.check(r, s) == CsrfGuard::Verdict::Deny, "csrf cross-site blocked");
auto r2 = post("/hooks/stripe");
check(g.check(r2, s) == CsrfGuard::Verdict::Exempt, "csrf exempt path");
}
// CORS: default deny, allow-list, preflight, wildcard guard.
{
CorsPolicy::Policy p;
check(CorsPolicy::validate(p).empty(), "cors empty valid");
CorsPolicy::Policy bad;
bad.allow_origins = {"*"};
bad.allow_credentials = true;
check(!CorsPolicy::validate(bad).empty(), "cors wildcard+creds rejected");
CorsPolicy closed(p, sink);
auto r = post("/api");
r.headers.emplace_back("origin", "https://evil.test");
HttpResponseView res;
closed.apply(r, res);
check(res.header("Access-Control-Allow-Origin").empty(), "cors default deny");
CorsPolicy::Policy open;
open.allow_origins = {"https://app.test"};
open.allow_credentials = true;
CorsPolicy c(open, sink);
auto r2 = post("/api");
r2.headers.emplace_back("origin", "https://app.test");
HttpResponseView res2;
c.apply(r2, res2);
check(res2.header("Access-Control-Allow-Origin") == "https://app.test" &&
res2.header("Access-Control-Allow-Credentials") == "true",
"cors exact origin with creds");
HttpRequestView pre;
pre.method = "OPTIONS";
pre.path = "/api";
pre.headers.emplace_back("origin", "https://app.test");
pre.headers.emplace_back("access-control-request-method", "POST");
HttpResponseView res3;
check(c.preflight(pre, res3) && res3.status == 204 &&
!res3.header("Access-Control-Allow-Methods").empty(),
"cors preflight");
HttpRequestView pre2 = pre;
pre2.headers.clear();
pre2.headers.emplace_back("origin", "https://evil.test");
pre2.headers.emplace_back("access-control-request-method", "POST");
HttpResponseView res4;
check(c.preflight(pre2, res4) && res4.status == 403, "cors preflight deny");
}
// Security headers: all six families, TLS-gated HSTS, opt-out.
{
SecurityHeaders h;
HttpRequestView r;
r.is_tls = true;
r.path = "/";
HttpResponseView res;
h.apply(r, res);
check(!res.header("Strict-Transport-Security").empty() &&
res.header("X-Frame-Options") == "DENY" &&
res.header("X-Content-Type-Options") == "nosniff" &&
!res.header("Referrer-Policy").empty() &&
!res.header("Permissions-Policy").empty() &&
!res.header("Content-Security-Policy").empty(),
"headers six families");
HttpRequestView plain;
plain.path = "/";
HttpResponseView res2;
h.apply(plain, res2);
check(res2.header("Strict-Transport-Security").empty() &&
!res2.header("X-Frame-Options").empty(),
"hsts tls only");
SecurityHeaders::Policy p;
p.opt_out_paths = {"/legacy"};
SecurityHeaders h2(p);
HttpRequestView leg;
leg.is_tls = true;
leg.path = "/legacy";
HttpResponseView res3;
h2.apply(leg, res3);
check(res3.header("X-Frame-Options").empty(), "headers opt-out");
}
// Rate limiter: burst then 429 with Retry-After.
{
RateLimiter::Policy p;
p.per_ip = {5, 1, 10}; // 5/min, no burst headroom
p.per_user = {1000, 2, 10};
RateLimiter lim(p, &sink);
std::uint64_t now = 9000;
lim.set_clock([&] { return now; });
HttpRequestView r;
r.peer_ip = "10.1.1.1";
r.path = "/t";
int allowed = 0;
for (int i = 0; i < 7; ++i) {
if (lim.check(r, "").allowed) ++allowed;
}
check(allowed == 5, "ratelimit burst cap");
HttpResponseView res;
check(!lim.enforce(r, res, "") && res.status == 429 &&
!res.header("Retry-After").empty(),
"ratelimit 429 with retry-after");
now += 61;
check(lim.check(r, "").allowed, "ratelimit refills");
}
// Body limits: 431/413 mapping, JSON depth, form fields.
{
BodyLimits lim(BodyLimits::Policy(), sink);
HttpRequestView r;
HttpResponseView res;
check(!lim.enforce(lim.check_headers(17000, 100, r), r, res) && res.status == 431,
"limits headers total 431");
check(!lim.enforce(lim.check_headers(100, 9000, r), r, res), "limits single 431");
check(!lim.enforce(lim.check_body(2 * 1024 * 1024, 0, r), r, res) && res.status == 413,
"limits body 413");
check(lim.enforce(lim.check_body(2 * 1024 * 1024, 5 * 1024 * 1024, r), r, res),
"limits route override");
check(!lim.enforce(lim.check_body(11 * 1024 * 1024, 100 * 1024 * 1024, r), r, res),
"limits override capped at 10MB");
std::string deep(33, '[');
check(!lim.check_json_depth(deep, r), "limits json depth 33");
check(lim.check_json_depth("{\"a\":[1,2]}", r), "limits json ok");
check(!lim.check_form_fields(1001, r) && lim.check_form_fields(1000, r),
"limits form fields");
}
// Path guard: traversal, abs, drive, symlink, jail.
{
std::string out;
check(PathGuard::canonicalize("/a/b/../c", out) && out == "/a/c", "path canonical");
check(!PathGuard::canonicalize("/../etc", out), "path climb rejected");
check(!PathGuard::canonicalize("/a/../../x", out), "path deep climb");
check(!PathGuard::canonicalize("C:\\win", out), "path drive rejected");
::mkdir("/tmp/molo_path_root", 0755);
::mkdir("/tmp/molo_path_root/sub", 0755);
::unlink("/tmp/molo_path_root/link");
check(::symlink("/etc", "/tmp/molo_path_root/link") == 0, "path fixture symlink");
check(PathGuard::check("/tmp/molo_path_root", "/sub", out, &sink, nullptr) &&
out == "/tmp/molo_path_root/sub",
"path jail ok");
check(!PathGuard::check("/tmp/molo_path_root", "/link/passwd", out, &sink, nullptr),
"path symlink rejected");
check(PathGuard::check("/tmp/molo_path_root", "/link", out, &sink, nullptr, true),
"path symlink allowed when opted in");
::unlink("/tmp/molo_path_root/link");
}
// SQL safety contract.
{
BoundQuery q{"SELECT * FROM t WHERE a = ? AND b = ?",
{{BoundValue::Type::Text, "x"}, {BoundValue::Type::Int, "1"}}};
std::string reason;
check(SqlSafety::check(q, reason), "sql bound ok");
BoundQuery bad{"SELECT * FROM t WHERE a = ?",
{{BoundValue::Type::Text, "x"}, {BoundValue::Type::Int, "1"}}};
check(!SqlSafety::check(bad, reason), "sql arity mismatch");
check(SqlSafety::quote_identifier("users") == "\"users\"", "sql quote ok");
check(SqlSafety::quote_identifier("a;b").empty(), "sql quote rejects");
}
std::printf(failures == 0 ? "ALL GUARD TESTS PASSED\n" : "FAILURES: %d\n", failures);
return failures == 0 ? 0 : 1;
}