Skip to content

Commit 7bc4d7d

Browse files
authored
3.x: security: Inconsistent scope enforcement for attach and associate action select fields (#19881)
1 parent e9f20ab commit 7bc4d7d

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

packages/tables/src/Actions/AssociateAction.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,24 @@ protected function setUp(): void
7474
/** @var HasMany | MorphMany $relationship */
7575
$relationship = Relation::noConstraints(fn () => $table->getRelationship());
7676

77-
$record = $relationship->getQuery()->find($data['recordId']);
77+
$relationshipQuery = $relationship->getQuery();
78+
79+
if ($this->modifyRecordSelectOptionsQueryUsing) {
80+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
81+
'query' => $relationshipQuery,
82+
'search' => null,
83+
]) ?? $relationshipQuery;
84+
}
85+
86+
$record = $relationshipQuery->find($data['recordId']);
7887

7988
foreach (($this->isMultiple ? $record : [$record]) as $record) {
80-
if ($record instanceof Model) {
81-
$this->record($record);
89+
if (! $record instanceof Model) {
90+
continue;
8291
}
8392

93+
$this->record($record);
94+
8495
/** @var BelongsTo $inverseRelationship */
8596
$inverseRelationship = $table->getInverseRelationshipFor($record);
8697

@@ -281,15 +292,35 @@ public function getRecordSelect(): Select
281292
->multiple($this->isMultiple())
282293
->searchable($this->getRecordSelectSearchColumns() ?? true)
283294
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(optionsLimit: $component->getOptionsLimit(), search: $search, searchColumns: $component->getSearchColumns()))
284-
->getOptionLabelUsing(function ($value) use ($table): string {
295+
->getOptionLabelUsing(function ($value) use ($table): ?string {
285296
$relationship = Relation::noConstraints(fn () => $table->getRelationship());
286297

287-
return $this->getRecordTitle($relationship->getQuery()->find($value));
298+
$relationshipQuery = $relationship->getQuery();
299+
300+
if ($this->modifyRecordSelectOptionsQueryUsing) {
301+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
302+
'query' => $relationshipQuery,
303+
'search' => null,
304+
]) ?? $relationshipQuery;
305+
}
306+
307+
$record = $relationshipQuery->find($value);
308+
309+
return $record ? $this->getRecordTitle($record) : null;
288310
})
289311
->getOptionLabelsUsing(function (array $values) use ($table): array {
290312
$relationship = Relation::noConstraints(fn () => $table->getRelationship());
291313

292-
return $relationship->getQuery()->find($values)
314+
$relationshipQuery = $relationship->getQuery();
315+
316+
if ($this->modifyRecordSelectOptionsQueryUsing) {
317+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
318+
'query' => $relationshipQuery,
319+
'search' => null,
320+
]) ?? $relationshipQuery;
321+
}
322+
323+
return $relationshipQuery->find($values)
293324
->mapWithKeys(fn (Model $record): array => [$record->getKey() => $this->getRecordTitle($record)])
294325
->all();
295326
})

packages/tables/src/Actions/AttachAction.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ protected function setUp(): void
7676

7777
$relationshipQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($relationship);
7878

79+
if ($this->modifyRecordSelectOptionsQueryUsing) {
80+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
81+
'query' => $relationshipQuery,
82+
'search' => null,
83+
]) ?? $relationshipQuery;
84+
}
85+
7986
$isMultiple = is_array($data['recordId']);
8087

8188
$record = $relationshipQuery
@@ -287,18 +294,34 @@ public function getRecordSelect(): Select
287294
->multiple($this->isMultiple())
288295
->searchable($this->getRecordSelectSearchColumns() ?? true)
289296
->getSearchResultsUsing(static fn (Select $component, string $search): array => $getOptions(optionsLimit: $component->getOptionsLimit(), search: $search, searchColumns: $component->getSearchColumns()))
290-
->getOptionLabelUsing(function ($value) use ($table): string {
297+
->getOptionLabelUsing(function ($value) use ($table): ?string {
291298
$relationship = Relation::noConstraints(fn () => $table->getRelationship());
292299

293300
$relationshipQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($relationship);
294301

295-
return $this->getRecordTitle($relationshipQuery->find($value));
302+
if ($this->modifyRecordSelectOptionsQueryUsing) {
303+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
304+
'query' => $relationshipQuery,
305+
'search' => null,
306+
]) ?? $relationshipQuery;
307+
}
308+
309+
$record = $relationshipQuery->find($value);
310+
311+
return $record ? $this->getRecordTitle($record) : null;
296312
})
297313
->getOptionLabelsUsing(function (array $values) use ($table): array {
298314
$relationship = Relation::noConstraints(fn () => $table->getRelationship());
299315

300316
$relationshipQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($relationship);
301317

318+
if ($this->modifyRecordSelectOptionsQueryUsing) {
319+
$relationshipQuery = $this->evaluate($this->modifyRecordSelectOptionsQueryUsing, [
320+
'query' => $relationshipQuery,
321+
'search' => null,
322+
]) ?? $relationshipQuery;
323+
}
324+
302325
return $relationshipQuery->find($values)
303326
->mapWithKeys(fn (Model $record): array => [$record->getKey() => $this->getRecordTitle($record)])
304327
->all();

0 commit comments

Comments
 (0)