0

Say i have a form with which user inputs some information and is submited to server using php and in PHP code i have say

$data = $_POST['data'];
// or
$data = strip_tags(@$_POST['data']);
  1. I want to know of the strip_tags() is enough to stop javascript injection through html forms. If not how else can this be prevented. I have read here.

  2. And also say i input javascript:void(document.bgColor="blue") in the browser address bar, this changes the whole site background color to blue. How can javascript injection through the address bar be prevented.

Thanks.

asked Jul 27, 2017 at 19:44
2
  • Using the filter_input() and filter_input_array() functions are typically the better approach. You should completely avoid using the superglobals $_GET and $_POST, etc, Commented Jul 27, 2017 at 19:47
  • Not even famous websites have that strong protection. Facebook has a good protection, and it still can call function, depending on browser ofc Commented Jul 27, 2017 at 19:54

4 Answers 4

3

i suggest to use htmlspecialchars when ever you want to output something to browser

echo htmlspecialchars($data, ENT_QUOTES, 'UTF-8');

checkout this

answered Jul 27, 2017 at 20:00
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much. Your answer helped.
2

For question 2, I'm not sure if that's even possible to prevent. It's not something I've ever considered before. It sounds like you're trying to prevent executing any javascript that wasn't included by you on the page, which would also mean blocking the devtools in the browser from executing anything in the console. This could potentially be hostile to your users, e.g. if they wanted to use a bookmarklet from Instapaper.

For 1, ultimately your goal is to avoid including this injected javascript from the form when you generate a new page. When you output the data from the form, you can wrap it in htmlspecialchars.

answered Jul 27, 2017 at 20:04

1 Comment

Thanks you very much. Your answer helped.
1

It's depend which output you are trying to get.

In some cases , you'll want to leave the HTML tags including script tags ,but you want that those elements will not run when you output them, in that case you should use htmlspecialchars($_POST['data']), (It's suggested to define also utf8 as the third parameter).

But if you want to remove entierly the tags than strip_tags will prevent XSS

answered Jul 27, 2017 at 19:57

1 Comment

Thanks you very much. Your answer helped.
1

One function cannot fully protect you from script injection. Consider the following program:

<?php
if(isset($_POST['height'])) 
 $height=htmlspecialchars($_POST['height'], ENT_QUOTES, 'UTF-8');
else $height=200;
if(isset($_POST['width'])) 
 $height=htmlspecialchars($_POST['width'], ENT_QUOTES, 'UTF-8');
else $width=300;
echo("
<!DOCTYPE html>
<html>
<body>
<iframe src='whatever' height=$height width=$width>
</iframe>
</body>
</html>
");

The input is sanitized, but javascript will still be executed through a simple injection vector like:

300 onload=alert(String.fromCharCode(88)+String.fromCharCode(83)+String.fromCharCode(83))

You still need to quote your attributes or you are vulnerable like this example.

Another semi-common injection vector exists when user input is echoed into javascript comments, and you can inject new lines or close the comment. I blame it on the 'this shit doesn't work as it should, but let's keep it around in a comment'-style of development.

Note: The XSS protection of many browsers will not run my simple example. If you want to try it use one without protection, or find a vector that defeats it (not sure if there is one for e.g. Chrome).

answered Jul 27, 2017 at 21:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.