> ## Content Index
> Fetch the complete content index at: https://www.binarysludge.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# XSS vector: URLs
- URL: https://www.binarysludge.com/2010/04/07/xss-vector-urls/
- Published: 2010-04-07T21:04:03.000Z
- Updated: 2010-04-13T11:24:27.000Z
- Author: Andrew Martin
- Tags: security, XSS, PHP, #Import 2026-10-22 12:56

> Injection is an attack that involves breaking out of a data context and switching into a code context through the use of special characters that are significant in the interpreter being used. - [OWASP](http://www.owasp.org/index.php/XSS%5F%28Cross%5FSite%5FScripting%29%5FPrevention%5FCheat%5FSheet?ref=binarysludge.com#Injection%5FTheory)

Many PHP applications will HTML encode any untrusted data using [htmlentities()](http://www.php.net/htmlentities?ref=binarysludge.com) irrespective of context. This is a problem as htmlentities does not mitigate certain XSS injections. For example, the output of any data that will be used as a URL: 

\[php\]echo '<a href="', htmlentities( 'javascript:alert("xss");' ), '">XSS</a>';\[/php\]

In this instance htmlentities is not sufficient protection, the above outputs

\[html\]<a href="javascript:alert(&quot;xss&quot;);">XSS</a>\[/html\]

To prevent this injection URLs should be validated on input, and htmlentites() encoded on output.

\[php\]  
$url = '[http://hostname/path?arg=value](http://hostname/path?arg=value&ref=binarysludge.com)';

$parsedUrl = parse\_url( $url );

if( $parsedUrl\['scheme'\] != 'http' && $parsedUrl\['scheme'\] != 'https' ) {  
// reject URL  
} else {  
$url = mysqli\_real\_escape\_string( $mysqli, $url );  
$sql = "INSERT INTO table (url) VALUES ('$url')";  
// insert query  
}

...

echo '<a href="', htmlentities( $url ), '">XSS</a>', "\\n";

\[/php\]

The URL now stored in the database should still be output using htmlentities() to encode quote marks that could inject further code, such as in this example:

\[html\][http://www.test.com/&quot](http://www.test.com/"?ref=binarysludge.com); onClick="javascript:alert('xss');"\[/html\]

For further information about XSS, the [OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet](http://www.owasp.org/index.php/XSS%5F%28Cross%5FSite%5FScripting%29%5FPrevention%5FCheat%5FSheet?ref=binarysludge.com) offers a list of XSS Prevention Rules, while RSnake's [XSS (Cross Site Scripting) Cheat Sheet](http://ha.ckers.org/xss.html?ref=binarysludge.com) is the definitive list of XSS injection test strings.