Are you a third party developer for Joomla! addons? Do you publish your programs on the Joomla! forge or on your website? Well, thank you for doing that, the community probably loves you for sharing your work!
However, there are a few things in terms of security that you should be aware of. Just having a component that runs fine on your computer is usually not enough! You need to take care of security, because otherwise your programm could easily ruin the websites of your customers.
So, lets just jump right into it. These are the topics I will deal with in this guide:
- 1. Secure your software against direct access
- 2. Secure your software against remote file inclusion
- 3. Secure your software against SQL injections
- 4. Secure your software against XSS scripting
- 5. Make sure your software does not need register_globals
- 6. Check access privileges of users
- 7. How to achieve raw component output (for pictures, RSS-feeds etc.)
- 8. Various things to be aware of
1. Secure your software against direct access
The files of your component will usually be called by Joomla!. Joomla! is a wrapper around your software, it provides many usefull features like user authentication and so on. Since developers usually test their components only through Joomla!, they tend to forget about the possibility of calling files directly. Instead of calling your component by
crackers also might try to use
As you can see, the PHP file will be executed directly, without Joomla! as a wrapper around it. Now, if your file only contains some classes or functions, but does not execute any code, there is nothing wrong about that:
Code: Select all
<?php
class myClass {
[SomeFunctionsHere]
}
function myFunction() {
[SomeCodeHere]
}
?>
Conclusion:
To make your component secure against direct access, insert this code line into the beginning of every PHP file that executes code:
Code: Select all
defined( '_VALID_MOS' ) or die( 'Restricted access' );
2. Secure your software against remote file inclusion
Now imagine, you have a line like this one in your code:
Code: Select all
include( $mosConfig_absolute_path . '/components/com_yourcomponent/yourcomponent.class.php' );
and actually sends back executable PHP code under the filename of that image. That code then is executed (assuming that register_globals is switched on in your webserver, which unfortunaltely is the case for many people) in your or your customers webserver with the permissions of the webserver. The attacker can do anything he wants to do (and what the webserver is allowed for) on your webserver! This is called remote file inclusion. Unfortunately, this is something even script kiddies can do easily.
There are also some more advanced technics out there that allow for remote file inclusion in some PHP versions even if you have switched register_globals off. Remote file inclusion only works on systems that have the PHP setting allow_url_fopen switched to on. But as this option is needed by many "good" programs as well, switching it off is not always a good idea.
Conclusion:
To secure your code against remote file inclusion, you need to make sure no unvalidated input is used when including files. At first, apply the solution from part 1 of this guide. Secondly, be very carefull with all calls to functions dealing with the file system, especially e.g. include, require, include_once, require_once, fopen. If you really need to include files with variable names, make sure to validate all these variables.
A good practice to include files is using constants [2.2]:
Code: Select all
define( 'YOURBASEPATH', dirname(__FILE__) );
require_once( YOURBASEPATH . '/file_to_include.php' );
3. Secure your software against SQL injections
SQL injections make it possible for attackers to modify certain unsafe SQL queries, your script executes, in such a way that it could alter data in your database or give out sensible data to the attacker. That is because of unvalidated user input.
Take a look at this code:
Code: Select all
$value = $_GET['value'];
$database->setQuery( "SELECT * FROM #__mytable WHERE id = $value" );
Conclusion:
Validate all user input before you use it in a SQL query. Apply
Code: Select all
$string = $database->getEscaped( $string );
Code: Select all
$value = intval( $value );
Again, for more information on SQL injections, please take a look at the listed resources, especially [3.2].
Also, make sure to use mosGetParam() [5.5] for retrieving user input from the request, e.g.:
Code: Select all
$value = mosGetParam( $_POST, 'value' );
$value = mosGetParam( $_POST, 'value', 'default' ); // This will return 'default' when $_POST['value'] is not set.
4. Secure your software against XSS
Cross Site Scripting (XSS) means executing script code (e.g. JavaScript) in a visitors browser. Be carefull not to echo out any unvalidated input to a user. Code like this is dangerous for your visitors:
Code: Select all
echo $_REQUEST['value'];
Use mosGetParam() [5.5] for retrieving user input from a request, it does strip out a pretty good amount of insecure stuff. But don't rely on it, also take a good look at places where you echo out things to the webbrowser. Apply
Code: Select all
$value = htmlspecialchars( $value );
5. Make sure your software does not need register_globals
Up to now, there are many programs that rely on register_globals being set to ON. PHP then imports all $_GET, $_POST, $_COOKIE data and some other variables into the global scope. When people program things correctly, there is not neccessarily anything wrong about it. But unfortunaltely there are very many programs out there using global variables in an insecure way. This might open up serious security holes. Therefore, users are advised to switch off register_globals, and more and more hosting companies do so for security reasons.
You should never use any uninstantiated variables. Make sure to properly fill each variable before using it. To check whether your component is capable of running without register_globals, you should do the following:
- Enable error reporting in PHP to see notices. This will give you some hints on which variables are used without prior initialization.
- Set register_globals to off in your php.ini.
- Set RG_EMULATION to 0 in globals.php in your Joomla! root folder.
Code: Select all
echo $GLOBALS['varname'];
Code: Select all
global $varname;
echo $varname;
6. Check access privileges of users
When giving access to certain components (or to certain database table rows) you might want to make sure that only registered or special users can access it. I'm not going into any ACL related issues here, I rather want to give you a short overview on how to distinguish guest, registered (and logged in) users, and special users (by default all users below Registered, meaning Authors, Publishers etc.).
Joomla! provides (again, only in in 1.0.x) the $my object which holds information about the current user. These are the settings for the different access types (only applies to the frontend):
- $my->gid = 0 ==> the user is not logged in
- $my->gid = 1 ==> the user is a registered user
- $my->gid = 2 ==> the user is a special user
You can check these values to block access to certain parts of your component.
Also, make sure not to present any information to a user he does not have access to. A simple SQL query that takes into account the permissions of the category for a certain databse entry (assuming your data is sorted into categories) might look like this:
Code: Select all
SELECT * FROM #__contact_details AS c
LEFT JOIN #__categories AS cat ON cat.id = c.catid
WHERE ( c.name LIKE '%$text%' )
AND c.published = 1
AND cat.published = 1
AND c.access <= $my->gid
AND cat.access <= $my->gid
7. How to achieve raw component output (for pictures, RSS-feeds etc.)
In some cases, users need to send out raw data (no Joomla! template around it) to the browser, for example binary pictures or XML data for RSS feeds. Developers tend to write their own entry point PHP files, but this should only be a last resort. It is better to let Joomla! handle things.
Conclusion:
You should add a new function to your component (and to the switch statement that handles the selected $task). Then, call your component like this:
If you really need to provide an entry point – a file that might be called directly – make sure to take care of part 2 of this guide. Secondly, the first thing you should do in your code is to include Joomla!'s globals.php (and if needed, Joomla!'s configuration.php).
Code: Select all
define( 'YOURBASEPATH', dirname(__FILE__) );
require_once( YOURBASEPATH . '/../../globals.php' );
require_once( YOURBASEPATH . '/../../configuration.php' );
8. Various things to be aware of
There are some more things you should not do, and also some functions you should not use at all.
- Don't use eval(). eval() is evil! :P
- Don't use the backtick operator [8.2], exec, shell_exec, system, popen and such functions
- Don't automatically send out an email to you whenever your component becomes installed somewhere. This will give you a bad reputation!
Resources
1. Secure your software against direct access 2. Secure your software against remote file inclusion
- [2.1] http://www.owasp.org/index.php/PHP_Top_ ... _Execution
- [2.2] http://dev.joomla.org/index.php?option= ... _constants For Joomla! 1.5
- [3.1] http://en.wikipedia.org/wiki/Sql_injection
- [3.2] http://php.net/manual/en/security.datab ... ection.php
- [3.3] http://www.owasp.org/index.php/PHP_Top_ ... _Injection
- There are also many good links within [3.1] and [3.3].
- [4.1] http://en.wikipedia.org/wiki/XSS
- [4.2] http://www.owasp.org/index.php/Cross_Site_Scripting
- [4.3] http://www.owasp.org/index.php/PHP_Top_ ... _scripting
- [4.4] http://php.net/manual/en/function.htmlspecialchars.php
- [5.1] http://php.net/manual/en/function.error-reporting.php Make sure your level for error_reporting includes E_NOTICE
- [5.2] http://php.net/manual/en/ini.core.php#i ... er-globals
- [5.3] http://php.net/manual/en/language.varia ... efined.php
- [5.4] http://www.hardened-php.net/index.76.html
- [5.5] http://forum.joomla.org/index.php/topic,15691.0.html
- No resources so far.
- No resources so far.