From 10ade235e36419e6e837c76187a333701ead31b9 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 6 Sep 2022 12:46:26 +0200 Subject: [PATCH] Prevent resource hint registration for pattern with leading slash Prior to this commit, if a ResourcePatternHint was created with a resource pattern with a leading slash, the hint was registered and eventually converted to configuration for the GraalVM native image compiler. However, such a resource pattern is invalid for GraalVM. Consequently, the registered resources were not available within the compiled native image. This commit ensures that registered patterns are applicable in a native image by preventing creation of a ResourcePatternHint with a pattern with a leading slash. Closes gh-29088 --- .../aot/hint/ResourcePatternHint.java | 11 +++++++++-- .../aot/hint/ResourcePatternHintTests.java | 12 +++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java index 5dd7a8f8bd..bb876e72c5 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourcePatternHint.java @@ -22,13 +22,14 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * A hint that describes resources that should be made available at runtime. * - *

The patterns may be a simple path which has a one-to-one mapping to a + *

Each pattern may be a simple path which has a one-to-one mapping to a * resource on the classpath, or alternatively may contain the special - * {@code *} character to indicate a wildcard search. For example: + * {@code *} character to indicate a wildcard match. For example: *

* + *

A resource pattern must not start with a slash ({@code /}). + * * @author Stephane Nicoll * @author Brian Clozel * @author Sebastien Deleuze + * @author Sam Brannen * @since 6.0 */ public final class ResourcePatternHint implements ConditionalHint { @@ -55,6 +59,8 @@ public final class ResourcePatternHint implements ConditionalHint { private final TypeReference reachableType; ResourcePatternHint(String pattern, @Nullable TypeReference reachableType) { + Assert.isTrue(!pattern.startsWith("/"), + () -> "Resource pattern [%s] must not start with a '/'".formatted(pattern)); this.pattern = pattern; this.reachableType = reachableType; } @@ -104,4 +110,5 @@ public final class ResourcePatternHint implements ConditionalHint { public int hashCode() { return Objects.hash(this.pattern, this.reachableType); } + } diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java index 3fcc567b4d..e3ba055458 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ResourcePatternHintTests.java @@ -19,13 +19,22 @@ package org.springframework.aot.hint; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Tests for {@link ResourcePatternHint}. * * @author Sebastien Deleuze + * @author Sam Brannen */ -public class ResourcePatternHintTests { +class ResourcePatternHintTests { + + @Test + void patternWithLeadingSlashIsRejected() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new ResourcePatternHint("/file.properties", null)) + .withMessage("Resource pattern [/file.properties] must not start with a '/'"); + } @Test void fileAtRoot() { @@ -66,4 +75,5 @@ public class ResourcePatternHintTests { .accepts("com/example/file.properties", "com/example/another/file.properties", "com/example/another") .rejects("file.properties", "com/file.properties"); } + }