2

I working out to remove special characters in passing parameters through an URL in case to avoid injection by intruders, for example I had the URL below:

www.sitename.com/people?job=manager

I added an alert script after the parameter like below:

www.sitename.com/people?job=manager"/><script>alert%2844749%29<%2fscript>

when I run the URL, the alert will popup, this might cause vulnerability in retrieve site information by this technique. I will use $_REQUEST to get the passing parameter to generate results. Is that any cure to escape URL injection techniques which I can apply to below?

$job = $_REQUEST["job"];

Thanks for advise.

shaedrich
  • 4,826
  • 2
  • 26
  • 35
conmen
  • 2,349
  • 16
  • 66
  • 98
  • 2
    Why does the alert pop up if you simply have this **in the URL**?! Are you outputting this as is into HTML? – deceze Jun 29 '12 at 06:55

7 Answers7

4

You need to use htmlentities() or htmlspecialchars() with ENT_QUOTES parameter, on all your variable.

For example for $job :

$job = htmlentities($_REQUEST["job"], ENT_QUOTES);

Don't need to escape special characters in url params.

Shrewk
  • 71
  • 2
0

You can use strip_tags()

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

OR use regular Exp--

$data = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi', '', $data);
swapnesh
  • 25,390
  • 22
  • 93
  • 124
0

Use htmlspecialchars to escape special html characters or strip_tags to remove all tags from the string.

Aurimas Ličkus
  • 9,518
  • 4
  • 22
  • 25
0

First of all don't use $_REQUEST and to protect against CSRF attacks you can either use html_entities() or strip_tags().

If you want certain tags to be allowed you can use HTML purifier.

Community
  • 1
  • 1
Shubham
  • 19,309
  • 17
  • 59
  • 84
0

A URL like

www.sitename.com/people?job=manager"/><script>alert%2844749%29<%2fscript>

will not do anything harmful in and off itself.

A URL like this, any value like this, plainly output into HTML will of course cause HTML injection. Which is why you need to HTML escape it:

<?php $url = 'www.sitename.com/people?job=manager"/><script>alert%2844749%29<%2fscript>'; ?>

<a href="<?php echo htmlspecialchars($url, ENT_QUOTES); ?>">Click here</a>
deceze
  • 491,798
  • 79
  • 706
  • 853
0

use urlencode function. Visit https://www.php.net/manual/en/function.urlencode.php for more info.

zainul ogna
  • 130
  • 1
  • 4
0

Instead you can use the following code to directly filter your input:

$job = filter_input(INPUT_GET, 'job', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
shaedrich
  • 4,826
  • 2
  • 26
  • 35