Changeset 1836

Show
Ignore:
Timestamp:
11/03/08 15:54:57 (2 months ago)
Author:
rmarianski
Message:

beginnings of using lxml.html cleaner to sanitize user embed markup

Location:
siteapp/branches/clean-embed
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • siteapp/branches/clean-embed/opengeo/almanac/configutil.py

    r1789 r1836  
    187187                    direct=True) 
    188188 
     189class EmbedRegister(ConfigLoadingUtil): 
     190    default = '/data/embed.cfg' 
     191 
     192embedreg = EmbedRegister.from_spec() 
     193grok.global_utility(embedreg, 
     194                    provides=IConfigValues, 
     195                    name="almanac.embed", 
     196                    direct=True) 
     197 
    189198 
    190199def test_suite(): 
  • siteapp/branches/clean-embed/opengeo/almanac/content.py

    r1827 r1836  
    22Our basic content and pedestrian add and edit views 
    33""" 
     4from lxml.html.clean import Cleaner 
    45from opengeo.almanac import app 
    56from opengeo.almanac import kml 
     
    169170    elif type == 'video': 
    170171        story.type = 'video' 
    171         story.body = extra_data['video'] 
     172        markup = extra_data['video'] 
     173        story.body = clean_embed_markup(markup) 
    172174    else: 
    173175        # assume text if it's not an image or video 
     
    191193 
    192194    return story 
     195 
     196 
     197def clean_embed_markup(markup): 
     198    cfg = getUtility(IConfigValues, name='almanac.embed') 
     199    cfg = cfg['embed'] 
     200    allow_tags = cfg.allow_tags.split(',') 
     201    host_whitelist = cfg.host_whitelist.split(',') 
     202 
     203    # prefix www subdomain to all hosts as a convenience 
     204    host_whitelist = set([x.strip() for x in host_whitelist]) 
     205    for host in list(host_whitelist): 
     206        if not host.startswith('www.'): 
     207            host_whitelist.add('www.' + host) 
     208 
     209    cleaner = Cleaner(allow_tags=allow_tags, 
     210                      remove_unknown_tags=False, 
     211                      whitelist_tags=allow_tags, 
     212                      host_whitelist=host_whitelist) 
     213    import pdb; pdb.set_trace() 
     214    return cleaner.clean_html(markup) 
    193215 
    194216class BaseModel(grok.Model): 
     
    823845            self.context.photoid = photoid 
    824846        elif self.context.type == 'video' and 'video' in self.request.form: 
    825             self.context.body = self.request.form['video'] 
     847            self.context.body = clean_embed_markup(self.request.form['video']) 
    826848        elif self.context.type == 'text' and 'words' in self.request.form: 
    827849            self.context.body = self.request.form['words']