Skip to content

Commit

Permalink
Merge pull request #924 from JackDanger/require-content-length-header
Browse files Browse the repository at this point in the history
Require content-length header
  • Loading branch information
spulec committed May 11, 2017
2 parents 2f923b8 + a2fd72d commit ff4649e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions moto/s3/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ def _handle_list_objects_v2(self, bucket_name, querystring):
)

def _bucket_response_put(self, request, body, region_name, bucket_name, querystring, headers):
if not request.headers.get('Content-Length'):
return 411, {}, "Content-Length required"
if 'versioning' in querystring:
ver = re.search('<Status>([A-Za-z]+)</Status>', body)
if ver:
Expand Down Expand Up @@ -355,6 +357,8 @@ def _bucket_response_delete(self, body, bucket_name, querystring, headers):
return 409, {}, template.render(bucket=removed_bucket)

def _bucket_response_post(self, request, body, bucket_name, headers):
if not request.headers.get('Content-Length'):
return 411, {}, "Content-Length required"
path = request.path if hasattr(request, 'path') else request.path_url
if self.is_delete_keys(request, path, bucket_name):
return self._bucket_response_delete_keys(request, body, bucket_name, headers)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_s3/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ def test_s3_server_post_to_bucket():
res = test_client.get('/the-key', 'http://tester.localhost:5000/')
res.status_code.should.equal(200)
res.data.should.equal(b"nothing")


def test_s3_server_post_without_content_length():
backend = server.create_backend_app("s3")
test_client = backend.test_client()

res = test_client.put('/', 'http://tester.localhost:5000/', environ_overrides={'CONTENT_LENGTH': ''})
res.status_code.should.equal(411)

res = test_client.post('/', "https://tester.localhost:5000/", environ_overrides={'CONTENT_LENGTH': ''})
res.status_code.should.equal(411)

0 comments on commit ff4649e

Please sign in to comment.