Skip to content

Commit 98c5799

Browse files
committed
check paths for fragments
1 parent 96977f2 commit 98c5799

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎src/ComponentResolver.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ public function resolve(string $alias, array $paths): ?string
2323
foreach ($paths as $path) {
2424
if (File::exists($possiblePath = $path.'/'.str_replace('.', '/', $alias).'.blade.php')) {
2525
return $this->extractComponentClass($alias, realpath($possiblePath));
26-
} elseif (is_array($component = FragmentAlias::decode($alias))) {
27-
return $this->extractComponentClass($alias, realpath($component['path']));
26+
}
27+
}
28+
29+
if (is_array($component = FragmentAlias::decode($alias)) &&
30+
($componentPath = realpath($component['path'])) !== false) {
31+
foreach (array_merge($paths, config('view.paths', [])) as $path) {
32+
if (($path = realpath($path)) !== false &&
33+
str_starts_with($componentPath, $path.DIRECTORY_SEPARATOR)) {
34+
return $this->extractComponentClass($alias, $componentPath);
35+
}
2836
}
2937
}
3038

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Livewire\Volt\ComponentFactory;
4+
use Livewire\Volt\ComponentResolver;
5+
use Livewire\Volt\FragmentAlias;
6+
7+
it('resolves fragment paths within a mounted directory', function () {
8+
$path = __DIR__.'/../Feature/resources/views/functional-api-pages/page-with-fragment.blade.php';
9+
$alias = FragmentAlias::encode('fragment', $path);
10+
11+
$factory = Mockery::mock(ComponentFactory::class);
12+
$factory->shouldReceive('make')
13+
->once()
14+
->with($alias, realpath($path))
15+
->andReturn('ResolvedComponent');
16+
17+
expect((new ComponentResolver($factory))->resolve($alias, [dirname($path)]))
18+
->toBe('ResolvedComponent');
19+
});
20+
21+
it('resolves fragment paths within a configured view directory', function () {
22+
$path = __DIR__.'/../Feature/resources/views/functional-api-pages/page-with-fragment.blade.php';
23+
$alias = FragmentAlias::encode('fragment', $path);
24+
25+
config()->set('view.paths', [dirname($path)]);
26+
27+
$factory = Mockery::mock(ComponentFactory::class);
28+
$factory->shouldReceive('make')
29+
->once()
30+
->with($alias, realpath($path))
31+
->andReturn('ResolvedComponent');
32+
33+
expect((new ComponentResolver($factory))->resolve($alias, []))
34+
->toBe('ResolvedComponent');
35+
});
36+
37+
it('does not resolve fragment paths outside the trusted directories', function () {
38+
$factory = Mockery::mock(ComponentFactory::class);
39+
$factory->shouldNotReceive('make');
40+
41+
$alias = FragmentAlias::encode('proof', __FILE__);
42+
43+
config()->set('view.paths', [
44+
__DIR__.'/../Feature/resources/views',
45+
]);
46+
47+
expect((new ComponentResolver($factory))->resolve($alias, [
48+
__DIR__.'/../Feature/resources/views/functional-api-pages',
49+
]))->toBeNull();
50+
});

0 commit comments

Comments
 (0)