| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 METADATA_REGEXP = re.compile(r'!\s*(\w+)\s*:\s*(.*)') | 142 METADATA_REGEXP = re.compile(r'!\s*(\w+)\s*:\s*(.*)') |
| 143 METADATA_KEYS = {'Homepage', 'Title', 'Expires', 'Checksum', 'Redirect', | 143 METADATA_KEYS = {'Homepage', 'Title', 'Expires', 'Checksum', 'Redirect', |
| 144 'Version'} | 144 'Version'} |
| 145 INCLUDE_REGEXP = re.compile(r'%include\s+(.+)%') | 145 INCLUDE_REGEXP = re.compile(r'%include\s+(.+)%') |
| 146 HEADER_REGEXP = re.compile(r'\[(Adblock(?:\s*Plus\s*[\d\.]+?)?)\]', flags=re.I) | 146 HEADER_REGEXP = re.compile(r'\[(Adblock(?:\s*Plus\s*[\d\.]+?)?)\]', flags=re.I) |
| 147 HIDING_FILTER_REGEXP = re.compile(r'^([^/*|@"!]*?)#([@?])?#(.+)$') | 147 HIDING_FILTER_REGEXP = re.compile(r'^([^/*|@"!]*?)#([@?])?#(.+)$') |
| 148 FILTER_OPTIONS_REGEXP = re.compile( | 148 FILTER_OPTIONS_REGEXP = re.compile( |
| 149 r'\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$' | 149 r'\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$' |
| 150 ) | 150 ) |
| 151 | 151 |
| 152 # Regular expression that matches an invalid Content Security Policy | |
|
rhowell
2018/06/07 18:16:55
Removing this, since we aren't currently checking
| |
| 153 INVALID_CSP_REGEXP = re.compile( | |
| 154 r'(;|^) ?(base-uri|referrer|report-to|report-uri|upgrade-insecure-requests)\ b' | |
| 155 ) | |
| 156 | |
| 157 | 152 |
| 158 def _parse_comment(text): | 153 def _parse_comment(text): |
| 159 match = METADATA_REGEXP.match(text) | 154 match = METADATA_REGEXP.match(text) |
| 160 if match and match.group(1) in METADATA_KEYS: | 155 if match and match.group(1) in METADATA_KEYS: |
| 161 return Metadata(match.group(1), match.group(2)) | 156 return Metadata(match.group(1), match.group(2)) |
| 162 return Comment(text[1:].strip()) | 157 return Comment(text[1:].strip()) |
| 163 | 158 |
| 164 | 159 |
| 165 def _parse_header(text): | 160 def _parse_header(text): |
| 166 match = HEADER_REGEXP.match(text) | 161 match = HEADER_REGEXP.match(text) |
| 167 if not match: | 162 if not match: |
| 168 raise ParseError('Malformed header', text) | 163 raise ParseError('Malformed header', text) |
| 169 return Header(match.group(1)) | 164 return Header(match.group(1)) |
| 170 | 165 |
| 171 | 166 |
| 172 def _parse_instruction(text): | 167 def _parse_instruction(text): |
| 173 match = INCLUDE_REGEXP.match(text) | 168 match = INCLUDE_REGEXP.match(text) |
| 174 if not match: | 169 if not match: |
| 175 raise ParseError('Unrecognized instruction', text) | 170 raise ParseError('Unrecognized instruction', text) |
| 176 return Include(match.group(1)) | 171 return Include(match.group(1)) |
| 177 | 172 |
| 178 | 173 |
| 179 def _parse_option(option): | 174 def _parse_option(option): |
| 180 if '=' in option: | 175 if '=' in option: |
| 181 return option.split('=', 1) | 176 return option.split('=', 1) |
| 182 if option.startswith('~'): | 177 if option.startswith('~'): |
|
Vasily Kuznetsov
2018/05/30 19:49:26
If we want to be able to handle the form "~csp=xxx
rhowell
2018/06/07 18:16:55
I talked to Dave, and he said that ~csp=xxxx and ~
Vasily Kuznetsov
2018/06/07 18:28:31
Let's switch the ifs -- this way both ~csp=xxx and
rhowell
2018/06/07 23:13:06
Done.
| |
| 183 return option[1:], False | 178 return option[1:], False |
| 184 return option, True | 179 return option, True |
| 185 | 180 |
| 186 | 181 |
| 187 def _parse_filter_option(option): | 182 def _parse_filter_option(option): |
| 188 name, value = _parse_option(option) | 183 name, value = _parse_option(option) |
| 189 | 184 |
| 190 # Handle special cases of multivalued options. | 185 # Handle special cases of multivalued options. |
| 191 if name == FILTER_OPTION.DOMAIN: | 186 if name == FILTER_OPTION.DOMAIN: |
| 192 value = [_parse_option(o) for o in value.split('|')] | 187 value = [_parse_option(o) for o in value.split('|')] |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 Raises | 313 Raises |
| 319 ------ | 314 ------ |
| 320 ParseError | 315 ParseError |
| 321 Thrown during iteration for invalid filter list lines. | 316 Thrown during iteration for invalid filter list lines. |
| 322 TypeError | 317 TypeError |
| 323 If `lines` is not iterable. | 318 If `lines` is not iterable. |
| 324 | 319 |
| 325 """ | 320 """ |
| 326 for line in lines: | 321 for line in lines: |
| 327 yield parse_line(line) | 322 yield parse_line(line) |
| LEFT | RIGHT |