-
Notifications
You must be signed in to change notification settings - Fork 5.1k
CAMEL-22849: Fix AS2 server wildcard pattern matching for requestUriPattern #21089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The AS2 server/listen functionality was not properly resolving requestUriPattern wildcards when selecting the appropriate consumer configuration. The getConfigurationForPath method in AS2ServerConnection was doing a simple Map.get() lookup which only supported exact matches. This fix adds wildcard pattern matching support to the getConfigurationForPath method: - First tries exact match for performance - Then tries pattern matching for wildcards (e.g., /consumer/*) - Prefers longer (more specific) patterns when multiple patterns match - Supports wildcard '*' which matches any sequence of characters The RequestHandlerRegistry from HttpCore5 already supports wildcard patterns when registering handlers, but the configuration lookup was failing because it required exact matches. This fix ensures that both the handler and configuration lookups support the same wildcard patterns.
This test verifies that the AS2 server correctly handles wildcard patterns in the requestUriPattern parameter. It tests that paths like /consumer/orders and /consumer/invoices both match the pattern /consumer/*. The test includes three scenarios: 1. Matching /consumer/orders against /consumer/* 2. Matching /consumer/invoices against /consumer/* 3. Matching nested paths like /consumer/orders/123 against /consumer/* All tests verify that the AS2 server correctly routes messages to the appropriate consumer based on wildcard pattern matching.
…uestUriPattern This commit adds support for wildcard patterns in the AS2 server component's requestUriPattern parameter. Previously, only exact path matching was supported, which limited the flexibility of AS2 server endpoint configuration. Changes: - Modified AS2ServerConnection.findConsumer() to use PathMatcher for pattern matching - Patterns are sorted by specificity: exact matches first, then wildcards by length - Added comprehensive integration tests for wildcard pattern functionality The implementation supports patterns like: - /consumer/* - matches any path starting with /consumer/ - /consumer/orders/* - matches paths starting with /consumer/orders/ - /consumer/orders - exact match only Exact matches take precedence over wildcard patterns, and more specific patterns take precedence over less specific ones.
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🤖 CI automation will test this PR automatically. 🐫 Apache Camel Committers, please review the following items:
|
Changed from longest-match to first-match ordering for wildcard pattern resolution. When multiple patterns match a request, the first registered pattern (in route definition order) now takes precedence. This approach is more deterministic and gives developers full control over pattern matching priority through route definition order. More specific patterns should be defined before more general patterns. Updated LinkedHashMap (wrapped in Collections.synchronizedMap()) to preserve insertion order, replacing ConcurrentHashMap which had no ordering guarantee.
|
There are uncommitted changes |
…AS2 wildcard patterns
…attern (#21089) The AS2 server/listen functionality was not properly resolving requestUriPattern wildcards when selecting the appropriate consumer configuration. The getConfigurationForPath method in AS2ServerConnection was doing a simple Map.get() lookup which only supported exact matches.
CAMEL-22849: Fix AS2 server wildcard pattern matching for requestUriPattern
Summary
This PR fixes a regression in the
camel-as2component where AS2 server routes configured with wildcard patterns inrequestUriPattern(e.g.,/receiver/*) were not correctly resolving the appropriateAS2ConsumerConfigurationfor incoming requests.Problem
In Camel 4.14.x, when an AS2 request was sent to a concrete path like
/receiver/test-1, the server would fail to find the consumer configuration for patterns like/receiver/*, logging:The root cause was that
AS2ServerConnection.getConfigurationForPath(String path)performed only an exact map lookup using the resolved request path, which would never match wildcard patterns stored as keys like/receiver/*.This broke encrypted and/or signed AS2 messages because the server could not locate the appropriate consumer configuration for decryption and signature validation. This was a regression from Camel 3.22.x where wildcard patterns worked correctly.
Solution
Updated
AS2ServerConnectionto support wildcard pattern matching:Pattern Matching Logic: Modified
getConfigurationForPath()to:First-Match Ordering: Uses
LinkedHashMap(wrapped inCollections.synchronizedMap()) to preserve insertion order:Regex-based Wildcard Support: Implemented
matchesPattern()method that:Pattern.quote()to ensure only*has special meaning/api/*/orders,/receiver/*,/*)Pattern Caching: Added a
compiledPatternscache to avoid recompiling regex patterns on every requestComprehensive Tests: Added
AS2ServerWildcardPatternITwith extensive test coverage for:/consumer/*)/consumer/orders/*)/api/*/orders/*)/*, special characters in paths)First-Match vs Longest-Match
This implementation uses first-match ordering (based on route definition order) rather than longest-match ordering. This decision was made because:
Breaking Change Consideration
Is this a breaking change? Technically yes, but the impact is negligible because:
Best Practice: When defining routes with overlapping wildcard patterns, define more specific patterns before more general ones:
Important Remarks
1.
Risk of Ambiguity✅ RESOLVEDOriginal Issue: When two patterns had the same "specificity length", the matching order was not guaranteed due to
ConcurrentHashMapiteration order.Solution Implemented: Changed to
LinkedHashMapwith first-match ordering. The first registered pattern (in route definition order) now takes precedence, making behavior 100% deterministic.Developer Responsibility: Define more specific patterns before more general patterns in route configuration.
2. Missing Edge Case Test
Issue: The current tests verify wildcard
*matching, but do not verify what happens if the pattern contains other regex special characters (like.or+).Current Protection: The implementation uses
Pattern.quote()to properly escape all regex special characters in each segment between wildcards, ensuring that only*is interpreted as a wildcard.Recommendation: Add a test case to explicitly verify that patterns containing regex special characters (e.g.,
/receiver/test.endpoint,/api/v1+2/orders) are treated as literal characters and not as regex operators. This would ensurePattern.quote()is working correctly and prevent future regressions.3. Security Consideration (Minor)
Issue: There is a theoretical risk of "ReDoS" (Regular Expression Denial of Service) if a malicious user configures an extremely complex pattern.
Current Risk Assessment: The risk is low in this implementation because:
.*for wildcards)Best Practice: This is noted as a good practice to keep in mind for future enhancements. If pattern complexity increases or if patterns become user-configurable, additional validation or complexity limits should be considered.
Testing
AS2ServerWildcardPatternITcovers:Compatibility
This change restores the wildcard pattern matching behavior from Camel 3.22.x. Since the wildcard feature was broken in 4.14.x, this is considered a bug fix rather than a breaking change.
Fixes: https://issues.apache.org/jira/browse/CAMEL-22849
Pull Request opened by Augment Code with guidance from the PR author