Die geistigen Wirrungen des Sebastian Bauer
Montag, 27. November 2006
Wie man unter PHP Eventhandler aus HTML Elementen filtert
Falls das mal jemand für eine Website, ein CMS oder ähnliches gebrauchen kann: Folgende Regular-Expression filtert Eventhandler (onclick, onmouseover usw.) automatisch aus HTML Elementen aus, da diese ein Sicherheitsrisiko (XSS) darstellen.
Die Funktion nimmt als Parameter einen String mit dem zu filternden Inhalt oder ein Array aus Strings an.
//Update (05.12.2006 - 09.24 Uhr): Hab die Funktion nochmals überarbeitet, da es teilweise durch greedy RegExes zu fehlerhaften Matches kommt, dem wird jetzt entgegengewirkt, indem zuerst alle Tags gesucht werden, welche überhaupt als möglicher Container eines Eventhandlers in Frage kommen und dann einzeln gefiltert werden.
Die Funktion nimmt als Parameter einen String mit dem zu filternden Inhalt oder ein Array aus Strings an.
- function stripEvents ($myVar) {
- foreach ($myVar as $k => $v)
- $myVar[$k] = stripEvents($v);
- return $myVar;
- }
- else {
- // First we loop through all tags that contain possible event handler.
- // To avoid endless loops (could happen if the first preg_match detects a possible
- // event handler, which in fact isn't one and won't be filtered by the second one), we
- // store the position where the first match occurs. If we get to this point again, we know
- // that we are beginning an endless loop and can escape from it.
- $lowestOffset = 0;
- if($lowestOffset < $tag[0][1])
- break;
- else {
- $lowestOffset = $tag[0][1];
- }
- // To rebuild the string without the eventhandler, we have to store the length of the tag
- // The offset where this tag begins is stored in $tag[0][1]
- // Now strip out the event handler
- // Split the complete source string into 2 parts without the modified HTML tag
- // Rebuild the source string containing the modified, event-handler-free HTML tag
- $myVar = $part1 . $tagStripped . $part2;
- }
- }
- return $myVar;
- }
//Update (05.12.2006 - 09.24 Uhr): Hab die Funktion nochmals überarbeitet, da es teilweise durch greedy RegExes zu fehlerhaften Matches kommt, dem wird jetzt entgegengewirkt, indem zuerst alle Tags gesucht werden, welche überhaupt als möglicher Container eines Eventhandlers in Frage kommen und dann einzeln gefiltert werden.
Geschrieben von Sebastian Bauer
in Security, Software & Coding
um
15:40
| Kommentare (2)
| Trackback (1)
(Seite 1 von 1, insgesamt 1 Einträge)
Suche
Kategorien
Twitter Aktivität
Gallerie
Tag Cloud
überwachung anonymizer bhut jolakia bluetooth car design dhtml gforce giiforce google hockenheimring html javascript laengsbeschleunigung lfs lfsworld linux live for speed mein roadster php PHP Conf. / Webinale 2007 piraten piratenpartei privacy privatsphäre remote render roadster schäuble security server smart stasi2.0 ubuntu web2.0 wii wiimote wiiremote wiiremotej wwweb
Beliebteste Einträge
Statistiken
Letzter Artikel: 07.08.2009 07:00
198 Artikel wurden geschrieben
236 Kommentare wurden abgegeben
Links
Verwaltung des Blogs
Sprachauswahl
Creative Commons
© Copyright 2008, Die Milchtüte design by Sebastian Bauer, based on a port for s9y by nerdwg.org


