Prevent XSS using DOM Extension and/or SimpleXML

This is a discussion on Prevent XSS using DOM Extension and/or SimpleXML within the PHP General forums, part of the PHP Programming Forums category; Hi there! Iīm building a form validator using PHP and JS. Itīs working fine by now, but I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-14-2006
Raphael Martins
 
Posts: n/a
Default Prevent XSS using DOM Extension and/or SimpleXML

Hi there!

Iīm building a form validator using PHP and JS. Itīs working fine by
now, but I want to make a little improvement. Here is how its working now:

1. The user fill the form. Every time he leaves the field, the JS
code match the value against a regexp to validate.
2. When the user submits the form, the PHP script match all the
values against the same regexp's.

Now, i want to validate my fields to prevent XSS, allowing my html tags
but only the attributes that I want.
I thought something like: (the tags and the valid attributes).
<?php
$form_html_validation = array(
"p"=>array(""),
"a"=>array("href","name","rel"),
"ol"=>array(""),
"ul"=>array(""),
"li"=>array(""),
"h2"=>array(""),
"h3"=>array(""),
"h4"=>array(""),
"h5"=>array(""),
"h6"=>array(""),
"strong"=>array(""),
"em"=>array("") );
$valid_elements = "<".join("><",array_keys($form_html_validation))." >";
$userInput = strip_tags($userInput,$valid_elements);
//perform DOM Attribute Validation
?>
But I donīt know how to loop over every attribute for each tag in the
DomTree.

Someone has any ideas?

Thank You
Reply With Quote
  #2 (permalink)  
Old 11-14-2006
Rob
 
Posts: n/a
Default Re: Prevent XSS using DOM Extension and/or SimpleXML

Raphael Martins wrote:

> But I donīt know how to loop over every attribute for each tag in the
> DomTree.


Not sure if you need to do this element by element or just want all
attributes, but here are two ways using DOM. They assume $dom is an
already loaded DOMDocument.

1 - Use XPath:

$xPath = new DOMXPath($dom);
$nodelist = $xPath->query("//@*");
foreach ($nodelist AS $attr) {
print $attr->nodeName." ".$attr->nodeValue."\n";
}


2 - Walk the tree manually:

function checkElement($node) {
$attlist = $node->attributes;
foreach ($attlist AS $attr) {
print $attr->nodeName." ".$attr->nodeValue."\n";
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes AS $child) {
if ($child->nodeType == XML_ELEMENT_NODE) {
checkElement($child);
}
}
}
}

$root = $dom->documentElement;
checkElement($root);

Rob
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:28 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0