cavisson-pythonagent 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cavisson_pythonagent-0.0.1.dist-info/METADATA +32 -0
- cavisson_pythonagent-0.0.1.dist-info/RECORD +180 -0
- cavisson_pythonagent-0.0.1.dist-info/WHEEL +5 -0
- cavisson_pythonagent-0.0.1.dist-info/licenses/LICENSE +19 -0
- cavisson_pythonagent-0.0.1.dist-info/top_level.txt +1 -0
- pythonagent/__init__.py +22 -0
- pythonagent/agent/__init__.py +219 -0
- pythonagent/agent/internal/__init__.py +1 -0
- pythonagent/agent/internal/agent.py +1651 -0
- pythonagent/agent/internal/framesinfo.py +152 -0
- pythonagent/agent/internal/heap_dump.py +39 -0
- pythonagent/agent/internal/intercept_module.py +70 -0
- pythonagent/agent/internal/logs.py +122 -0
- pythonagent/agent/internal/metadata/__init__.py +0 -0
- pythonagent/agent/internal/metadata/agent_meta_data.py +276 -0
- pythonagent/agent/internal/proc_compat.py +75 -0
- pythonagent/agent/internal/profile.py +47 -0
- pythonagent/agent/internal/provider.py +83 -0
- pythonagent/agent/internal/thread_dump.py +85 -0
- pythonagent/agent/internal/udp.py +245 -0
- pythonagent/agent/internal/udp_message.py +800 -0
- pythonagent/agent/probes/Instrumentation/__init__.py +340 -0
- pythonagent/agent/probes/Instrumentation/find.py +243 -0
- pythonagent/agent/probes/Instrumentation/module_version_resolver.py +155 -0
- pythonagent/agent/probes/Instrumentation/new_parser.py +64 -0
- pythonagent/agent/probes/Instrumentation/parser.py +129 -0
- pythonagent/agent/probes/__init__.py +219 -0
- pythonagent/agent/probes/base.py +303 -0
- pythonagent/agent/probes/cache/__init__.py +51 -0
- pythonagent/agent/probes/cache/redis.py +119 -0
- pythonagent/agent/probes/cache/redis_asyncio.py +83 -0
- pythonagent/agent/probes/coroutines/__init__.py +1 -0
- pythonagent/agent/probes/coroutines/asyncio.py +63 -0
- pythonagent/agent/probes/elasticdb/__init__.py +7 -0
- pythonagent/agent/probes/elasticdb/aelastic.py +54 -0
- pythonagent/agent/probes/frameworks/__init__.py +27 -0
- pythonagent/agent/probes/frameworks/agentprofiler.py +105 -0
- pythonagent/agent/probes/frameworks/aiohttp_web.py +155 -0
- pythonagent/agent/probes/frameworks/aisess.py +151 -0
- pythonagent/agent/probes/frameworks/asgi.py +340 -0
- pythonagent/agent/probes/frameworks/bottle.py +27 -0
- pythonagent/agent/probes/frameworks/cherry.py +25 -0
- pythonagent/agent/probes/frameworks/django.py +128 -0
- pythonagent/agent/probes/frameworks/falcon.py +21 -0
- pythonagent/agent/probes/frameworks/fastapi.py +35 -0
- pythonagent/agent/probes/frameworks/flask.py +30 -0
- pythonagent/agent/probes/frameworks/pyramid.py +56 -0
- pythonagent/agent/probes/frameworks/test.py +108 -0
- pythonagent/agent/probes/frameworks/tornado_async_web.py +117 -0
- pythonagent/agent/probes/frameworks/tornado_web.py +133 -0
- pythonagent/agent/probes/frameworks/wsgi.py +353 -0
- pythonagent/agent/probes/grpc/__init__.py +76 -0
- pythonagent/agent/probes/grpc/client_interceptor.py +132 -0
- pythonagent/agent/probes/grpc/server_interceptor.py +129 -0
- pythonagent/agent/probes/havoc/__init__.py +0 -0
- pythonagent/agent/probes/havoc/custom_memory_stress.py +186 -0
- pythonagent/agent/probes/havoc/custom_thread_stress.py +187 -0
- pythonagent/agent/probes/havoc/havoc_constants.py +218 -0
- pythonagent/agent/probes/havoc/havoc_manager.py +981 -0
- pythonagent/agent/probes/http/__init__.py +49 -0
- pythonagent/agent/probes/http/aiohttp_client.py +59 -0
- pythonagent/agent/probes/http/boto.py +12 -0
- pythonagent/agent/probes/http/httplib.py +110 -0
- pythonagent/agent/probes/http/httpx_client.py +116 -0
- pythonagent/agent/probes/http/requests.py +15 -0
- pythonagent/agent/probes/http/tornado_httpclient.py +85 -0
- pythonagent/agent/probes/http/urllib3.py +16 -0
- pythonagent/agent/probes/langchain/__init__.py +21 -0
- pythonagent/agent/probes/langchain/base_tool.py +95 -0
- pythonagent/agent/probes/langchain/langchain_community.py +136 -0
- pythonagent/agent/probes/langchain/langchain_core.py +32 -0
- pythonagent/agent/probes/langchain/langchain_openai.py +110 -0
- pythonagent/agent/probes/logging/__init__.py +106 -0
- pythonagent/agent/probes/message_brokers/__init__.py +4 -0
- pythonagent/agent/probes/message_brokers/pika.py +126 -0
- pythonagent/agent/probes/mongodb/__init__.py +6 -0
- pythonagent/agent/probes/mongodb/pymongo.py +286 -0
- pythonagent/agent/probes/openai/__init__.py +3 -0
- pythonagent/agent/probes/openai/openai.py +797 -0
- pythonagent/agent/probes/span.py +101 -0
- pythonagent/agent/probes/sql/__init__.py +13 -0
- pythonagent/agent/probes/sql/botocores3.py +51 -0
- pythonagent/agent/probes/sql/dbapi.py +285 -0
- pythonagent/agent/probes/sql/dynamodb.py +90 -0
- pythonagent/agent/probes/sql/mysql_connector.py +24 -0
- pythonagent/agent/probes/sql/mysql_connector_cext.py +24 -0
- pythonagent/agent/probes/sql/mysqldb.py +43 -0
- pythonagent/agent/probes/sql/psycopg2.py +174 -0
- pythonagent/agent/probes/sql/pymysql.py +25 -0
- pythonagent/bootstrap/__init__.py +0 -0
- pythonagent/bootstrap/cav_gunicorn.py +26 -0
- pythonagent/bootstrap/cavagent_lambda_wrapper.py +291 -0
- pythonagent/bootstrap/run.py +47 -0
- pythonagent/bootstrap/sitecustomize.py +287 -0
- pythonagent/cavisson/netdiagnostics/CavAgent/instrumentationprofile.json +26 -0
- pythonagent/cavisson/netdiagnostics/CavAgent/interceptor_points.txt +29 -0
- pythonagent/cavisson/netdiagnostics/python/CavAgent/instrumentationprofile.json +42 -0
- pythonagent/cavisson/netdiagnostics/python/CavAgent/interceptor_points.txt +29 -0
- pythonagent/cavisson/netdiagnostics/python/config/ndsettings.conf +6 -0
- pythonagent/config.py +279 -0
- pythonagent/find.py +72 -0
- pythonagent/find_mod_cls_name.py +54 -0
- pythonagent/lang.py +131 -0
- pythonagent/lib.py +91 -0
- pythonagent/main/__init__.py +0 -0
- pythonagent/main/pytrace/__init__.py +79 -0
- pythonagent/main/pytrace/commands/__init__.py +0 -0
- pythonagent/main/pytrace/commands/auto_discovery.py +25 -0
- pythonagent/main/pytrace/commands/run.py +401 -0
- pythonagent/main/pytrace/pytrace.py +133 -0
- pythonagent/main/wsgi.py +6 -0
- pythonagent/main.py +27 -0
- pythonagent/run.py +46 -0
- pythonagent/sqins.py +8 -0
- pythonagent/test.py +73 -0
- pythonagent/utils.py +168 -0
- pythonagent/vendor/__init__.py +0 -0
- pythonagent/vendor/pympler/__init__.py +1 -0
- pythonagent/vendor/pympler/asizeof.py +2810 -0
- pythonagent/vendor/pympler/charts.py +62 -0
- pythonagent/vendor/pympler/classtracker.py +590 -0
- pythonagent/vendor/pympler/classtracker_stats.py +780 -0
- pythonagent/vendor/pympler/garbagegraph.py +80 -0
- pythonagent/vendor/pympler/mprofile.py +97 -0
- pythonagent/vendor/pympler/muppy.py +275 -0
- pythonagent/vendor/pympler/panels.py +115 -0
- pythonagent/vendor/pympler/process.py +238 -0
- pythonagent/vendor/pympler/py.typed +0 -0
- pythonagent/vendor/pympler/refbrowser.py +451 -0
- pythonagent/vendor/pympler/refgraph.py +350 -0
- pythonagent/vendor/pympler/summary.py +321 -0
- pythonagent/vendor/pympler/tracker.py +267 -0
- pythonagent/vendor/pympler/util/__init__.py +0 -0
- pythonagent/vendor/pympler/util/bottle.py +3809 -0
- pythonagent/vendor/pympler/util/compat.py +23 -0
- pythonagent/vendor/pympler/util/stringutils.py +77 -0
- pythonagent/vendor/pympler/web.py +346 -0
- pythonagent/vendor/werkzeug/__init__.py +20 -0
- pythonagent/vendor/werkzeug/_compat.py +228 -0
- pythonagent/vendor/werkzeug/_internal.py +473 -0
- pythonagent/vendor/werkzeug/_reloader.py +341 -0
- pythonagent/vendor/werkzeug/datastructures.py +3120 -0
- pythonagent/vendor/werkzeug/debug/__init__.py +498 -0
- pythonagent/vendor/werkzeug/debug/console.py +218 -0
- pythonagent/vendor/werkzeug/debug/repr.py +297 -0
- pythonagent/vendor/werkzeug/debug/tbtools.py +628 -0
- pythonagent/vendor/werkzeug/exceptions.py +829 -0
- pythonagent/vendor/werkzeug/filesystem.py +64 -0
- pythonagent/vendor/werkzeug/formparser.py +584 -0
- pythonagent/vendor/werkzeug/http.py +1307 -0
- pythonagent/vendor/werkzeug/local.py +420 -0
- pythonagent/vendor/werkzeug/middleware/__init__.py +25 -0
- pythonagent/vendor/werkzeug/middleware/dispatcher.py +66 -0
- pythonagent/vendor/werkzeug/middleware/http_proxy.py +219 -0
- pythonagent/vendor/werkzeug/middleware/lint.py +408 -0
- pythonagent/vendor/werkzeug/middleware/profiler.py +132 -0
- pythonagent/vendor/werkzeug/middleware/proxy_fix.py +169 -0
- pythonagent/vendor/werkzeug/middleware/shared_data.py +293 -0
- pythonagent/vendor/werkzeug/posixemulation.py +117 -0
- pythonagent/vendor/werkzeug/routing.py +2210 -0
- pythonagent/vendor/werkzeug/security.py +249 -0
- pythonagent/vendor/werkzeug/serving.py +1117 -0
- pythonagent/vendor/werkzeug/test.py +1123 -0
- pythonagent/vendor/werkzeug/testapp.py +241 -0
- pythonagent/vendor/werkzeug/urls.py +1138 -0
- pythonagent/vendor/werkzeug/useragents.py +202 -0
- pythonagent/vendor/werkzeug/utils.py +778 -0
- pythonagent/vendor/werkzeug/wrappers/__init__.py +36 -0
- pythonagent/vendor/werkzeug/wrappers/accept.py +50 -0
- pythonagent/vendor/werkzeug/wrappers/auth.py +33 -0
- pythonagent/vendor/werkzeug/wrappers/base_request.py +673 -0
- pythonagent/vendor/werkzeug/wrappers/base_response.py +700 -0
- pythonagent/vendor/werkzeug/wrappers/common_descriptors.py +341 -0
- pythonagent/vendor/werkzeug/wrappers/cors.py +100 -0
- pythonagent/vendor/werkzeug/wrappers/etag.py +304 -0
- pythonagent/vendor/werkzeug/wrappers/json.py +145 -0
- pythonagent/vendor/werkzeug/wrappers/request.py +49 -0
- pythonagent/vendor/werkzeug/wrappers/response.py +84 -0
- pythonagent/vendor/werkzeug/wrappers/user_agent.py +14 -0
- pythonagent/vendor/werkzeug/wsgi.py +1000 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
werkzeug.security
|
|
4
|
+
~~~~~~~~~~~~~~~~~
|
|
5
|
+
|
|
6
|
+
Security related helpers such as secure password hashing tools.
|
|
7
|
+
|
|
8
|
+
:copyright: 2007 Pallets
|
|
9
|
+
:license: BSD-3-Clause
|
|
10
|
+
"""
|
|
11
|
+
import codecs
|
|
12
|
+
import hashlib
|
|
13
|
+
import hmac
|
|
14
|
+
import os
|
|
15
|
+
import posixpath
|
|
16
|
+
from random import SystemRandom
|
|
17
|
+
from struct import Struct
|
|
18
|
+
|
|
19
|
+
from ._compat import izip
|
|
20
|
+
from ._compat import PY2
|
|
21
|
+
from ._compat import range_type
|
|
22
|
+
from ._compat import text_type
|
|
23
|
+
from ._compat import to_bytes
|
|
24
|
+
from ._compat import to_native
|
|
25
|
+
|
|
26
|
+
SALT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
27
|
+
DEFAULT_PBKDF2_ITERATIONS = 150000
|
|
28
|
+
|
|
29
|
+
_pack_int = Struct(">I").pack
|
|
30
|
+
_builtin_safe_str_cmp = getattr(hmac, "compare_digest", None)
|
|
31
|
+
_sys_rng = SystemRandom()
|
|
32
|
+
_os_alt_seps = list(
|
|
33
|
+
sep for sep in [os.path.sep, os.path.altsep] if sep not in (None, "/")
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def pbkdf2_hex(
|
|
38
|
+
data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS, keylen=None, hashfunc=None
|
|
39
|
+
):
|
|
40
|
+
"""Like :func:`pbkdf2_bin`, but returns a hex-encoded string.
|
|
41
|
+
|
|
42
|
+
.. versionadded:: 0.9
|
|
43
|
+
|
|
44
|
+
:param data: the data to derive.
|
|
45
|
+
:param salt: the salt for the derivation.
|
|
46
|
+
:param iterations: the number of iterations.
|
|
47
|
+
:param keylen: the length of the resulting key. If not provided,
|
|
48
|
+
the digest size will be used.
|
|
49
|
+
:param hashfunc: the hash function to use. This can either be the
|
|
50
|
+
string name of a known hash function, or a function
|
|
51
|
+
from the hashlib module. Defaults to sha256.
|
|
52
|
+
"""
|
|
53
|
+
rv = pbkdf2_bin(data, salt, iterations, keylen, hashfunc)
|
|
54
|
+
return to_native(codecs.encode(rv, "hex_codec"))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def pbkdf2_bin(
|
|
58
|
+
data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS, keylen=None, hashfunc=None
|
|
59
|
+
):
|
|
60
|
+
"""Returns a binary digest for the PBKDF2 hash algorithm of `data`
|
|
61
|
+
with the given `salt`. It iterates `iterations` times and produces a
|
|
62
|
+
key of `keylen` bytes. By default, SHA-256 is used as hash function;
|
|
63
|
+
a different hashlib `hashfunc` can be provided.
|
|
64
|
+
|
|
65
|
+
.. versionadded:: 0.9
|
|
66
|
+
|
|
67
|
+
:param data: the data to derive.
|
|
68
|
+
:param salt: the salt for the derivation.
|
|
69
|
+
:param iterations: the number of iterations.
|
|
70
|
+
:param keylen: the length of the resulting key. If not provided
|
|
71
|
+
the digest size will be used.
|
|
72
|
+
:param hashfunc: the hash function to use. This can either be the
|
|
73
|
+
string name of a known hash function or a function
|
|
74
|
+
from the hashlib module. Defaults to sha256.
|
|
75
|
+
"""
|
|
76
|
+
if not hashfunc:
|
|
77
|
+
hashfunc = "sha256"
|
|
78
|
+
|
|
79
|
+
data = to_bytes(data)
|
|
80
|
+
salt = to_bytes(salt)
|
|
81
|
+
|
|
82
|
+
if callable(hashfunc):
|
|
83
|
+
_test_hash = hashfunc()
|
|
84
|
+
hash_name = getattr(_test_hash, "name", None)
|
|
85
|
+
else:
|
|
86
|
+
hash_name = hashfunc
|
|
87
|
+
return hashlib.pbkdf2_hmac(hash_name, data, salt, iterations, keylen)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def safe_str_cmp(a, b):
|
|
91
|
+
"""This function compares strings in somewhat constant time. This
|
|
92
|
+
requires that the length of at least one string is known in advance.
|
|
93
|
+
|
|
94
|
+
Returns `True` if the two strings are equal, or `False` if they are not.
|
|
95
|
+
|
|
96
|
+
.. versionadded:: 0.7
|
|
97
|
+
"""
|
|
98
|
+
if isinstance(a, text_type):
|
|
99
|
+
a = a.encode("utf-8")
|
|
100
|
+
if isinstance(b, text_type):
|
|
101
|
+
b = b.encode("utf-8")
|
|
102
|
+
|
|
103
|
+
if _builtin_safe_str_cmp is not None:
|
|
104
|
+
return _builtin_safe_str_cmp(a, b)
|
|
105
|
+
|
|
106
|
+
if len(a) != len(b):
|
|
107
|
+
return False
|
|
108
|
+
|
|
109
|
+
rv = 0
|
|
110
|
+
if PY2:
|
|
111
|
+
for x, y in izip(a, b):
|
|
112
|
+
rv |= ord(x) ^ ord(y)
|
|
113
|
+
else:
|
|
114
|
+
for x, y in izip(a, b):
|
|
115
|
+
rv |= x ^ y
|
|
116
|
+
|
|
117
|
+
return rv == 0
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def gen_salt(length):
|
|
121
|
+
"""Generate a random string of SALT_CHARS with specified ``length``."""
|
|
122
|
+
if length <= 0:
|
|
123
|
+
raise ValueError("Salt length must be positive")
|
|
124
|
+
return "".join(_sys_rng.choice(SALT_CHARS) for _ in range_type(length))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _hash_internal(method, salt, password):
|
|
128
|
+
"""Internal password hash helper. Supports plaintext without salt,
|
|
129
|
+
unsalted and salted passwords. In case salted passwords are used
|
|
130
|
+
hmac is used.
|
|
131
|
+
"""
|
|
132
|
+
if method == "plain":
|
|
133
|
+
return password, method
|
|
134
|
+
|
|
135
|
+
if isinstance(password, text_type):
|
|
136
|
+
password = password.encode("utf-8")
|
|
137
|
+
|
|
138
|
+
if method.startswith("pbkdf2:"):
|
|
139
|
+
args = method[7:].split(":")
|
|
140
|
+
if len(args) not in (1, 2):
|
|
141
|
+
raise ValueError("Invalid number of arguments for PBKDF2")
|
|
142
|
+
method = args.pop(0)
|
|
143
|
+
iterations = args and int(args[0] or 0) or DEFAULT_PBKDF2_ITERATIONS
|
|
144
|
+
is_pbkdf2 = True
|
|
145
|
+
actual_method = "pbkdf2:%s:%d" % (method, iterations)
|
|
146
|
+
else:
|
|
147
|
+
is_pbkdf2 = False
|
|
148
|
+
actual_method = method
|
|
149
|
+
|
|
150
|
+
if is_pbkdf2:
|
|
151
|
+
if not salt:
|
|
152
|
+
raise ValueError("Salt is required for PBKDF2")
|
|
153
|
+
rv = pbkdf2_hex(password, salt, iterations, hashfunc=method)
|
|
154
|
+
elif salt:
|
|
155
|
+
if isinstance(salt, text_type):
|
|
156
|
+
salt = salt.encode("utf-8")
|
|
157
|
+
mac = _create_mac(salt, password, method)
|
|
158
|
+
rv = mac.hexdigest()
|
|
159
|
+
else:
|
|
160
|
+
rv = hashlib.new(method, password).hexdigest()
|
|
161
|
+
return rv, actual_method
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _create_mac(key, msg, method):
|
|
165
|
+
if callable(method):
|
|
166
|
+
return hmac.HMAC(key, msg, method)
|
|
167
|
+
|
|
168
|
+
def hashfunc(d=b""):
|
|
169
|
+
return hashlib.new(method, d)
|
|
170
|
+
|
|
171
|
+
# Python 2.7 used ``hasattr(digestmod, '__call__')``
|
|
172
|
+
# to detect if hashfunc is callable
|
|
173
|
+
hashfunc.__call__ = hashfunc
|
|
174
|
+
return hmac.HMAC(key, msg, hashfunc)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def generate_password_hash(password, method="pbkdf2:sha256", salt_length=8):
|
|
178
|
+
"""Hash a password with the given method and salt with a string of
|
|
179
|
+
the given length. The format of the string returned includes the method
|
|
180
|
+
that was used so that :func:`check_password_hash` can check the hash.
|
|
181
|
+
|
|
182
|
+
The format for the hashed string looks like this::
|
|
183
|
+
|
|
184
|
+
method$salt$hash
|
|
185
|
+
|
|
186
|
+
This method can **not** generate unsalted passwords but it is possible
|
|
187
|
+
to set param method='plain' in order to enforce plaintext passwords.
|
|
188
|
+
If a salt is used, hmac is used internally to salt the password.
|
|
189
|
+
|
|
190
|
+
If PBKDF2 is wanted it can be enabled by setting the method to
|
|
191
|
+
``pbkdf2:method:iterations`` where iterations is optional::
|
|
192
|
+
|
|
193
|
+
pbkdf2:sha256:80000$salt$hash
|
|
194
|
+
pbkdf2:sha256$salt$hash
|
|
195
|
+
|
|
196
|
+
:param password: the password to hash.
|
|
197
|
+
:param method: the hash method to use (one that hashlib supports). Can
|
|
198
|
+
optionally be in the format ``pbkdf2:<method>[:iterations]``
|
|
199
|
+
to enable PBKDF2.
|
|
200
|
+
:param salt_length: the length of the salt in letters.
|
|
201
|
+
"""
|
|
202
|
+
salt = gen_salt(salt_length) if method != "plain" else ""
|
|
203
|
+
h, actual_method = _hash_internal(method, salt, password)
|
|
204
|
+
return "%s$%s$%s" % (actual_method, salt, h)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def check_password_hash(pwhash, password):
|
|
208
|
+
"""check a password against a given salted and hashed password value.
|
|
209
|
+
In order to support unsalted legacy passwords this method supports
|
|
210
|
+
plain text passwords, md5 and sha1 hashes (both salted and unsalted).
|
|
211
|
+
|
|
212
|
+
Returns `True` if the password matched, `False` otherwise.
|
|
213
|
+
|
|
214
|
+
:param pwhash: a hashed string like returned by
|
|
215
|
+
:func:`generate_password_hash`.
|
|
216
|
+
:param password: the plaintext password to compare against the hash.
|
|
217
|
+
"""
|
|
218
|
+
if pwhash.count("$") < 2:
|
|
219
|
+
return False
|
|
220
|
+
method, salt, hashval = pwhash.split("$", 2)
|
|
221
|
+
return safe_str_cmp(_hash_internal(method, salt, password)[0], hashval)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def safe_join(directory, *pathnames):
|
|
225
|
+
"""Safely join zero or more untrusted path components to a base
|
|
226
|
+
directory to avoid escaping the base directory.
|
|
227
|
+
|
|
228
|
+
:param directory: The trusted base directory.
|
|
229
|
+
:param pathnames: The untrusted path components relative to the
|
|
230
|
+
base directory.
|
|
231
|
+
:return: A safe path, otherwise ``None``.
|
|
232
|
+
"""
|
|
233
|
+
parts = [directory]
|
|
234
|
+
|
|
235
|
+
for filename in pathnames:
|
|
236
|
+
if filename != "":
|
|
237
|
+
filename = posixpath.normpath(filename)
|
|
238
|
+
|
|
239
|
+
if (
|
|
240
|
+
any(sep in filename for sep in _os_alt_seps)
|
|
241
|
+
or os.path.isabs(filename)
|
|
242
|
+
or filename == ".."
|
|
243
|
+
or filename.startswith("../")
|
|
244
|
+
):
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
parts.append(filename)
|
|
248
|
+
|
|
249
|
+
return posixpath.join(*parts)
|