Copilot commented on code in PR #3311:
URL: https://github.com/apache/cxf/pull/3311#discussion_r3575235821


##########
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java:
##########
@@ -293,6 +305,9 @@ private MultipartBody createAttachments(String 
propertyName) {
                                      inMessage),
                                      new ProvidersImpl(inMessage));
             newAttachments.add(first);
+            if (newAttachments.size() > maxAttachmentCount) {
+                throw new IOException("The message contains more attachments 
than are permitted");
+            }

Review Comment:
   AttachmentDeserializer.ATTACHMENT_MAX_COUNT limits the number of *child* 
attachments (i.e., Message.getAttachments()), but this new enforcement counts 
the root part too via newAttachments.size(). This makes the limit off by one 
(e.g., maxCount=30 would allow 30 attachments, but this will fail once the root 
part is added). Adjust the comparison to allow 1 extra for the root part (or 
track child attachment count separately).



##########
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/MessageContextImpl.java:
##########
@@ -273,6 +273,8 @@ private MultipartBody createAttachments(String 
propertyName) {
                 
m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_SIZE));
             inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE,
                 
m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE));
+            inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_COUNT,
+                    
m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_COUNT));

Review Comment:
   The PR title/description talks about limiting the maximum number of 
*attachment headers* collected, but this change is enforcing 
`attachment-max-count` (number of attachments). If the intent really is to cap 
header collection, the relevant property is 
`AttachmentDeserializer.ATTACHMENT_HEADERS_MAX_COUNT`, which is currently not 
propagated/handled here. Please clarify and either update the PR wording or 
extend the code to cover the headers-count property.



##########
rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java:
##########
@@ -0,0 +1,103 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import jakarta.ws.rs.core.MediaType;
+import org.apache.cxf.attachment.AttachmentDeserializer;
+import org.apache.cxf.jaxrs.ext.MessageContextImpl;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+public class MultipartProviderTest {
+    @Test
+    public void testChangingMaxAttachmentCount() throws Exception {
+        final Exchange exchange = new ExchangeImpl();
+        final MultipartProvider p = new MultipartProvider();
+        
+        StringBuilder sb = new StringBuilder(1000);
+        sb.append("SomeHeader: foo\n")
+            .append("------=_Part_34950_1098328613.1263781527359\n")
+            .append("Content-Type: text/xml; charset=UTF-8\n")
+            .append("Content-Transfer-Encoding: binary\n")
+            .append("Content-Id: 
<318731183421.1263781527359.IBM.WEBSERVICES@auhpap02>\n")
+            .append('\n')
+            .append("<envelope/>\n");
+
+        // Add many attachments
+        IntStream.range(0, 40).forEach(i -> {
+            sb.append("------=_Part_34950_1098328613.1263781527359\n")
+                .append("Content-Type: text/xml\n")
+                .append("Content-Transfer-Encoding: binary\n")
+                .append("Content-Id: <b86a5f2d-e7af-4e5e-b71a-9f6f2307cab0>\n")
+                .append('\n')
+                .append("<message>\n")
+                .append("------=_Part_34950_1098328613.1263781527359--\n");
+        });
+
+        // Too many attachments we'll not allow it
+        final Message msg = new MessageImpl();
+        msg.setExchange(exchange);
+        exchange.setInMessage(msg);
+        p.setMessageContext(new MessageContextImpl(msg));
+
+        msg.put(AttachmentDeserializer.ATTACHMENT_MAX_COUNT, "30");
+        msg.setContent(InputStream.class, new 
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)));
+        msg.put(Message.CONTENT_TYPE, "multipart/related");
+
+        assertThrows("Failure expected on too many attachments", 
RuntimeException.class,
+                () -> p.readFrom(Object.class, Object.class, new 
Annotation[]{},
+                    MediaType.APPLICATION_OCTET_STREAM_TYPE,
+                    new MetadataMap<String, String>(),
+                    msg.getContent(InputStream.class)));
+
+        // Now we'll allow it
+        final Message msg2 = new MessageImpl();
+        msg2.setExchange(exchange);
+        exchange.setInMessage(msg2);
+        p.setMessageContext(new MessageContextImpl(msg2));
+
+        msg2.put(AttachmentDeserializer.ATTACHMENT_MAX_COUNT, "60");
+        msg2.setContent(InputStream.class, new 
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)));
+        msg2.put(Message.CONTENT_TYPE, "multipart/related");
+
+        Map<?, ?> body = (Map<?, ?>) p.readFrom(Object.class, Object.class, 
new Annotation[]{},
+            MediaType.APPLICATION_OCTET_STREAM_TYPE,
+            new MetadataMap<String, String>(),
+            msg2.getContent(InputStream.class));
+
+        // Force it to load the attachments
+        assertEquals(3, body.size());

