🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.python
Article #27578

Re: Wpuszczanie whitelistowane tagi HTML w inpucie

#27578
From: Sławek Tuleja
Date: Mon, 05 Jul 2010 20:53
51 lines
3063 bytes
On Jul 5, 8:59 pm, Marek <ma...@octogan.usun.net> wrote:
> Witam,
>
> Z tego co wiem Django ma automatyczny mechanizm uniemożliwiający
> (utrudniający?) ataki typu XSS. Czy jednak jest mozliwość wybiórczego
> whitelistowania tagów np. HTML'a, które chcałbym aby były były
> interpretowane przez przeglądarke. Dla przykładu chciałbym aby tagi z
> inputu który poda user, typu <b></b>, <i></i> nie były escape'owane a
> tekst był rzeczywiście pogrubiony, kursywą etc. Ale żeby kwiatki typu
> <script> alert(1) </script>
> były wyświetlane jako: &lt;script&gt; alert(1) &lt;/script&gt; (czyli już
> normalne autoescape)
>
> pozdrawiam
>
> Marek

czesc Marek

faktycznie nie ma takiego filtru w django
- albo jest: usuwanie wszystkich tagow (striptags)
- alob usuwanie wybranych tagow (removedtags)

sprobuj sobie napisac taki filtr biorac pod uwage kod dla tych wyzej
wymienionych funkcji (ewentualnie mozna poszukac w internecie
rozwiazan (ja znalazlem takie: http://djangosnippets.org/snippets/1655/):

def removetags(value, tags):
    """Removes a space separated list of [X]HTML tags from the
output."""
    tags = [re.escape(tag) for tag in tags.split()]
    tags_re = u'(%s)' % u'|'.join(tags)
    starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
    endtag_re = re.compile(u'</%s>' % tags_re)
    value = starttag_re.sub(u'', value)
    value = endtag_re.sub(u'', value)
    return value
removetags.is_safe = True
removetags = stringfilter(removetags)

def striptags(value):
    """Strips all [X]HTML tags."""
    from django.utils.html import strip_tags
    return strip_tags(value)
striptags.is_safe = True
striptags = stringfilter(striptags)

def strip_tags(value):
    """Returns the given HTML with all tags stripped."""
    return re.sub(r'<[^>]*?>', '', force_unicode(value))
strip_tags = allow_lazy(strip_tags)
Technical Details
Message-ID: <2c0eb68c-d88c-4d46-9771-6b0410af6a8b@u26g2000yqu.googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!postnews.google.com!u26g2000yqu.googlegroups.com!not-for-mail
References: <i0ta2p$u05$1@news.onet.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!postnews.google.com!u26g2000yqu.googlegroups.com!not-for-mail
From: Sławek Tuleja <slawek.tuleja@gmail.com>
Newsgroups: pl.comp.lang.python
Subject: Re: Wpuszczanie whitelistowane tagi HTML w inpucie
Date: Mon, 5 Jul 2010 13:53:49 -0700 (PDT)
Organization: http://groups.google.com
Lines: 57
Message-ID: <2c0eb68c-d88c-4d46-9771-6b0410af6a8b@u26g2000yqu.googlegroups.com>
References: <i0ta2p$u05$1@news.onet.pl>
NNTP-Posting-Host: 95.48.17.250
Mime-Version: 1.0
X-Trace: posting.google.com 1278363229 11678 127.0.0.1 (5 Jul 2010 20:53:49 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 5 Jul 2010 20:53:49 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: u26g2000yqu.googlegroups.com; posting-host=95.48.17.250;  posting-account=SueW0AoAAACPDQHHUFuxpqDfu01VPDBS
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3)  Gecko/20100407 Ubuntu/9.10 (karmic) Firefox/3.6.3,gzip(gfe)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit