-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from JNU-econovation/feature/BE-39
[BE-39] XSS Validation
- Loading branch information
Showing
14 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
server/.gradle/7.6.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 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
89 changes: 89 additions & 0 deletions
89
server/Recruit-Api/src/main/java/com/econovation/recruit/utils/aop/XssValidationAspect.java
This file contains 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,89 @@ | ||
package com.econovation.recruit.utils.aop; | ||
|
||
import com.econovation.recruitdomain.domains.applicant.dto.BlockRequestDto; | ||
import com.econovation.recruitinfrastructure.slack.SlackMessageProvider; | ||
import com.econovation.recruitinfrastructure.slack.config.SlackProperties; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.safety.Whitelist; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class XssValidationAspect { | ||
private final SlackMessageProvider slackMessageProvider; | ||
private final SlackProperties slackProperties; | ||
|
||
@Around("@annotation(com.econovation.recruitcommon.annotation.XssProtected)") | ||
public Object validateXssInput(ProceedingJoinPoint joinPoint) throws Throwable { | ||
Object[] args = joinPoint.getArgs(); | ||
for (Object arg : args) { | ||
if (arg instanceof List) { | ||
validateDtoList((List<?>) arg); | ||
} | ||
} | ||
return joinPoint.proceed(); | ||
} | ||
|
||
private Boolean validateDtoList(List<?> dtoList) { | ||
for (Object dto : dtoList) { | ||
if (dto instanceof BlockRequestDto) { | ||
Boolean attackSignal = validateBlockRequestDto((BlockRequestDto) dto); | ||
if (attackSignal) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private Boolean validateBlockRequestDto(BlockRequestDto dto) { | ||
String sanitizedAnswer = Jsoup.clean(dto.getAnswer(), Whitelist.relaxed()); | ||
if (!dto.getAnswer().equals(sanitizedAnswer)) { | ||
slackMessageProvider.sendMessage( | ||
slackProperties.getUrl(), generateApplicantRegisterMessage(dto)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private String generateApplicantRegisterMessage(BlockRequestDto dto) { | ||
String inputAnswer = dto.getAnswer(); | ||
String sanitizedAnswer = Jsoup.clean(inputAnswer, Whitelist.relaxed()); | ||
|
||
if (!inputAnswer.equals(sanitizedAnswer)) { | ||
// XSS 공격이 의심되는 부분을 추출합니다. | ||
String detectedXss = extractDetectedXss(inputAnswer, sanitizedAnswer); | ||
|
||
// 경고 메시지 생성 | ||
return String.format( | ||
":interrobang::interrobang::interrobang: XSS 공격이 감지되었습니다.:interrobang::interrobang::interrobang:\n\n" | ||
+ ":party_parrot2: 입력값 : %s\n\n" | ||
+ ":party_parrot2: 변환된 공격 추출 값 : %s", | ||
inputAnswer, detectedXss); | ||
} | ||
|
||
// 공격이 감지되지 않은 경우 | ||
return "No XSS attacks detected."; | ||
} | ||
|
||
private String extractDetectedXss(String inputAnswer, String sanitizedAnswer) { | ||
// inputAnswer와 sanitizedAnswer를 비교하여 공격이 감지된 부분을 추출합니다. | ||
StringBuilder detectedXss = new StringBuilder(); | ||
|
||
for (int i = 0; i < inputAnswer.length(); i++) { | ||
if (i >= sanitizedAnswer.length() | ||
|| inputAnswer.charAt(i) != sanitizedAnswer.charAt(i)) { | ||
detectedXss.append(inputAnswer.charAt(i)); | ||
} | ||
} | ||
|
||
return detectedXss.toString(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...r/Recruit-Common/src/main/java/com/econovation/recruitcommon/annotation/XssProtected.java
This file contains 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,12 @@ | ||
package com.econovation.recruitcommon.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Documented | ||
@Target({ElementType.METHOD, ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface XssProtected {} |
This file contains 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
10 changes: 10 additions & 0 deletions
10
...ommon/src/main/java/com/econovation/recruitcommon/exception/XssScriptAttackException.java
This file contains 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,10 @@ | ||
package com.econovation.recruitcommon.exception; | ||
|
||
public class XssScriptAttackException extends RecruitCodeException { | ||
|
||
public static final RecruitCodeException EXCEPTION = new XssScriptAttackException(); | ||
|
||
private XssScriptAttackException() { | ||
super(GlobalErrorCode.XSS_SCRIPT_ATTACK); | ||
} | ||
} |
This file contains 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