-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanitize.py
More file actions
executable file
·181 lines (160 loc) · 7.11 KB
/
Copy pathsanitize.py
File metadata and controls
executable file
·181 lines (160 loc) · 7.11 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
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
##############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU LGPL License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# See the files gpl.txt and lgpl.txt packaged with this file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
__author__ = "Mike Rightmire"
__copyright__ = "BioCom Software"
__license__ = "LGPLv3"
__license_file__= "lgpl.txt"
__version__ = "0.9.7.2"
__maintainer__ = "Mike Rightmire"
__email__ = "Mike.Rightmire@BiocomSoftware.com"
__status__ = "Development"
##############################################################################
from BiocomCommon.confighandler import ConfigHandler
import inspect
# THESE MUST ALL BE LISTS OF STRINGS!!!!
whitelist_chars_default = []
whitelist_words_default = []
blacklist_chars_default = ["<",">", ";"]
blacklist_words_default = [
'su',
'sudo',
'rm',
'mkfs',
'dd',
'fork',
'while',
'tar',
'wget',
'sh',
'csh',
'tsh',
'bash',
'python',
'perl',
'chmod',
'chown',
'__attribute__',
]
def _split_to_char_and_word(L):
L = _stringlist(L)
#===========================================================================
# if not isinstance(L, list):
# err = ''.join(["Parameter must be a list of strings. (L='", str(L), "')."])
# raise ValueError(err)
#===========================================================================
# Parse for characters and strings
L_chars = []
L_words = []
for item in L:
if len(item) < 1: continue
elif len(item) < 2: L_chars.append(item)
L_words.append(item) # always add
#===========================================================================
# L_chars = _stringlist(L_chars)
# L_words = _stringlist(L_words)
#===========================================================================
return L_chars, L_words
def _stringlist(data):
""""""
if isinstance(data, list):
_list = data
# If dict, values only are checked
elif isinstance(data, dict):
for value in data.values():
_list.append(value)
# If anything else, list of strings
else: _list = str(data).split()
_return_list = []
for item in _list:
_return_list.append(str(item).lower())
return _return_list
def sanitize(data, blacklist = False, whitelist = False, error = True):
"""
Set defaults
"""
config = ConfigHandler()
# First check for configfile of just blacklist (not word or char)
if config.blacklist:
blacklist_chars, blacklist_words = _split_to_char_and_word(config.blacklist)
# check for config file blacklist_chars. This will override config file blacklist alone.
# if NOT elif
if config.blacklist_chars:
blacklist_chars = _stringlist(config.blacklist_chars)
else:
blacklist_chars = _stringlist(blacklist_chars_default)
# Blacklist_chars is now set by default.
# Now blacklist words
# if NOT elif
if config.blacklist_words:
blacklist_words = _stringlist(config.blacklist_words)
else:
blacklist_words = _stringlist(blacklist_words_default)
# Blacklist_words is now set by default.
# FINALLY, if overrides were passed in, use them instead
if blacklist:
blacklist_chars, blacklist_words = _split_to_char_and_word(blacklist)
# Next whitelist
# First check for configfile of just whitelist (not word or char)
if config.whitelist:
whitelist_chars, whitelist_words = _split_to_char_and_word(config.whitelist)
# check for config file whitelist. This will override config file whitelist alone.
# if NOT elif
if config.whitelist_chars:
whitelist_chars = _stringlist(config.whitelist_chars)
else:
whitelist_chars = _stringlist(whitelist_chars_default)
# whitelist_chars is now set by default.
# Now whitelist words
# if NOT elif
if config.whitelist_words:
whitelist_words = _stringlist(config.whitelist_words)
else:
whitelist_words = _stringlist(whitelist_words_default)
# Blacklist_words is now set by default.
# FINALLY, if overrides were passed in, use them instead
if whitelist:
whitelist_chars, whitelist_words = _split_to_char_and_word(whitelist)
# whitelist_words are parsed as words, however whitelist_chars are
# simple subtracted from the blacklist_chars
_tmp_list = []
for c in blacklist_chars:
if c not in whitelist_chars: _tmp_list.append(c)
blacklist_chars = _tmp_list
# Make stringlists
print '__new__.blacklist_chars = ', blacklist_chars #333
print '__new__.blacklist_words = ', blacklist_words #333
print '__new__.whitelist_chars = ', whitelist_chars #333
print '__new__.whitelist_words = ', whitelist_words #333
_datalist = _stringlist(data)
print '_datalist = ', _datalist #3333
# Start with whitelist words. If a white list is included, then each
# item in the data must be in the whitelist
if whitelist_words:
for word in _datalist:
# All the words in the _datalist must be in the whitelist if it is passed
if word.lower() not in whitelist_words:
print 'bad word not in whitelist:', word #3333
return False
# Whitelist CHARACTERS, on the other hand, are just subtracted from
# the blacklist characters
# Parse each word in passed in data
for word in _datalist:
# Check each word again word blacklist
if word.lower() in blacklist_words: return False
# and check each word for illegal characters
for c in word.lower():
if c in blacklist_chars: return False
# If here, alles ist gut!
return True
if __name__ == '__main__':
print sanitize('This is OK ', whitelist = ['this', 'is', 'ok',])