Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Purify

stevebauman/purify image

Purify stats

Downloads
2M
Stars
366
Open Issues
0
Forks
33

View on GitHub →

An HTML Purifier / Sanitizer for Laravel

Laravel Purify Package Summary

Laravel Purify is a package that provides a simple Laravel wrapper around the HTMLPurifier library, enabling developers to clean and filter HTML inputs within their Laravel applications effectively.

Key Features:

  • Flexible Cleaning: Purify allows cleaning of individual strings or arrays of HTML content, ensuring that your application is safe from XSS attacks while preserving the necessary HTML tags.
  • Dynamic Configuration: Users can specify different HTMLPurifier configurations on-the-fly, adapting to various content filtering needs across different parts of the application.
  • Custom HTML Definitions: Extend the built-in HTML definitions or create custom definitions to accommodate specific HTML elements and attributes.
  • Caching Mechanisms: Optimizes performance by caching definitions, with support for file-based and cache-based storage mechanisms.
  • Eloquent Model Casting: Utilizes custom casting to sanitize data automatically when retrieving or setting model attributes.
  • Support for HTML5 and Custom Tags: Includes HTML5 support not originally available in HTMLPurifier and allows for the customization to support unique tags like those from the Basecamp Trix editor.

Installation:

To install Purify, use Composer:

composer require stevebauman/purify

After installation, publish the configuration file:

php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"

Usage Examples:

Cleaning a String:

use Stevebauman\Purify\Facades\Purify;
 
$input = '<script>alert("bad");</script> <p>Good</p>';
$cleaned = Purify::clean($input); // Returns '<p>Good</p>'

Cleaning an Array:

$dirtyArray = ['<script>alert("bad");</script>', '<p>Good</p>'];
$cleanedArray = Purify::clean($dirtyArray);

Using Custom Configuration:

$cleaned = Purify::config('custom')->clean($input);

Configuration:

Modify the config/purify.php file to adjust HTMLPurifier settings, define custom rules, or manage caching preferences.

Cache Management:

To clear the HTMLPurifier cache, particularly after configuration changes:

php artisan purify:clear

Upgrading:

Steps are provided within the package's README for upgrading from older versions, ensuring a smooth transition and functionality retention.

Overall, Laravel Purify is an essential tool for developers needing robust HTML sanitization solutions, providing ease of use, configurability, and integration with Laravel's features such as Eloquent models.

stevebauman photo

I like to build things on the web 💻

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Stevebauman Purify Related Articles

Filter API Responses with Laravel Purity image

Filter API Responses with Laravel Purity

Read article
Top 10 Laravel Audit Security Issues image

Top 10 Laravel Audit Security Issues

Read article
Commonmark Markdown Parser v2.0 Beta 3 Released image

Commonmark Markdown Parser v2.0 Beta 3 Released

Read article
Parsedown v1.7.0 is released image

Parsedown v1.7.0 is released

Read article
The Artisan Files: Adam Wathan image

The Artisan Files: Adam Wathan

Read article
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum