Skip to content

[ADD] bpo-132142: --secure-root flag to SimpleHTTPRequestHandler to restrict file access #132224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
'.xz': 'application/x-xz',
}

def __init__(self, *args, directory=None, **kwargs):
def __init__(self, *args, directory=None, secure_root=False, **kwargs):
if directory is None:
directory = os.getcwd()
self.directory = os.fspath(directory)
self.secure_root = secure_root
super().__init__(*args, **kwargs)

def do_GET(self):
Expand Down Expand Up @@ -888,6 +889,7 @@ def translate_path(self, path):
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)

If secure_root is enabled, forbid serving files outside self.directory.
"""
# abandon query parameters
path = path.split('?',1)[0]
Expand All @@ -909,6 +911,17 @@ def translate_path(self, path):
path = os.path.join(path, word)
if trailing_slash:
path += '/'

# Secure root check (added functionality, doesn't alter original behavior)
if getattr(self, 'secure_root', False):
real_base = os.path.realpath(self.directory)
real_path = os.path.realpath(path)

# Ensure the requested path is under the server's root directory
if not real_path.startswith(real_base + os.sep) and real_path != real_base:
self.send_error(403, "Forbidden")
return ""

return path

def copyfile(self, source, outputfile):
Expand Down Expand Up @@ -1307,7 +1320,8 @@ def _get_best_family(*address):
def test(HandlerClass=BaseHTTPRequestHandler,
ServerClass=ThreadingHTTPServer,
protocol="HTTP/1.0", port=8000, bind=None,
tls_cert=None, tls_key=None, tls_password=None):
tls_cert=None, tls_key=None, tls_password=None,
secure_root=False):
"""Test the HTTP request handler class.

This runs an HTTP server on port 8000 (or the port argument).
Expand Down Expand Up @@ -1362,6 +1376,9 @@ def test(HandlerClass=BaseHTTPRequestHandler,
parser.add_argument('port', default=8000, type=int, nargs='?',
help='bind to this port '
'(default: %(default)s)')
parser.add_argument('--secure-root', action='store_true',
help='Disallow serving files outside the specified directory')

args = parser.parse_args()

if not args.tls_cert and args.tls_key:
Expand Down
Loading