Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
[3.15] gh-109638: Avoid pathological backtracking in csv.Sniffer (GH-153694)#154865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -332,20 +332,59 @@ def _guess_quote_and_delimiter(self, data, delimiters): | ||||||
| delim = '' | ||||||
| skipinitialspace = 0 | ||||||
| # if we see an extra quote between delimiters, we've got a | ||||||
| # double quoted format | ||||||
| dq_regexp = re.compile( | ||||||
| r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \ | ||||||
| {'delim':re.escape(delim), 'quote':quotechar}, re.MULTILINE) | ||||||
| doublequote = self._detect_doublequote(data, delim, quotechar) | ||||||
| return (quotechar, doublequote, delim, skipinitialspace) | ||||||
| if dq_regexp.search(data): | ||||||
| doublequote = True | ||||||
| else: | ||||||
| doublequote = False | ||||||
| def _detect_doublequote(self, data, delimiter, quotechar): | ||||||
| """ | ||||||
| Return whether a doubled quote occurs inside a quoted field. | ||||||
| return (quotechar, doublequote, delim, skipinitialspace) | ||||||
| The first regexp is a fast, linear pre-filter. Since it is not | ||||||
| anchored, a match can start at a delimiter inside another quoted | ||||||
| field. The second regexp rules out well-formed input without doubled | ||||||
| quotes. Both regexps use possessive repetition to avoid backtracking. | ||||||
| """ | ||||||
| import re | ||||||
| escaped_delimiter = re.escape(delimiter) | ||||||
| escaped_quote = re.escape(quotechar) | ||||||
| values = {'delim': escaped_delimiter, 'quote': escaped_quote} | ||||||
| if delimiter: | ||||||
| # Spaces after a space delimiter are delimiters, not padding. | ||||||
| values['space'] = '' if delimiter == ' ' else ' *+' | ||||||
| candidate = re.compile( | ||||||
| r"(?:%(delim)s|\r|^)%(space)s%(quote)s" | ||||||
| r"[^%(quote)s]*+%(quote)s%(quote)s" | ||||||
| r"(?:%(quote)s%(quote)s|[^%(quote)s]++)*+" | ||||||
| r"%(quote)s(?:%(delim)s|(?=\r)|$)" | ||||||
| % values, re.MULTILINE) | ||||||
| separator = rf"(?:{escaped_delimiter}|\r\n|\r|\n)" | ||||||
| plain = ( | ||||||
| rf"(?! *+{escaped_quote})" | ||||||
| rf"[^{escaped_delimiter}\r\n]*+") | ||||||
| else: | ||||||
| # An empty delimiter must not create a zero-width alternative | ||||||
| # which makes re.search() retry the pattern at every position. | ||||||
| candidate = re.compile( | ||||||
| r"(?:[\r\n]|\A) *+%(quote)s" | ||||||
| r"[^%(quote)s]*+%(quote)s%(quote)s" | ||||||
| r"(?:%(quote)s%(quote)s|[^%(quote)s]++)*+" | ||||||
| r"%(quote)s(?=[\r\n]|\Z)" | ||||||
| % values, re.MULTILINE) | ||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can skip this because there's no
Suggested change
| ||||||
| separator = r"(?:\r\n|\r|\n)" | ||||||
| plain = rf"(?! *+{escaped_quote})[^\r\n]*+" | ||||||
| if candidate.search(data) is None: | ||||||
| return False | ||||||
| quoted = ( | ||||||
| rf" *+{escaped_quote}[^{escaped_quote}]*+{escaped_quote}") | ||||||
| field = rf"(?:{quoted}|{plain})" | ||||||
| without_doublequote = re.compile( | ||||||
| rf"\A{field}(?:{separator}{field})*+\Z") | ||||||
| return without_doublequote.match(data) is None | ||||||
| def _guess_delimiter(self, data, delimiters): | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Prevent pathological regular expression backtracking in :class:`csv.Sniffer` | ||
| when detecting doubled quote characters. Improve detection for quoted fields, | ||
| avoid matching across quoted fields, and handle CRLF or CR record delimiters. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
lazy import reat the top, along with the other in this file.