Control frontend access to properties/methods in Livewire using PHP 8 attributes.

Overview

Livewire Access

This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as frontend-accessible.

The package ships with two pairs of traits and attributes. One for explicit access, and one for implicit access.

How it works

  • Components which implement the trait for explicit access will deny access to all properties and methods if they don't have the #[FrontendAccess] attribute.
  • Components which implement the trait for implicit access will allow access to all properties and methods unless they have the #[BlockFrontendAccess] attribute.

This acts as a layer on top of Livewire's public-check logic, but gives you much more fine grained control.

Why use this?

Sometimes, you may want allow access to a component's property in PHP — outside the component — while not allowing access from the frontend. For that, you can use the WithImplicitAccess trait. Frontend access will be enabled for all properties by default, but you can disable it for a specific property (or method).

Other times, you may simply want more assurance than Livewire provides out of the box. The WithExplicitAccess trait is made for that. It disables all frontend access, and requires you to manually enable it on specific properties/methods.

The second option is recommended, because it provides the most security benefits. Accidentally making methods public is common, and it can cause security issues. Disabling implicit access can be especially useful on teams with junior engineers who don't yet have a full understanding of Livewire's internals, but can be very productive with it.

Practical use case

Say you have a component with the following method:

public function getItemsProperty()
{
    return [
      ['secret' => false, 'name' => 'Item 1'],
      ['secret' => true, 'name' => 'Item 2'],
      ['secret' => true, 'name' => 'Item 3'],
      ['secret' => false, 'name' => 'Item 4'],
    ];
}

In the Blade template, you want to loop through the items and only display the non-secret ones.

@foreach($this->items->filter(...) as $item)

However, the entire dataset will be accessible from the frontend, even if you're not rendering any of the secret items.

The user can easily fetch the Livewire component in Developer Tools and make a call like this:

component.call('getItemsProperty');

It will return all of the data returned by the getItemsProperty() method in PHP.

Screen Shot 2021-03-17 at 21 53 00

You may think that in this case, you should just make the method protected/private. However, that would make it inaccessible from the Blade template. Even though Livewire uses $this in the template, it's accessing the object from the outside.

Which means that although Blade templates are completely server-rendered, and let you access any PHP code in a secure way, you cannot access many of the properties or methods of Livewire components without making them public, which can cause unexpected data leaks.

With this package, you can keep the property public and access it anywhere in PHP, while completely blocking any attempts at accessing it from the frontend.

Installation

PHP 8 is required.

composer require leanadmin/livewire-access

Usage

This package doesn't make any changes to your existing code. Components which don't implement either one of its traits will not be affected.

Explicit access

To enable the explicit access mode, i.e. only enable access to properties/methods that explicitly allow it, use the WithExplicitAccess trait.

use Livewire\Component;
use Lean\LivewireAccess\WithExplicitAccess;
use Lean\LivewireAccess\FrontendAccess;

class MyComponent extends Component
{
    // Use the trait on your component to enable this functionality
    use WithExplicitAccess;

    // Accessing this from the frontend will throw an exception
    public string $inaccessible;

    #[FrontendAccess]
    public string $accessible; // This property allows frontend access

    public function secretMethod()
    {
        // Calling this from the frontend will throw an exception
    }

    #[FrontendAccess]
    public function publicMethod()
    {
        // This method allows frontend access
    }
}

Implicit access

To enable the implicit access mode, i.e. keep using the same mode , use the WithExplicitAccess trait.

use Livewire\Component;
use Lean\LivewireAccess\WithImplicitAccess;
use Lean\LivewireAccess\BlockFrontendAccess;

class MyComponent extends Component
{
    // Use the trait on your component to enable this functionality
    use WithImplicitAccess;

    // This property allows frontend access
    public string $accessible;

    #[BlockFrontendAccess]
    public string $inaccessible; // This property blocks frontend access

    public function publicMethod()
    {
        // This method allows frontend access
    }

    #[BlockFrontendAccess]
    public function secretMethod()
    {
        // This method blocks frontend access
    }
}

Details

  • The properties still need to be public to be accessible.
  • The thrown exceptions are identical to those that Livewire would throw if the properties/methods were not public.

Development

Running all checks locally:

./check

Running tests:

phpunit

Code style will be automatically fixed by php-cs-fixer.

You might also like...
Guess attributes for Laravel model factories

Eloquent Populator This package provides default attributes for Laravel model factories by guessing the best Faker formatters from columns' names and

Make your own custom cast type for Laravel model attributes

Laravel Custom Casts Make your own cast type for Laravel model attributes Laravel custom casts works similarly to Eloquent attribute casting, but with

Generate previous attributes when saving Eloquent models
Generate previous attributes when saving Eloquent models

This package provides a trait that will generate previous attributes when saving any Eloquent model.

Provide all attributes (including irregular patterns) to Laravel Blade class components.

blade-wants-attributes blade-wants-attributes offers you the ability to use Blade/HTML-defined attributes within the constructors of Laravel Blade cla

Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.
Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.

