Skip to content

Commit

Permalink
When auto converting added TXT files with image references to TXTZ us…
Browse files Browse the repository at this point in the history
…e a full markdown parser to detect markdown images

Fixes a DoS caused by an inefficient regex for detecting markdown's crazy inline image syntax.
Reference: GHSL-2021-112
  • Loading branch information
kovidgoyal committed Aug 31, 2021
1 parent 01b302d commit 1a54a69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
33 changes: 3 additions & 30 deletions src/calibre/customize/builtins.py
Expand Up @@ -4,8 +4,7 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

import os, glob, re
from calibre import guess_type
import os, glob
from calibre.customize import (FileTypePlugin, MetadataReaderPlugin,
MetadataWriterPlugin, PreferencesPlugin, InterfaceActionBase, StoreBase)
from calibre.constants import numeric_version
Expand Down Expand Up @@ -60,34 +59,8 @@ class TXT2TXTZ(FileTypePlugin):
on_import = True

def _get_image_references(self, txt, base_dir):
from calibre.ebooks.oeb.base import OEB_IMAGES

images = []

# Textile
for m in re.finditer(r'(?mu)(?:[\[{])?\!(?:\. )?(?P<path>[^\s(!]+)\s?(?:\(([^\)]+)\))?\!(?::(\S+))?(?:[\]}]|(?=\s|$))', txt):
path = m.group('path')
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
images.append(path)

# Markdown inline
for m in re.finditer(r'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\((?P<path>[^\)]*)\)', txt): # noqa
path = m.group('path')
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
images.append(path)

# Markdown reference
refs = {}
for m in re.finditer(r'(?mu)^(\ ?\ ?\ ?)\[(?P<id>[^\]]*)\]:\s*(?P<path>[^\s]*)$', txt):
if m.group('id') and m.group('path'):
refs[m.group('id')] = m.group('path')
for m in re.finditer(r'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\[(?P<id>[^\]]*)\]', txt): # noqa
path = refs.get(m.group('id'), None)
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
images.append(path)

# Remove duplicates
return list(set(images))
from calibre.ebooks.txt.processor import get_images_from_polyglot_text
return get_images_from_polyglot_text(txt, base_dir)

def run(self, path_to_ebook):
from calibre.ebooks.metadata.opf2 import metadata_to_opf
Expand Down
27 changes: 27 additions & 0 deletions src/calibre/ebooks/txt/processor.py
Expand Up @@ -341,3 +341,30 @@ def detect_formatting_type(txt):
return 'textile'

return 'heuristic'


def get_images_from_polyglot_text(txt: str, base_dir: str = '') -> set:
from calibre.ebooks.oeb.base import OEB_IMAGES
from calibre import guess_type
if not base_dir:
base_dir = os.getcwd()
images = set()

def check_path(path: str) -> None:
if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
images.add(path)

# Textile
for m in re.finditer(r'(?mu)(?:[\[{])?\!(?:\. )?(?P<path>[^\s(!]+)\s?(?:\(([^\)]+)\))?\!(?::(\S+))?(?:[\]}]|(?=\s|$))', txt):
path = m.group('path')
check_path(path)

# Markdown
from markdown import Markdown
html = HTML_TEMPLATE % ('', Markdown().convert(txt))
from html5_parser import parse
root = parse(html)
for img in root.iterdescendants('img'):
path = img.get('src')
check_path(path)
return images

0 comments on commit 1a54a69

Please sign in to comment.