Skip to content

Commit c0bfbe2

Browse files
authored
Merge pull request #750 from code16/secured-embeds
Embed class validation
2 parents aa3e58c + dfafb44 commit c0bfbe2

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/Http/Controllers/Api/Embeds/HandlesEmbed.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ trait HandlesEmbed
99
{
1010
protected function getEmbedFromKey(string $embedKey): SharpFormEditorEmbed
1111
{
12-
$embed = app(Str::replace('.', '\\', $embedKey));
12+
$embedClass = Str::replace('.', '\\', $embedKey);
13+
14+
if (! is_a($embedClass, SharpFormEditorEmbed::class, true)) {
15+
throw new \Exception("Embed class $embedClass is not a SharpFormEditorEmbed");
16+
}
17+
18+
$embed = app($embedClass);
1319
$embed->buildEmbedConfig();
1420

1521
return $embed;

tests/Http/Api/Embeds/ApiEmbedsFormControllerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Code16\Sharp\Auth\SharpEntityPolicy;
44
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
5+
use Code16\Sharp\Tests\Fixtures\User;
56
use Code16\Sharp\Tests\Http\Api\Embeds\Fixtures\ApiEmbedsFormControllerTestEmbed;
67
use Illuminate\Support\Str;
78

@@ -191,3 +192,37 @@ public function view($user, $instanceId): bool
191192
)
192193
->assertJsonValidationErrorFor('name');
193194
});
195+
196+
it('fails if class is not an embed', function () {
197+
$this->withoutExceptionHandling();
198+
199+
expect(function () {
200+
$this
201+
->postJson(
202+
route('code16.sharp.api.embed.instance.form.show', [
203+
'embedKey' => str(User::class)->replace('\\', '.'),
204+
'entityKey' => 'person',
205+
'instanceId' => 1,
206+
]),
207+
[
208+
'name' => 'aaa',
209+
]
210+
);
211+
})->toThrow(Exception::class, sprintf('Embed class %s is not a SharpFormEditorEmbed', User::class));
212+
213+
expect(function () {
214+
$this
215+
->postJson(
216+
route('code16.sharp.api.embed.instance.form.update', [
217+
'embedKey' => str(User::class)->replace('\\', '.'),
218+
'entityKey' => 'person',
219+
'instanceId' => 1,
220+
]),
221+
[
222+
'name' => 'aaa',
223+
'bio' => ['text' => 'aaa'],
224+
]
225+
);
226+
})->toThrow(Exception::class, sprintf('Embed class %s is not a SharpFormEditorEmbed', User::class));
227+
228+
});

0 commit comments

Comments
 (0)