Do not trust php_self security issues

Source: Internet
Author: User

CopyCode The Code is as follows: <HTML>
<Body>
<? PHP
If (isset ($ _ request ['submitted']) & $ _ request ['submitted'] = '1 '){
Echo "form submitted! ";
}
?>
<Form action = "<? PHP echo $ _ server ['php _ Self '];?> ">
<Input type = "hidden" name = "submitted" value = "1"/>
<Input type = "Submit" value = "Submit! "/>
</Form>
</Body>
</Html>

Seemingly accurate code, but hidden in danger. Let's save it as Foo. php and then use it in the PHP environment.

 
Foo. php/% 22% 3E % 3 cscript % 3 ealert ('xss') % 3C/script % 3E % 3 cfoo

Access, you will find a javascript alert-this is obviously an XSS injection vulnerability. The reason is that

 
Echo $ _ server ['php _ Self '];

This statement directly outputs unfiltered values. Let's take a look at the description in the PHP manual.

 
'Php _ Self'

The filename of the currently executing script, relative to the document root.
For instance, $ _ server ['php _ Self '] in a script at the address
Http://example.com/test.php/foo.bar wocould be/test. php/Foo. Bar. The _ file __
Constant contains the full path and filename of the current (I. e. Encoded DED) file.
If PHP is running as a command-line processor this variable contains the script
Name since PHP 4.3.0. Previously it was not available.

The reason is clear. It turns out that $ _ server ['php _ Self '] is an environment variable provided by the server, but this is indeed the same as $ _ post and $ _ Get, which can be changed by users.

There are many other similar variables, such as $ _ cookie (if you want to "play" their cookies, we can't do it ). The solution is simple. Use such functions as strip_tags and htmlentities to filter or escape them.

 
Echo htmlentities ($ _ server ['php _ Self ']);

-- Split --

The above example requires us to always be cautious about coding. Chris shiflett summarized the following in his blog: the two basic security ideas of preventing XSS are:

 
Filter input
Escape output

I translated the above into"Filter input and escape output". For details, refer to his blog.Article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.