PureXML/Sources/Parsing/Limits.swift
Mihaela Mihaljevic 4856a3fcf4 feat(parser): add bounded-by-default limits (depth, name, content)
Add PureXML.Parsing.Limits: configurable caps on nesting depth (default 256,
matching libxml2), name length, and content length, enforced during scanning in
the event reader. This protects against pathological input (deeply nested or
oversized documents) and keeps the recursive Model.Node value safe to build and
tear down (extreme depth otherwise overflows on ARC release). Threaded through
Parser.parse and the top-level parse/events API. Reimplemented from libxml2's
bounded-by-default posture; no upstream code copied.
2026-06-08 00:40:34 +02:00

29 lines
1.2 KiB
Swift

public extension PureXML.Parsing {
/// Configurable, bounded-by-default parser limits. Exceeding any of them is a
/// parse error rather than unbounded work, which protects against pathological
/// input (deeply nested or oversized documents). The depth cap also keeps the
/// recursive ``PureXML/Model/Node`` value safe to build and tear down. The
/// defaults follow libxml2's posture.
struct Limits: Equatable, Sendable {
/// Maximum element nesting depth (libxml2 defaults to 256).
public var maxDepth: Int
/// Maximum length of a single name (element, attribute, or PI target).
public var maxNameLength: Int
/// Maximum length of a single text, CDATA, comment, or PI-data run.
public var maxContentLength: Int
public init(
maxDepth: Int = 256,
maxNameLength: Int = 50000,
maxContentLength: Int = 10_000_000,
) {
self.maxDepth = maxDepth
self.maxNameLength = maxNameLength
self.maxContentLength = maxContentLength
}
/// The default limits, matching libxml2's bounded-by-default posture.
public static let `default` = Limits()
}
}