Skip to content

Commit e3f3cd7

Browse files
committed
Add ability to whitelist edited class for security
1 parent b9f8274 commit e3f3cd7

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

client/js/ManyField.src.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
$(elem).find('.manyfield__row').each(function (r, row) {
1313
if (canRemove) {
1414
if (!$(row).find('.manyfield__remove').length) {
15-
$(row).prepend('<a class="btn btn-sm btn-danger manyfield__remove"><i class="fa fa-times"></i></a>');
15+
var href = $(this).data('inline-save').replace('saveRecord', 'deleteRecord');
16+
17+
href = href + '?ID='+ $(row).find('[name=ID]').val();
18+
19+
$(row).prepend('<a class="btn btn-sm btn-danger manyfield__remove" href="' + href + '"><i class="fa fa-times"></i></a>');
1620
}
1721
} else {
1822
field.find('.manyfield__remove').remove();

src/ManyField.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class ManyField extends CompositeField
7373
*/
7474
protected $ajaxUrl = false;
7575

76+
/**
77+
* @var string
78+
*/
79+
protected $manyFieldDataClass;
80+
7681
/**
7782
* Does creating a new row automatically call write to the database?
7883
*
@@ -366,6 +371,10 @@ public function saveRecord()
366371
$index = Controller::curr()->getRequest()->requestVar('ID');
367372
$class = Controller::curr()->getRequest()->requestVar('ClassName');
368373

374+
if (!$class) {
375+
$class = $this->manyFieldDataClass;
376+
}
377+
369378
if (!$class && $this->value) {
370379
$class = $this->value->dataClass();
371380
}
@@ -374,6 +383,10 @@ public function saveRecord()
374383
throw new Exception('saveRecord() must be passed an ID and ClassName');
375384
}
376385

386+
if ($this->manyFieldDataClass && $class !== $this->manyFieldDataClass) {
387+
throw new Exception('Invalid ClassName passed');
388+
}
389+
377390
$record = $class::get()->byId($index);
378391

379392
if (!$record || !$record->canEdit()) {
@@ -405,14 +418,22 @@ public function recordForm()
405418
$index = Controller::curr()->getRequest()->getVar('RecordID');
406419
$class = Controller::curr()->getRequest()->getVar('ClassName');
407420

421+
if (!$class) {
422+
$class = $this->manyFieldDataClass;
423+
}
424+
408425
if (!$index || !$class) {
409426
throw new Exception('recordForm() must be passed an RecordID and ClassName');
410427
}
411428

429+
if ($this->manyFieldDataClass && $class !== $this->manyFieldDataClass) {
430+
throw new Exception('Invalid ClassName passed');
431+
}
432+
412433
$record = $class::get()->byId($index);
413434

414435
if (!$record || !$record->canEdit()) {
415-
return Controller::curr()->httpError(400);
436+
return Controller::curr()->httpError(404);
416437
}
417438

418439
$response = new HTTPResponse();
@@ -439,10 +460,18 @@ public function deleteRecord()
439460
$index = Controller::curr()->getRequest()->getVar('ID');
440461
$class = Controller::curr()->getRequest()->getVar('ClassName');
441462

463+
if (!$class) {
464+
$class = $this->manyFieldDataClass;
465+
}
466+
442467
if (!$index || !$class) {
443468
throw new Exception('deleteRecord() must be passed an ID and ClassName');
444469
}
445470

471+
if ($this->manyFieldDataClass && $class !== $this->manyFieldDataClass) {
472+
throw new Exception('Invalid ClassName passed');
473+
}
474+
446475
$record = $class::get()->byId($index);
447476

448477
if (!$record || !$record->canDelete()) {
@@ -454,6 +483,18 @@ public function deleteRecord()
454483
return $this->forTemplate();
455484
}
456485

486+
/**
487+
* @param string
488+
*
489+
* @return self
490+
*/
491+
public function setDataClass($class)
492+
{
493+
$this->manyFieldDataClass = $class;
494+
495+
return $this;
496+
}
497+
457498
/**
458499
* Add URL
459500
*
@@ -634,8 +675,6 @@ public function updateRelation(DataObjectInterface $record, $delete = true)
634675
}
635676

636677
$existing = $record->{$this->name}();
637-
$removed = [];
638-
639678
// if no value then we should clear everything out
640679
if (!$this->value && $this->canRemove) {
641680
if ($delete) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<$Tag $AttributesHTML>
2+
<% if $Tag == 'fieldset' && $Legend %>
3+
<legend>$Legend</legend>
4+
<% end_if %>
5+
6+
$Field
7+
</$Tag>

0 commit comments

Comments
 (0)