Mitigation Strategy: Enable CSRF Protection
-
Description:
- Open the
config/config.php
file located in your application'sapplication/config/
directory. - Locate the configuration setting
$config['csrf_protection']
. - Change the value from
FALSE
toTRUE
:$config['csrf_protection'] = TRUE;
. - Optionally, customize CSRF settings like
$config['csrf_token_name']
,$config['csrf_cookie_name']
, and$config['csrf_expire']
inconfig/config.php
to adjust token names, cookie names, and expiration times. - Ensure you are using CodeIgniter's form helper (
form_open()
) to generate forms, which automatically includes CSRF tokens. For AJAX requests, you will need to manually include the CSRF token in your request headers or data.
- Open the
-
Threats Mitigated:
- Cross-Site Request Forgery (CSRF) - Severity: High - Attackers can force authenticated users to perform actions without their knowledge or consent.
-
Impact:
- CSRF - Impact: High - Significantly reduces the risk of CSRF attacks by validating the presence and correctness of a CSRF token in requests.
-
Currently Implemented:
- Yes, CSRF protection is enabled in
config/config.php
.
- Yes, CSRF protection is enabled in
-
Missing Implementation:
- N/A - CSRF protection is globally enabled. Verify that all forms are generated using CodeIgniter's form helper or that CSRF tokens are correctly implemented in AJAX requests.
Mitigation Strategy: Configure Secure Session Management (Database Driver)
-
Description:
- Open the
config/config.php
file in your application'sapplication/config/
directory. - Locate the configuration setting
$config['sess_driver']
. - Change the value from the default
'files'
to'database'
:$config['sess_driver'] = 'database';
. - Ensure your database configuration in
config/database.php
is correctly configured and the database user has the necessary permissions. - Create the required session table in your database as defined in CodeIgniter's documentation, typically using a migration.
- Further enhance session security by setting
$config['sess_cookie_secure'] = TRUE;
and$config['sess_cookie_httponly'] = TRUE;
inconfig/config.php
to restrict cookie transmission to HTTPS and prevent JavaScript access. Configure$config['sess_expiration']
and$config['sess_time_to_update']
to manage session lifetime and regeneration frequency.
- Open the
-
Threats Mitigated:
- Session Hijacking - Severity: High - Attackers can steal session IDs to impersonate users.
- Session Fixation - Severity: Medium - Attackers can force users to use a known session ID.
- Information Disclosure (Session Data) - Severity: Medium - Risk of session data exposure if stored insecurely.
-
Impact:
- Session Hijacking - Impact: High - Database session storage is more secure than file-based storage, especially in shared hosting.
- Session Fixation - Impact: Medium - Database driver, combined with secure cookie settings and session regeneration, strengthens protection against fixation.
- Information Disclosure (Session Data) - Impact: Medium - Database storage, when properly secured, reduces the risk of unauthorized access to session data compared to default file storage.
-
Currently Implemented:
- No, the application is currently using the default
'files'
session driver.
- No, the application is currently using the default
-
Missing Implementation:
- Session driver needs to be changed to
'database'
inconfig/config.php
. - Database session table needs to be created.
- Database configuration in
config/database.php
should be reviewed for security.
- Session driver needs to be changed to
Mitigation Strategy: Implement Context-Aware Output Encoding using esc()
-
Description:
- In your CodeIgniter views (
.php
files inapplication/views/
), identify all instances where dynamic data (user input, database data) is displayed. - Wrap each variable being outputted with CodeIgniter's
esc()
function. - Specify the appropriate context for encoding as the second parameter of
esc()
. Use'html'
for general HTML output,'js'
for JavaScript,'url'
for URLs,'css'
for CSS, and'attr'
for HTML attributes. Example:<?php echo esc($variable, 'html'); ?>
. - Train developers to consistently use
esc()
for all dynamic output in views to prevent XSS vulnerabilities.
- In your CodeIgniter views (
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High - Attackers can inject malicious scripts into web pages viewed by other users.
-
Impact:
- XSS - Impact: High -
esc()
function effectively prevents XSS by encoding output based on the context, neutralizing malicious scripts.
- XSS - Impact: High -
-
Currently Implemented:
- Partially implemented.
esc()
is used in some newer views, but older views may lack proper output encoding.
- Partially implemented.
-
Missing Implementation:
- Systematically audit all views to ensure
esc()
is used for all dynamic output. - Establish coding standards and developer training to mandate the use of
esc()
in all new code.
- Systematically audit all views to ensure
Mitigation Strategy: Utilize Parameterized Queries or Query Builder for Database Interactions
-
Description:
- When interacting with databases in CodeIgniter models and controllers, always use CodeIgniter's Query Builder or parameterized queries.
- Avoid constructing raw SQL queries using string concatenation, which is vulnerable to SQL injection.
- Use Query Builder methods like
$this->db->where()
,$this->db->insert()
,$this->db->update()
,$this->db->get()
, etc., to build database queries securely. - If raw queries are absolutely necessary, use query bindings (placeholders) with
$this->db->query()
to pass user inputs as parameters. Example:$this->db->query("SELECT * FROM users WHERE username = ?", array($username));
.
-
Threats Mitigated:
- SQL Injection - Severity: Critical - Attackers can manipulate database queries to gain unauthorized access, modify data, or compromise the database.
-
Impact:
- SQL Injection - Impact: High - Using Query Builder or parameterized queries effectively prevents SQL injection by separating SQL code from user-provided data.
-
Currently Implemented:
- Mostly implemented. Query Builder is generally used, but legacy code or specific areas might still use vulnerable raw queries.
-
Missing Implementation:
- Conduct a thorough code review of models and controllers to identify and refactor any instances of raw SQL queries built with string concatenation.
- Reinforce secure database query practices in developer training and code review processes.
Mitigation Strategy: Validate File Types and Extensions using Upload Library
-
Description:
- When implementing file upload functionality using CodeIgniter's Upload Library, configure the
$config['allowed_types']
setting. - Whitelist only the permitted file types and extensions in
$config['allowed_types']
. For example:$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
. Avoid blacklisting file types. - Set this configuration before initializing the Upload Library in your controller.
- Ensure file type validation is performed server-side by the Upload Library before moving the uploaded file. Do not rely solely on client-side validation.
- When implementing file upload functionality using CodeIgniter's Upload Library, configure the
-
Threats Mitigated:
- Remote Code Execution (File Upload) - Severity: High - Attackers could upload malicious executable files if file type validation is insufficient.
- Cross-Site Scripting (File Upload) - Severity: Medium - Risk of uploading files containing embedded scripts (e.g., SVG with JavaScript).
-
Impact:
- Remote Code Execution (File Upload) - Impact: High - Whitelisting file types significantly reduces the risk of uploading and executing malicious files.
- Cross-Site Scripting (File Upload) - Impact: Medium - Reduces the risk of XSS through uploaded files, especially when combined with secure handling and serving of uploaded content.
-
Currently Implemented:
- Partially implemented. File upload functionality exists, but
allowed_types
might not be strictly whitelisted in all upload handlers.
- Partially implemented. File upload functionality exists, but
-
Missing Implementation:
- Review all file upload implementations in controllers.
- Ensure
$config['allowed_types']
is configured with a strict whitelist of allowed extensions for each file upload feature. - Replace any blacklisting approaches with whitelisting.
Mitigation Strategy: Implement Secure Routing
-
Description:
- Carefully define routes in
application/config/routes.php
to control access to controllers and actions. - Use specific routes instead of relying heavily on default routing, which can sometimes expose unintended functionality.
- Protect administrative or sensitive functionalities by placing them under specific routes and implementing authentication and authorization checks within the corresponding controllers.
- Avoid overly broad or wildcard routes that might inadvertently expose actions or controllers.
- Carefully define routes in
-
Threats Mitigated:
- Unauthorized Access - Severity: Medium to High - Improperly configured routes can lead to unauthorized access to application features and data.
- Information Disclosure - Severity: Medium - Exposure of unintended functionalities or information due to misconfigured routing.
-
Impact:
- Unauthorized Access - Impact: Medium - Well-defined routes and access control in controllers limit unauthorized access.
- Information Disclosure - Impact: Medium - Secure routing reduces the risk of exposing unintended information or functionalities.
-
Currently Implemented:
- Generally implemented. Routes are defined in
routes.php
, but a review for overly permissive routes and proper protection of sensitive areas is recommended.
- Generally implemented. Routes are defined in
-
Missing Implementation:
- Review
routes.php
to ensure routes are specific and not overly permissive. - Verify that sensitive functionalities are protected by specific routes and access control mechanisms in controllers.
- Review
Mitigation Strategy: Utilize CodeIgniter's Logging for Error Handling
-
Description:
- Configure CodeIgniter's logging in
application/config/config.php
by setting$config['log_threshold']
to an appropriate level (e.g.,1
for errors,2
for debug and errors,3
for info, debug and errors,4
for all messages). - Ensure
$config['log_path']
is set to a secure directory outside the web root if possible, or within a protected directory. - In your controllers and models, use CodeIgniter's
log_message()
function to log errors, exceptions, and security-related events. Example:log_message('error', 'Database connection failed.');
. - Regularly monitor and review the generated log files to identify potential issues, errors, and security incidents.
- Configure CodeIgniter's logging in
-
Threats Mitigated:
- Information Disclosure (Error Details in Production) - Severity: Medium - Detailed error messages displayed to users in production can reveal sensitive information.
- Security Monitoring Gaps - Severity: Medium - Lack of logging hinders the ability to detect and respond to security incidents.
-
Impact:
- Information Disclosure (Error Details in Production) - Impact: Medium - Using logging and disabling
display_errors
prevents sensitive error details from being shown to users. - Security Monitoring Gaps - Impact: Medium - Logging provides valuable data for security monitoring and incident response.
- Information Disclosure (Error Details in Production) - Impact: Medium - Using logging and disabling
-
Currently Implemented:
- Yes, logging is configured and enabled.
-
Missing Implementation:
- Review
$config['log_threshold]
to ensure appropriate logging level is set. - Verify
$config['log_path]
points to a secure location. - Ensure
log_message()
is used strategically throughout the application to log relevant events, especially errors and security-related actions.
- Review
Mitigation Strategy: Disable display_errors
in Production
-
Description:
- Open the main
index.php
file in your web root directory. - Locate the line that sets
ENVIRONMENT
constant. Ensure it is set to'production'
for production environments. - Below the
ENVIRONMENT
setting, find the conditional block that checks the environment:
switch (ENVIRONMENT) { case 'development': error_reporting(-1); ini_set('display_errors', 1); break; case 'testing': case 'production': ini_set('display_errors', 0); if (version_compare(PHP_VERSION, '5.3', '>=')) { error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); } else { error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); } break; default: header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'An application error has occurred.'; exit(1); // EXIT_ERROR }
- Confirm that within the
'production'
case,ini_set('display_errors', 0);
is set to disable error display.
- Open the main
-
Threats Mitigated:
- Information Disclosure (Error Details) - Severity: Medium - Displaying PHP errors in production can reveal sensitive information about the application's internal workings, file paths, and database structure.
-
Impact:
- Information Disclosure (Error Details) - Impact: Medium - Disabling
display_errors
prevents the exposure of sensitive error details to users in production.
- Information Disclosure (Error Details) - Impact: Medium - Disabling
-
Currently Implemented:
- Yes,
display_errors
is disabled inindex.php
for the 'production' environment.
- Yes,
-
Missing Implementation:
- N/A -
display_errors
is correctly disabled for production. Ensure theENVIRONMENT
constant is correctly set to'production'
in production deployments.
- N/A -
Mitigation Strategy: Avoid Relying Solely on Global XSS Filtering
-
Description:
- While CodeIgniter offers a global XSS filter (
$config['global_xss_filtering']
inconfig/config.php
), do not rely on it as your primary XSS prevention mechanism. - Understand that global XSS filtering can be bypassed and may not be effective against all types of XSS attacks.
- Ensure that you are primarily using context-aware output encoding with
esc()
in your views as described in the "Implement Context-Aware Output Encoding usingesc()
" mitigation strategy. - Consider leaving
$config['global_xss_filtering']
set toFALSE
to avoid a false sense of security and to enforce the use ofesc()
for output encoding. If enabled, treat it as a supplementary, not primary, security measure.
- While CodeIgniter offers a global XSS filter (
-
Threats Mitigated:
- Cross-Site Scripting (XSS) - Severity: High - Relying solely on global XSS filtering can lead to vulnerabilities if the filter is bypassed or ineffective.
-
Impact:
- XSS - Impact: Medium (if relying on global filter) / Impact: Low (if using
esc()
primarily) - Global XSS filtering alone provides limited protection. Proper output encoding withesc()
is significantly more effective.
- XSS - Impact: Medium (if relying on global filter) / Impact: Low (if using
-
Currently Implemented:
- Global XSS filtering is currently disabled (
$config['global_xss_filtering'] = FALSE;
).
- Global XSS filtering is currently disabled (
-
Missing Implementation:
- N/A - Global XSS filtering is disabled, which encourages the correct approach of using
esc()
for output encoding. Ensure developers understand not to enable and rely on global XSS filtering as a primary security measure.
- N/A - Global XSS filtering is disabled, which encourages the correct approach of using