fix(security): honour Pods field restrictions (audit M4)
PodsFieldSource maps hidden, read_only, admin_only and restrict_role/-capability like Pods' own view: hidden -> new status HIDDEN, read_only -> READONLY, restricted and not permitted -> READONLY with read_only_restricted, else HIDDEN. HIDDEN fields are not described, read out, queried or written (they look unknown) but stay in the schema for the media usage search; write() re-checks before Pods' save(). Required-but-restricted fields no longer block a create. Pods is not active on DDEV: covered by unit tests with a field-option fixture. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -141,4 +141,78 @@ final class PodsFieldSourceTest extends TestCase
|
||||
|
||||
(new PodsFieldSource(null, static fn () => $pod))->write(5, 'lit_person', ['role' => 'x'], []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Security audit M4: Pods field restrictions, as Pods' own view applies them (Pods::view):
|
||||
* hidden → hidden; read_only → readonly; restricted (admin_only, restrict_role/-capability)
|
||||
* and not permitted → readonly with read_only_restricted, else hidden.
|
||||
*
|
||||
* @param array<string, bool|null> $permitted Field name => pods_permission() (null = unrestricted).
|
||||
* @return array<string, string> Field name => status.
|
||||
*/
|
||||
private function statuses(array $permitted): array
|
||||
{
|
||||
$pod = Mockery::mock();
|
||||
$pod->allows('get_type')->andReturn('post_type');
|
||||
$pod->allows('get_fields')->andReturn([
|
||||
$this->field('open', 'text'),
|
||||
$this->field('internal', 'text', ['hidden' => '1']),
|
||||
$this->field('fixed', 'text', ['read_only' => 1]),
|
||||
$this->field('boss', 'text', ['admin_only' => '1']),
|
||||
$this->field('team', 'text', ['restrict_role' => '1', 'read_only_restricted' => '1']),
|
||||
$this->field('cap', 'number', ['restrict_capability' => '1']),
|
||||
]);
|
||||
$source = new PodsFieldSource(
|
||||
static fn (string $name) => $pod,
|
||||
null,
|
||||
static fn (object $field): ?bool => $permitted[$field->get_name()] ?? null
|
||||
);
|
||||
|
||||
$statuses = [];
|
||||
foreach ($source->fieldsFor('lit_person') as $definition) {
|
||||
$statuses[$definition->key] = $definition->status;
|
||||
}
|
||||
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
public function testFieldRestrictionsBecomeReadonlyOrHidden(): void
|
||||
{
|
||||
self::assertSame(
|
||||
[
|
||||
'open' => FieldDefinition::WRITABLE,
|
||||
'internal' => FieldDefinition::HIDDEN,
|
||||
'fixed' => FieldDefinition::READONLY,
|
||||
'boss' => FieldDefinition::HIDDEN,
|
||||
'team' => FieldDefinition::READONLY,
|
||||
'cap' => FieldDefinition::HIDDEN,
|
||||
],
|
||||
$this->statuses(['boss' => false, 'team' => false, 'cap' => false])
|
||||
);
|
||||
self::assertSame(
|
||||
['open', 'internal', 'fixed', 'boss', 'team', 'cap'],
|
||||
array_keys($this->statuses([])),
|
||||
'hidden fields stay in the schema (media usage search), only their status changes'
|
||||
);
|
||||
$allowed = $this->statuses(['boss' => true, 'team' => true, 'cap' => true]);
|
||||
self::assertSame(FieldDefinition::WRITABLE, $allowed['boss']);
|
||||
self::assertSame(FieldDefinition::WRITABLE, $allowed['team']);
|
||||
self::assertSame(FieldDefinition::HIDDEN, $allowed['internal'], 'hidden is hidden for everyone, as in Pods');
|
||||
}
|
||||
|
||||
/** Re-check in write(): a restricted field is never handed to Pods' save(), which skips its own checks. */
|
||||
public function testWriteRefusesRestrictedFields(): void
|
||||
{
|
||||
$pod = Mockery::mock();
|
||||
$pod->expects('save')->never();
|
||||
$definitions = [
|
||||
'role' => new FieldDefinition('role', 'Funktion', 'string', 'pods', FieldDefinition::WRITABLE),
|
||||
'boss' => new FieldDefinition('boss', 'Boss', 'string', 'pods', FieldDefinition::HIDDEN),
|
||||
];
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new PodsFieldSource(null, static fn () => $pod))
|
||||
->write(5, 'lit_person', ['role' => 'x', 'boss' => 'y'], $definitions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,11 +113,14 @@ final class PostPresenterTest extends TestCase
|
||||
$password = new FieldDefinition('secret', 'Secret', 'password', 'pods', 'writable');
|
||||
$protected = new FieldDefinition('_internal', 'Internal', 'string', 'meta', 'readonly');
|
||||
$visible = new FieldDefinition('role', 'Funktion', 'string', 'pods', 'writable');
|
||||
$restricted = new FieldDefinition('boss', 'Boss', 'string', 'pods', FieldDefinition::HIDDEN);
|
||||
$source = Mockery::mock(FieldSource::class);
|
||||
$source->expects('read')->once()->with(5, $visible)->andReturn('Leitung');
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('taxonomies')->andReturn([]);
|
||||
$schema->allows('fields')->andReturn(['secret' => $password, '_internal' => $protected, 'role' => $visible]);
|
||||
$schema->allows('fields')->andReturn([
|
||||
'secret' => $password, '_internal' => $protected, 'role' => $visible, 'boss' => $restricted,
|
||||
]);
|
||||
$schema->allows('sourceOf')->andReturn($source);
|
||||
$post = new WP_Post(['ID' => 5, 'post_type' => 'lit_person']);
|
||||
|
||||
|
||||
@@ -76,7 +76,10 @@ final class ContentSchemaTest extends TestCase
|
||||
(object) ['name' => 'post_format', 'show_ui' => false, 'hierarchical' => false, 'labels' => (object) ['name' => 'Formate']],
|
||||
]);
|
||||
$schema = new ContentSchema(
|
||||
[$this->source('meta', [new FieldDefinition('footnotes', 'footnotes', 'string', 'meta', 'writable')])],
|
||||
[$this->source('meta', [
|
||||
new FieldDefinition('footnotes', 'footnotes', 'string', 'meta', 'writable'),
|
||||
new FieldDefinition('zz_hidden', 'Verborgen', 'string', 'pods', FieldDefinition::HIDDEN),
|
||||
])],
|
||||
static fn (string $type): bool => $type === 'page'
|
||||
);
|
||||
|
||||
@@ -92,6 +95,7 @@ final class ContentSchemaTest extends TestCase
|
||||
self::assertSame([['name' => 'category', 'label' => 'Kategorien', 'hierarchical' => true]], $withoutFields['taxonomies']);
|
||||
self::assertArrayNotHasKey('fields', $withoutFields);
|
||||
self::assertSame('footnotes', $withFields['fields'][0]['key']);
|
||||
self::assertCount(1, $withFields['fields'], 'hidden (restricted) fields are not described (M4)');
|
||||
self::assertNull($schema->describe('et_pb_layout', false));
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ final class FieldValidatorTest extends TestCase
|
||||
'secret' => new FieldDefinition('_secret', '_secret', 'string', 'meta', 'readonly'),
|
||||
'city' => new FieldDefinition('city', 'city', 'unknown', 'sample', 'unknown'),
|
||||
'pin' => new FieldDefinition('pin', 'PIN', 'password', 'pods', 'writable'),
|
||||
'boss' => new FieldDefinition('boss', 'Boss', 'string', 'pods', FieldDefinition::HIDDEN),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -72,8 +73,10 @@ final class FieldValidatorTest extends TestCase
|
||||
'mentor' => 7,
|
||||
'photos' => 3,
|
||||
'start' => '01.10.2026',
|
||||
'boss' => 'x',
|
||||
], false)['errors'];
|
||||
|
||||
self::assertSame($errors['nope'], $errors['boss'], 'a hidden field looks like an unknown one (M4)');
|
||||
self::assertStringContainsString('lit-content-types', $errors['nope']);
|
||||
self::assertSame('Feld ist schreibgeschützt.', $errors['secret']);
|
||||
self::assertSame('Erlaubte Werte: junior, senior.', $errors['level']);
|
||||
@@ -123,4 +126,15 @@ final class FieldValidatorTest extends TestCase
|
||||
self::assertCount(1, $result['warnings']);
|
||||
self::assertStringContainsString('city', $result['warnings'][0]);
|
||||
}
|
||||
|
||||
/** M4: a required field the user cannot write (restricted Pods field) cannot block a create. */
|
||||
public function testRequiredButRestrictedFieldsAreNotDemanded(): void
|
||||
{
|
||||
$definitions = [
|
||||
'boss' => new FieldDefinition('boss', 'Boss', 'string', 'pods', FieldDefinition::HIDDEN, true),
|
||||
'fixed' => new FieldDefinition('fixed', 'Fest', 'string', 'pods', FieldDefinition::READONLY, true),
|
||||
];
|
||||
|
||||
self::assertSame([], (new FieldValidator())->validate($definitions, [], true)['errors']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user