-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathwhitelist.py
More file actions
93 lines (84 loc) · 2.55 KB
/
whitelist.py
File metadata and controls
93 lines (84 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# app/utils/whitelist.py
import re
from typing import List, Pattern
from common.core.config import settings
from common.utils.utils import SQLBotLogUtil
wlist = [
"/",
"/docs",
"/login/*",
"*.ico",
"*.html",
"*.js",
"*.css",
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.svg",
"*.woff",
"*.woff2",
"*.ttf",
"*.eot",
"*.otf",
"*.css.map"
"/mcp*",
"/system/license",
"/system/config/key",
"/images/*",
"/sse",
"/system/appearance/ui",
"/system/appearance/picture/*",
"/system/assistant/validator*",
"/system/assistant/info/*",
"/system/assistant/app/*",
"/system/assistant/picture/*",
"/system/authentication/platform/status",
"/system/authentication/login/*",
"/system/authentication/sso/*",
"/system/platform/sso/*",
"/system/platform/client/*",
"/system/parameter/login"
]
class WhitelistChecker:
def __init__(self, paths: List[str] = None):
self.whitelist = paths or wlist
self._compiled_patterns: List[Pattern] = []
self._compile_patterns()
def _compile_patterns(self) -> None:
for pattern in self.whitelist:
if "*" in pattern:
regex_pattern = (
pattern.replace(".", r"\.")
.replace("*", ".*")
)
if not pattern.startswith("/"):
regex_pattern = f"^{regex_pattern}$"
else:
regex_pattern = f"^{regex_pattern}$"
try:
self._compiled_patterns.append(re.compile(regex_pattern))
except re.error:
SQLBotLogUtil.error(f"Invalid regex pattern: {regex_pattern}")
def is_whitelisted(self, path: str) -> bool:
prefix = settings.API_V1_STR
if path.startswith(prefix):
path = path[len(prefix):]
context_prefix = settings.CONTEXT_PATH
if context_prefix and path.startswith(context_prefix):
path = path[len(context_prefix):]
if not path:
path = '/'
if path in self.whitelist:
return True
path = path.rstrip('/')
return any(
pattern.match(path) is not None
for pattern in self._compiled_patterns
)
def add_path(self, path: str) -> None:
if path not in self.whitelist:
self.whitelist.append(path)
if "*" in path:
self._compile_patterns()
whiteUtils = WhitelistChecker()