Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public boolean isValidBlockStructure() {
}

public DocumentBlockCommand toCommand() {
return new DocumentBlockCommand(resolvedType(), content, role, blockId, blobKey, url);
return new DocumentBlockCommand(
resolvedType(),
RequestStringSanitizer.stripNullChar(content),
RequestStringSanitizer.stripNullChar(role),
blockId,
RequestStringSanitizer.stripNullChar(blobKey),
RequestStringSanitizer.stripNullChar(url)
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public record CreateDocumentRequest(
) {
public CreateDocumentCommand toCommand() {
return new CreateDocumentCommand(
title,
text,
RequestStringSanitizer.stripNullChar(title),
RequestStringSanitizer.stripNullChar(text),
paragraphs == null ? null : paragraphs.stream().map(CreateDocumentBlockRequest::toCommand).toList(),
folderId,
purpose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package or.hyu.ssd.api.document.request;

final class RequestStringSanitizer {

private RequestStringSanitizer() {
}

static String stripNullChar(String value) {
if (value == null || value.indexOf('\u0000') < 0) {
return value;
}
return value.replace("\u0000", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public record UpdateDocumentRequest(
) {
public UpdateDocumentCommand toCommand() {
return new UpdateDocumentCommand(
title,
text,
RequestStringSanitizer.stripNullChar(title),
RequestStringSanitizer.stripNullChar(text),
paragraphs == null ? null : paragraphs.stream().map(CreateDocumentBlockRequest::toCommand).toList()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ void purposeRejectsNull() {
.contains("문서 목적은 필수입니다");
}

@Test
@DisplayName("toCommand는 문자열의 NUL 문자(\\u0000)를 제거한다")
void toCommandStripsNullCharacter() {
// given
CreateDocumentRequest request = new CreateDocumentRequest(
"제목\u0000",
"본문\u0000텍스트",
Arrays.asList(new CreateDocumentBlockRequest(null, "문단\u0000내용", "\u0000##", 1, null, null)),
0L,
DocumentPurpose.WRITING
);

// when
var command = request.toCommand();

// then
assertThat(command.title()).isEqualTo("제목");
assertThat(command.text()).isEqualTo("본문텍스트");
assertThat(command.blocks()).hasSize(1);
assertThat(command.blocks().getFirst().content()).isEqualTo("문단내용");
assertThat(command.blocks().getFirst().role()).isEqualTo("##");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private static Set<String> extractMessages(Set<? extends ConstraintViolation<?>> violations) {
return violations.stream()
.map(ConstraintViolation::getMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class DiscordWebhookNotifier implements ErrorAlertNotifier {

private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(5);
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final int DISCORD_CONTENT_LIMIT = 2000;
private static final String TRUNCATION_SUFFIX = "\n...(truncated)";

private final DiscordProperties discordProperties;
private final Environment environment;
Expand Down Expand Up @@ -120,7 +122,7 @@ private String resolveEnvironment() {
}

private String buildPayload(String content) {
return "{\"content\":\"" + escapeJson(content) + "\"}";
return "{\"content\":\"" + escapeJson(truncateForDiscord(content)) + "\"}";
}

private String escapeJson(String value) {
Expand All @@ -130,4 +132,16 @@ private String escapeJson(String value) {
.replace("\r", "\\r")
.replace("\n", "\\n");
}

private String truncateForDiscord(String content) {
if (content == null || content.length() <= DISCORD_CONTENT_LIMIT) {
return content;
}

int maxPrefixLength = DISCORD_CONTENT_LIMIT - TRUNCATION_SUFFIX.length();
if (maxPrefixLength <= 0) {
return content.substring(0, DISCORD_CONTENT_LIMIT);
}
return content.substring(0, maxPrefixLength) + TRUNCATION_SUFFIX;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package or.hyu.ssd.external.alert.discord;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;
import or.hyu.ssd.external.config.DiscordProperties;

import java.lang.reflect.Method;

import static org.assertj.core.api.Assertions.assertThat;

class DiscordWebhookNotifierTest {

@Test
@DisplayName("디스코드 본문이 2000자를 넘으면 잘라낸다")
void truncateMessageWhenContentTooLong() throws Exception {
DiscordWebhookNotifier notifier = new DiscordWebhookNotifier(new DiscordProperties(), new MockEnvironment());
Method truncateMethod = DiscordWebhookNotifier.class.getDeclaredMethod("truncateForDiscord", String.class);
truncateMethod.setAccessible(true);

String longMessage = "a".repeat(2100);
String truncated = (String) truncateMethod.invoke(notifier, longMessage);

assertThat(truncated.length()).isLessThanOrEqualTo(2000);
assertThat(truncated).endsWith("\n...(truncated)");
}

@Test
@DisplayName("디스코드 본문이 2000자 이하면 그대로 둔다")
void keepMessageWhenWithinLimit() throws Exception {
DiscordWebhookNotifier notifier = new DiscordWebhookNotifier(new DiscordProperties(), new MockEnvironment());
Method truncateMethod = DiscordWebhookNotifier.class.getDeclaredMethod("truncateForDiscord", String.class);
truncateMethod.setAccessible(true);

String message = "a".repeat(2000);
String truncated = (String) truncateMethod.invoke(notifier, message);

assertThat(truncated).isEqualTo(message);
}
}
Loading