vpremiss / livewire-nonceable

需要Livewire的安全公开方法!

v1.4.0 2024-05-30 04:03 UTC

README

بسم الله الرحمن الرحيم

Livewire Nonceable

需要Livewire的安全公开方法!

Latest Version on Packagist GitHub Tests Action Status Codecov Total Downloads

描述

这个包的目的是为了解决Livewire当前的一个弱点,即强迫开发者将某些方法公开为 public,以便前端(AlpineJS)能够与其通信;参数包括...

如果你问,为什么不在Livewire组件中只做敏感、受保护的事情,为什么要一开始就公开它们?WELL,你试试使用 Sanctum,看看会发生什么!攻击被 auth:sanctum 中间件保护的API是 不可能的。唯一的办法是使用 axiosTALL 视图中认证Sanctum后与API通信。

这种 来回 的操作会让你思考如何保护这些 public 方法,防止客户端轻松地访问它们(即DDOS攻击)。我们得出结论,解决方案是将 NONCE 放入Laravel的 缓存 而不是会话中——因为Livewire使用持久化方法,并且需要能够从不同地方访问它。是的,nonce缓存键有一个唯一的标识符;这是所需接口的一部分。

感谢你来到我的——抱歉。享受这个包和像优秀铁匠工艺一样令人惊叹的堆叠!

安装

  1. 当然,确保已经安装了 Livewire

  2. 确保缓存设置正确并准备好使用。 Memcached 很酷!

  3. 通过 composer 安装该包

    composer require vpremiss/livewire-nonceable
  4. 使用以下 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许可协议

鸣谢


万物非主,唯有真主,穆罕默德是安拉的使者。