All Projects → stevebauman → Purify

stevebauman / Purify

An HTML Purifier / Sanitizer for Laravel

Labels

Projects that are alternatives of or similar to Purify

Laravel Localization Helpers
🎌 Artisan commands to generate and update lang files automatically
Stars: ✭ 190 (-2.06%)
Mutual labels:  laravel
Larapoll
A Laravel package to manage your polls
Stars: ✭ 189 (-2.58%)
Mutual labels:  laravel
Cms
Multilingual PHP CMS built with Laravel and bootstrap
Stars: ✭ 2,342 (+1107.22%)
Mutual labels:  laravel
Nebula
Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.
Stars: ✭ 190 (-2.06%)
Mutual labels:  laravel
Fortify Ui
Laravel Fortify driven replacement to the Laravel UI package
Stars: ✭ 192 (-1.03%)
Mutual labels:  laravel
Vue Routisan
Elegant, fluent route definitions for Vue Router, inspired by Laravel. v3 is currently in beta. [email protected]
Stars: ✭ 193 (-0.52%)
Mutual labels:  laravel
Laravel Bootstrap Components
Bootstrap components as Laravel components
Stars: ✭ 190 (-2.06%)
Mutual labels:  laravel
Laravel Cheat Sheet
Additional resource for the Udemy Laravel Essentials course
Stars: ✭ 194 (+0%)
Mutual labels:  laravel
Skeleton Nova Tool
A skeleton repository for Spatie's Nova Packages
Stars: ✭ 191 (-1.55%)
Mutual labels:  laravel
Laravel Shield
A HTTP basic auth middleware for Laravel
Stars: ✭ 193 (-0.52%)
Mutual labels:  laravel
Library Management System
📚 An automated library management system developed in Laravel 4.2 PHP MVC Framework
Stars: ✭ 189 (-2.58%)
Mutual labels:  laravel
Multi Tenant
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
Stars: ✭ 2,304 (+1087.63%)
Mutual labels:  laravel
Laravel Datatables Buttons
jQuery DataTables Buttons Plugin for Laravel.
Stars: ✭ 192 (-1.03%)
Mutual labels:  laravel
Laravel Castable Data Transfer Object
Automatically cast JSON columns to rich PHP objects in Laravel using Spatie's data-transfer-object class
Stars: ✭ 191 (-1.55%)
Mutual labels:  laravel
Laravel Option Framework
Manage your laravel application's dynamic settings in one place with various supported input types.
Stars: ✭ 194 (+0%)
Mutual labels:  laravel
Media Manager
A simple file browser and up-loader for Laravel written in Vue.JS
Stars: ✭ 190 (-2.06%)
Mutual labels:  laravel
Seo Manager
Seo Manager Package for Laravel ( with Localization )
Stars: ✭ 192 (-1.03%)
Mutual labels:  laravel
Core
AdminArchitect - Active Admin for Laravel
Stars: ✭ 194 (+0%)
Mutual labels:  laravel
Icalendar Generator
Generate calendars in the iCalendar format
Stars: ✭ 193 (-0.52%)
Mutual labels:  laravel
Laravel Userstamps
Laravel Userstamps provides an Eloquent trait which automatically maintains `created_by` and `updated_by` columns on your model, populated by the currently authenticated user in your application.
Stars: ✭ 193 (-0.52%)
Mutual labels:  laravel

Purify

Travis CI Scrutinizer Code Quality Latest Stable Version Total Downloads License

Purify is an HTML input sanitizer for Laravel.

It utilizes HTMLPurifier by ezyang.

Requirements

  • PHP >= 7.1
  • Laravel >= 5.5

Installation

To install Purify, run the following in the root of your project:

composer require stevebauman/purify

Then, publish the configuration file using:

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

If you are using Lumen, you should copy the config file purify.php by hand, and add this line to your bootstrap/app.php:

$app->register(Stevebauman\Purify\PurifyServiceProvider::class);

Usage

Cleaning a String

To clean a users input, simply use the clean method:

$input = '<script>alert("Harmful Script");</script> <p style="a style" class="a-different-class">Test</p>';

$cleaned = Purify::clean($input);

echo $cleaned; // Returns '<p class="a-different-class">Test</p>'
Cleaning an Array

Need to purify an array of user input? Just pass in an array:

$array = [
    '<script>alert("Harmful Script");</script> <p style="a style" class="a-different-class">Test</p>',
    '<script>alert("Harmful Script");</script> <p style="a style" class="a-different-class">Test</p>',
];

