diff --git a/include/mbs/script.hpp b/include/mbs/script.hpp index 4a08284..ee502d2 100644 --- a/include/mbs/script.hpp +++ b/include/mbs/script.hpp @@ -19,7 +19,7 @@ namespace mbs { }; /// Lex + parse + static checks (unknown methods / bad arity). - TestResult test(std::string_view source); + TestResult test(std::string_view source, std::size_t max_source_length = 0); struct EvalOk { nlohmann::json value; @@ -32,7 +32,8 @@ namespace mbs { using EvalValueResult = std::variant; - EvalValueResult eval_val(std::string_view source, const nlohmann::json &context); + EvalValueResult eval_val(std::string_view source, const nlohmann::json &context, + std::size_t max_source_length = 0); struct EvalBoolResult { bool ok{false}; @@ -41,5 +42,6 @@ namespace mbs { std::optional loc; }; - EvalBoolResult eval_true(std::string_view source, const nlohmann::json &context); + EvalBoolResult eval_true(std::string_view source, const nlohmann::json &context, + std::size_t max_source_length = 0); } // namespace mbs diff --git a/src/evaluator.cpp b/src/evaluator.cpp index 9262732..de4149f 100644 --- a/src/evaluator.cpp +++ b/src/evaluator.cpp @@ -38,7 +38,7 @@ bool is_integral_number(const nlohmann::json& j, std::int64_t* out) { return false; } if (intpart < static_cast(std::numeric_limits::min()) || - intpart > static_cast(std::numeric_limits::max())) { + intpart >= static_cast(std::numeric_limits::max()) + 1.0) { return false; } *out = static_cast(intpart); @@ -269,20 +269,28 @@ nlohmann::json eval_binary_shortcircuit(ast::BinOp op, const ast::Expr& expr, co return nullptr; case ast::BinOp::Add: if (l.is_number() && r.is_number()) { - return l.get() + r.get(); + double result = l.get() + r.get(); + if (!std::isfinite(result)) return nullptr; + return result; } if (l.is_string() && r.is_string()) { - return l.get_ref() + r.get_ref(); + std::string result = l.get_ref() + r.get_ref(); + if (result.size() > MAX_STRING_SIZE) return nullptr; + return result; } return nullptr; case ast::BinOp::Sub: if (l.is_number() && r.is_number()) { - return l.get() - r.get(); + double result = l.get() - r.get(); + if (!std::isfinite(result)) return nullptr; + return result; } return nullptr; case ast::BinOp::Mul: { if (l.is_number() && r.is_number()) { - return l.get() * r.get(); + double result = l.get() * r.get(); + if (!std::isfinite(result)) return nullptr; + return result; } if (l.is_string() && r.is_number()) { auto n = repeat_count(r); @@ -302,12 +310,16 @@ nlohmann::json eval_binary_shortcircuit(ast::BinOp op, const ast::Expr& expr, co if (rd == 0.0) { return nullptr; } - return l.get() / rd; + double result = l.get() / rd; + if (!std::isfinite(result)) return nullptr; + return result; } return nullptr; case ast::BinOp::Pow: if (l.is_number() && r.is_number()) { - return std::pow(l.get(), r.get()); + double result = std::pow(l.get(), r.get()); + if (!std::isfinite(result)) return nullptr; + return result; } return nullptr; default: @@ -362,7 +374,9 @@ nlohmann::json eval_expr(const ast::Expr& expr, const nlohmann::json& context) { if (!c.is_number()) { return nullptr; } - return -c.get(); + double result = -c.get(); + if (!std::isfinite(result)) return nullptr; + return result; } return nullptr; } diff --git a/src/lexer.cpp b/src/lexer.cpp index ceea479..bc01cf2 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace mbs { @@ -77,7 +78,14 @@ Token Lexer::lexNumber() { } } const std::string_view numText = src_.substr(begin, pos_ - begin); - const double v = std::stod(std::string(numText)); + double v = 0; + try { + v = std::stod(std::string(numText)); + } catch (const std::out_of_range&) { + throw std::runtime_error( + "number literal out of range at line " + std::to_string(start.line) + + " column " + std::to_string(start.column)); + } Token t{TokenKind::Number, numText, v, {}, start}; return t; } diff --git a/src/script.cpp b/src/script.cpp index 1d7234e..dd3c336 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -7,8 +7,11 @@ namespace mbs { -TestResult test(std::string_view source) { +TestResult test(std::string_view source, std::size_t max_source_length) { try { + if (max_source_length > 0 && source.size() > max_source_length) { + return TestResult{false, "source exceeds maximum length", std::nullopt}; + } const auto ast = parse(source); if (const auto err = semantic_check(*ast); err.has_value()) { return TestResult{false, *err, std::nullopt}; @@ -22,8 +25,12 @@ TestResult test(std::string_view source) { } } -EvalValueResult eval_val(const std::string_view source, const nlohmann::json& context) { +EvalValueResult eval_val(const std::string_view source, const nlohmann::json& context, + std::size_t max_source_length) { try { + if (max_source_length > 0 && source.size() > max_source_length) { + return EvalErr{"source exceeds maximum length", std::nullopt}; + } const auto ast = parse(source); if (const auto err = semantic_check(*ast); err.has_value()) { return EvalErr{*err, std::nullopt}; @@ -37,8 +44,9 @@ EvalValueResult eval_val(const std::string_view source, const nlohmann::json& co } } -EvalBoolResult eval_true(const std::string_view source, const nlohmann::json& context) { - const auto r = eval_val(source, context); +EvalBoolResult eval_true(const std::string_view source, const nlohmann::json& context, + std::size_t max_source_length) { + const auto r = eval_val(source, context, max_source_length); // Check if it resulted in an error if (std::holds_alternative(r)) { diff --git a/tests/test_script.cpp b/tests/test_script.cpp index 0580556..9f1dc85 100644 --- a/tests/test_script.cpp +++ b/tests/test_script.cpp @@ -165,3 +165,76 @@ TEST_CASE("moderate string repeat still works", "[security]") { REQUIRE(std::holds_alternative(r)); REQUIRE(std::get(r).value.get().size() == 200); } + +TEST_CASE("NaN/Infinity from arithmetic returns null (S4)", "[security]") { + SECTION("0 ^ -1 is Inf -> null") { + auto r = eval_val(R"(0 ^ -1)", json::object()); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); + } + SECTION("(-1) ^ 0.5 is NaN -> null") { + auto r = eval_val(R"((-1) ^ 0.5)", json::object()); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); + } + SECTION("1e308 + 1e308 is Inf -> null") { + json ctx = {{"big", 1e308}}; + auto r = eval_val(R"(@big + @big)", ctx); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); + } + SECTION("1e308 * 2 is Inf -> null") { + json ctx = {{"big", 1e308}}; + auto r = eval_val(R"(@big * 2)", ctx); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); + } + SECTION("subtraction overflow -> null") { + json ctx = {{"big", 1e308}, {"negbig", -1e308}}; + auto r = eval_val(R"(@negbig - @big)", ctx); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); + } +} + +TEST_CASE("string concatenation exceeding MAX_STRING_SIZE returns null (S6)", "[security]") { + std::string big(600000, 'a'); + json ctx = {{"s1", big}, {"s2", big}}; + auto r = eval_val(R"(@s1 + @s2)", ctx); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value.is_null()); +} + +TEST_CASE("max_source_length rejects oversized input (S7)", "[security]") { + SECTION("eval_val rejects long source") { + auto r = eval_val("1 + 2", json::object(), 3); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).error.find("exceeds maximum length") != std::string::npos); + } + SECTION("eval_val accepts source within limit") { + auto r = eval_val("1 + 2", json::object(), 100); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value == 3.0); + } + SECTION("eval_true rejects long source") { + auto r = eval_true("true", json::object(), 2); + REQUIRE_FALSE(r.ok); + REQUIRE(r.error.find("exceeds maximum length") != std::string::npos); + } + SECTION("test rejects long source") { + auto t = test("1 + 2", 3); + REQUIRE_FALSE(t.ok); + REQUIRE(t.error.find("exceeds maximum length") != std::string::npos); + } + SECTION("zero max_source_length means unlimited") { + auto r = eval_val("1 + 2", json::object(), 0); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).value == 3.0); + } +} + +TEST_CASE("out-of-range number literal gives descriptive error (S8)", "[security]") { + auto r = eval_val("1e99999", json::object()); + REQUIRE(std::holds_alternative(r)); + REQUIRE(std::get(r).error.find("out of range") != std::string::npos); +}