Validate document content length by extracted text, not JSON size

Add Node.TextLength() to walk the ProseMirror tree and sum actual user
text. Introduce ProseMirrorDocumentMaxTextLength validator that enforces
a 50k character limit on extracted text, paired with a 500k byte safety
cap on the raw JSON string. Extend e2e tests for content length.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-09 12:50:46 +04:00
parent 303455ded6
commit 7cd4606278
6 changed files with 228 additions and 6 deletions

View File

@@ -168,6 +168,20 @@ func (n Node) TableCellAttrs() (TableCellAttrs, error) {
return a, nil
}
// TextLength returns the total length of all text content in the node tree,
// measured in bytes (consistent with Go's len on strings). Only text carried
// by leaf text nodes is counted; structural markup is excluded.
func (n Node) TextLength() int {
length := 0
if n.Text != nil {
length += len(*n.Text)
}
for _, child := range n.Content {
length += child.TextLength()
}
return length
}
// LinkAttrs parses and returns the link attributes from a link mark.
func (m Mark) LinkAttrs() (LinkAttrs, error) {
var a LinkAttrs