Files
go-caatsm/internal/adapter/parser/aviation/regex_timeout_test.go
T
windyboyandClaude Sonnet 4.5 c0a66cf845 Enhance aviation parser with security fixes and comprehensive refactoring
This commit implements a complete refactoring of the ICAO aviation parser,
addressing 15 identified issues across security, performance, code quality,
and documentation.

Security Enhancements (P0 - Critical):
- Add input size validation (max 1800 chars per AFTN standard)
- Implement ReDoS protection with 100ms regex timeout mechanism
- Add field validation to prevent nil pointer dereferences
- Document intentional error handling pattern for audit compliance

Performance & Design Improvements (P1 - Important):
- Remove unnecessary mutex from BodyParser (eliminates serialization)
- Fix tokenizer slash handling logic
- Remove global logger dependencies (zap.S() calls)

Code Quality Improvements (P2):
- Refactor parseRemainingLines with clear helper functions
- Document all regex patterns with ICAO format specifications
- Replace magic numbers with named constants (5 new constants)
- Add error message sanitization to prevent data leakage

Documentation & Polish (P3):
- Create comprehensive package documentation (doc.go)
- Verify naming consistency across all functions
- Add 54 comprehensive tests (all passing)
- Verify performance with benchmarks (~10µs for simple messages)

New Files:
- validation.go: Input validation utilities with AFTN limits
- validation_test.go: Comprehensive validation tests
- regex_timeout.go: ReDoS protection mechanism
- regex_timeout_test.go: Timeout protection tests
- suite_test.go: Ginkgo test suite registration
- doc.go: Package-level documentation

All changes maintain backward compatibility and existing architecture
while significantly enhancing security, maintainability, and code quality.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-26 17:55:25 +08:00

101 lines
2.9 KiB
Go

package aviation
import (
"regexp"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Regex Timeout", func() {
Describe("MatchWithTimeout", func() {
Context("with simple pattern and normal input", func() {
It("should match successfully within timeout", func() {
re := regexp.MustCompile(`^(\w+)-(\w+)$`)
input := "ARR-CES5470"
match, err := MatchWithTimeout(re, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(HaveLen(3))
Expect(match[0]).To(Equal("ARR-CES5470"))
Expect(match[1]).To(Equal("ARR"))
Expect(match[2]).To(Equal("CES5470"))
})
})
Context("with pattern that doesn't match", func() {
It("should return nil match without error", func() {
re := regexp.MustCompile(`^(\w+)-(\w+)$`)
input := "INVALID FORMAT"
match, err := MatchWithTimeout(re, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeNil())
})
})
Context("with complex ARR pattern", func() {
It("should match ARR message within timeout", func() {
input := "(ARR-CES5470/A1234-ZBTJ-ZSHC1614)"
match, err := MatchWithTimeout(ArrPatternExpression, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).ToNot(BeNil())
})
})
Context("with complex DEP pattern", func() {
It("should match DEP message within timeout", func() {
input := "(DEP-CYZ9017/A5633-ZBTJ1638-ZSPD)"
match, err := MatchWithTimeout(DepPatternExpression, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).ToNot(BeNil())
})
})
Context("with very short timeout", func() {
It("should timeout on complex pattern", func() {
// Use a very short timeout to force timeout
veryShortTimeout := 1 * time.Nanosecond
re := regexp.MustCompile(`^(.+)+$`)
input := "aaaaaaaaaaaaaaaaaaaaaaaaaaaa!"
match, err := MatchWithTimeout(re, input, veryShortTimeout)
Expect(err).To(HaveOccurred())
Expect(match).To(BeNil())
valErr, ok := err.(*ValidationError)
Expect(ok).To(BeTrue())
Expect(valErr.Field).To(Equal("regex_timeout"))
Expect(valErr.Message).To(ContainSubstring("exceeded timeout"))
})
})
Context("with empty input", func() {
It("should handle empty input gracefully", func() {
re := regexp.MustCompile(`^(\w+)$`)
input := ""
match, err := MatchWithTimeout(re, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeNil())
})
})
Context("with named capture groups", func() {
It("should preserve named groups in match result", func() {
re := regexp.MustCompile(`^(?P<category>\w+)-(?P<number>\w+)$`)
input := "ARR-CES5470"
match, err := MatchWithTimeout(re, input, DefaultRegexTimeout)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(HaveLen(3))
Expect(match[0]).To(Equal("ARR-CES5470"))
})
})
})
})