|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.avro.io; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.*; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.util.Collections; |
| 27 | + |
| 28 | +import org.apache.avro.Schema; |
| 29 | +import org.apache.avro.generic.GenericData; |
| 30 | +import org.apache.avro.generic.GenericDatumReader; |
| 31 | +import org.apache.avro.generic.GenericDatumWriter; |
| 32 | +import org.apache.avro.generic.GenericRecord; |
| 33 | +import org.apache.avro.generic.GenericRecordBuilder; |
| 34 | +import org.apache.avro.specific.SpecificData; |
| 35 | +import org.apache.avro.util.ClassSecurityValidator; |
| 36 | +import org.apache.avro.util.ClassSecurityValidator.ClassSecurityPredicate; |
| 37 | +import org.junit.jupiter.api.AfterEach; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests that FastReaderBuilder.findClass() routes class loading through |
| 43 | + * ClassUtils.forName(), so that ClassSecurityValidator is applied consistently. |
| 44 | + */ |
| 45 | +public class TestFastReaderBuilderClassLoading { |
| 46 | + |
| 47 | + private static final String TEST_VALUE = "https://example.com"; |
| 48 | + |
| 49 | + private ClassSecurityPredicate originalValidator; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + public void saveValidator() { |
| 53 | + originalValidator = ClassSecurityValidator.getGlobal(); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterEach |
| 57 | + public void restoreValidator() { |
| 58 | + ClassSecurityValidator.setGlobal(originalValidator); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * When the validator blocks a class referenced by java-class, FastReaderBuilder |
| 63 | + * must NOT instantiate it. The value should be returned as a plain string. |
| 64 | + */ |
| 65 | + @Test |
| 66 | + void blockedClassIsNotInstantiated() { |
| 67 | + // Block java.net.URI |
| 68 | + ClassSecurityValidator.setGlobal(ClassSecurityValidator.composite(ClassSecurityValidator.DEFAULT_TRUSTED_CLASSES, |
| 69 | + ClassSecurityValidator.builder().add("org.apache.avro.util.Utf8").build())); |
| 70 | + |
| 71 | + GenericRecord result = readWithJavaClass("java.net.URI"); |
| 72 | + |
| 73 | + assertNotNull(result.get("value")); |
| 74 | + assertFalse(result.get("value") instanceof URI, "Blocked class should not be instantiated"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * When the validator trusts a class referenced by java-class, FastReaderBuilder |
| 79 | + * should instantiate it normally. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + void trustedClassIsInstantiated() { |
| 83 | + ClassSecurityValidator.setGlobal(ClassSecurityValidator.composite(ClassSecurityValidator.DEFAULT_TRUSTED_CLASSES, |
| 84 | + ClassSecurityValidator.builder().add("java.net.URI").add("org.apache.avro.util.Utf8").build())); |
| 85 | + |
| 86 | + GenericRecord result = readWithJavaClass("java.net.URI"); |
| 87 | + |
| 88 | + assertInstanceOf(URI.class, result.get("value")); |
| 89 | + assertEquals(URI.create(TEST_VALUE), result.get("value")); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Encode a string, then read it back through FastReaderBuilder with the given |
| 94 | + * java-class. |
| 95 | + */ |
| 96 | + private GenericRecord readWithJavaClass(String javaClass) { |
| 97 | + try { |
| 98 | + Schema stringSchema = Schema.create(Schema.Type.STRING); |
| 99 | + stringSchema.addProp(SpecificData.CLASS_PROP, javaClass); |
| 100 | + stringSchema.addProp(GenericData.STRING_PROP, GenericData.StringType.String.name()); |
| 101 | + |
| 102 | + Schema recordSchema = Schema.createRecord("TestRecord", null, "test", false); |
| 103 | + recordSchema.setFields(Collections.singletonList(new Schema.Field("value", stringSchema, null, null))); |
| 104 | + |
| 105 | + // Encode |
| 106 | + GenericRecord record = new GenericRecordBuilder(recordSchema).set("value", TEST_VALUE).build(); |
| 107 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 108 | + Encoder encoder = EncoderFactory.get().binaryEncoder(out, null); |
| 109 | + new GenericDatumWriter<GenericRecord>(recordSchema).write(record, encoder); |
| 110 | + encoder.flush(); |
| 111 | + |
| 112 | + // Decode with fast reader enabled |
| 113 | + GenericData data = new GenericData(); |
| 114 | + data.setFastReaderEnabled(true); |
| 115 | + GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(recordSchema, recordSchema, data); |
| 116 | + return reader.read(null, DecoderFactory.get().binaryDecoder(new ByteArrayInputStream(out.toByteArray()), null)); |
| 117 | + } catch (IOException e) { |
| 118 | + return fail("Unexpected IOException during encode/decode", e); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments