[13.x] Add RawTemplate for inline Blade components

Merged
JU
8d ago
+268-4

Inline components make it easy to accidentally introduce remote code execution. Take this component:

public function render(): string
{
    return $this->user->firstname . ' ' . $this->user->lastname;
}

It could look like it just renders the user's name, but because the string isn't a view name, Blade compiles it as a template. So if someone sets their first name to {{ exec('whoami') }}, it gets executed. We ran into this ourselves when an AI security tool flagged it in one of our projects.

This PR adds a RawTemplate class that components can return instead:

public function render(): RawTemplate
{
    return new RawTemplate($this->user->firstname . ' ' . $this->user->lastname);
}

Like with DB::raw(), my hope is that the name makes people stop and think about what they're passing in. The constructor takes a literal-string, so static analysis tools will complain about the code above.

It also gives the docs a place for a clear warning. Right now "Inline Component Views" doesn't mention the risk at all.

This PR is backwards compatible. If it gets merged, I intend to submit a PR to 14.x that makes strings returned from render() only resolve to views.