Skip to content

Commit 12853de

Browse files
committed
fix: 유효한 URL을 판별하는 정규식 수정
- 기존의 정규식에 Catastrophic Backtracking 문제가 있어 특정케이스에서 연산이 끝나지 않는 경우가 발생했기 때문에 새로운 정규식으로 변경함
1 parent b501069 commit 12853de

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

backend/src/project/dto/link/LinkCreateRequest.dto.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ function IsURL() {
1818
validator: {
1919
validate(value: any) {
2020
const URLPattern = new RegExp(
21-
'^(https?:\\/\\/)' +
22-
'((([a-z\\d가-힣]([a-z\\d가-힣-]*[a-z\\d가-힣])*)\\.?)+[a-z가-힣]{2,}|' +
23-
'((\\d{1,3}\\.){3}\\d{1,3}))' +
24-
'(\\:\\d+)?' +
25-
'(\\/[-a-z\\d%_.~+가-힣]*)*' +
26-
'(\\?[;&a-z\\d%_.~+=-가-힣]*)?' +
27-
'(\\#[-a-z\\d_가-힣]*)?$',
21+
'^((?:http|https)://)' +
22+
'(?:\\S+(?::\\S*)?@)?' +
23+
'(?:(?:' +
24+
'(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])' +
25+
'(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}' +
26+
'(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))' +
27+
'|' +
28+
'(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)' +
29+
'(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*' +
30+
'(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' +
31+
')|' +
32+
'localhost' +
33+
')' +
34+
'(?::\\d{2,5})?' +
35+
'(?:(/|\\?|#)[^\\s]*)?$',
2836
'i',
2937
);
3038
return URLPattern.test(value);

frontend/src/utils/isValidURL.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
const isValidURL = (url: string) => {
22
const pattern = new RegExp(
3-
"^(https?:\\/\\/)?" + // 프로토콜
4-
"((([a-z\\d가-힣]([a-z\\d가-힣-]*[a-z\\d가-힣])*)\\.?)+[a-z가-힣]{2,}|" + // 도메인명
5-
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR IP (v4) 주소
6-
"(\\:\\d+)?" + // 포트
7-
"(\\/[-a-z\\d%_.~+가-힣]*)*" + // 경로
8-
"(\\?[;&a-z\\d%_.~+=-가-힣]*)?" + // 쿼리 문자열
9-
"(\\#[-a-z\\d_가-힣]*)?$",
3+
"^((?:http|https)://)" +
4+
"(?:\\S+(?::\\S*)?@)?" +
5+
"(?:(?:" +
6+
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
7+
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
8+
"(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
9+
"|" +
10+
"(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
11+
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
12+
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
13+
")|" +
14+
"localhost" +
15+
")" +
16+
"(?::\\d{2,5})?" +
17+
"(?:(/|\\?|#)[^\\s]*)?$",
1018
"i"
1119
);
1220

0 commit comments

Comments
 (0)