vpremiss / livewire-nonceable
需要Livewire的安全公开方法!
Requires
- php: ^8.2
- laravel/framework: ^11.0
- livewire/livewire: ^3.0
- nesbot/carbon: ^3.4
- predis/predis: ^2.0
- spatie/laravel-package-tools: ^1.16
- vpremiss/crafty: ^4.9.8
Requires (Dev)
- larastan/larastan: ^2.9
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- pestphp/pest-plugin-livewire: ^2.1
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
بسم الله الرحمن الرحيمLivewire Nonceable
需要Livewire的安全公开方法!
描述
这个包的目的是为了解决Livewire当前的一个弱点,即强迫开发者将某些方法公开为 public
,以便前端(AlpineJS)能够与其通信;参数包括...
如果你问,为什么不在Livewire组件中只做敏感、受保护的事情,为什么要一开始就公开它们?WELL,你试试使用 Sanctum,看看会发生什么!攻击被 auth:sanctum
中间件保护的API是 不可能的。唯一的办法是使用 axios
在 TALL 视图中认证Sanctum后与API通信。
这种 来回 的操作会让你思考如何保护这些 public
方法,防止客户端轻松地访问它们(即DDOS攻击)。我们得出结论,解决方案是将 NONCE
放入Laravel的 缓存 而不是会话中——因为Livewire使用持久化方法,并且需要能够从不同地方访问它。是的,nonce缓存键有一个唯一的标识符;这是所需接口的一部分。
感谢你来到我的——抱歉。享受这个包和像优秀铁匠工艺一样令人惊叹的堆叠!
安装
-
当然,确保已经安装了 Livewire。
-
确保缓存设置正确并准备好使用。 Memcached 很酷!
-
通过 composer 安装该包
composer require vpremiss/livewire-nonceable
-
使用以下 Artisan 命令发布配置文件
php artisan vendor:publish --tag="livewire-nonceable-config"
用法
-
在你的Livewire组件中实现我们的 Noncing 接口及其方法。然后应用 Nonceable 特性。
use Livewire\Component; use VPremiss\LivewireNonceable\Interfaces\Noncing; use VPremiss\LivewireNonceable\Traits\Nonceable; class FuzzySearch extends Component implements Noncing { use Nonceable; public function getNonces(): array { return [ 'complex-searching' => 5, // the nonce title, plus 5 seconds lasting in cache // 'heavy-processing' => 10, as another example ]; } public function getNonceUniqueId(): string { return (string)auth()->user()->id; // After ensuring authentication, of course! } public function getNonceValidation(): bool { return auth()->user()->id === $this->getNonceUniqueId(); } // This method is initiated securely protected function validateSearch($query) { // $validatedQuery = some validations $nonce = $this->generateNonce('complex-searching'); $this->dispatch( 'searching-began', // receive in the front-end (SPA) query: $validatedQuery, nonce: $nonce, ); } // This is hit back from AlpineJS using axios public function complexSearch($responseFromApi, $nonce) { // Approach 1 // Or use the opposite ! $this->doesNonceExist($title, $nonce) method if ($this->isNonceSense('complex-searching', $nonce)) { // throw new NoncenseException('Nonce mismatch. Somebody is playing around!'); } $this->deleteNonce('complex-searching', $nonce); // Approach 2 if (!$this->validatedNonce('complex-searching', $nonce)) { // throw or whatever but that's all, since it would have deleted the nonce otherwise } // do the complex searching now with an ease of mind... } }
-
您还可以从视图中使用这两个检查方法
<div x-on:searching-began.window="callApi($event.detail.query, $event.detail.nonce)" x-data='{ callApi(query, nonce) { // or the opposite ! $wire.isNonceSense(title, nonce) if ($wire.doesNonceExist("complex-searching", nonce)) { axios .post( "{{ "some-route" }}", { query: query, nonce: nonce, }, ) .then(response => $wire.complexSearch(response, nonce)) .catch(error => console.error(error)); } }, }' > {{-- ... --}} </div>
再次强调:我们 不能 绕过不将 complexSearch 方法公开,因为我们需要从Sanctum允许调用受保护路由的唯一地方调用它:前端...
如果您找到了更好的处理方法,请在 讨论 部分告诉我们。
API
以下是 LivewireNonceable
包提供的关键方法及其描述的表格
包开发
- 在 [
TestCase
] 文件中将localTimezone
修改为您的时区。
变更日志
您可以通过WTD在线查看该软件包的变更日志。
进度
支持
通过赞助或一次性捐赠支持软件包的持续维护以及其他项目的开发,如果您愿意的话。
愿真主接受您的努力;阿米乃。
许可协议
此软件包是开源软件,采用MIT许可协议。
鸣谢
- ChatGPT
- Graphite
- Laravel
- Livewire
- Spatie
- BeyondCode
- 贡献者
- 以及本软件包依赖的所有后端软件包和服务...
- 以及在我们整个旅程中向我们提供支持和我们从他们那里学到知识的慷慨的个人...
万物非主,唯有真主,穆罕默德是安拉的使者。