|
| 1 | +/* |
| 2 | + * Copyright 2026-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function.context.config; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import ch.qos.logback.classic.Level; |
| 23 | +import ch.qos.logback.classic.Logger; |
| 24 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 25 | +import ch.qos.logback.core.read.ListAppender; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | +import tools.jackson.databind.ObjectMapper; |
| 31 | + |
| 32 | +import org.springframework.cloud.function.context.config.JsonMessageConverterTests.Person; |
| 33 | +import org.springframework.cloud.function.json.JacksonMapper; |
| 34 | +import org.springframework.messaging.Message; |
| 35 | +import org.springframework.messaging.MessageHeaders; |
| 36 | +import org.springframework.messaging.support.MessageBuilder; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for the handling of unexpected input by {@link JsonMessageConverter}. |
| 42 | + * |
| 43 | + * @author Oleg Zhurakousky |
| 44 | + */ |
| 45 | +public class JsonMessageConverterRobustnessTests { |
| 46 | + |
| 47 | + private final Logger logger = (Logger) LoggerFactory.getLogger(JsonMessageConverter.class); |
| 48 | + |
| 49 | + private final ListAppender<ILoggingEvent> appender = new ListAppender<>(); |
| 50 | + |
| 51 | + private Level originalLevel; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void attach() { |
| 55 | + this.originalLevel = this.logger.getLevel(); |
| 56 | + this.logger.setLevel(Level.DEBUG); |
| 57 | + this.appender.start(); |
| 58 | + this.logger.addAppender(this.appender); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + void detach() { |
| 63 | + this.logger.detachAppender(this.appender); |
| 64 | + this.logger.setLevel(this.originalLevel); |
| 65 | + } |
| 66 | + |
| 67 | + private JsonMessageConverter converter() { |
| 68 | + return new JsonMessageConverter(new JacksonMapper(new ObjectMapper())); |
| 69 | + } |
| 70 | + |
| 71 | + private static Message<byte[]> typed(String body, String type) { |
| 72 | + return MessageBuilder.withPayload(body.getBytes(StandardCharsets.UTF_8)) |
| 73 | + .setHeader(MessageHeaders.CONTENT_TYPE, "application/json;type=" + type).build(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void failedConversionWithoutTargetClassReturnsNull() { |
| 78 | + assertThat(converter().convertFromInternal(typed("not json", Person.class.getName()), null, null)).isNull(); |
| 79 | + assertThat(converter().convertFromInternal(typed("not json", Person.class.getName()), Object.class, null)) |
| 80 | + .isNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void failedConversionDoesNotLogThePayload() { |
| 85 | + String secret = "s3cr3t-token-value"; |
| 86 | + assertThat(converter().convertFromInternal(typed("{\"name\": " + secret, Person.class.getName()), |
| 87 | + Person.class, null)).isNull(); |
| 88 | + List<String> messages = this.appender.list.stream().map(ILoggingEvent::getFormattedMessage).toList(); |
| 89 | + assertThat(messages).isNotEmpty().noneMatch(m -> m.contains(secret)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void typeThatIsNotOnTheAllowListIsNotLoadedAndTheReasonIsLogged() { |
| 94 | + String type = "java.lang.ProcessBuilder"; |
| 95 | + Message<byte[]> message = typed("{\"a\":1}", type); |
| 96 | + assertThat(converter().convertFromInternal(message, Object.class, null)).isSameAs(message.getPayload()); |
| 97 | + assertThat(this.appender.list.stream().map(ILoggingEvent::getFormattedMessage)) |
| 98 | + .anyMatch(m -> m.contains(type) && m.contains("allow")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void deeplyNestedJsonIsRejectedInsteadOfExhaustingTheStack() { |
| 103 | + String nested = "[".repeat(200_000) + "]".repeat(200_000); |
| 104 | + Message<byte[]> message = MessageBuilder.withPayload(nested.getBytes(StandardCharsets.UTF_8)) |
| 105 | + .setHeader(MessageHeaders.CONTENT_TYPE, "application/json").build(); |
| 106 | + assertThat(converter().convertFromInternal(message, Object.class, null)).isNotNull(); |
| 107 | + assertThat(converter().convertFromInternal(message, List.class, null)).isNull(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments