mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* Cap markdown block nesting depth blockQuoteStart and listStart recursed once per nested '>' or list marker on a line, guarded only by an indentation check that bounds CommonMark indentation, not nesting count. A single crafted line (e.g. "- " repeated tens of thousands of times) forced recursion and slice-copy work that grew super-linearly with nesting depth, letting an authenticated user burn a disproportionate amount of server CPU per request. Thread a depth counter through blockStart, blockStartOrParagraph, blockQuoteStart, and listStart, and stop opening new nested blocks past maxNestingDepth. This bounds nesting whether it is built by one line or accrued across many, and caps the recursion and copy cost to a small constant regardless of input size. Also apply the existing maxLen byte cap directly inside Parse(), not just Inspect(), so any caller of Parse is protected uniformly. * Add tests for markdown block nesting depth cap Cover the new maxNestingDepth guard: rejecting a well-formed block quote/list item once the depth limit is reached, capping recursion for a single deeply-nested line, and honoring depth already accrued by open ancestor blocks. Also cover Parse()'s byte-length cap and add benchmarks showing the nesting-depth cases now scale roughly linearly with input size instead of super-linearly. * Reject oversized posts before markdown parsing createPost, updatePost, and patchPost ran PostWithProxyRemovedFromImageURLs, which parses the raw message, before the message-length check inside Post.IsValid ever ran. An oversized message paid the full parsing cost even though it was always going to be rejected. Add rejectOversizedMessage, mirroring the rune-count check in Post.IsValid, and call it in all three handlers before any markdown processing touches the message. * Add tests for oversized post/patch rejection Cover the rejectOversizedMessage check added for createPost, updatePost, and patchPost: a message over MaxPostSize is rejected with a 400 in all three handlers before it reaches markdown processing. * Sync markdown parse cap with real max post size Parse and Inspect capped input at a hardcoded ~128KB, assuming a 64KB max post size. The real configured limit comes from the posts.message column and can be raised past that default, so on installations with an enlarged column, markdown parsing (mentions, links, formatting) was silently skipped for legitimately-sized posts. Have the markdown package pull its cap from a function registered via SetMaxPostSizeFunc, defaulting to the old conservative constant only if nothing was ever registered. PlatformService.New registers PlatformService.MaxPostSize itself once the store is ready, so the cap always reflects the real limit regardless of which code path parses markdown first, and the store layer stays unaware that markdown parsing exists. * Add tests for markdown max post size sync Cover SetMaxPostSizeFunc raising the cap and being called on every MaxLen invocation. Also verify, end to end against a real database, that PlatformService.New wires markdown.MaxLen to the real configured max post size. * Fix tests * Address review comments - Simplify setting the maximum number of runes for a post - Simplify comments - Rename variables and functions to reflect whether they represent bytes or runes.
151 lines
4.1 KiB
Go
151 lines
4.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package markdown
|
|
|
|
import (
|
|
"slices"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// defaultMaxPostRunes is used only if no value has been registered via
|
|
// SetMaxPostRunes, which should not happen in practice. It assumes a post is at
|
|
// most 64KiB in DB. Assuming four bytes per rune, it gives us 16*1024 runes.
|
|
// If we allow scanning up to twice (arbitrary value) that value, we get:
|
|
const defaultMaxPostRunes = 2 * 16 * 1024
|
|
|
|
var maxPostRunes atomic.Int64
|
|
|
|
func init() {
|
|
maxPostRunes.Store(defaultMaxPostRunes)
|
|
}
|
|
|
|
// SetMaxPostRunes registers the real configured maximum post size, in runes, that MaxLen uses to
|
|
// compute the maximum markdown input length. This lets MaxLen reflect the real limit regardless
|
|
// of which code path first parses markdown, rather than depending on that path having already
|
|
// pushed the value in. Safe to call concurrently with MaxLen.
|
|
func SetMaxPostRunes(size int) {
|
|
maxPostRunes.Store(int64(size))
|
|
}
|
|
|
|
// MaxLen returns the current maximum markdown input length, in bytes, enforced by Parse and
|
|
// Inspect: four times the maximum post size, in runes, registered via SetMaxPostRunes (or
|
|
// defaultMaxPostRunes if it hasn't been called yet), assuming a worst case of four bytes per
|
|
// rune.
|
|
func MaxLen() int {
|
|
return 4 * int(maxPostRunes.Load())
|
|
}
|
|
|
|
// Inspect traverses the markdown tree in depth-first order. If f returns true, Inspect invokes f
|
|
// recursively for each child of the block or inline, followed by a call of f(nil).
|
|
func Inspect(markdown string, f func(any) bool) {
|
|
if len(markdown) > MaxLen() {
|
|
return
|
|
}
|
|
document, referenceDefinitions := Parse(markdown)
|
|
InspectBlock(document, func(block Block) bool {
|
|
if !f(block) {
|
|
return false
|
|
}
|
|
switch v := block.(type) {
|
|
case *Paragraph:
|
|
for _, inline := range MergeInlineText(v.ParseInlines(referenceDefinitions)) {
|
|
InspectInline(inline, func(inline Inline) bool {
|
|
return f(inline)
|
|
})
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
// InspectBlock traverses the blocks in depth-first order, starting with block. If f returns true,
|
|
// InspectBlock invokes f recursively for each child of the block, followed by a call of f(nil).
|
|
func InspectBlock(block Block, f func(Block) bool) {
|
|
stack := []Block{block}
|
|
// Using seen for backtracking
|
|
seen := map[Block]bool{}
|
|
|
|
for len(stack) > 0 {
|
|
// "peek" the node from the stack
|
|
block := stack[len(stack)-1]
|
|
|
|
if seen[block] {
|
|
// "pop" the node only when backtracking(seen)
|
|
stack = stack[:len(stack)-1]
|
|
f(nil)
|
|
continue
|
|
}
|
|
seen[block] = true
|
|
|
|
// Process the node
|
|
if !f(block) {
|
|
continue
|
|
}
|
|
|
|
switch v := block.(type) {
|
|
case *Document:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *List:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *ListItem:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *BlockQuote:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// InspectInline traverses the blocks in depth-first order, starting with block. If f returns true,
|
|
// InspectInline invokes f recursively for each child of the block, followed by a call of f(nil).
|
|
func InspectInline(inline Inline, f func(Inline) bool) {
|
|
stack := []Inline{inline}
|
|
// Using seen for backtracking
|
|
seen := map[Inline]bool{}
|
|
|
|
for len(stack) > 0 {
|
|
// "peek" the node from the stack
|
|
inline := stack[len(stack)-1]
|
|
|
|
if seen[inline] {
|
|
// "pop" the node only when backtracking(seen)
|
|
stack = stack[:len(stack)-1]
|
|
f(nil)
|
|
continue
|
|
}
|
|
seen[inline] = true
|
|
|
|
// Process the node
|
|
if !f(inline) {
|
|
continue
|
|
}
|
|
|
|
switch v := inline.(type) {
|
|
case *InlineImage:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *InlineLink:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *ReferenceImage:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
case *ReferenceLink:
|
|
for _, v0 := range slices.Backward(v.Children) {
|
|
stack = append(stack, v0)
|
|
}
|
|
}
|
|
}
|
|
}
|