|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.pipes.core.serialization; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +import java.io.StringReader; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.apache.tika.metadata.Metadata; |
| 31 | +import org.apache.tika.parser.ParseContext; |
| 32 | +import org.apache.tika.pipes.api.FetchEmitTuple; |
| 33 | +import org.apache.tika.pipes.api.emitter.EmitKey; |
| 34 | +import org.apache.tika.pipes.api.fetcher.FetchKey; |
| 35 | + |
| 36 | +/** |
| 37 | + * End-to-end checks at the actual wire entry points. A FetchEmitTuple is always untrusted request |
| 38 | + * input, so its parseContext must not introduce a wire-blocked component. All three entry points |
| 39 | + * (/pipes, /async, fork IPC) share {@code FetchEmitTupleDeserializer}, which enforces this. |
| 40 | + */ |
| 41 | +public class WireRestrictedFetchEmitTupleTest { |
| 42 | + |
| 43 | + private static final String WIRE_BLOCKED_PARSE_CONTEXT = |
| 44 | + "\"parse-context\":{\"typed\":{\"external-parser\":{\"config\":{" + |
| 45 | + "\"commandLine\":[\"/bin/sh\",\"-c\",\"echo x\"]," + |
| 46 | + "\"supportedTypes\":[\"text/plain\"]}}}}"; |
| 47 | + |
| 48 | + private static String tuple(String parseContextField) { |
| 49 | + return "{\"id\":\"t\",\"fetcher\":\"f\",\"fetchKey\":\"k\"," + |
| 50 | + "\"emitter\":\"e\",\"emitKey\":\"ek\",\"onParseException\":\"skip\"" + |
| 51 | + (parseContextField.isEmpty() ? "" : "," + parseContextField) + "}"; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void pipesEndpointRejectsParserInjection() { |
| 56 | + Exception e = assertThrows(Exception.class, |
| 57 | + () -> JsonFetchEmitTuple.fromJson(new StringReader(tuple(WIRE_BLOCKED_PARSE_CONTEXT)))); |
| 58 | + assertTrue(root(e).contains("may not be supplied via a request parseContext"), |
| 59 | + "expected wire-blocked rejection, got: " + root(e)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void asyncEndpointRejectsParserInjection() { |
| 64 | + Exception e = assertThrows(Exception.class, |
| 65 | + () -> JsonFetchEmitTupleList.fromJson(new StringReader("[" + tuple(WIRE_BLOCKED_PARSE_CONTEXT) + "]"))); |
| 66 | + assertTrue(root(e).contains("may not be supplied via a request parseContext"), |
| 67 | + "expected wire-blocked rejection, got: " + root(e)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void pipesEndpointAllowsSafeParseContext() throws Exception { |
| 72 | + String safe = "\"parse-context\":{" + |
| 73 | + "\"basic-content-handler-factory\":{\"type\":\"XML\",\"writeLimit\":1000}," + |
| 74 | + "\"timeout-limits\":{\"progressTimeoutMillis\":5000,\"totalTaskTimeoutMillis\":60000}}"; |
| 75 | + FetchEmitTuple t = JsonFetchEmitTuple.fromJson(new StringReader(tuple(safe))); |
| 76 | + assertNotNull(t); |
| 77 | + assertTrue(t.getParseContext().hasJsonConfig("timeout-limits")); |
| 78 | + assertTrue(t.getParseContext().hasJsonConfig("basic-content-handler-factory")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void ipcRoundTripsSafeTuple() throws Exception { |
| 83 | + FetchEmitTuple t = new FetchEmitTuple("t", new FetchKey("f", "k"), |
| 84 | + new EmitKey("e", "ek"), new Metadata(), new ParseContext(), |
| 85 | + FetchEmitTuple.ON_PARSE_EXCEPTION.SKIP); |
| 86 | + byte[] bytes = JsonPipesIpc.toBytes(t); |
| 87 | + FetchEmitTuple back = JsonPipesIpc.fromBytes(bytes, FetchEmitTuple.class); |
| 88 | + assertEquals(t, back); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void forkIpcRejectsParserInjection() throws Exception { |
| 93 | + // The fork-IPC path uses Smile but shares the same restricted FetchEmitTupleDeserializer. |
| 94 | + byte[] smile = new ObjectMapper(new SmileFactory()) |
| 95 | + .writeValueAsBytes(new ObjectMapper().readTree(tuple(WIRE_BLOCKED_PARSE_CONTEXT))); |
| 96 | + Exception e = assertThrows(Exception.class, |
| 97 | + () -> JsonPipesIpc.fromBytes(smile, FetchEmitTuple.class)); |
| 98 | + assertTrue(root(e).contains("may not be supplied via a request parseContext"), |
| 99 | + "expected wire-blocked rejection at fork IPC, got: " + root(e)); |
| 100 | + } |
| 101 | + |
| 102 | + private static String root(Throwable t) { |
| 103 | + Throwable r = t; |
| 104 | + while (r.getCause() != null && r.getCause() != r) { |
| 105 | + r = r.getCause(); |
| 106 | + } |
| 107 | + return String.valueOf(r.getMessage()); |
| 108 | + } |
| 109 | +} |
0 commit comments