Changeset 7658
- Timestamp:
- Nov 8, 2008, 7:24:00 PM (15 years ago)
- Location:
- branches/0.11-stable/trac
- Files:
-
- 2 edited
-
util/html.py (modified) (1 diff)
-
wiki/formatter.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11-stable/trac/util/html.py
r6904 r7658 15 15 16 16 from genshi import Markup, escape, unescape 17 from genshi.core import stripentities, striptags 17 from genshi.core import stripentities, striptags, START, END 18 18 from genshi.builder import Element, ElementFactory, Fragment 19 from genshi.filters.html import HTMLSanitizer 19 20 20 __all__ = ['escape', 'unescape', 'html', 'plaintext'] 21 __all__ = ['escape', 'unescape', 'html', 'plaintext', 'TracHTMLSanitizer'] 22 23 24 class TracHTMLSanitizer(HTMLSanitizer): 25 26 UNSAFE_CSS = ['position'] 27 28 def __init__(self): 29 safe_attrs = HTMLSanitizer.SAFE_ATTRS | set(['style']) 30 super(TracHTMLSanitizer, self).__init__(safe_attrs=safe_attrs) 31 32 def sanitize_css(self, text): 33 decls = [] 34 text = self._strip_css_comments(self._replace_unicode_escapes(text)) 35 for decl in filter(None, text.split(';')): 36 decl = decl.strip() 37 if not decl: 38 continue 39 try: 40 prop, value = decl.split(':', 1) 41 except ValueError: 42 continue 43 if not self.is_safe_css(prop.strip().lower(), value.strip()): 44 continue 45 is_evil = False 46 if 'expression' in decl: 47 is_evil = True 48 for match in re.finditer(r'url\s*\(([^)]+)', decl): 49 if not self.is_safe_uri(match.group(1)): 50 is_evil = True 51 break 52 if not is_evil: 53 decls.append(decl.strip()) 54 return decls 55 56 def __call__(self, stream): 57 """Remove input type="password" elements from the stream 58 """ 59 suppress = False 60 for kind, data, pos in super(TracHTMLSanitizer, self).__call__(stream): 61 if kind is START: 62 tag, attrs = data 63 if (tag == 'input' and 64 attrs.get('type', '').lower() == 'password'): 65 suppress = True 66 else: 67 yield kind, data, pos 68 elif kind is END: 69 if not suppress: 70 yield kind, data, pos 71 suppress = False 72 else: 73 yield kind, data, pos 74 75 def is_safe_css(self, prop, value): 76 """Determine whether the given css property declaration is to be 77 considered safe for inclusion in the output. 78 """ 79 if prop in self.UNSAFE_CSS: 80 return False 81 # Negative margins can be used for phishing 82 elif prop.startswith('margin') and '-' in value: 83 return False 84 return True 21 85 22 86 -
branches/0.11-stable/trac/wiki/formatter.py
r7619 r7658 27 27 from genshi.builder import tag, Element 28 28 from genshi.core import Stream, Markup, escape 29 from genshi.filters import HTMLSanitizer30 29 from genshi.input import HTMLParser, ParseError 31 30 from genshi.util import plaintext … … 39 38 from trac.util.text import shorten_line, to_unicode, \ 40 39 unicode_quote, unicode_quote_plus 40 from trac.util.html import TracHTMLSanitizer 41 41 from trac.util.translation import _ 42 42 … … 89 89 'Span': self._span_processor} 90 90 91 self._sanitizer = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | 92 set(['style'])) 91 self._sanitizer = TracHTMLSanitizer() 93 92 94 93 self.processor = builtin_processors.get(name)
Note:
See TracChangeset
for help on using the changeset viewer.
