Skip to content

Commit

Permalink
Prevent cgi bug with binary content.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiarn committed Jan 31, 2023
1 parent c4276da commit e026af1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ajenti-core/aj/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import cgi
import gevent
import tempfile
import gzip
import json
import math
Expand Down Expand Up @@ -83,6 +84,16 @@ def handle(self, http_context):
if output is not None:
return output

class CGIFieldStorage(cgi.FieldStorage):
# Fix cgi bug when a put request does not have a content-disposition
# See https://github.com/python/cpython/issues/71964
# TODO : cgi module will be deprecated in Python 3.11

def make_file(self, binary=None):
"""
Always open a tempfile as binary
"""
return tempfile.TemporaryFile("wb+")

class HttpContext():
"""
Expand Down Expand Up @@ -142,7 +153,7 @@ def __init__(self, env, start_response=None):
# Avoid compatibility problem
logging.warning("Body converted to bytes!")
self.body = self.body.encode()
self.form_cgi_query = cgi.FieldStorage(
self.form_cgi_query = CGIFieldStorage(
fp=BytesIO(self.body),
environ=self.env,
keep_blank_values=1
Expand Down

0 comments on commit e026af1

Please sign in to comment.