Saving and displaying safely
The HTML a rich text editor produces is user input. Sanitise it on the server when you save it, and again when you output it. The editor cleans content in the browser as well, but anyone can post to your endpoint without going through the editor — the server is the only place you can trust.
With Laravel
On save: the SanitizedHtml cast
use OhashiEditor\Laravel\Casts\SanitizedHtml;
class Post extends Model
{
protected function casts(): array
{
return ['body' => SanitizedHtml::class];
}
}
Whatever is assigned to body is sanitised before it reaches the database.
null stays null.
On output: <x-ohashi-content>
<x-ohashi-content :html="$post->body" class="prose" />
<!-- renders -->
<div class="orte-rendered-content prose">…sanitised HTML…</div>
It sanitises every time, whatever is stored: rows saved before you added the
cast, or written to the database directly, are covered too. The wrapper has no
styles of its own, so your typography (for example Tailwind's prose) applies.
Do not write {!! $post->body !!}. Use <x-ohashi-content>.
Limit the length in your form request
Input over 2 MiB (configurable) is rejected with an exception rather than cut short — cutting would save part of the text and silently lose the rest. Validate the length first so users get a proper message:
public function rules(): array
{
return [
// Laravel's max counts characters. Japanese text is about 3 bytes per
// character, so stay well below the byte limit.
'body' => ['required', 'string', 'max:300000'],
];
}
To change the limit:
OHASHI_EDITOR_MAX_INPUT_BYTES=4194304
Without Laravel
The sanitiser has no Laravel dependency:
use OhashiEditor\HtmlSanitizer;
use OhashiEditor\Exception\SanitizationException;
$sanitizer = new HtmlSanitizer();
try {
$clean = $sanitizer->sanitize($_POST['body'] ?? '');
} catch (SanitizationException $e) {
// $e->errorCode() === OhashiEditor\ErrorCode::InputTooLong (OE-SAN-3002)
}
echo '<div class="orte-rendered-content">' . $sanitizer->sanitize($storedHtml) . '</div>';
What is kept
The sanitiser keeps what the editor can produce and removes everything else.
| Kept | Elements |
|---|---|
| Text | p br h2 h3 h4 strong b em i s del u code pre blockquote hr |
| Lists | ul ol li |
| Links | a with href (http, https, mailto, tel, relative) and target="_blank" only. Every link gets rel="noopener noreferrer" |
| Images | img with src (http, https, relative, or PNG/JPEG/GIF/WebP/AVIF data URLs), alt, width (number or percentage) |
| Tables | table thead tbody tr th td with small colspan / rowspan |
- Wrappers such as
<span>,<div>and<font>are removed but their text is kept. <script>,<style>,<iframe>,<svg>, forms and similar elements are removed with their content, as are non-standard elements.style,class,idand every event handler attribute (onclick, …) are removed.