Skip to content

Commit e1d5235

Browse files
Escape reserved path characters in the remote images post-transform (#12253)
Co-authored-by: Adam Turner <[email protected]>
1 parent 532ad03 commit e1d5235

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

sphinx/transforms/post_transforms/images.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
MAX_FILENAME_LEN = 32
2727
CRITICAL_PATH_CHAR_RE = re.compile('[:;<>|*" ]')
28+
# Replace reserved Windows or Unix path characters with '/'.
29+
_URI_TO_PATH = {
30+
ord(k): '/' for k in ('"', '&', '*', '/', ':', '<', '>', '?', '\\', '|')
31+
}
2832

2933

3034
class BaseImageConverter(SphinxTransform):
@@ -64,8 +68,7 @@ def handle(self, node: nodes.image) -> None:
6468
basename = sha1(filename.encode(), usedforsecurity=False).hexdigest() + ext
6569
basename = CRITICAL_PATH_CHAR_RE.sub("_", basename)
6670

67-
dirname = node['uri'].replace('://', '/').translate({ord("?"): "/",
68-
ord("&"): "/"})
71+
dirname = node['uri'].replace('://', '/').translate(_URI_TO_PATH)
6972
if len(dirname) > MAX_FILENAME_LEN:
7073
dirname = sha1(dirname.encode(), usedforsecurity=False).hexdigest()
7174
ensuredir(os.path.join(self.imagedir, dirname))

0 commit comments

Comments
 (0)