Data: qualify TOML parser limits (#46)
This commit is contained in:
1 parent
b3c566ed4d
commit
9ccfbf7838
2 files changed
+358
-6
No files matched your search
@@ -347,8 +347,17 @@ namespace Void::Data::Toml
|
||||
};
|
||||
|
||||
struct ParseOptions {
|
||||
/// Maximum nesting level reached from the document root. It is
|
||||
/// enforced uniformly for values (arrays, inline tables), dotted key
|
||||
/// paths, table headers and array-of-tables headers.
|
||||
std::size_t max_depth{64};
|
||||
std::size_t max_input_bytes{16ULL * 1024ULL * 1024ULL};
|
||||
/// Maximum number of document nodes (table entries, array elements
|
||||
/// and array-of-tables entries plus their tables). Zero disables the
|
||||
/// limit, preserving the historical behaviour. `max_input_bytes`
|
||||
/// already bounds node count linearly; this knob bounds the constant
|
||||
/// memory factor for untrusted input.
|
||||
std::size_t max_nodes{0};
|
||||
};
|
||||
|
||||
namespace Detail
|
||||
@@ -534,7 +543,7 @@ namespace Void::Data::Toml
|
||||
parse_table_header(doc);
|
||||
} else {
|
||||
parse_key_value_into(*current_table_, *current_meta_,
|
||||
false, 0U);
|
||||
false, table_depth_);
|
||||
skip_ws();
|
||||
skip_comment_to_eol();
|
||||
if (pos_ < input_.size() && !consume_newline()) {
|
||||
@@ -914,11 +923,19 @@ namespace Void::Data::Toml
|
||||
return KeyPart{std::move(bare), start};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<KeyPart> parse_key_path()
|
||||
/// Parse a dotted key path, rejecting it as soon as it grows past
|
||||
/// `max_segments`. Checking while parsing keeps a pathologically
|
||||
/// long path from being materialised before the depth limit applies.
|
||||
[[nodiscard]] std::vector<KeyPart>
|
||||
parse_key_path(std::size_t max_segments)
|
||||
{
|
||||
std::vector<KeyPart> path;
|
||||
path.push_back(parse_simple_key());
|
||||
while (true) {
|
||||
if (path.size() > max_segments) {
|
||||
fail("key path exceeds max_depth",
|
||||
path.back().origin);
|
||||
}
|
||||
skip_ws();
|
||||
if (pos_ < input_.size() && input_[pos_] == '.') {
|
||||
(void)advance_byte();
|
||||
@@ -931,6 +948,32 @@ namespace Void::Data::Toml
|
||||
return path;
|
||||
}
|
||||
|
||||
/// A dotted key of N segments nests its value N-1 levels below the
|
||||
/// current base depth, so it may hold one more segment than a table
|
||||
/// header at the same limit.
|
||||
[[nodiscard]] std::size_t
|
||||
key_path_budget(std::size_t depth) const noexcept
|
||||
{
|
||||
if (depth > options_.max_depth) {
|
||||
return 0;
|
||||
}
|
||||
const std::size_t remaining = options_.max_depth - depth;
|
||||
if (remaining == std::numeric_limits<std::size_t>::max()) {
|
||||
return remaining;
|
||||
}
|
||||
return remaining + 1U;
|
||||
}
|
||||
|
||||
void note_node()
|
||||
{
|
||||
++node_count_;
|
||||
if (options_.max_nodes != 0 &&
|
||||
node_count_ > options_.max_nodes) {
|
||||
fail(std::format("TOML document exceeds max_nodes ({})",
|
||||
options_.max_nodes));
|
||||
}
|
||||
}
|
||||
|
||||
void parse_table_header(Document& doc)
|
||||
{
|
||||
const Origin hdr_origin = here();
|
||||
@@ -941,7 +984,8 @@ namespace Void::Data::Toml
|
||||
(void)advance_byte();
|
||||
}
|
||||
skip_ws();
|
||||
auto path = parse_key_path();
|
||||
auto path = parse_key_path(options_.max_depth);
|
||||
table_depth_ = path.size();
|
||||
skip_ws();
|
||||
if (pos_ >= input_.size() || input_[pos_] != ']') {
|
||||
fail("expected ']' closing table header", hdr_origin);
|
||||
@@ -975,6 +1019,7 @@ namespace Void::Data::Toml
|
||||
auto [mit, mok] = cur_meta->children.emplace(
|
||||
seg.name, std::move(meta_ptr));
|
||||
static_cast<void>(mok);
|
||||
note_node();
|
||||
cur_tbl = it->second.as_table();
|
||||
cur_meta = mit->second.get();
|
||||
} else {
|
||||
@@ -1014,6 +1059,7 @@ namespace Void::Data::Toml
|
||||
auto [mit, mok] = cur_meta->children.emplace(
|
||||
leaf.name, std::move(meta_ptr));
|
||||
static_cast<void>(mok);
|
||||
note_node();
|
||||
current_table_ = it->second.as_table();
|
||||
current_meta_ = mit->second.get();
|
||||
return;
|
||||
@@ -1046,6 +1092,8 @@ namespace Void::Data::Toml
|
||||
MetaNode* next_meta = meta_ptr->aot_items.back().get();
|
||||
cur_meta->children.emplace(leaf.name,
|
||||
std::move(meta_ptr));
|
||||
note_node(); // the array-of-tables value
|
||||
note_node(); // its first table
|
||||
current_table_ =
|
||||
&it->second.as_array_of_tables()->tables.back();
|
||||
current_meta_ = next_meta;
|
||||
@@ -1061,6 +1109,7 @@ namespace Void::Data::Toml
|
||||
}
|
||||
auto* aot = existing->as_array_of_tables();
|
||||
aot->tables.push_back(Table{});
|
||||
note_node();
|
||||
auto elem_meta = std::make_unique<MetaNode>();
|
||||
elem_meta->state = NodeState::ExplicitTable;
|
||||
leaf_meta->aot_items.push_back(std::move(elem_meta));
|
||||
@@ -1072,14 +1121,15 @@ namespace Void::Data::Toml
|
||||
void parse_key_value_into(Table& root_tbl, MetaNode& root_meta,
|
||||
bool in_inline, std::size_t depth)
|
||||
{
|
||||
auto path = parse_key_path();
|
||||
auto path = parse_key_path(key_path_budget(depth));
|
||||
const std::size_t value_depth = depth + path.size() - 1U;
|
||||
skip_ws();
|
||||
if (pos_ >= input_.size() || input_[pos_] != '=') {
|
||||
fail("expected '=' after key");
|
||||
}
|
||||
(void)advance_byte();
|
||||
skip_ws();
|
||||
Value val = parse_value(depth);
|
||||
Value val = parse_value(value_depth);
|
||||
|
||||
Table* cur_tbl = &root_tbl;
|
||||
MetaNode* cur_meta = &root_meta;
|
||||
@@ -1097,6 +1147,7 @@ namespace Void::Data::Toml
|
||||
auto [mit, mok] = cur_meta->children.emplace(
|
||||
seg.name, std::move(meta_ptr));
|
||||
static_cast<void>(mok);
|
||||
note_node();
|
||||
cur_tbl = it->second.as_table();
|
||||
cur_meta = mit->second.get();
|
||||
} else {
|
||||
@@ -1134,6 +1185,7 @@ namespace Void::Data::Toml
|
||||
meta_ptr->state = val_state;
|
||||
cur_meta->children.emplace(leaf.name, std::move(meta_ptr));
|
||||
cur_tbl->entries.emplace(leaf.name, std::move(val));
|
||||
note_node();
|
||||
}
|
||||
|
||||
[[nodiscard]] Value parse_value(std::size_t depth)
|
||||
@@ -1173,6 +1225,7 @@ namespace Void::Data::Toml
|
||||
return Value{std::move(arr), start};
|
||||
}
|
||||
arr.elements.push_back(parse_value(depth + 1U));
|
||||
note_node();
|
||||
skip_ws_and_comments(true);
|
||||
if (pos_ >= input_.size()) {
|
||||
fail("unterminated array", start);
|
||||
@@ -1681,6 +1734,8 @@ namespace Void::Data::Toml
|
||||
MetaNode root_meta_{};
|
||||
Table* current_table_{nullptr};
|
||||
MetaNode* current_meta_{nullptr};
|
||||
std::size_t table_depth_{0};
|
||||
std::size_t node_count_{0};
|
||||
};
|
||||
|
||||
inline void append_escaped_basic(std::string& out, std::string_view s)
|
||||
|
||||
@@ -232,4 +232,301 @@ Void::Testing::Test toml_reports_exact_origin_and_rejects_errors()
|
||||
Void::Testing::expect_eq(
|
||||
*super_after->root.find("a")->as_table()->find("y")->as_integer(),
|
||||
std::int64_t{2});
|
||||
}
|
||||
}
|
||||
namespace toml_limits_test_detail
|
||||
{
|
||||
[[nodiscard]] std::string repeated_key_path(std::size_t segments)
|
||||
{
|
||||
std::string out;
|
||||
for (std::size_t i = 0; i < segments; ++i) {
|
||||
if (i != 0) {
|
||||
out.push_back('.');
|
||||
}
|
||||
out.push_back('a');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string nested_arrays(std::size_t depth)
|
||||
{
|
||||
return std::string(depth, '[') + "1" + std::string(depth, ']');
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string nested_inline_tables(std::size_t depth)
|
||||
{
|
||||
std::string out;
|
||||
for (std::size_t i = 0; i < depth; ++i) {
|
||||
out += "{ k = ";
|
||||
}
|
||||
out.push_back('1');
|
||||
for (std::size_t i = 0; i < depth; ++i) {
|
||||
out += " }";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace toml_limits_test_detail
|
||||
|
||||
Void::Testing::Test toml_depth_limit_is_uniform_for_key_paths()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
using toml_limits_test_detail::repeated_key_path;
|
||||
|
||||
const std::string at_limit = repeated_key_path(64);
|
||||
const std::string over_limit = repeated_key_path(65);
|
||||
|
||||
// Table headers count every segment as one nesting level.
|
||||
const auto header_ok = parse("[" + at_limit + "]\nx = 1\n");
|
||||
Void::Testing::require(header_ok.has_value());
|
||||
|
||||
const auto header_over = parse("[" + over_limit + "]\n");
|
||||
Void::Testing::require(!header_over.has_value());
|
||||
Void::Testing::expect_eq(header_over.error().origin.line,
|
||||
std::size_t{1});
|
||||
Void::Testing::expect_eq(header_over.error().message,
|
||||
std::string{"key path exceeds max_depth"});
|
||||
|
||||
// Array-of-tables headers obey the same limit.
|
||||
const auto aot_over = parse("[[" + over_limit + "]]\n");
|
||||
Void::Testing::require(!aot_over.has_value());
|
||||
|
||||
// A dotted key of N segments nests its value N-1 levels, so it may use
|
||||
// one more segment than a table header at the same limit.
|
||||
const auto dotted_ok = parse(repeated_key_path(65) + " = 1\n");
|
||||
Void::Testing::require(dotted_ok.has_value());
|
||||
|
||||
const auto dotted_over = parse(repeated_key_path(66) + " = 1\n");
|
||||
Void::Testing::require(!dotted_over.has_value());
|
||||
Void::Testing::expect_eq(dotted_over.error().message,
|
||||
std::string{"key path exceeds max_depth"});
|
||||
|
||||
// Explicit small limits behave the same way.
|
||||
Void::Testing::require(!parse("a.b.c = 1\n", ParseOptions{.max_depth = 1})
|
||||
.has_value());
|
||||
Void::Testing::require(parse("a.b = 1\n", ParseOptions{.max_depth = 1})
|
||||
.has_value());
|
||||
Void::Testing::require(parse("[a.b]\n", ParseOptions{.max_depth = 2})
|
||||
.has_value());
|
||||
Void::Testing::require(!parse("[a.b.c]\n", ParseOptions{.max_depth = 2})
|
||||
.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_depth_limit_is_uniform_for_values()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
using toml_limits_test_detail::nested_arrays;
|
||||
using toml_limits_test_detail::nested_inline_tables;
|
||||
|
||||
Void::Testing::require(
|
||||
parse("a = " + nested_arrays(64) + "\n").has_value());
|
||||
Void::Testing::require(
|
||||
!parse("a = " + nested_arrays(65) + "\n").has_value());
|
||||
|
||||
Void::Testing::require(
|
||||
parse("a = " + nested_inline_tables(64) + "\n").has_value());
|
||||
Void::Testing::require(
|
||||
!parse("a = " + nested_inline_tables(65) + "\n").has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_depth_limit_combines_tables_and_values()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
using toml_limits_test_detail::nested_arrays;
|
||||
|
||||
// A value inside [t] starts one level deep, so only 63 nested arrays fit
|
||||
// under the default limit of 64.
|
||||
Void::Testing::require(
|
||||
parse("[t]\na = " + nested_arrays(63) + "\n").has_value());
|
||||
Void::Testing::require(
|
||||
!parse("[t]\na = " + nested_arrays(64) + "\n").has_value());
|
||||
|
||||
// Dotted key paths contribute their intermediate tables too.
|
||||
Void::Testing::require(parse("a.b = " + nested_arrays(63) + "\n")
|
||||
.has_value());
|
||||
Void::Testing::require(!parse("a.b = " + nested_arrays(64) + "\n")
|
||||
.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_input_byte_limit_boundaries()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
|
||||
const std::string doc = "a = 1\n";
|
||||
Void::Testing::require(
|
||||
parse(doc, ParseOptions{.max_input_bytes = doc.size()}).has_value());
|
||||
|
||||
const auto under =
|
||||
parse(doc, ParseOptions{.max_input_bytes = doc.size() - 1});
|
||||
Void::Testing::require(!under.has_value());
|
||||
Void::Testing::expect_eq(under.error().origin.line, std::size_t{1});
|
||||
Void::Testing::expect_eq(under.error().message,
|
||||
std::string{"TOML input exceeds max_input_bytes"});
|
||||
|
||||
// A huge but valid scalar fits under the default 16 MiB limit.
|
||||
const std::string scalar_body(1024U * 1024U, 'x');
|
||||
const std::string huge = "s = \"" + scalar_body + "\"\n";
|
||||
const auto huge_ok = parse(huge);
|
||||
Void::Testing::require(huge_ok.has_value());
|
||||
Void::Testing::require(huge_ok->root.find("s") != nullptr);
|
||||
Void::Testing::expect_eq(huge_ok->root.find("s")->as_string()->size(),
|
||||
scalar_body.size());
|
||||
|
||||
Void::Testing::require(
|
||||
!parse(huge, ParseOptions{.max_input_bytes = huge.size() - 1})
|
||||
.has_value());
|
||||
|
||||
// A huge numeric scalar is rejected as out of range, not accepted.
|
||||
const std::string huge_int = "i = " + std::string(64, '9') + "\n";
|
||||
const auto int_over = parse(huge_int);
|
||||
Void::Testing::require(!int_over.has_value());
|
||||
Void::Testing::expect_eq(int_over.error().message,
|
||||
std::string{"integer literal out of int64 range"});
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_node_limit_is_opt_in()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
|
||||
// a = [1, 2, 3] creates one table entry plus three array elements.
|
||||
const std::string doc = "a = [1, 2, 3]\n";
|
||||
Void::Testing::require(
|
||||
parse(doc, ParseOptions{.max_nodes = 4}).has_value());
|
||||
|
||||
const auto over = parse(doc, ParseOptions{.max_nodes = 3});
|
||||
Void::Testing::require(!over.has_value());
|
||||
Void::Testing::expect(
|
||||
over.error().message.find("max_nodes") != std::string::npos);
|
||||
|
||||
// Default (zero) keeps the limit disabled.
|
||||
Void::Testing::require(parse(doc).has_value());
|
||||
|
||||
// Dotted keys and array-of-tables entries count their intermediate
|
||||
// containers as well.
|
||||
Void::Testing::require(
|
||||
parse("a.b.c = 1\n", ParseOptions{.max_nodes = 3}).has_value());
|
||||
Void::Testing::require(
|
||||
!parse("a.b.c = 1\n", ParseOptions{.max_nodes = 2}).has_value());
|
||||
Void::Testing::require(
|
||||
parse("[[p]]\n", ParseOptions{.max_nodes = 2}).has_value());
|
||||
Void::Testing::require(
|
||||
!parse("[[p]]\n", ParseOptions{.max_nodes = 1}).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_rejects_invalid_utf8_with_stable_diagnostics()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
|
||||
const std::string invalid = "a = 1\n\xFF = 2\n";
|
||||
const auto first = parse(invalid);
|
||||
const auto second = parse(invalid);
|
||||
Void::Testing::require(!first.has_value());
|
||||
Void::Testing::require(!second.has_value());
|
||||
Void::Testing::expect_eq(first.error().origin.line, std::size_t{2});
|
||||
Void::Testing::expect_eq(first.error().origin.column, std::size_t{1});
|
||||
Void::Testing::expect_eq(
|
||||
first.error().message,
|
||||
std::string{"invalid UTF-8 byte sequence in TOML document"});
|
||||
Void::Testing::expect_eq(second.error().origin.line,
|
||||
first.error().origin.line);
|
||||
Void::Testing::expect_eq(second.error().origin.column,
|
||||
first.error().origin.column);
|
||||
Void::Testing::expect_eq(second.error().message, first.error().message);
|
||||
|
||||
// Overlong encoding, UTF-16 surrogate and truncated sequence.
|
||||
Void::Testing::require(!parse(std::string{"s = \"\xC0\x80\"\n"})
|
||||
.has_value());
|
||||
Void::Testing::require(!parse(std::string{"s = \"\xED\xA0\x80\"\n"})
|
||||
.has_value());
|
||||
Void::Testing::require(!parse(std::string{"s = \"\xE2\x82\"\n"})
|
||||
.has_value());
|
||||
|
||||
// Valid multi-byte UTF-8 remains accepted.
|
||||
const auto valid = parse("s = \"h\xC3\xA9llo \xF0\x9F\x98\x80\"\n");
|
||||
Void::Testing::require(valid.has_value());
|
||||
Void::Testing::expect_eq(valid->root.find("s")->as_string()->size(),
|
||||
std::size_t{11});
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_duplicate_diagnostics_are_stable()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
|
||||
const std::string dup = "a = 1\nb = 2\na = 3\n";
|
||||
const auto first = parse(dup);
|
||||
const auto second = parse(dup);
|
||||
Void::Testing::require(!first.has_value());
|
||||
Void::Testing::require(!second.has_value());
|
||||
Void::Testing::expect_eq(first.error().origin.line, std::size_t{3});
|
||||
Void::Testing::expect_eq(first.error().origin.column, std::size_t{1});
|
||||
Void::Testing::expect_eq(first.error().message,
|
||||
std::string{"duplicate key 'a'"});
|
||||
Void::Testing::expect_eq(second.error().message, first.error().message);
|
||||
Void::Testing::expect_eq(second.error().origin.line,
|
||||
first.error().origin.line);
|
||||
|
||||
const auto dup_table = parse("[pkg]\nname = \"x\"\n[pkg]\n");
|
||||
Void::Testing::require(!dup_table.has_value());
|
||||
Void::Testing::expect_eq(dup_table.error().origin.line, std::size_t{3});
|
||||
|
||||
const auto dup_dotted = parse("a.b = 1\na.b = 2\n");
|
||||
Void::Testing::require(!dup_dotted.has_value());
|
||||
Void::Testing::expect_eq(dup_dotted.error().origin.line, std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test toml_valid_documents_regression()
|
||||
{
|
||||
using namespace Void::Data::Toml;
|
||||
|
||||
constexpr std::string_view kDoc = R"toml(
|
||||
title = "limits"
|
||||
numbers = [1, 2.5, -3, 0x10]
|
||||
matrix = [[1, 2], [3, 4]]
|
||||
inline = { a = 1, b.c = "d", nested = { e = true } }
|
||||
"quoted.key" = "value"
|
||||
unicode = "héllo 😀"
|
||||
|
||||
[server]
|
||||
host = "localhost"
|
||||
ports = [8080, 8443]
|
||||
|
||||
[server.tls]
|
||||
enabled = true
|
||||
cipher = "TLS_AES_256_GCM_SHA384"
|
||||
|
||||
[[items]]
|
||||
name = "one"
|
||||
|
||||
[[items]]
|
||||
name = "two"
|
||||
tags = ["a", "b"]
|
||||
|
||||
[items.meta]
|
||||
count = 2
|
||||
)toml";
|
||||
|
||||
const auto parsed = parse(kDoc);
|
||||
Void::Testing::require(parsed.has_value());
|
||||
|
||||
const auto& root = parsed->root;
|
||||
Void::Testing::require(root.find("matrix") != nullptr);
|
||||
Void::Testing::expect_eq(root.find("matrix")->as_array()->size(),
|
||||
std::size_t{2});
|
||||
Void::Testing::require(root.find("inline") != nullptr);
|
||||
const auto* inline_tbl = root.find("inline")->as_table();
|
||||
Void::Testing::require(inline_tbl != nullptr);
|
||||
Void::Testing::expect_eq(
|
||||
*inline_tbl->find("b")->as_table()->find("c")->as_string(),
|
||||
std::string{"d"});
|
||||
|
||||
const auto* items = root.find("items")->as_array_of_tables();
|
||||
Void::Testing::require(items != nullptr);
|
||||
Void::Testing::expect_eq(items->tables.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(
|
||||
*items->tables[1].find("meta")->as_table()->find("count")->as_integer(),
|
||||
std::int64_t{2});
|
||||
|
||||
// Canonical serialization round-trips without changing the document.
|
||||
const std::string once = serialize(*parsed);
|
||||
const auto reparsed = parse(once);
|
||||
Void::Testing::require(reparsed.has_value());
|
||||
Void::Testing::expect_eq(serialize(*reparsed), once);
|
||||
}
|
||||
Reference in new issue
Block a user