redaelfillali/laravel-secure-model
Composer 安装命令:
composer require redaelfillali/laravel-secure-model
包简介
Eloquent base model with auto-sanitized getters and setters.
README 文档
README
An Eloquent base model that automatically sanitizes specified attributes on get and set, protecting your application from XSS vulnerabilities out of the box.
Features
- Automatically purifies HTML on both read (
getAttribute) and write (setAttribute) - Powered by stevebauman/purify (HTMLPurifier wrapper)
- Zero-configuration: just list the attributes to sanitize
- Supports Laravel 9, 10, 11, 12, and 13
- Supports PHP 8.1, 8.2, 8.3, and 8.4
Requirements
| Dependency | Version |
|---|---|
| PHP | ^8.1 | ^8.2 | ^8.3 | ^8.4 |
| Laravel | ^9.0 | ^10.0 | ^11.0 | ^12.0 | ^13.0 |
| stevebauman/purify | ^6.3 |
Installation
composer require redaelfillali/laravel-secure-model
The service provider is registered automatically via Laravel's package auto-discovery.
Optionally publish the Purify configuration to customise the HTML rules:
php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"
Usage
Extend SecureModel instead of the default Eloquent Model and declare the attributes you want automatically sanitized in the $sanitizeAttributes array:
<?php use Redaelfillali\LaravelSecureModel\SecureModel; class Post extends SecureModel { // These attributes will be purified on every get and set protected array $sanitizeAttributes = ['title', 'body', 'excerpt']; }
That's it — any XSS payloads stored in or read from the listed attributes will be stripped automatically:
$post = new Post(); $post->body = '<p>Hello</p><script>alert("xss")</script>'; // The <script> tag is stripped; safe HTML is preserved. echo $post->body; // <p>Hello</p>
Attributes not listed in $sanitizeAttributes are left completely untouched, so only the fields you care about are affected.
How it works
SecureModel overrides two Eloquent methods:
| Method | Behaviour |
|---|---|
setAttribute($key, $value) |
Sanitizes the value before it is stored in the model's attribute bag |
getAttribute($key) |
Sanitizes the value when it is retrieved from the model |
Only attributes listed in $sanitizeAttributes and whose value is a string are passed through Purify::clean(). All other types (int, null, arrays …) are returned as-is.
Testing
composer test
Tests are written with Pest and use Orchestra Testbench for a full in-process Laravel environment.
License
The MIT License (MIT). See LICENSE for details.
redaelfillali/laravel-secure-model 适用场景与选型建议
redaelfillali/laravel-secure-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 351 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 redaelfillali/laravel-secure-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 redaelfillali/laravel-secure-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 351
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-22