Files
mbs/tests/test_script.cpp
Allan K. Koech 9d5571b7d2 feat: critical security fixes - string repeat cap, parser/evaluator depth limits
- Cap string repeat to MAX_STRING_SIZE (1MB) to prevent DoS via integer overflow
- Consolidate duplicated string*number/number*string repeat blocks into helper
- Add parser depth counter (limit 512) to prevent stack overflow from nested parens/unary chains
- Add evaluator depth counter (limit 512) to prevent stack overflow during eval
- Add EvalError type for evaluation-time errors
- Add test cases for all security fixes
2026-08-20 01:14:52 +03:00

168 lines
5.4 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <mbs/script.hpp>
#include <nlohmann/json.hpp>
using mbs::EvalErr;
using mbs::EvalOk;
using mbs::eval_true;
using mbs::eval_val;
using mbs::test;
using nlohmann::json;
TEST_CASE("single-line comments are skipped", "[lexer]") {
auto r = eval_val(
R"(
// comment
1 + 2 // trailing
)",
json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 3.0);
}
TEST_CASE("string times integer repeats like Python", "[ops]") {
auto r = eval_val(R"("ab" * 3)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "ababab");
}
TEST_CASE("integer times string repeats", "[ops]") {
auto r = eval_val(R"(4 * "x")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "xxxx");
}
TEST_CASE("string times non-integral number is invalid (null)", "[ops]") {
auto r = eval_val(R"("a" * 2.5)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("string times zero yields empty string", "[ops]") {
auto r = eval_val(R"("hi" * 0)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "");
}
TEST_CASE("power requires numeric operands", "[ops]") {
SECTION("string ^ number -> null") {
auto r = eval_val(R"("a" ^ 2)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("number ^ string -> null") {
auto r = eval_val(R"(2 ^ "b")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
SECTION("valid number power") {
auto r = eval_val(R"(2 ^ 3)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 8.0);
}
}
TEST_CASE("invalid mixed arithmetic returns null", "[ops]") {
auto r = eval_val(R"("a" + 1)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("string + string concatenation", "[ops]") {
auto r = eval_val(R"("foo" + "bar")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == "foobar");
}
TEST_CASE("test() rejects unknown methods", "[test]") {
auto t = test(R"(@x.unknown())");
REQUIRE_FALSE(t.ok);
REQUIRE(t.error.find("unknown method") != std::string::npos);
}
TEST_CASE("test() accepts known script", "[test]") {
auto t = test(R"(@user.age > 0 && ['a'].has("a"))");
REQUIRE(t.ok);
}
TEST_CASE("eval_true uses truthiness", "[eval]") {
json ctx = {{"flag", false}};
auto b = eval_true("@flag", ctx);
REQUIRE(b.ok);
REQUIRE_FALSE(b.value);
}
TEST_CASE("context variable missing is null", "[eval]") {
auto r = eval_val("@missing", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("len on number returns number", "[builtin]") {
auto r = eval_val(R"( (5).len() )", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == 5.0);
}
TEST_CASE("array literal has membership", "[eval]") {
json ctx = {{"role", "student"}};
auto r = eval_val(R"(['student','teacher'].has(@role))", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == true);
}
TEST_CASE("short-circuit and skips rhs", "[eval]") {
json ctx = json::object();
auto r = eval_val(R"(false && @nope.foo)", ctx);
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value == false);
}
TEST_CASE("string repeat with huge count returns null", "[security]") {
auto r = eval_val(R"("x" * 999999999)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("number * string with huge count returns null", "[security]") {
auto r = eval_val(R"(999999999 * "y")", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.is_null());
}
TEST_CASE("deeply nested parentheses throw parse depth error", "[security]") {
std::string expr(2000, '(');
expr += "1";
expr += std::string(2000, ')');
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("deeply nested") != std::string::npos);
}
TEST_CASE("deeply nested unary chains throw parse depth error", "[security]") {
std::string expr(2000, '!');
expr += "true";
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
REQUIRE(std::get<EvalErr>(r).error.find("deeply nested") != std::string::npos);
}
TEST_CASE("evaluation depth exceeded throws error", "[security]") {
std::string expr(600, '(');
expr += "1";
expr += std::string(600, ')');
auto r = eval_val(expr, json::object());
REQUIRE(std::holds_alternative<EvalErr>(r));
auto& err = std::get<EvalErr>(r).error;
bool is_depth_error = err.find("deeply nested") != std::string::npos ||
err.find("depth exceeded") != std::string::npos;
REQUIRE(is_depth_error);
}
TEST_CASE("moderate string repeat still works", "[security]") {
auto r = eval_val(R"("ab" * 100)", json::object());
REQUIRE(std::holds_alternative<EvalOk>(r));
REQUIRE(std::get<EvalOk>(r).value.get<std::string>().size() == 200);
}