$cleaned = Purify::clean($array);

var_dump($cleaned); // Returns [0] => '<p class="a-different-class">Test</p>' [1] => '<p class="a-different-class">Test</p>'
Dynamic Configuration

Need a different configuration for a single input? Pass in a configuration array into the second parameter:

$config = ['HTML.Allowed' => 'div,b,a[href]'];

$cleaned = Purify::clean($input, $config);

Note: Configuration passed into the second parameter is not merged with your current configuration.

$config = ['HTML.Allowed' => 'div,b,a[href]'];

$cleaned = Purify::clean($input, $config);
Replacing the HTML Purifier instance

Need to replace the HTML Purifier instance with your own? Call the setPurifier() method:

$purifier = new HTMLPurifier();

Purify::setPurifier($purifier);

Practices

If you're looking into sanitization, you're likely wanting to sanitize inputted user HTML content that is then stored in your database to be rendered onto your application.

In this scenario, it's likely best practice to sanitize on the way out instead of the on the way in. Remember, the database doesn't care what text it contains.

This way you can allow anything to be inserted in the database, and have strong sanization rules on the way out.

This helps tremendously if you change your sanization requirements later down the line, then all rendered content will follow these sanization rules.

Configuration

Inside the configuration file, the entire settings array is passed directly to the HTML Purifier configuration, so feel free to customize it however you wish. For the configuration documentation, please visit the HTML Purifier Website:

http://htmlpurifier.org/live/configdoc/plain.html

Custom Configuration Rules

There's multiple ways of creating custom rules on the HTML Purifier instance.

Below is an example service provider you can use as a starting point to add rules to the instance. This provider gives compatibility with Basecamp's Trix WYSIWYG editor:

Credit to Antonio Primera for resolving some HTML Purifier configuration issues with trix.

<?php

namespace App\Providers;

use HTMLPurifier_HTMLDefinition;
use Stevebauman\Purify\Facades\Purify;
use Illuminate\Support\ServiceProvider;

class PurifySetupProvider extends ServiceProvider
{
    const DEFINITION_ID = 'trix-editor';
    const DEFINITION_REV = 1;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        /** @var \HTMLPurifier $purifier */
        $purifier = Purify::getPurifier();

        /** @var \HTMLPurifier_Config $config */
        $config = $purifier->config;

        $config->set('HTML.DefinitionID', static::DEFINITION_ID);
        $config->set('HTML.DefinitionRev', static::DEFINITION_REV);

        if ($def = $config->maybeGetRawHTMLDefinition()) {
            $this->setupDefinitions($def);
        }

        $purifier->config = $config;
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Adds elements and attributes to the HTML purifier
     * definition required by the trix editor.
     *
     * @param HTMLPurifier_HTMLDefinition $def
     */
    protected function setupDefinitions(HTMLPurifier_HTMLDefinition $def)
    {
        $def->addElement('figure', 'Inline', 'Inline', 'Common');
        $def->addAttribute('figure', 'class', 'Text');

        $def->addElement('figcaption', 'Inline', 'Inline', 'Common');
        $def->addAttribute('figcaption', 'class', 'Text');
        $def->addAttribute('figcaption', 'data-trix-placeholder', 'Text');

        $def->addAttribute('a', 'rel', 'Text');
        $def->addAttribute('a', 'tabindex', 'Text');
        $def->addAttribute('a', 'contenteditable', 'Enum#true,false');
        $def->addAttribute('a', 'data-trix-attachment', 'Text');
        $def->addAttribute('a', 'data-trix-content-type', 'Text');
        $def->addAttribute('a', 'data-trix-id', 'Number');

        $def->addElement('span', 'Block', 'Flow', 'Common');
        $def->addAttribute('span', 'data-trix-cursor-target', 'Enum#right,left');
        $def->addAttribute('span', 'data-trix-serialize', 'Enum#true,false');

        $def->addAttribute('img', 'data-trix-mutable', 'Enum#true,false');
        $def->addAttribute('img', 'data-trix-store-key', 'Text');
    }
}

After this service provider is created, make sure you insert it into your providers array in the app/config.php file, and update your HTML.Allowed string in the config/purify.php file.

Note: Remember that after this definition is created, and you have ran Purify::clean(), the definition will be cached, and you will have to clear it from your storage/app/purify folder if you want to make changes to the definition.

Otherwise, you will have to change the definition version number or ID for it to be re-cached.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].