Skip to content

Commit

Permalink
Sanitize output and chunk it at DATA_UPLOAD_MAX_MEMORY_SIZE
Browse files Browse the repository at this point in the history
before sending it over the API and get a 400.
  • Loading branch information
humitos committed Dec 10, 2018
1 parent cf071ed commit cd8c0ba
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions readthedocs/doc_builder/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
absolute_import, division, print_function, unicode_literals)

import logging
import json
import os
import re
import socket
Expand Down Expand Up @@ -186,6 +187,9 @@ def sanitize_output(self, output):
avoid PostgreSQL db to fail:
https://code.djangoproject.com/ticket/28201
3. Chunk at around ``DATA_UPLOAD_MAX_MEMORY_SIZE`` bytes to be sent
over the API call request
:param output: stdout/stderr to be sanitized
:type output: bytes
Expand All @@ -198,6 +202,18 @@ def sanitize_output(self, output):
sanitized = sanitized.replace('\x00', '')
except (TypeError, AttributeError):
sanitized = None

# Chunk the output data to be less than ``DATA_UPLOAD_MAX_MEMORY_SIZE``
output_length = len(output)
# Left some extra space for the rest of the request data
threshold = 512 * 1204 # 512Kb
allowed_length = settings.DATA_UPLOAD_MAX_MEMORY_SIZE - threshold
if output_length > allowed_length:
sanitized = sanitized[:allowed_length]
sanitized += '\n\n\nOutput is too big. Chunked at {} bytes'.format(
allowed_length,
)

return sanitized

def get_command(self):
Expand Down

0 comments on commit cd8c0ba

Please sign in to comment.