Files
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

89 lines
3.3 KiB
Go

// Package aviation provides parsing capabilities for ICAO aviation telegrams.
//
// This package implements a robust parser for ICAO-format aviation messages
// following AFTN (Aeronautical Fixed Telecommunication Network) standards.
// It supports multiple message types used in civil aviation operations.
//
// # Supported Message Types
//
// The parser handles five primary message categories:
//
// - ARR (Arrival): Aircraft arrival notifications with departure/arrival airports and times
// - DEP (Departure): Aircraft departure notifications with departure/destination airports
// - CNL (Cancellation): Flight cancellation messages
// - DLA (Delay): Flight delay notifications with updated times
// - FPL (Flight Plan): Complete flight plan messages per ICAO Doc 4444
//
// # Architecture
//
// The parser uses a registry-based architecture with specialized parsers for each
// message category. The main components are:
//
// - Parse(): Entry point for parsing complete telegrams (header + body)
// - ParseHeader(): Extracts header metadata (addresses, originator, timestamps)
// - BodyParser: Routes body content to category-specific parsers
// - CategoryParser: Interface implemented by each message type parser
//
// # Security Features
//
// The parser includes multiple security protections:
//
// - Input size validation (max 1800 chars per AFTN standard)
// - ReDoS protection with 100ms regex timeout
// - Field validation to prevent nil pointer dereferences
// - Error message sanitization to prevent data leakage
//
// # Usage Example
//
// rawTelegram := `ZCZC ABC123 261530
// FF ZBBBZPZX
// 261530 ZBBBYMYX
// (ARR-CES5470/A1234-ZBTJ-ZSHC1614)
// NNNN`
//
// parsed, err := aviation.Parse(rawTelegram)
// if err != nil {
// // Parse failed - check parsed.Status for error type
// log.Printf("Parse error: %v, status: %s", err, parsed.Status)
// return
// }
//
// // Parse succeeded - access structured data
// if arr, ok := parsed.BodyData.(*domain.ARR); ok {
// fmt.Printf("Flight %s arrived at %s\n", arr.AircraftID, arr.ArrivalAirport)
// }
//
// # Error Handling
//
// The Parse() function follows a unique error handling pattern: it ALWAYS returns
// a non-nil ParsedTelegram, even when an error occurs. This allows callers to
// persist failed parse attempts with error details for audit and compliance.
//
// Error categories:
//
// - MessageStatusHeaderError: Invalid header format or size validation failure
// - MessageStatusBodyError: Invalid body format or unsupported message type
// - MessageStatusParsed: Successful parse
//
// Parser failures are considered permanent (should be ACK'd in message queue systems).
// The returned ParsedTelegram contains error details in the ErrorReason field.
//
// # Performance
//
// The parser is designed for high-throughput message processing:
//
// - Zero-allocation tokenization where possible
// - Compiled regex patterns (initialized once at startup)
// - No global state or locks (thread-safe by design)
// - Batch-friendly (no shared mutable state between Parse() calls)
//
// # Standards Compliance
//
// This implementation follows:
//
// - ICAO Doc 4444 (PANS-ATM) for flight plan format
// - ICAO Annex 10 for AFTN message structure
// - AFTN size limits (1800 characters maximum)
//
package aviation