feat: 新增@Xss字段校验装饰器
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
|
import re
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from exceptions.exception import FieldValidatorException
|
from exceptions.exception import FieldValidatorException
|
||||||
|
from utils.string_util import StringUtil
|
||||||
|
|
||||||
|
|
||||||
class ValidateFields:
|
class ValidateFields:
|
||||||
@@ -106,3 +108,33 @@ class Size:
|
|||||||
raise FieldValidatorException(message=self.message if self.message else f'{self.field_name}长度不能大于{self.max_length}')
|
raise FieldValidatorException(message=self.message if self.message else f'{self.field_name}长度不能大于{self.max_length}')
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
class Xss:
|
||||||
|
"""
|
||||||
|
字段Xss校验装饰器
|
||||||
|
"""
|
||||||
|
HTML_PATTERN = '<(\S*?)[^>]*>.*?|<.*? />'
|
||||||
|
|
||||||
|
def __init__(self, field_name: str, message: Optional[str] = None):
|
||||||
|
"""
|
||||||
|
字段Xss校验装饰器
|
||||||
|
:param field_name: 需要校验的字段名称
|
||||||
|
:param message: 校验失败的提示信息
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
self.field_name = field_name
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __call__(self, func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
check_model = args[0]
|
||||||
|
if isinstance(check_model, BaseModel):
|
||||||
|
field_value = getattr(check_model, self.field_name)
|
||||||
|
if not StringUtil.is_blank(field_value):
|
||||||
|
pattern = re.compile(self.HTML_PATTERN)
|
||||||
|
if pattern.search(field_value):
|
||||||
|
raise FieldValidatorException(message=self.message if self.message else f'{self.field_name}不能包含脚本字符')
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
Reference in New Issue
Block a user