annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/wsgiref/validate.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
jpayne@68 2 # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
jpayne@68 3 # Also licenced under the Apache License, 2.0: http://opensource.org/licenses/apache2.0.php
jpayne@68 4 # Licensed to PSF under a Contributor Agreement
jpayne@68 5 """
jpayne@68 6 Middleware to check for obedience to the WSGI specification.
jpayne@68 7
jpayne@68 8 Some of the things this checks:
jpayne@68 9
jpayne@68 10 * Signature of the application and start_response (including that
jpayne@68 11 keyword arguments are not used).
jpayne@68 12
jpayne@68 13 * Environment checks:
jpayne@68 14
jpayne@68 15 - Environment is a dictionary (and not a subclass).
jpayne@68 16
jpayne@68 17 - That all the required keys are in the environment: REQUEST_METHOD,
jpayne@68 18 SERVER_NAME, SERVER_PORT, wsgi.version, wsgi.input, wsgi.errors,
jpayne@68 19 wsgi.multithread, wsgi.multiprocess, wsgi.run_once
jpayne@68 20
jpayne@68 21 - That HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH are not in the
jpayne@68 22 environment (these headers should appear as CONTENT_LENGTH and
jpayne@68 23 CONTENT_TYPE).
jpayne@68 24
jpayne@68 25 - Warns if QUERY_STRING is missing, as the cgi module acts
jpayne@68 26 unpredictably in that case.
jpayne@68 27
jpayne@68 28 - That CGI-style variables (that don't contain a .) have
jpayne@68 29 (non-unicode) string values
jpayne@68 30
jpayne@68 31 - That wsgi.version is a tuple
jpayne@68 32
jpayne@68 33 - That wsgi.url_scheme is 'http' or 'https' (@@: is this too
jpayne@68 34 restrictive?)
jpayne@68 35
jpayne@68 36 - Warns if the REQUEST_METHOD is not known (@@: probably too
jpayne@68 37 restrictive).
jpayne@68 38
jpayne@68 39 - That SCRIPT_NAME and PATH_INFO are empty or start with /
jpayne@68 40
jpayne@68 41 - That at least one of SCRIPT_NAME or PATH_INFO are set.
jpayne@68 42
jpayne@68 43 - That CONTENT_LENGTH is a positive integer.
jpayne@68 44
jpayne@68 45 - That SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should
jpayne@68 46 be '/').
jpayne@68 47
jpayne@68 48 - That wsgi.input has the methods read, readline, readlines, and
jpayne@68 49 __iter__
jpayne@68 50
jpayne@68 51 - That wsgi.errors has the methods flush, write, writelines
jpayne@68 52
jpayne@68 53 * The status is a string, contains a space, starts with an integer,
jpayne@68 54 and that integer is in range (> 100).
jpayne@68 55
jpayne@68 56 * That the headers is a list (not a subclass, not another kind of
jpayne@68 57 sequence).
jpayne@68 58
jpayne@68 59 * That the items of the headers are tuples of strings.
jpayne@68 60
jpayne@68 61 * That there is no 'status' header (that is used in CGI, but not in
jpayne@68 62 WSGI).
jpayne@68 63
jpayne@68 64 * That the headers don't contain newlines or colons, end in _ or -, or
jpayne@68 65 contain characters codes below 037.
jpayne@68 66
jpayne@68 67 * That Content-Type is given if there is content (CGI often has a
jpayne@68 68 default content type, but WSGI does not).
jpayne@68 69
jpayne@68 70 * That no Content-Type is given when there is no content (@@: is this
jpayne@68 71 too restrictive?)
jpayne@68 72
jpayne@68 73 * That the exc_info argument to start_response is a tuple or None.
jpayne@68 74
jpayne@68 75 * That all calls to the writer are with strings, and no other methods
jpayne@68 76 on the writer are accessed.
jpayne@68 77
jpayne@68 78 * That wsgi.input is used properly:
jpayne@68 79
jpayne@68 80 - .read() is called with exactly one argument
jpayne@68 81
jpayne@68 82 - That it returns a string
jpayne@68 83
jpayne@68 84 - That readline, readlines, and __iter__ return strings
jpayne@68 85
jpayne@68 86 - That .close() is not called
jpayne@68 87
jpayne@68 88 - No other methods are provided
jpayne@68 89
jpayne@68 90 * That wsgi.errors is used properly:
jpayne@68 91
jpayne@68 92 - .write() and .writelines() is called with a string
jpayne@68 93
jpayne@68 94 - That .close() is not called, and no other methods are provided.
jpayne@68 95
jpayne@68 96 * The response iterator:
jpayne@68 97
jpayne@68 98 - That it is not a string (it should be a list of a single string; a
jpayne@68 99 string will work, but perform horribly).
jpayne@68 100
jpayne@68 101 - That .__next__() returns a string
jpayne@68 102
jpayne@68 103 - That the iterator is not iterated over until start_response has
jpayne@68 104 been called (that can signal either a server or application
jpayne@68 105 error).
jpayne@68 106
jpayne@68 107 - That .close() is called (doesn't raise exception, only prints to
jpayne@68 108 sys.stderr, because we only know it isn't called when the object
jpayne@68 109 is garbage collected).
jpayne@68 110 """
jpayne@68 111 __all__ = ['validator']
jpayne@68 112
jpayne@68 113
jpayne@68 114 import re
jpayne@68 115 import sys
jpayne@68 116 import warnings
jpayne@68 117
jpayne@68 118 header_re = re.compile(r'^[a-zA-Z][a-zA-Z0-9\-_]*$')
jpayne@68 119 bad_header_value_re = re.compile(r'[\000-\037]')
jpayne@68 120
jpayne@68 121 class WSGIWarning(Warning):
jpayne@68 122 """
jpayne@68 123 Raised in response to WSGI-spec-related warnings
jpayne@68 124 """
jpayne@68 125
jpayne@68 126 def assert_(cond, *args):
jpayne@68 127 if not cond:
jpayne@68 128 raise AssertionError(*args)
jpayne@68 129
jpayne@68 130 def check_string_type(value, title):
jpayne@68 131 if type (value) is str:
jpayne@68 132 return value
jpayne@68 133 raise AssertionError(
jpayne@68 134 "{0} must be of type str (got {1})".format(title, repr(value)))
jpayne@68 135
jpayne@68 136 def validator(application):
jpayne@68 137
jpayne@68 138 """
jpayne@68 139 When applied between a WSGI server and a WSGI application, this
jpayne@68 140 middleware will check for WSGI compliancy on a number of levels.
jpayne@68 141 This middleware does not modify the request or response in any
jpayne@68 142 way, but will raise an AssertionError if anything seems off
jpayne@68 143 (except for a failure to close the application iterator, which
jpayne@68 144 will be printed to stderr -- there's no way to raise an exception
jpayne@68 145 at that point).
jpayne@68 146 """
jpayne@68 147
jpayne@68 148 def lint_app(*args, **kw):
jpayne@68 149 assert_(len(args) == 2, "Two arguments required")
jpayne@68 150 assert_(not kw, "No keyword arguments allowed")
jpayne@68 151 environ, start_response = args
jpayne@68 152
jpayne@68 153 check_environ(environ)
jpayne@68 154
jpayne@68 155 # We use this to check if the application returns without
jpayne@68 156 # calling start_response:
jpayne@68 157 start_response_started = []
jpayne@68 158
jpayne@68 159 def start_response_wrapper(*args, **kw):
jpayne@68 160 assert_(len(args) == 2 or len(args) == 3, (
jpayne@68 161 "Invalid number of arguments: %s" % (args,)))
jpayne@68 162 assert_(not kw, "No keyword arguments allowed")
jpayne@68 163 status = args[0]
jpayne@68 164 headers = args[1]
jpayne@68 165 if len(args) == 3:
jpayne@68 166 exc_info = args[2]
jpayne@68 167 else:
jpayne@68 168 exc_info = None
jpayne@68 169
jpayne@68 170 check_status(status)
jpayne@68 171 check_headers(headers)
jpayne@68 172 check_content_type(status, headers)
jpayne@68 173 check_exc_info(exc_info)
jpayne@68 174
jpayne@68 175 start_response_started.append(None)
jpayne@68 176 return WriteWrapper(start_response(*args))
jpayne@68 177
jpayne@68 178 environ['wsgi.input'] = InputWrapper(environ['wsgi.input'])
jpayne@68 179 environ['wsgi.errors'] = ErrorWrapper(environ['wsgi.errors'])
jpayne@68 180
jpayne@68 181 iterator = application(environ, start_response_wrapper)
jpayne@68 182 assert_(iterator is not None and iterator != False,
jpayne@68 183 "The application must return an iterator, if only an empty list")
jpayne@68 184
jpayne@68 185 check_iterator(iterator)
jpayne@68 186
jpayne@68 187 return IteratorWrapper(iterator, start_response_started)
jpayne@68 188
jpayne@68 189 return lint_app
jpayne@68 190
jpayne@68 191 class InputWrapper:
jpayne@68 192
jpayne@68 193 def __init__(self, wsgi_input):
jpayne@68 194 self.input = wsgi_input
jpayne@68 195
jpayne@68 196 def read(self, *args):
jpayne@68 197 assert_(len(args) == 1)
jpayne@68 198 v = self.input.read(*args)
jpayne@68 199 assert_(type(v) is bytes)
jpayne@68 200 return v
jpayne@68 201
jpayne@68 202 def readline(self, *args):
jpayne@68 203 assert_(len(args) <= 1)
jpayne@68 204 v = self.input.readline(*args)
jpayne@68 205 assert_(type(v) is bytes)
jpayne@68 206 return v
jpayne@68 207
jpayne@68 208 def readlines(self, *args):
jpayne@68 209 assert_(len(args) <= 1)
jpayne@68 210 lines = self.input.readlines(*args)
jpayne@68 211 assert_(type(lines) is list)
jpayne@68 212 for line in lines:
jpayne@68 213 assert_(type(line) is bytes)
jpayne@68 214 return lines
jpayne@68 215
jpayne@68 216 def __iter__(self):
jpayne@68 217 while 1:
jpayne@68 218 line = self.readline()
jpayne@68 219 if not line:
jpayne@68 220 return
jpayne@68 221 yield line
jpayne@68 222
jpayne@68 223 def close(self):
jpayne@68 224 assert_(0, "input.close() must not be called")
jpayne@68 225
jpayne@68 226 class ErrorWrapper:
jpayne@68 227
jpayne@68 228 def __init__(self, wsgi_errors):
jpayne@68 229 self.errors = wsgi_errors
jpayne@68 230
jpayne@68 231 def write(self, s):
jpayne@68 232 assert_(type(s) is str)
jpayne@68 233 self.errors.write(s)
jpayne@68 234
jpayne@68 235 def flush(self):
jpayne@68 236 self.errors.flush()
jpayne@68 237
jpayne@68 238 def writelines(self, seq):
jpayne@68 239 for line in seq:
jpayne@68 240 self.write(line)
jpayne@68 241
jpayne@68 242 def close(self):
jpayne@68 243 assert_(0, "errors.close() must not be called")
jpayne@68 244
jpayne@68 245 class WriteWrapper:
jpayne@68 246
jpayne@68 247 def __init__(self, wsgi_writer):
jpayne@68 248 self.writer = wsgi_writer
jpayne@68 249
jpayne@68 250 def __call__(self, s):
jpayne@68 251 assert_(type(s) is bytes)
jpayne@68 252 self.writer(s)
jpayne@68 253
jpayne@68 254 class PartialIteratorWrapper:
jpayne@68 255
jpayne@68 256 def __init__(self, wsgi_iterator):
jpayne@68 257 self.iterator = wsgi_iterator
jpayne@68 258
jpayne@68 259 def __iter__(self):
jpayne@68 260 # We want to make sure __iter__ is called
jpayne@68 261 return IteratorWrapper(self.iterator, None)
jpayne@68 262
jpayne@68 263 class IteratorWrapper:
jpayne@68 264
jpayne@68 265 def __init__(self, wsgi_iterator, check_start_response):
jpayne@68 266 self.original_iterator = wsgi_iterator
jpayne@68 267 self.iterator = iter(wsgi_iterator)
jpayne@68 268 self.closed = False
jpayne@68 269 self.check_start_response = check_start_response
jpayne@68 270
jpayne@68 271 def __iter__(self):
jpayne@68 272 return self
jpayne@68 273
jpayne@68 274 def __next__(self):
jpayne@68 275 assert_(not self.closed,
jpayne@68 276 "Iterator read after closed")
jpayne@68 277 v = next(self.iterator)
jpayne@68 278 if type(v) is not bytes:
jpayne@68 279 assert_(False, "Iterator yielded non-bytestring (%r)" % (v,))
jpayne@68 280 if self.check_start_response is not None:
jpayne@68 281 assert_(self.check_start_response,
jpayne@68 282 "The application returns and we started iterating over its body, but start_response has not yet been called")
jpayne@68 283 self.check_start_response = None
jpayne@68 284 return v
jpayne@68 285
jpayne@68 286 def close(self):
jpayne@68 287 self.closed = True
jpayne@68 288 if hasattr(self.original_iterator, 'close'):
jpayne@68 289 self.original_iterator.close()
jpayne@68 290
jpayne@68 291 def __del__(self):
jpayne@68 292 if not self.closed:
jpayne@68 293 sys.stderr.write(
jpayne@68 294 "Iterator garbage collected without being closed")
jpayne@68 295 assert_(self.closed,
jpayne@68 296 "Iterator garbage collected without being closed")
jpayne@68 297
jpayne@68 298 def check_environ(environ):
jpayne@68 299 assert_(type(environ) is dict,
jpayne@68 300 "Environment is not of the right type: %r (environment: %r)"
jpayne@68 301 % (type(environ), environ))
jpayne@68 302
jpayne@68 303 for key in ['REQUEST_METHOD', 'SERVER_NAME', 'SERVER_PORT',
jpayne@68 304 'wsgi.version', 'wsgi.input', 'wsgi.errors',
jpayne@68 305 'wsgi.multithread', 'wsgi.multiprocess',
jpayne@68 306 'wsgi.run_once']:
jpayne@68 307 assert_(key in environ,
jpayne@68 308 "Environment missing required key: %r" % (key,))
jpayne@68 309
jpayne@68 310 for key in ['HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH']:
jpayne@68 311 assert_(key not in environ,
jpayne@68 312 "Environment should not have the key: %s "
jpayne@68 313 "(use %s instead)" % (key, key[5:]))
jpayne@68 314
jpayne@68 315 if 'QUERY_STRING' not in environ:
jpayne@68 316 warnings.warn(
jpayne@68 317 'QUERY_STRING is not in the WSGI environment; the cgi '
jpayne@68 318 'module will use sys.argv when this variable is missing, '
jpayne@68 319 'so application errors are more likely',
jpayne@68 320 WSGIWarning)
jpayne@68 321
jpayne@68 322 for key in environ.keys():
jpayne@68 323 if '.' in key:
jpayne@68 324 # Extension, we don't care about its type
jpayne@68 325 continue
jpayne@68 326 assert_(type(environ[key]) is str,
jpayne@68 327 "Environmental variable %s is not a string: %r (value: %r)"
jpayne@68 328 % (key, type(environ[key]), environ[key]))
jpayne@68 329
jpayne@68 330 assert_(type(environ['wsgi.version']) is tuple,
jpayne@68 331 "wsgi.version should be a tuple (%r)" % (environ['wsgi.version'],))
jpayne@68 332 assert_(environ['wsgi.url_scheme'] in ('http', 'https'),
jpayne@68 333 "wsgi.url_scheme unknown: %r" % environ['wsgi.url_scheme'])
jpayne@68 334
jpayne@68 335 check_input(environ['wsgi.input'])
jpayne@68 336 check_errors(environ['wsgi.errors'])
jpayne@68 337
jpayne@68 338 # @@: these need filling out:
jpayne@68 339 if environ['REQUEST_METHOD'] not in (
jpayne@68 340 'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'TRACE'):
jpayne@68 341 warnings.warn(
jpayne@68 342 "Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'],
jpayne@68 343 WSGIWarning)
jpayne@68 344
jpayne@68 345 assert_(not environ.get('SCRIPT_NAME')
jpayne@68 346 or environ['SCRIPT_NAME'].startswith('/'),
jpayne@68 347 "SCRIPT_NAME doesn't start with /: %r" % environ['SCRIPT_NAME'])
jpayne@68 348 assert_(not environ.get('PATH_INFO')
jpayne@68 349 or environ['PATH_INFO'].startswith('/'),
jpayne@68 350 "PATH_INFO doesn't start with /: %r" % environ['PATH_INFO'])
jpayne@68 351 if environ.get('CONTENT_LENGTH'):
jpayne@68 352 assert_(int(environ['CONTENT_LENGTH']) >= 0,
jpayne@68 353 "Invalid CONTENT_LENGTH: %r" % environ['CONTENT_LENGTH'])
jpayne@68 354
jpayne@68 355 if not environ.get('SCRIPT_NAME'):
jpayne@68 356 assert_('PATH_INFO' in environ,
jpayne@68 357 "One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO "
jpayne@68 358 "should at least be '/' if SCRIPT_NAME is empty)")
jpayne@68 359 assert_(environ.get('SCRIPT_NAME') != '/',
jpayne@68 360 "SCRIPT_NAME cannot be '/'; it should instead be '', and "
jpayne@68 361 "PATH_INFO should be '/'")
jpayne@68 362
jpayne@68 363 def check_input(wsgi_input):
jpayne@68 364 for attr in ['read', 'readline', 'readlines', '__iter__']:
jpayne@68 365 assert_(hasattr(wsgi_input, attr),
jpayne@68 366 "wsgi.input (%r) doesn't have the attribute %s"
jpayne@68 367 % (wsgi_input, attr))
jpayne@68 368
jpayne@68 369 def check_errors(wsgi_errors):
jpayne@68 370 for attr in ['flush', 'write', 'writelines']:
jpayne@68 371 assert_(hasattr(wsgi_errors, attr),
jpayne@68 372 "wsgi.errors (%r) doesn't have the attribute %s"
jpayne@68 373 % (wsgi_errors, attr))
jpayne@68 374
jpayne@68 375 def check_status(status):
jpayne@68 376 status = check_string_type(status, "Status")
jpayne@68 377 # Implicitly check that we can turn it into an integer:
jpayne@68 378 status_code = status.split(None, 1)[0]
jpayne@68 379 assert_(len(status_code) == 3,
jpayne@68 380 "Status codes must be three characters: %r" % status_code)
jpayne@68 381 status_int = int(status_code)
jpayne@68 382 assert_(status_int >= 100, "Status code is invalid: %r" % status_int)
jpayne@68 383 if len(status) < 4 or status[3] != ' ':
jpayne@68 384 warnings.warn(
jpayne@68 385 "The status string (%r) should be a three-digit integer "
jpayne@68 386 "followed by a single space and a status explanation"
jpayne@68 387 % status, WSGIWarning)
jpayne@68 388
jpayne@68 389 def check_headers(headers):
jpayne@68 390 assert_(type(headers) is list,
jpayne@68 391 "Headers (%r) must be of type list: %r"
jpayne@68 392 % (headers, type(headers)))
jpayne@68 393 for item in headers:
jpayne@68 394 assert_(type(item) is tuple,
jpayne@68 395 "Individual headers (%r) must be of type tuple: %r"
jpayne@68 396 % (item, type(item)))
jpayne@68 397 assert_(len(item) == 2)
jpayne@68 398 name, value = item
jpayne@68 399 name = check_string_type(name, "Header name")
jpayne@68 400 value = check_string_type(value, "Header value")
jpayne@68 401 assert_(name.lower() != 'status',
jpayne@68 402 "The Status header cannot be used; it conflicts with CGI "
jpayne@68 403 "script, and HTTP status is not given through headers "
jpayne@68 404 "(value: %r)." % value)
jpayne@68 405 assert_('\n' not in name and ':' not in name,
jpayne@68 406 "Header names may not contain ':' or '\\n': %r" % name)
jpayne@68 407 assert_(header_re.search(name), "Bad header name: %r" % name)
jpayne@68 408 assert_(not name.endswith('-') and not name.endswith('_'),
jpayne@68 409 "Names may not end in '-' or '_': %r" % name)
jpayne@68 410 if bad_header_value_re.search(value):
jpayne@68 411 assert_(0, "Bad header value: %r (bad char: %r)"
jpayne@68 412 % (value, bad_header_value_re.search(value).group(0)))
jpayne@68 413
jpayne@68 414 def check_content_type(status, headers):
jpayne@68 415 status = check_string_type(status, "Status")
jpayne@68 416 code = int(status.split(None, 1)[0])
jpayne@68 417 # @@: need one more person to verify this interpretation of RFC 2616
jpayne@68 418 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
jpayne@68 419 NO_MESSAGE_BODY = (204, 304)
jpayne@68 420 for name, value in headers:
jpayne@68 421 name = check_string_type(name, "Header name")
jpayne@68 422 if name.lower() == 'content-type':
jpayne@68 423 if code not in NO_MESSAGE_BODY:
jpayne@68 424 return
jpayne@68 425 assert_(0, ("Content-Type header found in a %s response, "
jpayne@68 426 "which must not return content.") % code)
jpayne@68 427 if code not in NO_MESSAGE_BODY:
jpayne@68 428 assert_(0, "No Content-Type header found in headers (%s)" % headers)
jpayne@68 429
jpayne@68 430 def check_exc_info(exc_info):
jpayne@68 431 assert_(exc_info is None or type(exc_info) is tuple,
jpayne@68 432 "exc_info (%r) is not a tuple: %r" % (exc_info, type(exc_info)))
jpayne@68 433 # More exc_info checks?
jpayne@68 434
jpayne@68 435 def check_iterator(iterator):
jpayne@68 436 # Technically a bytestring is legal, which is why it's a really bad
jpayne@68 437 # idea, because it may cause the response to be returned
jpayne@68 438 # character-by-character
jpayne@68 439 assert_(not isinstance(iterator, (str, bytes)),
jpayne@68 440 "You should not return a string as your application iterator, "
jpayne@68 441 "instead return a single-item list containing a bytestring.")