Make sure LazyAttachmentCollection respects max attachment count in all cases - #3344
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens enforcement of the configured maximum attachment count by adding additional checks in LazyAttachmentCollection and expanding test coverage to exercise multiple lazy-loading access paths that previously could bypass the limit.
Changes:
- Add max-attachment-count checks to
LazyAttachmentCollectionforhasNext(...), iterator-based traversal, and mutation (add/addAll). - Expand
AttachmentDeserializerTestwith targeted tests that force attachment loading viasize(), iterators,hasNext(), andcreateDataHandlerMap(). - Update
MTOMBase64Testserver configuration to raise the attachment max count to avoid failing when many MTOM parts are produced.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| systests/databinding/src/test/java/org/apache/cxf/systest/jaxb/MTOMBase64Test.java | Raises attachment max-count configuration for the MTOM base64 systest scenario. |
| core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java | Adds/reshapes tests to exercise multiple lazy attachment loading paths and validate max-count enforcement. |
| core/src/main/java/org/apache/cxf/attachment/LazyAttachmentCollection.java | Adds max attachment count checks across iterator/hasNext and collection mutation paths. |
Comments suppressed due to low confidence (2)
core/src/main/java/org/apache/cxf/attachment/LazyAttachmentCollection.java:103
- Iterator-based lazy loading still permits reading one attachment beyond maxAttachmentCount (the check uses ">" before readNext()). Also, if the collection has already exceeded the limit, the early-return branch can return true without surfacing the limit violation. This should fail as soon as the configured maximum is reached/exceeded.
public boolean hasNext() {
if (attachments.size() > current) {
return true;
}
core/src/main/java/org/apache/cxf/attachment/LazyAttachmentCollection.java:150
- add(Attachment) has the same off-by-one issue: when attachments.size() == maxAttachmentCount it still allows adding another element, resulting in max+1 attachments. Use ">=" to enforce the configured limit.
public boolean add(Attachment arg0) {
if (attachments.size() > maxAttachmentCount) {
throw new RuntimeException(new IOException("The message contains more attachments than are permitted"));
}
return attachments.add(arg0);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
coheigea
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make sure
LazyAttachmentCollectionrespects max attachment count in all cases, Several gaps have been identified and reported whereLazyAttachmentCollectionusage may bypass the max attachment count verification,