Merge issue-46-toml-limits: TOML parser limits (closes #46)
This commit is contained in:
commit
a14d063598
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)
|
||||
|
||||
Reference in new issue
Block a user