oscerd commented on code in PR #26656:
URL: https://github.com/apache/camel/pull/26656#discussion_r4071004816
##########
components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/HttpMessageUtils.java:
##########
@@ -335,9 +342,28 @@ public static ApplicationEntity
extractEdiPayloadFromCompressedEntity(
ApplicationPkcs7MimeCompressedDataEntity compressedDataEntity,
DecrpytingAndSigningInfo decrpytingAndSigningInfo,
boolean hasValidSignature)
throws HttpException {
+ return extractEdiPayloadFromCompressedEntity(compressedDataEntity,
decrpytingAndSigningInfo, hasValidSignature,
+ MAX_COMPRESSED_ENTITY_EXPANSION);
+ }
+
+ /**
+ * As
+ * {@link
#extractEdiPayloadFromCompressedEntity(ApplicationPkcs7MimeCompressedDataEntity,
DecrpytingAndSigningInfo, boolean)},
+ * with an explicit bound on how far the compressed entity may expand.
+ *
+ * @param maxExpandedSize the largest expansion to accept, in bytes
+ */
Review Comment:
Fixed in 74791e1. `MicUtils#findSignedDataEntity` now decompresses the
compressed-data message with `new
ZlibExpanderProvider(HttpMessageUtils.MAX_COMPRESSED_ENTITY_EXPANSION)` — the
same bound as the `HttpMessageUtils` path — closing the pre-authentication
MIC-computation exposure. Added
`MicUtilsTest#createReceivedContentMicForCompressedMessageStaysWithinBound`,
which drives a compressed-data message through `createReceivedContentMic` and
asserts a within-bound payload still decompresses and yields a MIC.
Refusal itself is already covered structurally by
`CompressedEntityExpansionBoundTest` on the shared
`ZlibExpanderProvider(bound)` mechanism; a dedicated refusal test on this path
would need a >100 MB expansion, since the MIC call uses the fixed constant
rather than an injectable bound.
_Claude Code on behalf of oscerd_
##########
components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/CompressedEntityExpansionBoundTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.camel.component.as2.api.util;
+
+import org.apache.camel.component.as2.api.entity.ApplicationEDIFACTEntity;
+import org.apache.camel.component.as2.api.entity.ApplicationEntity;
+import
org.apache.camel.component.as2.api.entity.ApplicationPkcs7MimeCompressedDataEntity;
+import org.apache.hc.core5.http.HttpException;
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
+import org.bouncycastle.cms.jcajce.ZlibCompressor;
+import org.bouncycastle.operator.OutputCompressor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A compressed entity is expanded while the payload is extracted, which
happens before the signature has been
+ * established. The expansion must therefore be bounded rather than
proportional to whatever an unauthenticated sender
+ * claims.
+ */
+class CompressedEntityExpansionBoundTest {
+
+ /** Highly compressible, so the entity on the wire is a tiny fraction of
what it expands to. */
+ private static final String PAYLOAD = "A".repeat(200_000);
+
+ private static final HttpMessageUtils.DecrpytingAndSigningInfo NO_SECURITY
+ = new HttpMessageUtils.DecrpytingAndSigningInfo(null, null);
+
+ @Test
+ void expansionBeyondTheBoundIsRefused() throws Exception {
+ ApplicationPkcs7MimeCompressedDataEntity compressed =
compressedEntity();
+
+ // the failure must come from the bound itself, not from anything
downstream, so assert the type
+ // the failure must come from the expander refusing, not from anything
downstream
Review Comment:
Done in 74791e1 — collapsed to a single line (keeping the ", so assert the
type" rationale).
_Claude Code on behalf of oscerd_
##########
components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/CompressedEntityExpansionBoundTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.camel.component.as2.api.util;
+
+import org.apache.camel.component.as2.api.entity.ApplicationEDIFACTEntity;
+import org.apache.camel.component.as2.api.entity.ApplicationEntity;
+import
org.apache.camel.component.as2.api.entity.ApplicationPkcs7MimeCompressedDataEntity;
+import org.apache.hc.core5.http.HttpException;
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
+import org.bouncycastle.cms.jcajce.ZlibCompressor;
+import org.bouncycastle.operator.OutputCompressor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A compressed entity is expanded while the payload is extracted, which
happens before the signature has been
+ * established. The expansion must therefore be bounded rather than
proportional to whatever an unauthenticated sender
+ * claims.
+ */
+class CompressedEntityExpansionBoundTest {
+
+ /** Highly compressible, so the entity on the wire is a tiny fraction of
what it expands to. */
+ private static final String PAYLOAD = "A".repeat(200_000);
+
+ private static final HttpMessageUtils.DecrpytingAndSigningInfo NO_SECURITY
+ = new HttpMessageUtils.DecrpytingAndSigningInfo(null, null);
+
+ @Test
+ void expansionBeyondTheBoundIsRefused() throws Exception {
+ ApplicationPkcs7MimeCompressedDataEntity compressed =
compressedEntity();
+
+ // the failure must come from the bound itself, not from anything
downstream, so assert the type
+ // the failure must come from the expander refusing, not from anything
downstream
Review Comment:
Done in 74791e1 — merged to your suggested single line: `// the failure must
come from the expander refusing, not from anything downstream, so assert the
type`.
_Claude Code on behalf of oscerd_
##########
components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/CompressedEntityExpansionBoundTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.camel.component.as2.api.util;
+
+import org.apache.camel.component.as2.api.entity.ApplicationEDIFACTEntity;
+import org.apache.camel.component.as2.api.entity.ApplicationEntity;
+import
org.apache.camel.component.as2.api.entity.ApplicationPkcs7MimeCompressedDataEntity;
+import org.apache.hc.core5.http.HttpException;
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
+import org.bouncycastle.cms.jcajce.ZlibCompressor;
+import org.bouncycastle.operator.OutputCompressor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A compressed entity is expanded while the payload is extracted, which
happens before the signature has been
+ * established. The expansion must therefore be bounded rather than
proportional to whatever an unauthenticated sender
+ * claims.
+ */
+class CompressedEntityExpansionBoundTest {
+
+ /** Highly compressible, so the entity on the wire is a tiny fraction of
what it expands to. */
+ private static final String PAYLOAD = "A".repeat(200_000);
+
+ private static final HttpMessageUtils.DecrpytingAndSigningInfo NO_SECURITY
+ = new HttpMessageUtils.DecrpytingAndSigningInfo(null, null);
+
+ @Test
+ void expansionBeyondTheBoundIsRefused() throws Exception {
+ ApplicationPkcs7MimeCompressedDataEntity compressed =
compressedEntity();
+
+ // the failure must come from the bound itself, not from anything
downstream, so assert the type
+ // the failure must come from the expander refusing, not from anything
downstream
+ HttpException thrown = assertThrows(HttpException.class,
+ () ->
HttpMessageUtils.extractEdiPayloadFromCompressedEntity(compressed, NO_SECURITY,
false, 1024L),
+ "expanding well past the bound must fail rather than
allocate");
+ assertTrue(thrown.getMessage().contains("decompress"),
+ "expected the expander to refuse, but failed with: " +
thrown.getMessage());
+ }
+
+ @Test
+ void expansionWithinTheBoundStillWorks() throws Exception {
+ ApplicationPkcs7MimeCompressedDataEntity compressed =
compressedEntity();
+
+ ApplicationEntity entity =
HttpMessageUtils.extractEdiPayloadFromCompressedEntity(
+ compressed, NO_SECURITY, false, 10L * 1024 * 1024);
+ assertInstanceOf(ApplicationEntity.class, entity);
+ }
+
+ private static ApplicationPkcs7MimeCompressedDataEntity compressedEntity()
throws Exception {
+ ApplicationEDIFACTEntity ediEntity = new ApplicationEDIFACTEntity(
+ PAYLOAD.getBytes(java.nio.charset.StandardCharsets.US_ASCII),
"US-ASCII", "7bit", false, null);
Review Comment:
Done in 74791e1 — added `import java.nio.charset.StandardCharsets;` and use
the simple name.
_Claude Code on behalf of oscerd_
--
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]