fix(db): bound asset DSL length and parenthesis depth

ParseDSL recursed on every '(' with no depth or size cap, so a
query of nested parentheses could grow the goroutine stack until
the process died. Cap input at 4 KiB and nesting at 32.
This commit is contained in:
Coff0xc
2026-11-19 14:25:04 +01:00
committed by BaaRai
parent 432c882bca
commit 16b04cfb48
3 changed files with 38 additions and 0 deletions
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Asset DSL nesting**: `ParseDSL` refuses queries longer than 4 KiB or nested more than 32 parentheses deep, so a crafted `dsl` cannot grow the goroutine stack without bound.
- **Retry-After overflow**: a 429/503 `Retry-After` of huge seconds no longer wraps `time.Duration` into a tiny delay. Values above `MaxDelay` are not retried.
- **Conversation message pages**: `GET /api/conversations/{id}/messages` clamps `limit` (200 for history, 1000 for `?since=`), so a huge query cannot load the whole transcript in one response.
- **Findings CSV formula cells**: `FindingsCSV` prefixes name, class, summary, task, and other text fields that start with `=`, `+`, `-`, `@`, tab, or CR so Excel/LibreOffice do not evaluate them as formulas when the export is opened.
+14
View File
@@ -136,9 +136,15 @@ func tokenize(s string) ([]tok, error) {
// and_expr = atom (AND atom)*
// atom = FIELD | BARE | '(' expr ')'
const (
dslMaxLen = 4096
dslMaxDepth = 32
)
type dslParser struct {
tokens []tok
pos int
depth int
}
func (p *dslParser) peek() tok {
@@ -201,8 +207,13 @@ func (p *dslParser) parseAtom() (*astNode, error) {
p.consume()
return leafNode(*t.expr), nil
case tkLP:
if p.depth >= dslMaxDepth {
return nil, fmt.Errorf("DSL syntax error: nested parentheses too deep")
}
p.depth++
p.consume()
node, err := p.parseOr()
p.depth--
if err != nil {
return nil, err
}
@@ -234,6 +245,9 @@ func ParseDSL(s string) (*astNode, error) {
if strings.TrimSpace(s) == "" {
return nil, nil
}
if len(s) > dslMaxLen {
return nil, fmt.Errorf("DSL query is too long")
}
tokens, err := tokenize(s)
if err != nil {
return nil, err
+23
View File
@@ -0,0 +1,23 @@
package db
import (
"strings"
"testing"
)
func TestParseDSLRejectsDeepNesting(t *testing.T) {
deep := strings.Repeat("(", dslMaxDepth+1) + "a" + strings.Repeat(")", dslMaxDepth+1)
if _, err := ParseDSL(deep); err == nil {
t.Fatal("deep nesting accepted")
}
ok := strings.Repeat("(", dslMaxDepth) + "a" + strings.Repeat(")", dslMaxDepth)
if _, err := ParseDSL(ok); err != nil {
t.Fatalf("depth %d refused: %v", dslMaxDepth, err)
}
}
func TestParseDSLRejectsOversize(t *testing.T) {
if _, err := ParseDSL(strings.Repeat("a", dslMaxLen+1)); err == nil {
t.Fatal("oversize query accepted")
}
}