Review Comment:
   This test currently checks body.size() on a Map returned from 
MultipartProvider.readFrom(Object.class,...). Map size reflects the number of 
distinct content-type keys (it overwrites entries), not the number of MIME 
parts/attachments. It also expects 3 entries, but with this payload there are 
only two distinct Content-Types ("text/xml; charset=UTF-8" for the root and 
"text/xml" for children). Assert on MultipartBody.getAllAttachments().size() 
instead so the test validates attachment loading/counting.



##########
rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/EntityPartProviderTest.java:
##########
@@ -0,0 +1,139 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import jakarta.ws.rs.core.EntityPart;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.ext.ContextResolver;
+import jakarta.ws.rs.ext.ExceptionMapper;
+import jakarta.ws.rs.ext.MessageBodyReader;
+import jakarta.ws.rs.ext.MessageBodyWriter;
+import jakarta.ws.rs.ext.Providers;
+import org.apache.cxf.attachment.AttachmentDeserializer;
+import org.apache.cxf.jaxrs.ext.MessageContextImpl;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+public class EntityPartProviderTest {
+    private final Providers providers = new Providers() {
+        @Override
+        public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type,
+                Type genericType, Annotation[] annotations, MediaType 
mediaType) {
+            return new BinaryDataProvider<>();
+        }
+
+        @Override
+        public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type,
+                Type genericType, Annotation[] annotations, MediaType 
mediaType) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public <T extends Throwable> ExceptionMapper<T> 
getExceptionMapper(Class<T> type) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public <T> ContextResolver<T> getContextResolver(Class<T> contextType, 
MediaType mediaType) {
+            throw new UnsupportedOperationException();
+        }
+        
+    };
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testChangingMaxAttachmentCount() throws Exception {
+        final Exchange exchange = new ExchangeImpl();
+        final EntityPartProvider p = new EntityPartProvider();
+
+        StringBuilder sb = new StringBuilder(1000);
+        sb.append("SomeHeader: foo\n")
+            .append("------=_Part_34950_1098328613.1263781527359\n")
+            .append("Content-Type: text/xml; charset=UTF-8\n")
+            .append("Content-Transfer-Encoding: binary\n")
+            .append("Content-Id: 
<318731183421.1263781527359.IBM.WEBSERVICES@auhpap02>\n")
+            .append('\n')
+            .append("<envelope/>\n");
+
+        // Add many attachments
+        IntStream.range(0, 40).forEach(i -> {
+            sb.append("------=_Part_34950_1098328613.1263781527359\n")
+                .append("Content-Type: text/xml\n")
+                .append("Content-Transfer-Encoding: binary\n")
+                .append("Content-Id: <b86a5f2d-e7af-4e5e-b71a-9f6f2307cab0>\n")
+                .append('\n')
+                .append("<message>\n")
+                .append("------=_Part_34950_1098328613.1263781527359--\n");
+        });
+
+        // Too many attachments we'll not allow it
+        final Message msg = new MessageImpl();
+        msg.setExchange(exchange);
+        exchange.setInMessage(msg);
+        p.setMessageContext(new MessageContextImpl(msg));
+        p.setProviders(providers);
+
+        msg.put(AttachmentDeserializer.ATTACHMENT_MAX_COUNT, "30");
+        msg.setContent(InputStream.class, new 
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)));
+        msg.put(Message.CONTENT_TYPE, "multipart/related");
+
+        assertThrows("Failure expected on too many attachments", 
RuntimeException.class,
+                () -> p.readFrom((Class<List<EntityPart>>) (Class<?>) 
List.class, EntityPart.class, new Annotation[]{},
+                    MediaType.APPLICATION_OCTET_STREAM_TYPE,
+                    new MetadataMap<String, String>(),
+                    msg.getContent(InputStream.class)));
+
+        // Now we'll allow it
+        final Message msg2 = new MessageImpl();
+        msg2.setExchange(exchange);
+        exchange.setInMessage(msg2);
+        p.setMessageContext(new MessageContextImpl(msg2));
+        p.setMessageContext(new MessageContextImpl(msg2));

Review Comment:
   Duplicate call to setMessageContext; the second invocation is redundant and 
can be removed to avoid confusion about intended setup.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to