|
29 | 29 | import org.xml.sax.helpers.DefaultHandler; |
30 | 30 |
|
31 | 31 | import org.apache.tika.exception.TikaException; |
| 32 | +import org.apache.tika.io.TemporaryResources; |
32 | 33 | import org.apache.tika.io.TikaInputStream; |
| 34 | +import org.apache.tika.metadata.HttpHeaders; |
| 35 | +import org.apache.tika.metadata.Metadata; |
33 | 36 |
|
34 | 37 | /** |
35 | 38 | * Tests for the {@link SecureContentHandler} class. |
@@ -108,6 +111,31 @@ public void testManyCharactersPerByte() throws IOException { |
108 | 111 | } |
109 | 112 | } |
110 | 113 |
|
| 114 | + /** |
| 115 | + * A one-shot stream carrying a declared Content-Length far above what it holds: |
| 116 | + * the claim must not become the input byte count, or the ratio never trips. |
| 117 | + */ |
| 118 | + @Test |
| 119 | + public void testDeclaredLengthDoesNotInflateByteCount() throws IOException { |
| 120 | + Metadata metadata = new Metadata(); |
| 121 | + //a plausible lie: large enough that no ratio trips, small enough that |
| 122 | + //byteCount * ratio does not overflow and trip for the wrong reason |
| 123 | + metadata.set(HttpHeaders.CONTENT_LENGTH, Long.toString(100L * 1024 * 1024 * 1024)); |
| 124 | + try (TikaInputStream lying = TikaInputStream.get(new NullInputStream(MANY_BYTES), |
| 125 | + new TemporaryResources(), metadata)) { |
| 126 | + SecureContentHandler lyingHandler = |
| 127 | + new SecureContentHandler(new DefaultHandler(), lying); |
| 128 | + char[] ch = new char[1000]; |
| 129 | + for (int i = 0; i < MANY_BYTES; i++) { |
| 130 | + lying.read(); |
| 131 | + lyingHandler.characters(ch, 0, ch.length); |
| 132 | + } |
| 133 | + fail("Expected SAXException not thrown"); |
| 134 | + } catch (SAXException e) { |
| 135 | + // expected |
| 136 | + } |
| 137 | + } |
| 138 | + |
111 | 139 | @Test |
112 | 140 | public void testSomeCharactersWithoutInput() throws IOException { |
113 | 141 | try { |
|
0 commit comments