feat(workflow): example covers security, platform, enterprise and resilience features
105 route tests, matrix crawl, spec and README updated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
// Route: GET /security/admin (Bearer token, role permission check)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
using molo::example::fw::Method;
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(contains(f.client->get("/security/roles").body, "admin:manage"), "roles page lists permissions");
|
||||
auto none = f.client->get("/security/admin");
|
||||
check(none.status == 401 && none.headers.get("WWW-Authenticate") == "Bearer", "no token 401");
|
||||
auto junk = f.client->request(Method::Get, "/security/admin", "", {{"Authorization", "Bearer nope"}});
|
||||
check(junk.status == 401, "unknown token 401");
|
||||
auto viewer = f.client->request(Method::Get, "/security/admin", "", {{"Authorization", "Bearer viewer-token"}});
|
||||
check(viewer.status == 403 && contains(viewer.body, "viewer"), "viewer 403");
|
||||
auto admin = f.client->request(Method::Get, "/security/admin", "", {{"Authorization", "Bearer admin-token"}});
|
||||
check(admin.status == 200 && contains(admin.body, "\"role\":\"admin\""), "admin 200");
|
||||
check(f.client->post("/security/admin", "x").status == 405, "POST 405");
|
||||
check(contains(f.client->get("/security/denials").body, "lacks admin:manage"), "side effect: denial recorded");
|
||||
return finish("security/admin");
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Route: OPTIONS /security/cors (origin allowlist preflight)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
using molo::example::fw::Method;
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(f.client->get("/security/cors").status == 200, "info page 200");
|
||||
auto ok = f.client->request(Method::Options, "/security/cors", "",
|
||||
{{"Origin", "https://app.example.com"}});
|
||||
check(ok.status == 204, "allowed origin is 204");
|
||||
check(ok.headers.get("Access-Control-Allow-Origin") == "https://app.example.com", "origin echoed");
|
||||
check(ok.headers.has("Access-Control-Allow-Methods"), "methods header");
|
||||
auto bad = f.client->request(Method::Options, "/security/cors", "", {{"Origin", "https://evil.example"}});
|
||||
check(bad.status == 403, "unknown origin is 403");
|
||||
check(!bad.headers.has("Access-Control-Allow-Origin"), "no allow header on denial");
|
||||
auto none = f.client->request(Method::Options, "/security/cors");
|
||||
check(none.status == 403, "missing origin is 403");
|
||||
auto post = f.client->post("/security/cors", "x");
|
||||
check(post.status == 405, "POST is 405");
|
||||
check(contains(f.client->get("/security/denials").body, "CorsPolicy"), "side effect: denial recorded");
|
||||
return finish("security/cors");
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Route: /security/csrf (signed token, inline 403 on bad token)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
static const char* kForm = "application/x-www-form-urlencoded";
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto page = f.client->get("/security/csrf");
|
||||
check(page.status == 200 && contains(page.body, "<form"), "form renders");
|
||||
std::size_t at = page.body.find("value=\"");
|
||||
at = page.body.find("name=\"csrf_token\" value=\"");
|
||||
check(at != std::string::npos, "token field prefilled");
|
||||
at += std::string("name=\"csrf_token\" value=\"").size();
|
||||
std::string token = page.body.substr(at, page.body.find('"', at) - at);
|
||||
auto ok = f.client->post("/security/csrf", "csrf_token=" + token + "¬e=hi", kForm);
|
||||
check(ok.status == 200 && contains(ok.body, "Saved: hi"), "valid token accepted");
|
||||
auto missing = f.client->post("/security/csrf", "note=hi", kForm);
|
||||
check(missing.status == 403, "missing token is 403");
|
||||
check(contains(missing.body, "form-errors") && contains(missing.body, "invalid CSRF token"), "inline html error");
|
||||
check(!contains(missing.body, "\"error\""), "no raw json");
|
||||
auto bad = f.client->post("/security/csrf", "csrf_token=abc.def¬e=hi", kForm);
|
||||
check(bad.status == 403, "forged token is 403");
|
||||
auto longnote = f.client->post("/security/csrf", "csrf_token=" + token + "¬e=" + std::string(81, 'x'), kForm);
|
||||
check(longnote.status == 422, "note over 80 chars is 422");
|
||||
auto api = f.client->request(molo::example::fw::Method::Post, "/security/csrf", "note=hi",
|
||||
{{"Content-Type", kForm}, {"Accept", "application/json"}});
|
||||
check(api.status == 403 && contains(api.body, "CsrfGuard"), "json client gets json");
|
||||
auto denials = f.client->get("/security/denials");
|
||||
check(contains(denials.body, "CsrfGuard"), "side effect: denial recorded");
|
||||
return finish("security/csrf");
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Route: GET /security/denials (capped ring of refusals)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
static const char* kForm = "application/x-www-form-urlencoded";
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(contains(f.client->get("/security/denials").body, "\"total\":0"), "starts empty");
|
||||
for (int i = 0; i < 120; ++i) f.client->post("/security/guard", "kind=x&value=a", kForm);
|
||||
auto r = f.client->get("/security/denials");
|
||||
check(r.status == 200 && contains(r.body, "\"total\":120"), "total counts all");
|
||||
std::size_t n = 0, p = 0;
|
||||
while ((p = r.body.find("\"guard\"", p)) != std::string::npos) { ++n; p += 7; }
|
||||
check(n == 100, "ring capped at 100");
|
||||
check(f.client->post("/security/denials", "x").status == 405, "POST 405");
|
||||
return finish("security/denials");
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Route: /security/guard (SqlSafety and PathGuard rejection)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
static const char* kForm = "application/x-www-form-urlencoded";
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(contains(f.client->get("/security/guard").body, "<form"), "form renders");
|
||||
auto ok = f.client->post("/security/guard", "kind=sql&value=ada", kForm);
|
||||
check(ok.status == 200 && contains(ok.body, "safe for sql"), "safe sql value");
|
||||
auto sql = f.client->post("/security/guard", "kind=sql&value=x%27+OR+1%3D1+--", kForm);
|
||||
check(sql.status == 422 && contains(sql.body, "form-errors"), "sql injection inline 422");
|
||||
auto path = f.client->post("/security/guard", "kind=path&value=..%2F..%2Fetc%2Fpasswd", kForm);
|
||||
check(path.status == 422 && contains(path.body, "traversal"), "traversal rejected");
|
||||
auto abs = f.client->post("/security/guard", "kind=path&value=%2Fetc%2Fpasswd", kForm);
|
||||
check(abs.status == 422 && contains(abs.body, "absolute"), "absolute rejected");
|
||||
check(f.client->post("/security/guard", "kind=x&value=a", kForm).status == 422, "bad kind 422");
|
||||
check(f.client->post("/security/guard", "kind=sql&value=" + std::string(513, 'a'), kForm).status == 422, "over cap 422");
|
||||
check(contains(f.client->get("/security/denials").body, "PathGuard"), "side effect: denial recorded");
|
||||
return finish("security/guard");
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Route: GET /security/headers (hardening headers applied)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto r = f.client->get("/security/headers");
|
||||
check(r.status == 200, "status 200");
|
||||
check(r.headers.get("X-Content-Type-Options") == "nosniff", "nosniff");
|
||||
check(r.headers.get("X-Frame-Options") == "DENY", "frame deny");
|
||||
check(r.headers.has("Strict-Transport-Security"), "hsts");
|
||||
check(contains(r.body, "Referrer-Policy"), "body lists headers");
|
||||
check(f.client->post("/security/headers", "x").status == 405, "POST is 405");
|
||||
return finish("security/headers");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Route: /security/login (HTML login form, session cookie)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
static const char* kForm = "application/x-www-form-urlencoded";
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(contains(f.client->get("/security/login").body, "type=\"password\""), "form renders");
|
||||
check(f.client->get("/security/whoami").status == 401, "anonymous whoami 401");
|
||||
auto bad = f.client->post("/security/login", "username=ada&password=nope", kForm);
|
||||
check(bad.status == 401 && contains(bad.body, "wrong username or password"), "bad creds inline 401");
|
||||
check(!contains(bad.body, "\"error\""), "no raw json");
|
||||
auto empty = f.client->post("/security/login", "username=&password=", kForm);
|
||||
check(empty.status == 422 && contains(empty.body, "form-errors"), "empty inline 422");
|
||||
auto ok = f.client->post("/security/login", "username=ada&password=correct-horse", kForm);
|
||||
check(ok.status == 200 && contains(ok.body, "Signed in as ada"), "login ok");
|
||||
check(contains(ok.headers.get("Set-Cookie"), "login_sid="), "session cookie set");
|
||||
auto who = f.client->get("/security/whoami");
|
||||
check(who.status == 200 && contains(who.body, "\"user\":\"ada\""), "whoami via cookie");
|
||||
check(f.services.sessions->size() >= 1, "side effect: session stored");
|
||||
return finish("security/login");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Route: GET /security/rate (5 hits per 60 s per client, then 429)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
using molo::example::fw::Method;
|
||||
int main() {
|
||||
Fixture f;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
auto r = f.client->request(Method::Get, "/security/rate", "", {{"X-Client-Id", "a"}});
|
||||
check(r.status == 200, "hit " + std::to_string(i + 1) + " allowed");
|
||||
if (i == 4) check(contains(r.body, "\"remaining\":0"), "remaining reaches 0");
|
||||
}
|
||||
auto limited = f.client->request(Method::Get, "/security/rate", "", {{"X-Client-Id", "a"}});
|
||||
check(limited.status == 429, "6th hit is 429");
|
||||
int retry = std::atoi(limited.headers.get("Retry-After").c_str());
|
||||
check(retry >= 1 && retry <= 61, "Retry-After within window");
|
||||
auto other = f.client->request(Method::Get, "/security/rate", "", {{"X-Client-Id", "b"}});
|
||||
check(other.status == 200, "other client unaffected");
|
||||
check(f.client->post("/security/rate", "x").status == 405, "POST is 405");
|
||||
check(contains(f.client->get("/security/denials").body, "RateLimiter"), "side effect: denial recorded");
|
||||
return finish("security/rate");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Route: GET /security/secrets (values are always masked)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto list = f.client->get("/security/secrets");
|
||||
check(list.status == 200 && contains(list.body, "DB_PASSWORD"), "names listed");
|
||||
check(!contains(list.body, "demo-db-password"), "no values in list");
|
||||
auto one = f.client->get("/security/secrets?name=API_KEY");
|
||||
check(one.status == 200 && contains(one.body, "de*"), "masked value");
|
||||
check(!contains(one.body, "demo-api-key-abcdef"), "full value never returned");
|
||||
check(f.client->get("/security/secrets?name=NOPE").status == 404, "unknown 404");
|
||||
check(f.client->post("/security/secrets", "x").status == 405, "POST 405");
|
||||
return finish("security/secrets");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Route: /security/upload (extension allowlist, size cap, magic bytes)
|
||||
#include "support/Assert.hpp"
|
||||
using namespace molo::example::test;
|
||||
static const char* kForm = "application/x-www-form-urlencoded";
|
||||
int main() {
|
||||
Fixture f;
|
||||
check(contains(f.client->get("/security/upload").body, "<form"), "form renders");
|
||||
auto ok = f.client->post("/security/upload", "filename=a.pdf&content_hex=255044462d31", kForm);
|
||||
check(ok.status == 200 && contains(ok.body, "Accepted a.pdf"), "valid pdf accepted");
|
||||
auto fake = f.client->post("/security/upload", "filename=a.png&content_hex=255044462d31", kForm);
|
||||
check(fake.status == 422 && contains(fake.body, "magic bytes"), "magic mismatch inline 422");
|
||||
auto ext = f.client->post("/security/upload", "filename=a.exe&content_hex=4d5a", kForm);
|
||||
check(ext.status == 422 && contains(ext.body, "extension not allowed"), "bad extension");
|
||||
auto path = f.client->post("/security/upload", "filename=..%2Fa.txt&content_hex=6869", kForm);
|
||||
check(path.status == 422, "path in name rejected");
|
||||
auto hex = f.client->post("/security/upload", "filename=a.txt&content_hex=zz", kForm);
|
||||
check(hex.status == 422 && contains(hex.body, "hex digits"), "bad hex");
|
||||
auto big = f.client->post("/security/upload", "filename=a.txt&content_hex=" + std::string(2050, '4') + std::string(2050, '1'), kForm);
|
||||
check(big.status == 422 || big.status == 413, "oversize rejected");
|
||||
auto bigok = f.client->post("/security/upload", "filename=a.txt&content_hex=" + std::string(2050, '6'), kForm);
|
||||
check(bigok.status == 413, "over 1024 bytes is 413");
|
||||
check(contains(f.client->get("/security/denials").body, "UploadValidator"), "side effect: denial recorded");
|
||||
return finish("security/upload");
|
||||
}
|
||||
Reference in New Issue
Block a user