Security
Security is the foundation of Tofido Code Manager. When a plugin allows executing custom code, every layer of protection matters. This document explains the security architecture and how to use it effectively.
Safe Mode for PHP Snippets
Safe Mode is the most important security feature for PHP snippets. It creates an isolated execution environment where you can test code without risking your live site.
How Safe Mode Works
When you enable Safe Mode for a PHP snippet:
- The snippet executes in a separate PHP process with limited scope
- All output is captured and displayed for review
- If a fatal error occurs, the snippet is automatically disabled
- No changes are made to the database or filesystem during testing
- The execution timeout is limited to prevent infinite loops
Using Safe Mode
- Create or edit a PHP snippet
- Write your code in the editor
- Click Test in Safe Mode before publishing
- Review the output and any warnings
- If successful, publish the snippet
Never publish a PHP snippet without testing in Safe Mode first. Even experienced developers can make mistakes that cause fatal errors. Safe Mode is your safety net.
Dangerous Function Scanning
The plugin automatically scans PHP snippets for functions that could compromise site security or stability.
Scanned Function Categories
- File System:
eval(),exec(),system(),passthru(),shell_exec(),proc_open() - Database: Raw SQL execution without preparation
- Network: Unrestricted external requests
- Code Injection:
create_function(),assert()
Warning Levels
- Block: The snippet cannot be published until the function is removed or replaced
- Warning: The snippet can be published, but a prominent warning is displayed
- Info: Informational notice about potential risks
If you believe a warning is a false positive for your use case, you can add an inline comment explaining the necessity:
// tofido-allow: shell_exec
// Required for server-side image optimization
$output = shell_exec('optipng -o2 image.png');
Capability Checks
Every management action in Tofido Code Manager requires explicit WordPress capabilities. The minimum required capability is manage_options, which is only granted to administrators by default.
Protected Actions
- Creating, editing, or deleting snippets
- Changing snippet status (draft/published)
- Importing or exporting snippets
- Modifying plugin settings
- Restoring revisions
Custom Capability Filter
Developers can customize the required capability using the filter:
// Require custom capability for snippet management
add_filter('tofido_code_manager_capability', function($cap) {
return 'custom_snippet_capability';
});
Nonce Protection
All AJAX requests and form submissions include WordPress nonce verification. This prevents Cross-Site Request Forgery (CSRF) attacks.
Nonces are generated per-user, per-action, and time-limited. Even if an attacker tricks an admin into visiting a malicious link, no snippet management actions can be performed without a valid nonce.
AJAX Security Validation
The plugin's AJAX endpoints implement multiple security layers:
- Nonce verification on every request
- Capability checking before processing
- Input sanitization for all user-provided data
- Output escaping for all responses
- Rate limiting on snippet execution endpoints
Automatic Error Recovery
If a published PHP snippet causes a fatal error:
- The error is caught by a custom error handler
- The problematic snippet is automatically disabled
- An admin notification is sent with error details
- The site continues functioning normally
- The snippet can be fixed and re-enabled from the dashboard
This recovery system ensures that even the most severe coding mistakes cannot take your site offline.
Syntax Validation
Before any snippet is saved, its syntax is validated:
- CSS: Parsed for malformed rules, missing braces, and invalid selectors
- JavaScript: Checked for syntax errors using a JavaScript parser
- PHP: Validated with
php -lequivalent linting - HTML: Checked for unclosed tags and malformed attributes
Syntax errors are highlighted in the editor with line numbers and descriptive messages.
Import Safety
When importing snippets via JSON:
- All imported snippets are set to Draft status by default
- JSON structure is validated against the schema
- Each snippet undergoes the same syntax validation as manual creation
- Dangerous function scanning applies to imported PHP snippets
- Import source tracking is logged for audit purposes
Always review imported snippets before publishing. Even if they come from a trusted source, your environment may differ from the author's. Test each snippet in Safe Mode if it contains PHP.
PHP Execution Model
PHP snippets in Tofido Code Manager execute entirely in memory. Unlike some competing solutions:
- No cache files are written to the filesystem for PHP snippets
- No eval() is used - code runs through proper WordPress hook integration
- No persistent storage of executable code outside the database
- Execution is scoped to prevent variable leakage
This design minimizes the attack surface and ensures that disabling a snippet immediately stops its execution.
Security Checklist
Before publishing any snippet, verify:
- PHP snippets have been tested in Safe Mode
- No dangerous function warnings are ignored without justification
- Display conditions are correctly configured
- The snippet is necessary and optimized
- Imported snippets have been reviewed and tested
- Only trusted administrators have snippet management access