in Education by
When receiving user input on forms I want to detect whether fields like "username" or "address" does not contain markup that has a special meaning in XML (RSS feeds) or (X)HTML (when displayed). So which of these is the correct way to detect whether the input entered doesn't contain any special characters in HTML and XML context? if (mb_strpos($data, '<') === FALSE AND mb_strpos($data, '>') === FALSE) or if (htmlspecialchars($data, ENT_NOQUOTES, 'UTF-8') === $data) or if (preg_match("/[^\p{L}\-.']/u", $text)) // problem: also caches symbols Have I missed anything else,like byte sequences or other tricky ways to get markup tags around things like "javascript:"? As far as I'm aware, all XSS and CSFR attacks require < or > around the values to get the browser to execute the code (well at least from Internet Explorer 6 or later anyway) - is this correct? I am not looking for something to reduce or filter input. I just want to locate dangerous character sequences when used in XML or HTML context. (strip_tags() is horribly unsafe. As the manual says, it doesn't check for malformed HTML.) Update I think I need to clarify that there are a lot people mistaking this question for a question about basic security via "escaping" or "filtering" dangerous characters. This is not that question, and most of the simple answers given wouldn't solve that problem anyway. Update 2: Example User submits input if (mb_strpos($data, '<') === FALSE AND mb_strpos($data, '>') === FALSE) I save it Now that the data is in my application I do two things with it - 1) display in a format like HTML - or 2) display inside a format element for editing. The first one is safe in XML and HTML context

<?php print $input; ?>

' <?php print $input; ?> The second form is more dangerous, but it should still be safe: "> Update 3: Working Code You can download the gist I created and run the code as a text or HTML response to see what I'm talking about. This simple check passes the http://ha.ckers.org XSS Cheat Sheet, and I can't find anything that makes it though. (I'm ignoring Internet Explorer 6 and below). I started another bounty to award someone that can show a problem with this approach or a weakness in its implementation. Update 4: Ask a DOM It's the DOM that we want to protect - so why not just ask it? Timur's answer lead to this: function not_markup($string) { libxml_use_internal_errors(true); if ($xml = simplexml_load_string("$string")) { return $xml->children()->count() === 0; } } if (not_markup($_POST['title'])) ... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I don't think you need to implement a huge algorithm to check if string has unsafe data - filters and regular expressions do the work. But, if you need a more complex check, maybe this will fit your needs: <?php $strings = array(); $strings[] = <<<EOD ';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//-->">'>alert(String.fromCharCode(88,83,83)) EOD; $strings[] = <<<EOD '';!--"<XSS>=&{()} EOD; $strings[] = <<<EOD <SCRIPT SRC=http://ha.ckers.org/xss.js> EOD; $strings[] = <<<EOD This is a safe text EOD; $strings[] = <<<EOD <IMG SRC="javascript:alert('XSS');"> EOD; $strings[] = <<<EOD <IMG SRC=javascript:alert('XSS')> EOD; $strings[] = <<<EOD <IMG SRC=javascript:alert('XSS')> EOD; $strings[] = <<<EOD perl -e 'print "<IMG SRC=java\0script:alert(\"XSS\")>";' > out EOD; $strings[] = <<<EOD <SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"> EOD; $strings[] = <<<EOD </TITLE>alert("XSS"); EOD; libxml_use_internal_errors(true); $sourceXML = 'value'; $sourceXMLDocument = simplexml_load_string($sourceXML); $sourceCount = $sourceXMLDocument->children()->count(); foreach( $strings as $string ){ $unsafe = false; $XML = ''.$string.''; $XMLDocument = simplexml_load_string($XML); if( $XMLDocument===false ){ $unsafe = true; }else{ $count = $XMLDocument->children()->count(); if( $count!=$sourceCount ){ $unsafe = true; } } echo ($unsafe?'Unsafe':'Safe').':
'.htmlspecialchars($string,ENT_QUOTES,'utf-8').'

'."\n"; } ?>

Related questions

0 votes
    Read the following statement carefully and find out whether it is correct about the hacking or not? It can be possible that ... some cases, it can be referred to as a legal task...
asked Mar 4, 2021 in Technology by JackTerrance
0 votes
    __________ can contain heterogeneous inputs. (a) Matrix (b) Data Frames (c) Matrix and Data Frames (d) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Templates can contain HTML and text, mixed with Handlebars expressions. (1)True (2)False...
asked Aug 24, 2021 in Technology by JackTerrance
0 votes
    __________ is a Neural Nets way of classifying inputs. A. Classification B. Learning C. Forward Propagation D. Activation...
asked Dec 26, 2022 in Technology by JackTerrance
0 votes
    __________ is a Neural Nets way of classifying inputs. A. Classification B. Learning C. Forward Propagation D. Activation...
asked Nov 13, 2022 in Education by JackTerrance
0 votes
    _____________________ is a Neural Nets way of classifying inputs. _____________________ is a Neural Nets way of ... ) Forward Propagation (c) Activation (d) Classification...
asked Oct 19, 2020 in Technology by Editorial Staff
0 votes
    write a program that inputs a strig and then print the string, equal to number of times its length Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    The correct or acceptable way of using the internet is called as Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    State whether the following statement are true or false with reasons. Legal rights are not universal. Please answer the above question....
asked Aug 4, 2022 in Education by JackTerrance
0 votes
    I want to know whether the user allows autostart permissions of the app. I have already tried this: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    Which part of code gets executed whether exception is caught or not? (a) finally (b) try (c) catch (d ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which function in R language is used to find out whether the means of 2 groups are equal to each other ... Debugging of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Help needed Please write a program to check whether a number is special number or not . 100 points Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Algorithm to find whether the given number is palindrome or not Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    Write a program which sends the name of a text file from the client to server and display the message on the ... file present or not. Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
...