mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Escape reserved path characters in the remote images post-transform (#12253)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
@@ -25,6 +25,10 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_FILENAME_LEN = 32
|
||||
CRITICAL_PATH_CHAR_RE = re.compile('[:;<>|*" ]')
|
||||
# Replace reserved Windows or Unix path characters with '/'.
|
||||
_URI_TO_PATH = {
|
||||
ord(k): '/' for k in ('"', '&', '*', '/', ':', '<', '>', '?', '\\', '|')
|
||||
}
|
||||
|
||||
|
||||
class BaseImageConverter(SphinxTransform):
|
||||
@@ -64,8 +68,7 @@ class ImageDownloader(BaseImageConverter):
|
||||
basename = sha1(filename.encode(), usedforsecurity=False).hexdigest() + ext
|
||||
basename = CRITICAL_PATH_CHAR_RE.sub("_", basename)
|
||||
|
||||
dirname = node['uri'].replace('://', '/').translate({ord("?"): "/",
|
||||
ord("&"): "/"})
|
||||
dirname = node['uri'].replace('://', '/').translate(_URI_TO_PATH)
|
||||
if len(dirname) > MAX_FILENAME_LEN:
|
||||
dirname = sha1(dirname.encode(), usedforsecurity=False).hexdigest()
|
||||
ensuredir(os.path.join(self.imagedir, dirname))
|
||||
|
||||
Reference in New Issue
Block a user