Skip to content

Commit

Permalink
bpo-43223: Fix Open Redirection In http.server module
Browse files Browse the repository at this point in the history
Fix an open redirection vulnerability in the HTTP server when a URL contains ``//``.
Added test case for bpo-43223 patch
  • Loading branch information
hamzaavvan committed May 7, 2021
1 parent 5eb7796 commit 42eb552
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import socket # For gethostbyaddr()
import socketserver
import sys
import re
import time
import urllib.parse
import contextlib
Expand Down Expand Up @@ -332,6 +333,12 @@ def parse_request(self):
return False
self.command, self.path = command, path

# bpo-43223: The purpose of replacing '//' with '/' is to protect against
# open redirect attacks reside within http.server module which can be triggered
# if the path contains '//' at the beginning because web clients treat //path as
# an absolute url without scheme (similar to http://path) rather than a relative path
self.path = re.sub(r'^(/)+', '/', self.path)

# Examine the headers and look for a Connection directive.
try:
self.headers = http.client.parse_headers(self.rfile,
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_http/test_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest
import re

class TestHTTP(unittest.TestCase):

def test_http_parse_request(self):
self.assertEqual(re.sub(r'^/+', '/', '//test.com'), '/test.com', '//test.com should be converted to a proper relative path')
self.assertEqual(re.sub(r'^/+', '/', '///test.com'), '/test.com', '///test.com should be converted to a proper relative path')

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`http.server`: Fix an open redirection vulnerability in the HTTP server when an URL contains ``//``.
Vulnerability discovered and fixed by Hamza Avvan.

0 comments on commit 42eb552

Please sign in to comment.