From a56d4f2a926017b872ec67cc0637032145f731a6 Mon Sep 17 00:00:00 2001 From: contour <40132326+Contour-D@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:15:21 +0800 Subject: [PATCH] Fix the meta description with special characters causing the page to display abnormally (#4031) 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 #### What this PR does / why we need it: Fix the problem that the meta description with special characters causes the page to display abnormally. Use htmlEscape in org.springframework.web.util.HtmlUtils to escape the special characters contained in the original description. see #4000 #### Which issue(s) this PR fixes: Fixes #4000 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note NONE ``` --- .../app/theme/dialect/ContentTemplateHeadProcessor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/run/halo/app/theme/dialect/ContentTemplateHeadProcessor.java b/application/src/main/java/run/halo/app/theme/dialect/ContentTemplateHeadProcessor.java index 38ecf8191..55c89c665 100644 --- a/application/src/main/java/run/halo/app/theme/dialect/ContentTemplateHeadProcessor.java +++ b/application/src/main/java/run/halo/app/theme/dialect/ContentTemplateHeadProcessor.java @@ -10,6 +10,7 @@ import java.util.Map; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; +import org.springframework.web.util.HtmlUtils; import org.thymeleaf.context.ITemplateContext; import org.thymeleaf.model.IModel; import org.thymeleaf.model.IModelFactory; @@ -70,18 +71,19 @@ public class ContentTemplateHeadProcessor implements TemplateHeadProcessor { static List> excerptToMetaDescriptionIfAbsent( List> htmlMetas, String excerpt) { - final String excerptNullSafe = StringUtils.defaultString(excerpt); + String excerptNullSafe = StringUtils.defaultString(excerpt); + final String excerptSafe = HtmlUtils.htmlEscape(excerptNullSafe); List> metas = new ArrayList<>(defaultIfNull(htmlMetas, List.of())); metas.stream() .filter(map -> Meta.DESCRIPTION.equals(map.get(Meta.NAME))) .distinct() .findFirst() .ifPresentOrElse(map -> - map.put(Meta.CONTENT, defaultIfBlank(map.get(Meta.CONTENT), excerptNullSafe)), + map.put(Meta.CONTENT, defaultIfBlank(map.get(Meta.CONTENT), excerptSafe)), () -> { Map map = new HashMap<>(); map.put(Meta.NAME, Meta.DESCRIPTION); - map.put(Meta.CONTENT, excerptNullSafe); + map.put(Meta.CONTENT, excerptSafe); metas.add(map); }); return metas;