diff --git a/CHANGELOG.md b/CHANGELOG.md index e4076e0..7f48566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/db/asset_dsl.go b/db/asset_dsl.go index 8d2b633..654b8eb 100644 --- a/db/asset_dsl.go +++ b/db/asset_dsl.go @@ -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 diff --git a/db/asset_dsl_limit_test.go b/db/asset_dsl_limit_test.go new file mode 100644 index 0000000..bea4883 --- /dev/null +++ b/db/asset_dsl_limit_test.go @@ -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") + } +}