Copilot commented on code in PR #3039:
URL: https://github.com/apache/tika/pull/3039#discussion_r3814943057
##########
tika-pipes/tika-pipes-core/src/main/java/org/apache/tika/pipes/core/server/PipesWorker.java:
##########
@@ -516,7 +510,33 @@ protected ParseDataOrPipesResult parseFromTuple() throws
TikaException, Interrup
}
}
-
+ /**
+ * Carries the caller-supplied detection hints from the tuple metadata
across the
+ * fresh-metadata boundary into the metadata used for fetch and detection.
+ * <p>
+ * Only the resource name and the {@code Content-Type} soft hint are
carried.
+ * {@code Content-Type} is applied by {@code MimeTypes.detect} via {@code
applyHint},
+ * which keeps it only when it equals or specializes the magic-detected
type (e.g.
+ * {@code image/tiff} -> {@code image/x-raw-nikon} for a NEF supplied
without a
+ * filename). The {@code CONTENT_TYPE_USER_OVERRIDE} key is deliberately
NOT carried:
+ * it short-circuits detection unconditionally and would let a caller
force any type.
+ *
+ * @param tupleMetadata the caller-supplied metadata (may be null)
+ * @param target the fresh metadata used for fetch and detection
+ */
+ static void carryCallerHints(Metadata tupleMetadata, Metadata target) {
+ if (tupleMetadata == null) {
+ return;
+ }
+ String suppliedName =
tupleMetadata.get(TikaCoreProperties.RESOURCE_NAME_KEY);
+ if (!StringUtils.isBlank(suppliedName)) {
+ target.set(TikaCoreProperties.RESOURCE_NAME_KEY, suppliedName);
+ }
+ String suppliedContentType =
tupleMetadata.get(HttpHeaders.CONTENT_TYPE);
+ if (!StringUtils.isBlank(suppliedContentType)) {
+ target.set(HttpHeaders.CONTENT_TYPE, suppliedContentType);
+ }
Review Comment:
Consider normalizing the carried hints before setting them on `target`. As
written, leading/trailing whitespace (or other non-canonical formatting) will
be preserved, which can reduce the effectiveness of detection hints. A concrete
improvement is to trim the values (e.g., use a trim-to-null approach) before
`target.set(...)`; for `Content-Type`, optionally parse/validate and only carry
it when it parses as a media type.
##########
tika-pipes/tika-pipes-core/src/test/java/org/apache/tika/pipes/core/server/PipesWorkerCallerHintsTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.tika.pipes.core.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.tika.metadata.HttpHeaders;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TikaCoreProperties;
+
+/**
+ * Unit tests for {@link PipesWorker#carryCallerHints(Metadata, Metadata)},
which carries the
+ * caller-supplied detection hints across the worker's fresh-metadata boundary.
+ */
+public class PipesWorkerCallerHintsTest {
+
+ @Test
+ public void testCarriesResourceNameAndContentType() {
+ Metadata tuple = new Metadata();
+ tuple.set(TikaCoreProperties.RESOURCE_NAME_KEY, "photo.nef");
+ tuple.set(HttpHeaders.CONTENT_TYPE, "image/x-raw-nikon");
+
+ Metadata target = new Metadata();
+ PipesWorker.carryCallerHints(tuple, target);
+
+ assertEquals("photo.nef",
target.get(TikaCoreProperties.RESOURCE_NAME_KEY));
+ assertEquals("image/x-raw-nikon",
target.get(HttpHeaders.CONTENT_TYPE));
+ }
+
+ /**
+ * The Content-Type is carried only as a soft hint. The unconditional
override key
+ * must never be carried, or a caller could force any type past detection.
+ */
+ @Test
+ public void testDoesNotCarryUserOverride() {
+ Metadata tuple = new Metadata();
+ tuple.set(TikaCoreProperties.CONTENT_TYPE_USER_OVERRIDE,
"image/x-raw-nikon");
+
+ Metadata target = new Metadata();
+ PipesWorker.carryCallerHints(tuple, target);
+
+ assertNull(target.get(TikaCoreProperties.CONTENT_TYPE_USER_OVERRIDE));
Review Comment:
The PR description mentions explicitly not carrying
`CONTENT_TYPE_PARSER_OVERRIDE` as well as `CONTENT_TYPE_USER_OVERRIDE`, but the
tests only assert the user-override key is not propagated. Add a regression
assertion (or a separate test) that
`TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE` is never carried, to lock in
the intended security contract and prevent future regressions.
##########
CHANGES.txt:
##########
@@ -493,6 +493,13 @@ Release 4.0.0 - 8/18/2026
that exceed the limit return PAYLOAD_LIMIT_EXCEEDED instead of causing
heap exhaustion; crash messages are also size-capped (TIKA-4793).
+ * Pipes now carries the caller-supplied Content-Type across the worker's
+ fresh-metadata boundary as a soft detection hint, so /unpack, /async and
+ /pipes can route on a client Content-Type (e.g. image/x-raw-nikon for a
NEF
+ sent without a filename), not only on the filename. The hint only refines
+ within the magic-detected type hierarchy; the CONTENT_TYPE_USER_OVERRIDE
key
+ is deliberately not carried (TIKA-4825).
Review Comment:
The PR description calls out `/unpack/all` in addition to `/unpack`, but the
CHANGES entry lists only `/unpack, /async and /pipes`. If `/unpack/all` is also
affected by the same pipes/worker path, consider updating this entry to include
it (or reword to cover all relevant endpoints consistently).
--
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]