-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Make sure MessageContextImpl respects the limit to the maximum number of attachment to be collected #3311
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
Merged
Merged
Make sure MessageContextImpl respects the limit to the maximum number of attachment to be collected #3311
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
170 changes: 170 additions & 0 deletions
170
rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/EntityPartProviderTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /** | ||
| * 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)); | ||
|
|
||
| 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"); | ||
|
|
||
| List<EntityPart> body = p.readFrom((Class<List<EntityPart>>) (Class<?>) List.class, | ||
| EntityPart.class, new Annotation[]{}, | ||
| MediaType.APPLICATION_OCTET_STREAM_TYPE, | ||
| new MetadataMap<String, String>(), | ||
| msg2.getContent(InputStream.class)); | ||
|
|
||
| // Force it to load the attachments | ||
| assertEquals(41, body.size()); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Test | ||
| public void testManyAttachmentHeaders() throws Exception { | ||
| final Exchange exchange = new ExchangeImpl(); | ||
| final EntityPartProvider p = new EntityPartProvider(); | ||
|
|
||
| StringBuilder sb = new StringBuilder(10000); | ||
| // Add many attachment headers | ||
| sb.append("------=_Part_34950_1098328613.1263781527359\n"); | ||
| IntStream.range(0, 1000).forEach(i -> sb.append("Header-").append(i).append(": foo").append(i).append('\n')); | ||
| sb.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"); | ||
|
|
||
| final Message msg = new MessageImpl(); | ||
| msg.setExchange(exchange); | ||
| exchange.setInMessage(msg); | ||
| p.setMessageContext(new MessageContextImpl(msg)); | ||
|
|
||
| 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 attachment headers", 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))); | ||
| } | ||
| } |
133 changes: 133 additions & 0 deletions
133
rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MultipartProviderTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /** | ||
| * 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()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testManyAttachmentHeaders() throws Exception { | ||
| final Exchange exchange = new ExchangeImpl(); | ||
| final MultipartProvider p = new MultipartProvider(); | ||
|
|
||
| StringBuilder sb = new StringBuilder(10000); | ||
| // Add many attachment headers | ||
| sb.append("------=_Part_34950_1098328613.1263781527359\n"); | ||
| IntStream.range(0, 1000).forEach(i -> sb.append("Header-").append(i).append(": foo").append(i).append('\n')); | ||
| sb.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"); | ||
|
|
||
| final Message msg = new MessageImpl(); | ||
| msg.setExchange(exchange); | ||
| exchange.setInMessage(msg); | ||
| p.setMessageContext(new MessageContextImpl(msg)); | ||
|
|
||
| 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 attachment headers", RuntimeException.class, | ||
| () -> p.readFrom(Object.class, Object.class, new Annotation[]{}, | ||
| MediaType.APPLICATION_OCTET_STREAM_TYPE, | ||
| new MetadataMap<String, String>(), | ||
| msg.getContent(InputStream.class))); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.