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

View File

@@ -421,6 +421,82 @@ func TestLinkAttrs(t *testing.T) {
assert.Equal(t, "Example", *attrs.Title)
}
func TestTextLength(t *testing.T) {
t.Parallel()
t.Run(
"empty doc",
func(t *testing.T) {
t.Parallel()
n := Node{Type: NodeDoc}
assert.Equal(t, 0, n.TextLength())
},
)
t.Run(
"single text node",
func(t *testing.T) {
t.Parallel()
text := "hello"
n := Node{Type: NodeText, Text: &text}
assert.Equal(t, 5, n.TextLength())
},
)
t.Run(
"paragraph with text",
func(t *testing.T) {
t.Parallel()
raw := `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"hello world"}]}]}`
doc, err := Parse(raw)
require.NoError(t, err)
assert.Equal(t, 11, doc.TextLength())
},
)
t.Run(
"multiple paragraphs",
func(t *testing.T) {
t.Parallel()
raw := `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"aaa"}]},{"type":"paragraph","content":[{"type":"text","text":"bb"}]}]}`
doc, err := Parse(raw)
require.NoError(t, err)
assert.Equal(t, 5, doc.TextLength())
},
)
t.Run(
"formatted text counts only text",
func(t *testing.T) {
t.Parallel()
raw := `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"plain "},{"type":"text","marks":[{"type":"bold"}],"text":"bold"}]}]}`
doc, err := Parse(raw)
require.NoError(t, err)
assert.Equal(t, 10, doc.TextLength())
},
)
t.Run(
"nested list structure",
func(t *testing.T) {
t.Parallel()
raw := `{"type":"doc","content":[{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"item 1"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"item 2"}]}]}]}]}`
doc, err := Parse(raw)
require.NoError(t, err)
assert.Equal(t, 12, doc.TextLength())
},
)
t.Run(
"testdata document",
func(t *testing.T) {
t.Parallel()
doc := loadTestDocument(t)
assert.Greater(t, doc.TextLength(), 0)
},
)
}
func TestNodeWithNoAttrs(t *testing.T) {
t.Parallel()