-
Notifications
You must be signed in to change notification settings - Fork 0
/
validations.go
190 lines (170 loc) · 4.2 KB
/
validations.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// GO Lang :: SmartGo / Web Server / Validations :: Smart.Go.Framework
// (c) 2020-2024 unix-world.org
// r.20240114.2007 :: STABLE
// Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)
package websrv
import (
smart "github.com/unix-world/smartgo"
)
const (
REGEX_SAFE_WEB_ROUTE string = `^[_a-zA-Z0-9\-\.@,;\!\/]+$` // SAFETY: SUPPORT ONLY THESE CHARACTERS IN WEB ROUTES ... ; w3s alike ; must support everything in REGEX_SMART_SAFE_FILE_NAME except: #, includding A-Z which on register route may be disallowed
)
func webUrlRouteIsValid(route string) bool { // just for URL Routes
//--
defer smart.PanicHandler()
//--
route = smart.StrTrimWhitespaces(route)
//--
if(route == "/") {
return true // particular case
} //end if
//--
if(route == "") {
return false // must be at least slash
} //end if
//--
if(!smart.StrStartsWith(route, "/")) {
return false
} //end if
//--
if(smart.IsPathAlikeWithSafeFixedPath(route, true) != true) { // need to fix trailing slashes, it is a dir path
return false
} //end if
//--
if((route == ".") || (route == "..") || (smart.StrContains(route, "..")) || (smart.StrContains(route, "\\")) || (smart.StrContains(route, ":"))) {
return false
} //end if
if(smart.StrContains(route, "#")) {
return false
} //end if
//--
if(smart.PathIsBackwardUnsafe(route) == true) {
return false
} //end if
//--
if(!smart.StrRegexMatchString(REGEX_SAFE_WEB_ROUTE, route)) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webUrlPathIsValid(urlPath string) bool { // just for URL Paths, a sub-set of URL Routes
//--
defer smart.PanicHandler()
//--
if(!webUrlRouteIsValid(urlPath)) {
return false
} //end if
//--
if(urlPath == "/") {
return true // special case
} //end if
//--
urlPath = smart.StrTrimLeft(urlPath, "/") // fix: for the test below, it filesys paths allowed must not start with a slash /
if(!webPathIsValid(urlPath)) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webPathIsValid(path string) bool { // must work for dir or file
//--
defer smart.PanicHandler()
//--
path = smart.StrTrimWhitespaces(path)
if(path == "") {
return false
} //end if
//--
if((path == ".") || (path == "./") || (path == "..") || smart.StrContains(path, "..") || smart.StrContains(path, " ") || smart.StrContains(path, "\\") || smart.StrContains(path, ":")) {
return false
} //end if
//--
if(smart.StrStartsWith(path, "/") == true) { // safety: dissalow start with / ; will be later checked for absolute path, but this is much clear to have also
return false
} //end if
//--
if(smart.IsPathAlikeWithSafeFixedPath(path, true) != true) { // need to fix trailing slashes, it can be a dir path
return false
} //end if
//--
if((smart.PathIsEmptyOrRoot(path) == true) || (smart.PathIsSafeValidPath(path) != true) || (smart.PathIsBackwardUnsafe(path) == true)) {
return false
} //end if
if((smart.PathIsAbsolute(path) == true) || (smart.PathIsSafeValidSafePath(path) != true)) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webDirIsValid(path string) bool {
//--
defer smart.PanicHandler()
//--
path = smart.StrTrimWhitespaces(path)
if(path == "") {
return false
} //end if
//--
if(!webPathIsValid(path)) {
return false
} //end if
//--
if(smart.StrEndsWith(path, "/") != true) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webPathExists(path string) bool {
//--
defer smart.PanicHandler()
//--
path = smart.StrTrimWhitespaces(path)
//--
if((smart.PathIsSafeValidPath(path) != true) || (!smart.PathExists(path))) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webDirExists(path string) bool {
//--
defer smart.PanicHandler()
//--
path = smart.StrTrimWhitespaces(path)
//--
if(!webPathExists(path)) {
return false
} //end if
//--
if(!smart.PathIsDir(path)) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
func webFileExists(path string) bool {
//--
defer smart.PanicHandler()
//--
path = smart.StrTrimWhitespaces(path)
//--
if(!webPathExists(path)) {
return false
} //end if
//--
if(!smart.PathIsFile(path)) {
return false
} //end if
//--
return true
//--
} //END FUNCTION
// #END