-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/#200 핸들 필터링 추가 #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jes0131
wants to merge
4
commits into
main
Choose a base branch
from
feat/#200
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/#200 핸들 필터링 추가 #204
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/gbsw/snapy/global/filter/BannedWordFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.gbsw.snapy.global.filter; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class BannedWordFilter { | ||
|
|
||
| private static final String BANNED_WORDS_PATH = "filter/banned-words.txt"; | ||
|
|
||
| private Set<String> bannedWords; | ||
|
|
||
| @PostConstruct | ||
| public void init() throws IOException { | ||
| ClassPathResource resource = new ClassPathResource(BANNED_WORDS_PATH); | ||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) { | ||
| bannedWords = reader.lines() | ||
| .map(String::trim) | ||
| .filter(line -> !line.isEmpty() && !line.startsWith("#")) | ||
| .map(String::toLowerCase) | ||
| .collect(Collectors.toUnmodifiableSet()); | ||
| } | ||
| log.info("BannedWordFilter loaded {} words", bannedWords.size()); | ||
| } | ||
|
|
||
| public boolean containsBannedWord(String input) { | ||
| if (input == null || input.isBlank()) { | ||
| return false; | ||
| } | ||
| String normalized = input.toLowerCase().replaceAll("\\s+", ""); | ||
| return bannedWords.stream().anyMatch(normalized::contains); | ||
|
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 치명적 오류: 단순 부분 문자열 매칭으로 인한 대규모 오탐 발생
특히 금칙어 목록에 "sex", "ass", "cum" 등 짧은 단어가 포함되어 있어 영향 범위가 매우 큽니다. 🔧 단어 경계를 고려한 정규식 기반 매칭 방식으로 개선+import java.util.regex.Pattern;
+
`@Slf4j`
`@Component`
public class BannedWordFilter {
private static final String BANNED_WORDS_PATH = "filter/banned-words.txt";
- private Set<String> bannedWords;
+ private Set<Pattern> bannedWordPatterns;
`@PostConstruct`
public void init() throws IOException {
ClassPathResource resource = new ClassPathResource(BANNED_WORDS_PATH);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
- bannedWords = reader.lines()
+ bannedWordPatterns = reader.lines()
.map(String::trim)
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
.map(String::toLowerCase)
+ .map(word -> Pattern.compile("\\b" + Pattern.quote(word) + "\\b", Pattern.CASE_INSENSITIVE))
- .collect(Collectors.toUnmodifiableSet());
+ .collect(Collectors.toSet());
}
- log.info("BannedWordFilter loaded {} words", bannedWords.size());
+ log.info("BannedWordFilter loaded {} patterns", bannedWordPatterns.size());
}
public boolean containsBannedWord(String input) {
if (input == null || input.isBlank()) {
return false;
}
- String normalized = input.toLowerCase().replaceAll("\\s+", "");
- return bannedWords.stream().anyMatch(normalized::contains);
+ // 공백 회피를 방지하기 위해 공백 제거 버전도 검사
+ String withSpaces = input.toLowerCase();
+ String withoutSpaces = withSpaces.replaceAll("\\s+", "");
+
+ return bannedWordPatterns.stream()
+ .anyMatch(pattern -> pattern.matcher(withSpaces).find()
+ || pattern.matcher(withoutSpaces).find());
}
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IOException전파로 인한 애플리케이션 시작 실패 가능성@PostConstruct메서드가IOException을 throws하므로,banned-words.txt파일이 누락되거나 읽을 수 없는 경우 애플리케이션 시작이 실패합니다. 개발 환경과 프로덕션 환경 간 배포 시 파일 누락 위험이 있습니다.더 명확한 에러 메시지와 함께 초기화 실패를 처리하는 것을 권장합니다.
💡 개선된 에러 처리
🤖 Prompt for AI Agents