Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/medilies/xssless

Clean your rich text from XSS threats.
https://github.com/medilies/xssless

cleaner html laravel purifier sanitizer xss

Last synced: 1 day ago
JSON representation

Clean your rich text from XSS threats.

Awesome Lists containing this project

README

        

# Clean your rich text from XSS threats

[![Latest Version on Packagist](https://img.shields.io/packagist/v/medilies/xssless.svg?style=flat-square)](https://packagist.org/packages/medilies/xssless)
[![pest](https://img.shields.io/github/actions/workflow/status/medilies/xssless/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/medilies/xssless/actions?query=workflow%3Arun-tests+branch%3Amain)
[![phpstan](https://img.shields.io/github/actions/workflow/status/medilies/xssless/phpstan.yml?branch=main&label=phpstan&style=flat-square)](https://github.com/medilies/xssless/actions?query=workflow%3A"phpstan"+branch%3Amain)

![workflow](./workflow.png)

## Why use Xssless

- Your application features a [Rich Text Editor](https://en.wikipedia.org/wiki/Online_rich-text_editor) and you want to prevent all XSS.
- You want full HTML5 & CSS3 support.
- You want to allow all safe HTML elements, their attributes, and CSS properties without going deep into whitelist configs.

The default driver aligns with [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#html-sanitization) recommendations:

> ... OWASP recommends **DOMPurify** for HTML Sanitization.

## Requirements

- PHP >= 8.2
- ext-json
- Node >= 18
- NPM

## Installation

Install the package via composer:

```bash
composer require medilies/xssless
```

For non Laravel projects, pick a config and run the following code:

```php
$config = new Medilies\Xssless\Dompurify\DompurifyCliConfig('node', 'npm');

(new Medilies\Xssless\Xssless)
->using($config)
->setup();
```

For Laravel projects, run the following command:

```shell
php artisan xssless:setup
```

## Usage

Using `Medilies\Xssless\Dompurify\DompurifyCliConfig`:

```php
(new Medilies\Xssless\Xssless)
->using(new Medilies\Xssless\Dompurify\DompurifyCliConfig)
->clean($html);
```

Using `Medilies\Xssless\Dompurify\DompurifyServiceConfig`:

```php
$config = new Medilies\Xssless\Dompurify\DompurifyServiceConfig(
host: '127.0.0.1',
port: 63000
);

$xssless = (new Medilies\Xssless\Xssless)
->using($config);

/**
* It is better to have this part in a separate script
* that runs continuously and independently from your app
*/
$xssless->start();

$xssless->clean($html);
```

### Laravel usage

You can publish the config file with:

```bash
php artisan vendor:publish --tag="xssless-config"
```

This is the contents of the published config file:

```php
return [
'default' => 'dompurify-cli',

'drivers' => [
'dompurify-cli' => new DompurifyCliConfig(
node: env('NODE_PATH', 'node'), // @phpstan-ignore argument.type
npm: env('NPM_PATH', 'npm'), // @phpstan-ignore argument.type
binary: null,
tempFolder: null,
),

'dompurify-service' => new DompurifyServiceConfig(
node: env('NODE_PATH', 'node'), // @phpstan-ignore argument.type
npm: env('NPM_PATH', 'npm'), // @phpstan-ignore argument.type
host: '127.0.0.1',
port: 63000,
binary: null,
),
],
];
```

Run the following command (Not required by all drivers):

```shell
php artisan xssless:start
```

Use the facade:

```php
Medilies\Xssless\Laravel\Facades\Xssless::clean($html);
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [medilies](https://github.com/medilies)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.