From 1992916ab6bca6682b98cf30b4ad41d7cbc4aea0 Mon Sep 17 00:00:00 2001 From: John Niang Date: Mon, 22 Jul 2024 12:35:32 +0800 Subject: [PATCH] Respond 404 for non-exist theme resources instead of 500 (#6340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.18.x #### What this PR does / why we need it: This PR checks readable of theme resources while getting resources to prevent Halo from throwing FileNotFoundException. #### Which issue(s) this PR fixes: Fixes #6338 #### Special notes for your reviewer: 1. Try to request 2. See the result #### Does this PR introduce a user-facing change? ```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 e066be10d..bcdb9eaa7 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 @@ public class ThemeWebFluxConfigurer implements WebFluxConfigurer { 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 2aeec5cab..e60e2870c 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 @@ class WebFluxConfigTest { } } + @Nested + class StaticResourcesTest { + + @Test + void shouldRespond404WhenThemeResourceNotFound() { + webClient.get().uri("/themes/fake-theme/assets/favicon.ico") + .exchange() + .expectStatus().isNotFound(); + } + } } \ No newline at end of file