|
| 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.server.core; |
| 18 | + |
| 19 | +import java.io.FilterInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | + |
| 23 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 24 | +import jakarta.ws.rs.container.ContainerRequestFilter; |
| 25 | +import jakarta.ws.rs.core.MediaType; |
| 26 | +import jakarta.ws.rs.core.Response; |
| 27 | +import jakarta.ws.rs.ext.Provider; |
| 28 | + |
| 29 | +/** |
| 30 | + * Rejects request bodies larger than {@code maxRequestSizeBytes}. |
| 31 | + * <p> |
| 32 | + * A declared Content-Length over the limit is refused before the body is read. Requests |
| 33 | + * without a usable Content-Length -- chunked transfer encoding, in particular -- are |
| 34 | + * counted as they are consumed, so the limit holds whether or not the client is honest |
| 35 | + * about the size. |
| 36 | + */ |
| 37 | +@Provider |
| 38 | +public class MaxRequestSizeFilter implements ContainerRequestFilter { |
| 39 | + |
| 40 | + static final String TOO_LARGE_MESSAGE = "Request body exceeds maxRequestSizeBytes"; |
| 41 | + |
| 42 | + private final long maxRequestSizeBytes; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param maxRequestSizeBytes maximum request body in bytes; negative disables the limit |
| 46 | + */ |
| 47 | + public MaxRequestSizeFilter(long maxRequestSizeBytes) { |
| 48 | + this.maxRequestSizeBytes = maxRequestSizeBytes; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void filter(ContainerRequestContext requestContext) { |
| 53 | + if (maxRequestSizeBytes < 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if (requestContext.getLength() > maxRequestSizeBytes) { |
| 57 | + requestContext.abortWith(tooLarge()); |
| 58 | + return; |
| 59 | + } |
| 60 | + requestContext.setEntityStream( |
| 61 | + new BoundedInputStream(requestContext.getEntityStream(), maxRequestSizeBytes)); |
| 62 | + } |
| 63 | + |
| 64 | + private static Response tooLarge() { |
| 65 | + return Response |
| 66 | + .status(Response.Status.REQUEST_ENTITY_TOO_LARGE) |
| 67 | + .entity(TOO_LARGE_MESSAGE) |
| 68 | + .type(MediaType.TEXT_PLAIN) |
| 69 | + .build(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Throws once more than {@code limit} bytes have been read. Deliberately not |
| 74 | + * silent truncation: a caller that sent too much must not receive a 200 describing |
| 75 | + * a prefix of their document. |
| 76 | + */ |
| 77 | + private static final class BoundedInputStream extends FilterInputStream { |
| 78 | + |
| 79 | + private final long limit; |
| 80 | + private long count; |
| 81 | + |
| 82 | + private BoundedInputStream(InputStream in, long limit) { |
| 83 | + super(in); |
| 84 | + this.limit = limit; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int read() throws IOException { |
| 89 | + int c = super.read(); |
| 90 | + if (c != -1) { |
| 91 | + add(1); |
| 92 | + } |
| 93 | + return c; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public int read(byte[] b, int off, int len) throws IOException { |
| 98 | + int read = super.read(b, off, len); |
| 99 | + if (read > 0) { |
| 100 | + add(read); |
| 101 | + } |
| 102 | + return read; |
| 103 | + } |
| 104 | + |
| 105 | + private void add(int n) throws IOException { |
| 106 | + count += n; |
| 107 | + if (count > limit) { |
| 108 | + throw new IOException(TOO_LARGE_MESSAGE + " (" + limit + ")"); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |