httpdis 0.6.34__tar.gz → 0.6.35__tar.gz

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.
Files changed (37) hide show
  1. {httpdis-0.6.34/httpdis.egg-info → httpdis-0.6.35}/PKG-INFO +18 -1
  2. {httpdis-0.6.34 → httpdis-0.6.35}/README.md +16 -0
  3. httpdis-0.6.35/RELEASE +1 -0
  4. httpdis-0.6.35/VERSION +1 -0
  5. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/httpdis.py +109 -5
  6. {httpdis-0.6.34 → httpdis-0.6.35/httpdis.egg-info}/PKG-INFO +18 -1
  7. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis.egg-info/requires.txt +3 -0
  8. {httpdis-0.6.34 → httpdis-0.6.35}/requirements.txt +1 -0
  9. {httpdis-0.6.34 → httpdis-0.6.35}/setup.yml +2 -2
  10. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_context.py +63 -2
  11. httpdis-0.6.34/RELEASE +0 -1
  12. httpdis-0.6.34/VERSION +0 -1
  13. {httpdis-0.6.34 → httpdis-0.6.35}/LICENSE +0 -0
  14. {httpdis-0.6.34 → httpdis-0.6.35}/MANIFEST.in +0 -0
  15. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/__init__.py +0 -0
  16. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/auth_backend.py +0 -0
  17. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/auth_browser.py +0 -0
  18. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/auth_sqlite.py +0 -0
  19. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/authentication.py +0 -0
  20. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/config.py +0 -0
  21. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/content_disposition.py +0 -0
  22. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/ext/__init__.py +0 -0
  23. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis/ext/httpdis_json.py +0 -0
  24. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis.egg-info/SOURCES.txt +0 -0
  25. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis.egg-info/dependency_links.txt +0 -0
  26. {httpdis-0.6.34 → httpdis-0.6.35}/httpdis.egg-info/top_level.txt +0 -0
  27. {httpdis-0.6.34 → httpdis-0.6.35}/pyproject.toml +0 -0
  28. {httpdis-0.6.34 → httpdis-0.6.35}/setup.cfg +0 -0
  29. {httpdis-0.6.34 → httpdis-0.6.35}/setup.py +0 -0
  30. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_audit_regressions.py +0 -0
  31. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_auth_backend.py +0 -0
  32. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_auth_browser.py +0 -0
  33. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_auth_sqlite.py +0 -0
  34. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_authentication_provider.py +0 -0
  35. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_body_limits.py +0 -0
  36. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_http_contract_errors.py +0 -0
  37. {httpdis-0.6.34 → httpdis-0.6.35}/tests/test_httpdis.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpdis
3
- Version: 0.6.34
3
+ Version: 0.6.35
4
4
  Summary: httpdis
5
5
  Home-page: https://github.com/decryptus/httpdis
6
6
  Author: Adrien Delle Cave
@@ -25,6 +25,7 @@ Requires-Dist: sonicprobe>=0.3.52
25
25
  Requires-Dist: six>=1.13.0
26
26
  Requires-Dist: legacy-cgi; python_version >= "3.13"
27
27
  Requires-Dist: legacycrypt; python_version >= "3.13"
28
+ Requires-Dist: monotonic; python_version < "3"
28
29
  Provides-Extra: auth
29
30
  Requires-Dist: argon2-cffi<26,>=23.1; python_version >= "3.10" and extra == "auth"
30
31
  Requires-Dist: sonicprobe>=0.3.55; extra == "auth"
@@ -120,6 +121,22 @@ parsing. Routing and header/body-size checks may run before authentication;
120
121
  unknown routes and wrong methods do not inherit another method's auth policy.
121
122
  Applications still own operation-level authorization. Public routes are unchanged.
122
123
 
124
+ ## Request read deadlines and query limits
125
+
126
+ The default handler allows 10 seconds in total for the request line and all
127
+ headers. A client that stalls or trickles headers past this deadline loses its
128
+ connection, freeing the worker. After routing, size checks and authentication,
129
+ a nonempty body has a fresh budget of 10 seconds plus one second per 32 KiB,
130
+ up to 120 seconds. A body read timeout returns HTTP 408 and closes the connection.
131
+ A shorter socket timeout set by a custom handler still applies. Handler subclasses
132
+ can set `header_read_seconds`, `body_grace_seconds`, `body_bytes_per_second`
133
+ and `body_max_seconds` to positive values appropriate for their clients.
134
+
135
+ A request target accepts up to 1,024 ampersand-separated query fields
136
+ (including empty fields) and 32 bracket levels per name. Larger queries return
137
+ HTTP 414 before routing. Encode brackets in URLs, for example
138
+ `?tag%5B%5D=blue&tag%5B%5D=green`.
139
+
123
140
  ## Request body limits
124
141
 
125
142
  Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
@@ -77,6 +77,22 @@ parsing. Routing and header/body-size checks may run before authentication;
77
77
  unknown routes and wrong methods do not inherit another method's auth policy.
78
78
  Applications still own operation-level authorization. Public routes are unchanged.
79
79
 
80
+ ## Request read deadlines and query limits
81
+
82
+ The default handler allows 10 seconds in total for the request line and all
83
+ headers. A client that stalls or trickles headers past this deadline loses its
84
+ connection, freeing the worker. After routing, size checks and authentication,
85
+ a nonempty body has a fresh budget of 10 seconds plus one second per 32 KiB,
86
+ up to 120 seconds. A body read timeout returns HTTP 408 and closes the connection.
87
+ A shorter socket timeout set by a custom handler still applies. Handler subclasses
88
+ can set `header_read_seconds`, `body_grace_seconds`, `body_bytes_per_second`
89
+ and `body_max_seconds` to positive values appropriate for their clients.
90
+
91
+ A request target accepts up to 1,024 ampersand-separated query fields
92
+ (including empty fields) and 32 bracket levels per name. Larger queries return
93
+ HTTP 414 before routing. Encode brackets in URLs, for example
94
+ `?tag%5B%5D=blue&tag%5B%5D=green`.
95
+
80
96
  ## Request body limits
81
97
 
82
98
  Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
httpdis-0.6.35/RELEASE ADDED
@@ -0,0 +1 @@
1
+ 0.6.35
httpdis-0.6.35/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.35
@@ -21,6 +21,7 @@ import binascii
21
21
  import threading
22
22
  from hmac import compare_digest
23
23
  import errno
24
+ import io
24
25
  import json
25
26
  import logging
26
27
  import os
@@ -30,6 +31,10 @@ import socket
30
31
  import sys
31
32
  import time
32
33
  import traceback
34
+ try:
35
+ from time import monotonic
36
+ except ImportError: # Python 2.7
37
+ from monotonic import monotonic
33
38
  #import weakref
34
39
 
35
40
  import cgi
@@ -80,6 +85,13 @@ from .config import (BUFFER_SIZE, # pylint: disable=unused-import
80
85
 
81
86
  LOG = logging.getLogger('httpdis') # pylint: disable-msg=C0103
82
87
  _CONTENT_LENGTH = re.compile(r'[0-9]+\Z')
88
+ _QUERY_BRACKETS = re.compile(r'\[([^\]]*)\]')
89
+ MAX_QUERY_FIELDS = 1024
90
+ MAX_QUERY_DEPTH = 32
91
+ HEADER_READ_SECONDS = 10
92
+ BODY_GRACE_SECONDS = 10
93
+ BODY_BYTES_PER_SECOND = 32 * 1024
94
+ BODY_MAX_SECONDS = 120
83
95
  _BODYLESS_CODES = (204, 205, 304)
84
96
  _NO_LENGTH_CODES = (204, 304)
85
97
 
@@ -333,11 +345,70 @@ class HttpAuthentication(object): # pylint: disable=useless-object-inheritance
333
345
  return req_error(code = 403)
334
346
 
335
347
 
348
+ class _DeadlineSocketInput(io.RawIOBase):
349
+ """Apply the phase budget on each receive, below any input buffering."""
350
+
351
+ def __init__(self, connection):
352
+ io.RawIOBase.__init__(self)
353
+ self.connection = connection
354
+ self.deadline = None
355
+
356
+ def readable(self):
357
+ return True
358
+
359
+ def readinto(self, buffer):
360
+ if self.deadline is None:
361
+ return self.connection.recv_into(buffer)
362
+ remaining = self.deadline - monotonic()
363
+ if remaining <= 0:
364
+ raise socket.timeout('Request read deadline exceeded')
365
+ previous = self.connection.gettimeout()
366
+ self.connection.settimeout(remaining if previous is None else min(remaining, previous))
367
+ try:
368
+ return self.connection.recv_into(buffer)
369
+ finally:
370
+ self.connection.settimeout(previous)
371
+
372
+
336
373
  class HttpReqHandler(BaseHTTPRequestHandler):
337
374
  """
338
375
  Handle one HTTP request
339
376
  """
340
377
 
378
+ _deadline_input = None
379
+ header_read_seconds = HEADER_READ_SECONDS
380
+ body_grace_seconds = BODY_GRACE_SECONDS
381
+ body_bytes_per_second = BODY_BYTES_PER_SECOND
382
+ body_max_seconds = BODY_MAX_SECONDS
383
+
384
+ def setup(self):
385
+ BaseHTTPRequestHandler.setup(self)
386
+ self.rfile.close()
387
+ self._deadline_input = _DeadlineSocketInput(self.connection)
388
+ if self.rbufsize == 0:
389
+ self.rfile = self._deadline_input
390
+ else:
391
+ size = self.rbufsize if self.rbufsize > 0 else io.DEFAULT_BUFFER_SIZE
392
+ self.rfile = io.BufferedReader(self._deadline_input, size)
393
+
394
+ def handle_one_request(self):
395
+ if self._deadline_input is None:
396
+ return BaseHTTPRequestHandler.handle_one_request(self)
397
+ self._deadline_input.deadline = monotonic() + self.header_read_seconds
398
+ try:
399
+ return BaseHTTPRequestHandler.handle_one_request(self)
400
+ except socket.timeout:
401
+ self.close_connection = True
402
+ finally:
403
+ self._deadline_input.deadline = None
404
+
405
+ def parse_request(self):
406
+ try:
407
+ return BaseHTTPRequestHandler.parse_request(self)
408
+ finally:
409
+ if self._deadline_input is not None:
410
+ self._deadline_input.deadline = None
411
+
341
412
  _DEFAULT_CONTENT_TYPE = 'text/plain'
342
413
  _ALLOWED_CONTENT_TYPES = []
343
414
  _ALLOWED_MULTIPART_FORM = True
@@ -681,7 +752,10 @@ class HttpReqHandler(BaseHTTPRequestHandler):
681
752
  if not isinstance(query, (list, tuple)):
682
753
  return None
683
754
 
755
+ if len(query) > MAX_QUERY_FIELDS:
756
+ raise HttpReqError(414, 'Too many query fields')
684
757
  ret = {}
758
+ next_indices = {}
685
759
 
686
760
  for x in query:
687
761
  if not x:
@@ -707,9 +781,11 @@ class HttpReqHandler(BaseHTTPRequestHandler):
707
781
  if key not in ret:
708
782
  ret[key] = {}
709
783
 
710
- matched = re.findall(r'\[([^\]]*)\]', x[0][lbracket:])
784
+ matched = _QUERY_BRACKETS.findall(x[0], lbracket, x[0].rfind(']') + 1)
711
785
  nb = len(matched)
712
786
 
787
+ if nb > MAX_QUERY_DEPTH:
788
+ raise HttpReqError(414, 'Query nesting exceeds limit')
713
789
  if nb == 0:
714
790
  ret[key] = value
715
791
  continue
@@ -722,8 +798,13 @@ class HttpReqHandler(BaseHTTPRequestHandler):
722
798
 
723
799
  for i, k in enumerate(matched):
724
800
  if k == '':
725
- while j in ref:
726
- j += 1
801
+ # Literal indices are strings. Only [] can allocate integer
802
+ # keys. Retain the owner to prevent id reuse after a later
803
+ # field replaces a dictionary with a scalar.
804
+ entry = next_indices.get(id(ref))
805
+ if entry is not None:
806
+ j = max(j, entry[1])
807
+ next_indices[id(ref)] = (ref, j + 1)
727
808
  k = j
728
809
 
729
810
  if i == (nb - 1):
@@ -752,6 +833,12 @@ class HttpReqHandler(BaseHTTPRequestHandler):
752
833
  if helpers.has_len(path):
753
834
  path = re.sub(r'^/+', '/', path)
754
835
 
836
+ # Bound field splitting and percent decoding in urisup as well.
837
+ target = path.partition('#')[0]
838
+ query_start = target.find('?')
839
+ if query_start >= 0 and target.count('&', query_start) >= MAX_QUERY_FIELDS:
840
+ raise self.req_error(414, 'Too many query fields')
841
+
755
842
  (path,
756
843
  query,
757
844
  self._fragment) = urisup.uri_help_split(path)[2:]
@@ -765,7 +852,10 @@ class HttpReqHandler(BaseHTTPRequestHandler):
765
852
  self._path = re.sub(r'/+', '/', path)
766
853
 
767
854
  if query:
768
- self._query_params = self.querylist_to_dict(query)
855
+ try:
856
+ self._query_params = self.querylist_to_dict(query)
857
+ except HttpReqError as error:
858
+ raise self.req_error(error.code, error.text)
769
859
 
770
860
  return self._path
771
861
  except urisup.InvalidURIError as e:
@@ -962,6 +1052,20 @@ class HttpReqHandler(BaseHTTPRequestHandler):
962
1052
  raise self.req_error(400, 'Conflicting Content-Length headers')
963
1053
  return values[0] if values else 0
964
1054
 
1055
+ def _read_body(self, length):
1056
+ if self._deadline_input is None:
1057
+ return self.rfile.read(length)
1058
+ budget = min(self.body_max_seconds,
1059
+ self.body_grace_seconds + float(length) / self.body_bytes_per_second)
1060
+ self._deadline_input.deadline = monotonic() + budget
1061
+ try:
1062
+ return self.rfile.read(length)
1063
+ except socket.timeout:
1064
+ self.close_connection = True
1065
+ raise self.req_error(408, 'Request body read deadline exceeded')
1066
+ finally:
1067
+ self._deadline_input.deadline = None
1068
+
965
1069
  def data_from_payload(self, cmd):
966
1070
  """
967
1071
  Callback for .execute_command() for PATCH/POST/PUT requests
@@ -1021,7 +1125,7 @@ class HttpReqHandler(BaseHTTPRequestHandler):
1021
1125
  self.authenticate(self._cmd.auth_users)
1022
1126
 
1023
1127
  if clen > 0:
1024
- payload = self.rfile.read(clen)
1128
+ payload = self._read_body(clen)
1025
1129
  if len(payload) != clen:
1026
1130
  raise self.req_error(400, 'Incomplete request body')
1027
1131
  self._payload = BytesIO(payload)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpdis
3
- Version: 0.6.34
3
+ Version: 0.6.35
4
4
  Summary: httpdis
5
5
  Home-page: https://github.com/decryptus/httpdis
6
6
  Author: Adrien Delle Cave
@@ -25,6 +25,7 @@ Requires-Dist: sonicprobe>=0.3.52
25
25
  Requires-Dist: six>=1.13.0
26
26
  Requires-Dist: legacy-cgi; python_version >= "3.13"
27
27
  Requires-Dist: legacycrypt; python_version >= "3.13"
28
+ Requires-Dist: monotonic; python_version < "3"
28
29
  Provides-Extra: auth
29
30
  Requires-Dist: argon2-cffi<26,>=23.1; python_version >= "3.10" and extra == "auth"
30
31
  Requires-Dist: sonicprobe>=0.3.55; extra == "auth"
@@ -120,6 +121,22 @@ parsing. Routing and header/body-size checks may run before authentication;
120
121
  unknown routes and wrong methods do not inherit another method's auth policy.
121
122
  Applications still own operation-level authorization. Public routes are unchanged.
122
123
 
124
+ ## Request read deadlines and query limits
125
+
126
+ The default handler allows 10 seconds in total for the request line and all
127
+ headers. A client that stalls or trickles headers past this deadline loses its
128
+ connection, freeing the worker. After routing, size checks and authentication,
129
+ a nonempty body has a fresh budget of 10 seconds plus one second per 32 KiB,
130
+ up to 120 seconds. A body read timeout returns HTTP 408 and closes the connection.
131
+ A shorter socket timeout set by a custom handler still applies. Handler subclasses
132
+ can set `header_read_seconds`, `body_grace_seconds`, `body_bytes_per_second`
133
+ and `body_max_seconds` to positive values appropriate for their clients.
134
+
135
+ A request target accepts up to 1,024 ampersand-separated query fields
136
+ (including empty fields) and 32 bracket levels per name. Larger queries return
137
+ HTTP 414 before routing. Encode brackets in URLs, for example
138
+ `?tag%5B%5D=blue&tag%5B%5D=green`.
139
+
123
140
  ## Request body limits
124
141
 
125
142
  Since HTTPdis 0.6.28, `register(..., max_body_size=...)` sets the body limit
@@ -2,6 +2,9 @@ python-magic
2
2
  sonicprobe>=0.3.52
3
3
  six>=1.13.0
4
4
 
5
+ [:python_version < "3"]
6
+ monotonic
7
+
5
8
  [:python_version >= "3.13"]
6
9
  legacy-cgi
7
10
  legacycrypt
@@ -3,3 +3,4 @@ sonicprobe>=0.3.52
3
3
  six>=1.13.0
4
4
  legacy-cgi; python_version >= '3.13'
5
5
  legacycrypt; python_version >= '3.13'
6
+ monotonic; python_version < "3"
@@ -4,8 +4,8 @@ description: httpdis
4
4
  author: Adrien Delle Cave
5
5
  author_email: pypi@doowan.net
6
6
  copyright: '2022-2026 Adrien Delle Cave'
7
- release: '0.6.34'
8
- version: '0.6.34'
7
+ release: '0.6.35'
8
+ version: '0.6.35'
9
9
  license: License GPL-3
10
10
  url: https://github.com/decryptus/httpdis
11
11
  python_requires:
@@ -9,6 +9,7 @@ import shutil
9
9
  import socket
10
10
  import tempfile
11
11
  import threading
12
+ import time
12
13
  import unittest
13
14
 
14
15
  try:
@@ -54,7 +55,7 @@ class ContextTests(unittest.TestCase):
54
55
  stream.write(user + ':{SHA}' + digest + '\n')
55
56
  return path
56
57
 
57
- def start(self, label, user, limit):
58
+ def start(self, label, user, limit, handler=j.HttpReqHandler):
58
59
  context = j.HttpServerContext()
59
60
  ready = threading.Event()
60
61
  hooks = []
@@ -73,7 +74,7 @@ class ContextTests(unittest.TestCase):
73
74
  'max_workers': 2})
74
75
  def run():
75
76
  try:
76
- context.run()
77
+ context.run(http_req_handler=handler)
77
78
  except Exception as error:
78
79
  self.errors.append(error)
79
80
  ready.set()
@@ -97,6 +98,66 @@ class ContextTests(unittest.TestCase):
97
98
  finally:
98
99
  connection.close()
99
100
 
101
+ def test_idle_connections_release_all_workers(self):
102
+ class FastHeader(j.HttpReqHandler):
103
+ header_read_seconds = .25
104
+ context, _, _ = self.start('live', 'alice', 64, FastHeader)
105
+ address = ('127.0.0.1', context.server.server_port)
106
+ idle = []
107
+ try:
108
+ for _ in range(2):
109
+ idle.append(socket.create_connection(address, 2))
110
+ self.assertEqual(self.request(context, '/live')[0], 200)
111
+ for client in idle:
112
+ self.assertEqual(client.recv(1), b'')
113
+ finally:
114
+ for client in idle:
115
+ client.close()
116
+
117
+ def test_trickled_headers_and_body_have_total_deadlines(self):
118
+ class ShortBudget(j.HttpReqHandler):
119
+ header_read_seconds = .3
120
+ body_max_seconds = .3
121
+ context, _, _ = self.start('live', 'alice', 256, ShortBudget)
122
+ address = ('127.0.0.1', context.server.server_port)
123
+ for start, expected in (
124
+ (b'GET /live HTTP/1.0\r\nX-Slow: ', b''),
125
+ (b'POST /echo HTTP/1.0\r\nContent-Type: application/json\r\n'
126
+ b'Content-Length: 100\r\n\r\n', b'408')):
127
+ client = socket.create_connection(address, 2)
128
+ try:
129
+ client.sendall(start)
130
+ for _ in range(12):
131
+ try:
132
+ client.sendall(b' ')
133
+ except socket.error:
134
+ break
135
+ time.sleep(.05)
136
+ reply = client.recv(4096)
137
+ if expected:
138
+ self.assertIn(expected, reply.split(b'\r\n', 1)[0])
139
+ else:
140
+ self.assertEqual(reply, b'')
141
+ finally:
142
+ client.close()
143
+ self.assertEqual(self.request(context, '/live')[0], 200)
144
+
145
+ def test_query_limits_before_authentication_and_routing(self):
146
+ context, _, _ = self.start('live', 'alice', 64)
147
+ fields = '&'.join(['a%5B%5D=x'] * h.MAX_QUERY_FIELDS)
148
+ self.assertEqual(self.request(context, '/identity?' + fields)[0], 401)
149
+ self.assertEqual(self.request(context, '/identity?' + fields + '&b')[0], 414)
150
+ nested = 'x' + '%5Bv%5D' * h.MAX_QUERY_DEPTH
151
+ self.assertEqual(self.request(context, '/missing?' + nested)[0], 404)
152
+ self.assertEqual(self.request(context, '/missing?' + nested + '%5Bv%5D')[0], 414)
153
+
154
+ def test_query_append_preserves_indices_and_subtree_replacement(self):
155
+ query = [('a[0]', 'named'), ('a[]', 'first'), ('a[][]', 'nested'),
156
+ ('b[k][]', 0), ('b[k]', 'reset'), ('b[k][]', 1), ('a[]', 'last')]
157
+ self.assertEqual(h.HttpReqHandler.querylist_to_dict(query),
158
+ {'a': {'0': 'named', 0: 'first', 1: {1: 'nested'}, 2: 'last'},
159
+ 'b': {'k': {0: 1}}})
160
+
100
161
  def test_two_servers_isolate_routes_auth_options_and_shutdown(self):
101
162
  original_version = j.HttpReqHandler.server_version
102
163
  left, left_thread, left_hooks = self.start('left', 'alice', 4)
httpdis-0.6.34/RELEASE DELETED
@@ -1 +0,0 @@
1
- 0.6.34
httpdis-0.6.34/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.6.34
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes