Skip to content

Commit 0d9f2d2

Browse files
committed
Skip posix path traversal test rows on a Windows filesystem
The parametrized traversal tests added in 1.4.2 run every spelling of a traversal URI against both posixpath and ntpath, forcing os.path to each in turn. The ntpath half works on any host, as ntpath accepts forward slashes, but the posixpath half cannot work on Windows: TemplateLookup.get_template() converts the configured directory with dir_.replace(os.path.sep, posixpath.sep), so with os.path forced to posixpath the separator sought is "/" and the directory's real backslashes are left in place. The joined path then normalizes to something that does not exist, and get_template() raises TopLevelLookupException before the traversal check is ever consulted, failing eleven cases on the Windows jobs. The posixpath rows are now skipped when os.name is "nt". Coverage is not lost: those rows continue to run on the Linux and macOS jobs, and on Windows the ntpath rows exercise the check against the real os.path rather than an emulation of it. Only the test suite was affected. The traversal check itself is unchanged. Fixes: #441 Change-Id: I4cbb5f1d1793aecd1aafd288d771de596da4b88b
1 parent 45a738c commit 0d9f2d2

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎doc/build/unreleased/441.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. change::
2+
:tags: bug, tests
3+
:tickets: 441
4+
5+
Fixed regression caused in 1.4.2 where tests added to the suite were
6+
unable to run directly on Windows, due to posix mechanics: the tests force
7+
``os.path`` to ``posixpath``, whereas
8+
:meth:`.TemplateLookup.get_template` converts the configured directory
9+
using ``os.path.sep``. These tests are now skipped on that platform,
10+
where the traversal check is instead exercised against ``ntpath``
11+
natively.

‎test/test_lookup.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,23 @@ def test_dont_accept_relative_outside_of_root(self):
134134
runtime._lookup_template(ctx, "foo/../index.html", index.uri)
135135

136136
@pytest.mark.parametrize(
137-
"ospath", [posixpath, ntpath], ids=["posix", "windows"]
137+
"ospath",
138+
[
139+
# get_template() converts the configured directory using
140+
# os.path.sep; forcing posixpath on a Windows host would
141+
# leave the directory's real backslashes in place and the
142+
# template would not be located at all, so this half of the
143+
# matrix only applies where the filesystem is posix
144+
pytest.param(
145+
posixpath,
146+
marks=pytest.mark.skipif(
147+
os.name == "nt",
148+
reason="posix path semantics need a posix filesystem",
149+
),
150+
id="posix",
151+
),
152+
pytest.param(ntpath, id="windows"),
153+
],
138154
)
139155
@pytest.mark.parametrize(
140156
"uri",

0 commit comments

Comments
 (0)