2

I have one problem regarding the data insertion in PHP.

In my site there is a message system.

So when my inbox loads it gives one JavaScript alert.

I have searched a lot in my site and finally I found that someone have send me a message with the text below.

<script>
  alert(5)
</script>

So how can I restrict the script code being inserted in my database?

I am running on PHP.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avinash
  • 6,064
  • 15
  • 62
  • 95

8 Answers8

7

There is no problem with JavaScript code being stored in the database. The actual problem is with non-HTML content being taken from the database and displayed to the user as if it were HTML. The correct approach would be to make sure your rendering code treats text as text, not as HTML.

In PHP, this would be done by calling htmlspecialchars on the inbox contents when displaying the inbox (possibly along with nl2br and maybe turning links to <a> tags).

Avoid using striptags for text content: as an user, I might want to type a message like:

... and to create a link, use <a href="your-url-here">your-text-here</a> ...

striptags would eliminate the tag, htmlspecialchars would make the text appear as it was typed.

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
5

You should not restrict it to be inserted into the database (if StackOverflow would restrict it, we would not be able to post code examples here!)

You should better control how you display it. For instance, add htmlentities() or htmlspecialchars() to your echo call.

naivists
  • 32,681
  • 5
  • 61
  • 85
5

This is called XSS. There are numerous threads about it on SO.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Just being pedantic here, but it's not *necessarily* XSS. It's just script injection which *could* be used for XSS and lots of other nastiness. – nickf Jan 05 '10 at 08:00
3

User input should be escaped before outputting it.

Whenever you're displaying something a user submitted, run it through htmlspecialchars() first. This'll turn HTML code into safe output.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
3

You should use strip_tags. If you still want to allow some HTML, then add a whitelist in the second parameter.

I should add a really big caveat here. If you're leaving any tags in a strip_tags whitelist, you can still be susceptible to javascript injection. Assume you're allowing the seemingly innocuous tags <strong> and <em>:

Strip tags will still allow all attributes, including event handlers
like <strong onmouseover="window.href=http://mydodgysite.com">this</strong>.

You have a couple of serious options:

  • strip_tags with no whitelist. Safe, but doesn't allow for any formatting, and may cause problems with strings like this: "x<y, but y>4" --> "x4"
  • htmlentities. Use this when displaying the data on the screen (not on the data before you put it in the database). It's safe, but doesn't allow for formatting.
  • A different markup system than HTML, for example: Markdown, Wiki markup, BB Code. Requires rendering to convert back to HTML, but it's mostly safe and can be quite flexible.
nickf
  • 537,072
  • 198
  • 649
  • 721
  • yes this is the nice information to prevent the script injection... Thanks a lot, i will keep in mind from now.. – Avinash Jan 05 '10 at 06:39
2

Take a look at the htmlspecialchars() function. It converts < > ' " and & to their html entity equilivents, meaning <script> will become &lt;script&gt;

Yacoby
  • 54,544
  • 15
  • 116
  • 120
1

You can use strip_tags(). The second argument of this function will allow you to list an explicit list of which tags are allowable:

// Allow <p> and <a>, <script> will be stripped
echo strip_tags($text, '<p><a>');

You may also consider htmlspecialchars(), which converts characters like < into &lt;, causing the browser to interpret them as text, rather than code:

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
Sampson
  • 265,109
  • 74
  • 539
  • 565
0

If I understand you right, you're just looking for two simple commands:

$message = str_replace($message, "<", "&lt;");
$message = str_replace($message, ">", "&gt;");
Bobby
  • 11,419
  • 5
  • 44
  • 69
  • so after that i echo the script code it will alert the again, so it will be better to remove the script tags from the db value... What you think for this case.....? – Avinash Jan 04 '10 at 14:02