Skip to content

Commit

Permalink
Escape reserved path characters in the remote images post-transform (s…
Browse files Browse the repository at this point in the history
…phinx-doc#12253)

Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
jayaddison and AA-Turner committed Apr 12, 2024
1 parent 532ad03 commit e1d5235
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sphinx/transforms/post_transforms/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

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):
Expand Down Expand Up @@ -64,8 +68,7 @@ def handle(self, node: nodes.image) -> None:
basename = sha1(filename.encode(), usedforsecurity=False).hexdigest() + ext
basename = CRITICAL_PATH_CHAR_RE.sub("_", basename)

dirname = node['uri'].replace(':https://', '/').translate({ord("?"): "/",
ord("&"): "/"})
dirname = node['uri'].replace(':https://', '/').translate(_URI_TO_PATH)
if len(dirname) > MAX_FILENAME_LEN:
dirname = sha1(dirname.encode(), usedforsecurity=False).hexdigest()
ensuredir(os.path.join(self.imagedir, dirname))
Expand Down

0 comments on commit e1d5235

Please sign in to comment.