260803_01_ENH: <qrcode> 태그 <img>로 치환해서 전송 및 템플릿 저장시 일부 태그에 대한 스타일 화이트리스트 처리
This commit is contained in:
@@ -6,6 +6,7 @@ import com.kri.common.feature.util.StringUtil;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.dto.template.TemplateBasicDto;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Pattern;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.safety.Safelist;
|
||||
@@ -15,6 +16,10 @@ public final class TemplateContentSanitizer {
|
||||
// 필요 시 시스템 정책에 맞게 조정
|
||||
// (KOR 환경에서 SMS는 바이트 기준으로 제한을 두는 경우가 흔함)
|
||||
private static final int SMS_MAX_BYTES_UTF8 = 90;
|
||||
private static final Pattern ESCAPED_QRCODE_OPEN_TAG_PATTERN =
|
||||
Pattern.compile("(?i)<\\s*qrcode\\b([\\s\\S]*?)/?\\s*>");
|
||||
private static final Pattern ESCAPED_QRCODE_CLOSE_TAG_PATTERN =
|
||||
Pattern.compile("(?i)</\\s*qrcode\\s*>");
|
||||
|
||||
private TemplateContentSanitizer() {
|
||||
}
|
||||
@@ -68,7 +73,16 @@ public final class TemplateContentSanitizer {
|
||||
return null;
|
||||
}
|
||||
|
||||
html = normalizeEscapedQrcodeTags(html);
|
||||
|
||||
Safelist safelist = Safelist.relaxed()
|
||||
.addTags("mark")
|
||||
.addAttributes("mark", "style", "data-color")
|
||||
.addAttributes("p", "style")
|
||||
.addAttributes("span", "style")
|
||||
.addAttributes("div", "style")
|
||||
.addTags("qrcode")
|
||||
.addAttributes("qrcode", "src", "width", "height", "alt")
|
||||
.addProtocols("a", "href", "http", "https", "mailto")
|
||||
.addProtocols("img", "src", "http", "https");
|
||||
|
||||
@@ -79,6 +93,12 @@ public final class TemplateContentSanitizer {
|
||||
return cleaned.trim();
|
||||
}
|
||||
|
||||
private static String normalizeEscapedQrcodeTags(String html) {
|
||||
String normalized = ESCAPED_QRCODE_OPEN_TAG_PATTERN.matcher(html)
|
||||
.replaceAll("<qrcode$1>");
|
||||
return ESCAPED_QRCODE_CLOSE_TAG_PATTERN.matcher(normalized).replaceAll("");
|
||||
}
|
||||
|
||||
/**
|
||||
* SMS/LMS body: HTML -> 평문(개행 보존)
|
||||
*/
|
||||
|
||||
@@ -71,7 +71,7 @@ public class TemplateController {
|
||||
return newTmplat;
|
||||
}
|
||||
|
||||
@Operation(summary = "시나리오 이미지 업로드")
|
||||
@Operation(summary = "바디에 사용하는 이미지 업로드")
|
||||
@PutMapping("/body-image/{itrntPrjctCd}")
|
||||
public String uploadContactSenarioImage(@PathVariable String itrntPrjctCd,
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
|
||||
@@ -31,6 +31,7 @@ public abstract class AbstractPringoSendService extends AbstractSendService {
|
||||
|
||||
private final PringoApiClient pringoApiClient;
|
||||
private final EmailResultPoller emailResultPoller;
|
||||
private final EmailBodyRenderer emailBodyRenderer;
|
||||
|
||||
protected AbstractPringoSendService(
|
||||
SendBulkJdbcWriter sendBulkJdbcWriter,
|
||||
@@ -39,11 +40,13 @@ public abstract class AbstractPringoSendService extends AbstractSendService {
|
||||
SendRepository sendRepository,
|
||||
TemplateService templateService,
|
||||
PringoApiClient pringoApiClient,
|
||||
EmailResultPoller emailResultPoller
|
||||
EmailResultPoller emailResultPoller,
|
||||
EmailBodyRenderer emailBodyRenderer
|
||||
) {
|
||||
super(sendBulkJdbcWriter, templateRepository, bulkSendRepository, sendRepository, templateService);
|
||||
this.pringoApiClient = pringoApiClient;
|
||||
this.emailResultPoller = emailResultPoller;
|
||||
this.emailBodyRenderer = emailBodyRenderer;
|
||||
}
|
||||
|
||||
protected abstract ChannelType defaultChannelType();
|
||||
@@ -53,6 +56,11 @@ public abstract class AbstractPringoSendService extends AbstractSendService {
|
||||
return defaultChannelType();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String renderMessageBody(String body, Map<String, String> msgParam) {
|
||||
return emailBodyRenderer.render(body, msgParam);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SendInternalResultDto doSendInternal(
|
||||
Boolean isResve,
|
||||
|
||||
@@ -133,7 +133,7 @@ public abstract class AbstractSendService {
|
||||
}
|
||||
|
||||
String msgTitle = replacePlaceholder(tmplat.getTmplatTitle(), dest.getMsgParam());
|
||||
String msgBody = replacePlaceholder(tmplat.getTmplatBody(), dest.getMsgParam());
|
||||
String msgBody = renderMessageBody(tmplat.getTmplatBody(), dest.getMsgParam());
|
||||
|
||||
SendInternalResultDto sendResult = doSendInternal(
|
||||
req.getResve(),
|
||||
@@ -237,7 +237,7 @@ public abstract class AbstractSendService {
|
||||
}
|
||||
|
||||
String msgTitle = replacePlaceholder(tmplat.getTmplatTitle(), dest.getMsgParam());
|
||||
String msgBody = replacePlaceholder(tmplat.getTmplatBody(), dest.getMsgParam());
|
||||
String msgBody = renderMessageBody(tmplat.getTmplatBody(), dest.getMsgParam());
|
||||
|
||||
SendInternalResultDto sendResult = doSendInternal(
|
||||
isResve,
|
||||
@@ -318,6 +318,10 @@ public abstract class AbstractSendService {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected String renderMessageBody(String body, Map<String, String> msgParam) {
|
||||
return replacePlaceholder(body, msgParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 자식 클래스가 구현: 실제 발송 수행
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.kri.notihub.server.service;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EmailBodyRenderer {
|
||||
|
||||
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("(?:\\[\\$|#\\{)([^\\]}]+)(?:]|})");
|
||||
private static final Pattern UNRESOLVED_PLACEHOLDER_PATTERN = Pattern.compile("(?:\\[\\$|#\\{)[^\\]}]+(?:]|})");
|
||||
private static final Pattern ESCAPED_QRCODE_OPEN_TAG_PATTERN =
|
||||
Pattern.compile("(?i)<\\s*qrcode\\b([\\s\\S]*?)/?\\s*>");
|
||||
private static final Pattern ESCAPED_QRCODE_CLOSE_TAG_PATTERN =
|
||||
Pattern.compile("(?i)</\\s*qrcode\\s*>");
|
||||
private static final Pattern QRCODE_OPEN_TAG_PATTERN = Pattern.compile("(?i)<\\s*qrcode\\b([^>]*)/?>");
|
||||
private static final Pattern QRCODE_CLOSE_TAG_PATTERN = Pattern.compile("(?i)</\\s*qrcode\\s*>");
|
||||
private final String qrPublicBaseUrl;
|
||||
|
||||
public EmailBodyRenderer(@Value("${kri.qr.public-base-url}") String qrPublicBaseUrl) {
|
||||
this.qrPublicBaseUrl = qrPublicBaseUrl.endsWith("/")
|
||||
? qrPublicBaseUrl.substring(0, qrPublicBaseUrl.length() - 1)
|
||||
: qrPublicBaseUrl;
|
||||
}
|
||||
|
||||
public String render(String html, Map<String, String> msgParam) {
|
||||
String preparedHtml = replaceQrcodeTagsWithImages(html);
|
||||
String replacedHtml = replacePlaceholder(preparedHtml, msgParam);
|
||||
return replaceQrcodeImagesWithApiUrls(replacedHtml);
|
||||
}
|
||||
|
||||
private String replaceQrcodeTagsWithImages(String html) {
|
||||
if (html == null || html.isEmpty()) {
|
||||
return html;
|
||||
}
|
||||
|
||||
String normalizedHtml = normalizeQrcodeTags(html);
|
||||
Document doc = Jsoup.parseBodyFragment(normalizedHtml);
|
||||
doc.outputSettings(new Document.OutputSettings().prettyPrint(false));
|
||||
|
||||
return doc.body().html();
|
||||
}
|
||||
|
||||
private String normalizeQrcodeTags(String html) {
|
||||
String normalized = ESCAPED_QRCODE_OPEN_TAG_PATTERN.matcher(html)
|
||||
.replaceAll("<qrcode$1>");
|
||||
normalized = ESCAPED_QRCODE_CLOSE_TAG_PATTERN.matcher(normalized).replaceAll("");
|
||||
normalized = QRCODE_OPEN_TAG_PATTERN.matcher(normalized)
|
||||
.replaceAll("<img data-qrcode=\"true\"$1>");
|
||||
return QRCODE_CLOSE_TAG_PATTERN.matcher(normalized).replaceAll("");
|
||||
}
|
||||
|
||||
private String replaceQrcodeImagesWithApiUrls(String html) {
|
||||
if (html == null || html.isEmpty()) {
|
||||
return html;
|
||||
}
|
||||
|
||||
Document doc = Jsoup.parseBodyFragment(html);
|
||||
doc.outputSettings(new Document.OutputSettings().prettyPrint(false));
|
||||
|
||||
for (Element img : doc.select("img[data-qrcode=true]")) {
|
||||
String qrText = img.attr("src");
|
||||
if (!isValidQrUrl(qrText)) {
|
||||
img.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
String encodedUrl = Base64.getUrlEncoder().withoutPadding()
|
||||
.encodeToString(qrText.getBytes(StandardCharsets.UTF_8));
|
||||
img.attr("src", qrPublicBaseUrl + "/" + encodedUrl);
|
||||
img.removeAttr("data-qrcode");
|
||||
}
|
||||
|
||||
return doc.body().html();
|
||||
}
|
||||
|
||||
private String replacePlaceholder(String text, Map<String, String> msgParam) {
|
||||
if (text == null || text.isEmpty() || msgParam == null || msgParam.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
Matcher matcher = PLACEHOLDER_PATTERN.matcher(text);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
String varName = matcher.group(1);
|
||||
String value = msgParam.get(varName);
|
||||
|
||||
if (value == null) {
|
||||
matcher.appendReplacement(sb, Matcher.quoteReplacement(matcher.group(0)));
|
||||
} else {
|
||||
matcher.appendReplacement(sb, Matcher.quoteReplacement(value));
|
||||
}
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean isValidQrUrl(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
if (UNRESOLVED_PLACEHOLDER_PATTERN.matcher(value).find()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
URI uri = new URI(value);
|
||||
String scheme = uri.getScheme();
|
||||
return ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))
|
||||
&& uri.getHost() != null
|
||||
&& !uri.getHost().isBlank();
|
||||
} catch (URISyntaxException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.kri.notihub.server.service;
|
||||
|
||||
import com.kri.common.feature.excel.ExcelMapper;
|
||||
import com.kri.common.feature.excel.ExcelUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.page.PageRequestDto;
|
||||
import com.kri.common.feature.web.page.PageResponseDto;
|
||||
@@ -31,7 +30,6 @@ import com.kri.notihub.server.persistence.repository.BulkSendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendResultCodeRepository;
|
||||
import com.kri.notihub.server.persistence.repository.UnitPriceRepository;
|
||||
import jakarta.validation.Valid;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@@ -20,9 +20,11 @@ public class SendEmailService extends AbstractPringoSendService {
|
||||
SendRepository sendRepository,
|
||||
TemplateService templateService,
|
||||
PringoApiClient pringoApiClient,
|
||||
EmailResultPoller emailResultPoller
|
||||
EmailResultPoller emailResultPoller,
|
||||
EmailBodyRenderer emailBodyRenderer
|
||||
) {
|
||||
super(sendBulkJdbcWriter, templateRepository, bulkSendRepository, sendRepository, templateService, pringoApiClient, emailResultPoller);
|
||||
super(sendBulkJdbcWriter, templateRepository, bulkSendRepository, sendRepository, templateService,
|
||||
pringoApiClient, emailResultPoller, emailBodyRenderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user