From 19e45e0d3ce881fbde4eabcabaf184aa23df9539 Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:49:32 +0800 Subject: [PATCH] [release-2.17] Respond 404 for non-exist theme resources instead of 500 (#6355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an automated cherry-pick of #6340 /assign JohnNiang ```release-note 修复访问不存在的主题资源时出现服务器异常的问题 ``` --- .../halo/app/theme/config/ThemeWebFluxConfigurer.java | 3 +++ .../java/run/halo/app/config/WebFluxConfigTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/application/src/main/java/run/halo/app/theme/config/ThemeWebFluxConfigurer.java b/application/src/main/java/run/halo/app/theme/config/ThemeWebFluxConfigurer.java index e066be10d7..bcdb9eaa7c 100644 --- a/application/src/main/java/run/halo/app/theme/config/ThemeWebFluxConfigurer.java +++ b/application/src/main/java/run/halo/app/theme/config/ThemeWebFluxConfigurer.java @@ -80,6 +80,9 @@ protected Mono resolveResourceInternal(ServerWebExchange exchange, var assetsPath = themeRoot.resolve(themeName + "/templates/assets/" + resourcePaths); FileUtils.checkDirectoryTraversal(themeRoot, assetsPath); var location = new FileSystemResource(assetsPath); + if (!location.isReadable()) { + return Mono.empty(); + } return Mono.just(location); } diff --git a/application/src/test/java/run/halo/app/config/WebFluxConfigTest.java b/application/src/test/java/run/halo/app/config/WebFluxConfigTest.java index 2aeec5cab3..e60e2870c6 100644 --- a/application/src/test/java/run/halo/app/config/WebFluxConfigTest.java +++ b/application/src/test/java/run/halo/app/config/WebFluxConfigTest.java @@ -144,4 +144,14 @@ void shouldResponseNotFoundWhenAssetsNotExist() { } } + @Nested + class StaticResourcesTest { + + @Test + void shouldRespond404WhenThemeResourceNotFound() { + webClient.get().uri("/themes/fake-theme/assets/favicon.ico") + .exchange() + .expectStatus().isNotFound(); + } + } } \ No newline at end of file