bastinald/ux Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire. Features Automatic migrations Automatic routing Automatic passwo

⚡ PowerGrid generates Advanced Datatables using Laravel Livewire.
⚡ PowerGrid generates Advanced Datatables using Laravel Livewire.

📚 Documentation | 🔥 Features | ⌨️ Get started Livewire ⚡ PowerGrid ⚡ PowerGrid creates modern, powerful and easy to customize Datatables based on La

Simple CRUD + Search using Laravel 8 and Livewire 2

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

A Laravel starter kit with auth scaffolding using Livewire & Bootstrap.

Zephyr This package is a Laravel starter kit with auth scaffolding using Livewire & Bootstrap. It was created for people who prefer using Livewire & B

Quickly identify controller methods with no route in your Laravel applications.
Quickly identify controller methods with no route in your Laravel applications.

Orphan Controller Quickly identify controller methods with no route in your Laravel applications. Installation You can install the package via Compose

Comments
  • Add support for Laravel 9 and phpcsfixer

    Add support for Laravel 9 and phpcsfixer

    This package does not use Laravel components except one for dev orchestra/testbench. Livewire now supports Laravel 9 so our package. Other than that added the phpcsfixer, check the script, and simplify the CI file.

    opened by abrardev99 3
  • Allow access to all methods defined in the Livewire namespace

    Allow access to all methods defined in the Livewire namespace

    Currently there's an issue with pagination, because methods like gotoPage() are defined in a LW trait, and there's no way we can add #[FrontendAccess] there.

    So a possible solution is to check if a method was defined anywhere in Livewire\ and if so, allow all calls to it.

    opened by stancl 0
  • Prevent all frontend access to explicit properties

    Prevent all frontend access to explicit properties

    First of all, I love your work, it's been a lifesaver.

    However, I have a little issue. Currently, setting a component with WithExplicitAccess prevents public methods from being called and public properties from being modified in the frontend.

    However, what if you want to prevent the public properties from being accessed by the frontend whatsoever. That is Livewire.first().myProperty should not return the value of myProperty whatsoever, but you still want Blade to be able to securely access the property?

    opened by titonova 7
Releases(v0.1.2)
  • v0.1.2(Feb 8, 2022)

    What's Changed

    • Add support for Laravel 9 and phpcsfixer by @abrardev99 in https://github.com/archtechx/livewire-access/pull/2

    New Contributors

    • @abrardev99 made their first contribution in https://github.com/archtechx/livewire-access/pull/2

    Full Changelog: https://github.com/archtechx/livewire-access/compare/v0.1.1...v0.1.2

    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Apr 25, 2021)

  • v0.1.0(Mar 17, 2021)

Owner
ARCHTECH
Meticulously architected web applications.
ARCHTECH
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
A dynamic table component for Laravel Livewire - For Slack access, visit:

A dynamic Laravel Livewire component for data tables. Bootstrap 4 Demo | Bootstrap 5 Demo | Tailwind Demo | Demo Repository Installation You can insta

Anthony Rappa 1.3k Jan 1, 2023
Laravel-comments-livewire - Livewire components for the laravel-comments package

Associate comments and reactions with Eloquent models This package contains Livewire components to be used with the spatie/laravel-comments package. S

Spatie 15 Jan 18, 2022
🔌 Autowire and configure using PHP 8 Attributes in Laravel.

?? Autowire for Laravel Autowire and configure using PHP 8 Attributes in Laravel. Installation Via Composer composer require jeroen-g/autowire You wil

JeroenG 13 Oct 7, 2022
🕵️ Inspect Laravel Eloquent models to collect properties, relationships and more.

??️ Eloquent Inspector Inspect Laravel Eloquent models to collect properties, relationships and more. Install Via Composer composer require cerbero/el

Andrea Marco Sartori 111 Nov 4, 2022
PcTools is a proyect developed using {JavaScript,HTML5,CSS} for frontend and {PHP => Mysql} for backend.

PcTools-Proyect PcTools is a proyect developed using {JavaScript,HTML5,CSS} for frontend and {PHP => Mysql} for backend. Future Improvements # Replace

Ihab Fallahy 1 Feb 5, 2022
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Adnane Kadri 49 Jun 22, 2022
A simple pure PHP RADIUS client supporting Standard and Vendor-Specific Attributes in single file

BlockBox-Radius A simple pure PHP RADIUS client supporting Standard and Vendor-Specific Attributes in single file Author: Daren Yeh [email protected]

null 2 Oct 2, 2022
Easily validate data attributes through a remote request

Laravel Remote Rule Easily validate data attributes through a remote request. This package allows you to define a subset of custom rules to validate a

H-FARM Innovation 27 Nov 20, 2022
Cast your Eloquent model attributes to Value Objects with ease.

Laravel Value Objects Cast your Eloquent model attributes to value objects with ease! Requirements This package requires PHP >= 5.4. Using the latest

Red Crystal Code 23 Dec 30, 2022