Skip to content

Commit

Permalink
Fix #1112: download role creates duplicated copies
Browse files Browse the repository at this point in the history
`:download:` role creates duplicated copies when the document contains
two or more the role for the same file, but in different form.  It
considers two paths are different when one contains relative path like
`path/to/../file.dat`.

Internally, `env.relfn2path()` does not normalize the given path in
relative form.  As a result, download role can't detect the same paths
are given.  This adds `os.path.normpath()` to `env.relfn2path()` to
normalize the path.
  • Loading branch information
tk0miya committed Jan 18, 2021
1 parent 596dfba commit bc56384
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Bugs fixed
* C, C++: in general fix intersphinx and role lookup types.
* #8683: :confval:`html_last_updated_fmt` does not support UTC offset (%z)
* #8683: :confval:`html_last_updated_fmt` generates wrong time zone for %Z
* #1112: ``download`` role creates duplicated copies when relative path is
specified

Testing
--------
Expand Down
7 changes: 4 additions & 3 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import os
import pickle
import posixpath
import warnings
from collections import defaultdict
from copy import copy
Expand Down Expand Up @@ -356,9 +357,9 @@ def relfn2path(self, filename: str, docname: str = None) -> Tuple[str, str]:
docdir = path.dirname(self.doc2path(docname or self.docname,
base=None))
rel_fn = path.join(docdir, filename)
# the path.abspath() might seem redundant, but otherwise artifacts
# such as ".." will remain in the path
return rel_fn, path.abspath(path.join(self.srcdir, rel_fn))

return (posixpath.normpath(rel_fn),
path.normpath(path.join(self.srcdir, rel_fn)))

@property
def found_docs(self) -> Set[str]:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def test_env_relfn2path(app):
assert relfn == '../logo.jpg'
assert absfn == app.srcdir.parent / 'logo.jpg'

# relative path traversal
relfn, absfn = app.env.relfn2path('subdir/../logo.jpg', 'index')
assert relfn == 'logo.jpg'
assert absfn == app.srcdir / 'logo.jpg'

# omit docname (w/ current docname)
app.env.temp_data['docname'] = 'subdir/document'
relfn, absfn = app.env.relfn2path('images/logo.jpg')
Expand Down

0 comments on commit bc56384

Please sign in to comment.