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
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
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