Skip to content

Commit d0c945a

Browse files
claudebdarnell
authored andcommitted
web: Reject NTFS alternate data streams in StaticFileHandler
On Windows, a path like "file.txt:stream" names an alternate data stream of file.txt, and abspath, stat and open all accept it, so the containment checks passed and the stream was served. Reject any colon below the static root on Windows, in _check_in_root so that both get_absolute_path (and therefore get_version/static_url) and validate_absolute_path are covered. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VamvWp9NTgSqGSAV89kGD1
1 parent 7e841ab commit d0c945a

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎tornado/test/web_test.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,62 @@ def test_symlink_escaping_roots_rejected(self):
20942094
self.assertEqual(response.code, 403)
20952095

20962096

2097+
@unittest.skipIf(os.name != "nt", "NTFS alternate data streams are Windows-only")
2098+
class StaticFileAlternateDataStreamTest(WebTestCase):
2099+
"""On Windows, ``file.txt:name`` names an NTFS alternate data stream of
2100+
``file.txt``. Streams are not files in the static directory and must not
2101+
be served, nor read by ``get_version``.
2102+
"""
2103+
2104+
def setUp(self):
2105+
self.tmpdir = tempfile.mkdtemp()
2106+
self.addCleanup(shutil.rmtree, self.tmpdir)
2107+
self.public = os.path.join(self.tmpdir, "public.txt")
2108+
with open(self.public, "wb") as f:
2109+
f.write(b"public")
2110+
try:
2111+
with open(self.public + ":secret", "wb") as f:
2112+
f.write(b"secret")
2113+
except OSError:
2114+
# Not every filesystem (e.g. FAT, some network shares) supports
2115+
# alternate data streams.
2116+
self.skipTest("filesystem does not support alternate data streams")
2117+
StaticFileHandler.reset()
2118+
self.addCleanup(StaticFileHandler.reset)
2119+
super().setUp()
2120+
2121+
def get_handlers(self):
2122+
return [("/static/(.*)", StaticFileHandler, dict(path=self.tmpdir))]
2123+
2124+
def get_app_kwargs(self):
2125+
return dict()
2126+
2127+
def test_public_file(self):
2128+
response = self.fetch("/static/public.txt")
2129+
self.assertEqual(response.code, 200)
2130+
self.assertEqual(response.body, b"public")
2131+
2132+
def test_alternate_data_stream(self):
2133+
for path in [
2134+
"public.txt:secret",
2135+
"public.txt%3Asecret",
2136+
"public.txt:secret:$DATA",
2137+
"public.txt::$DATA",
2138+
]:
2139+
with self.subTest(path=path):
2140+
with ExpectLog(gen_log, ".*is not a file"):
2141+
response = self.fetch("/static/" + path)
2142+
self.assertEqual(response.code, 403)
2143+
self.assertNotIn(b"secret", response.body)
2144+
2145+
def test_alternate_data_stream_version(self):
2146+
with self.assertRaises(HTTPError) as cm:
2147+
StaticFileHandler.get_version(
2148+
dict(static_path=self.tmpdir), "public.txt:secret"
2149+
)
2150+
self.assertEqual(cm.exception.status_code, 403)
2151+
2152+
20972153
class StaticFileSymlinkedRootTest(WebTestCase):
20982154
"""The root itself may be reached through a symlink; that must not make
20992155
every request look like an escape.

‎tornado/web.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,16 @@ def _check_in_root(root: str, absolute_path: str, path_message: str) -> str:
33363336
# the requested path so a request to root/ will match.
33373337
if not (absolute_path + os.path.sep).startswith(root):
33383338
raise HTTPError(403, "%s is not in root static directory", path_message)
3339+
if os.name == "nt" and ":" in absolute_path[len(root) :]:
3340+
# On Windows, "file.txt:name" names the NTFS alternate data
3341+
# stream "name" of file.txt. abspath, stat and open all accept
3342+
# this syntax, so without this check the stream (which may
3343+
# hold data such as the Zone.Identifier recorded for downloaded
3344+
# files) would be served as if it were a file in the static
3345+
# directory. A colon can never appear in an ordinary Windows
3346+
# file name, so rejecting it anywhere below the root (the root
3347+
# itself contains the drive letter's colon) loses nothing.
3348+
raise HTTPError(403, "%s is not a file", path_message)
33393349
return root
33403350

33413351
@staticmethod

0 commit comments

Comments
 (0)