TenantAtlas/tests/Feature/Intune/PolicySnapshotRedactionTest.php
ahmido cd811cff4f Spec 120: harden secret redaction integrity (#146)
## Summary
- replace broad substring-based masking with a shared exact/path-based secret classifier and workspace-scoped fingerprint hashing
- persist protected snapshot metadata on `policy_versions` and keep secret-only changes visible in compare, drift, restore, review, verification, and ops surfaces
- add Spec 120 artifacts, audit documentation, and focused Pest regression coverage for snapshot, audit, verification, review-pack, and notification behavior

## Validation
- `vendor/bin/sail artisan test --compact tests/Feature/Intune/PolicySnapshotRedactionTest.php tests/Feature/Intune/PolicySnapshotFingerprintIsolationTest.php tests/Feature/ReviewPack/ReviewPackRedactionIntegrityTest.php tests/Feature/OpsUx/OperationRunNotificationRedactionTest.php tests/Feature/Verification/VerificationReportViewerDbOnlyTest.php`
- `vendor/bin/sail bin pint --dirty --format agent`

## Spec / checklist status
| Checklist | Total | Completed | Incomplete | Status |
|-----------|-------|-----------|------------|--------|
| requirements.md | 16 | 16 | 0 | ✓ PASS |

- `tasks.md`: T001-T032 complete
- `tasks.md`: T033 manual quickstart validation is still open and noted for follow-up

## Filament / platform notes
- Livewire v4 compliance is unchanged
- no panel provider changes; `bootstrap/providers.php` remains the registration location
- no new globally searchable resources were introduced, so global search requirements are unchanged
- no new destructive Filament actions were added
- no new Filament assets were added; no `filament:assets` deployment change is required

## Testing coverage touched
- snapshot persistence and fingerprint isolation
- compare/drift protected-change evidence
- audit, verification, review-pack, ops-failure, and notification sanitization
- viewer/read-only Filament presentation updates

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #146
2026-03-07 16:43:01 +00:00

100 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Services\Drift\DriftHasher;
use App\Services\Drift\Normalizers\AssignmentsNormalizer;
use App\Services\Drift\Normalizers\ScopeTagsNormalizer;
use App\Services\Drift\Normalizers\SettingsNormalizer;
use App\Services\Intune\VersionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\ProtectedSnapshotAssertions;
uses(RefreshDatabase::class);
it('redacts secrets before persisting snapshots and keeps secret-only changes distinct', function (): void {
[$user, $tenant] = createUserWithTenant(role: 'owner');
$policy = Policy::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'policy_type' => 'settingsCatalogPolicy',
'platform' => 'windows10',
]);
/** @var VersionService $service */
$service = app(VersionService::class);
$version1 = $service->captureVersion(
policy: $policy,
payload: [
'wifi' => [
'ssid' => 'Corp',
'password' => 'super-secret-1',
],
'settings' => [
'example' => true,
],
],
createdBy: $user->email,
);
$version2 = $service->captureVersion(
policy: $policy,
payload: [
'wifi' => [
'ssid' => 'Corp',
'password' => 'super-secret-2',
],
'settings' => [
'example' => true,
],
],
createdBy: $user->email,
);
$fresh1 = PolicyVersion::query()->findOrFail((int) $version1->getKey());
$fresh2 = PolicyVersion::query()->findOrFail((int) $version2->getKey());
ProtectedSnapshotAssertions::assertRedactedPath($fresh1->snapshot, '/wifi/password');
ProtectedSnapshotAssertions::assertRedactedPath($fresh2->snapshot, '/wifi/password');
ProtectedSnapshotAssertions::assertFingerprint($fresh1->secret_fingerprints, 'snapshot', '/wifi/password');
ProtectedSnapshotAssertions::assertFingerprint($fresh2->secret_fingerprints, 'snapshot', '/wifi/password');
expect($fresh1->snapshot['wifi']['ssid'])->toBe('Corp');
expect($fresh2->snapshot['wifi']['ssid'])->toBe('Corp');
expect($fresh1->redaction_version)->toBe(1);
expect($fresh2->redaction_version)->toBe(1);
$settingsNormalizer = app(SettingsNormalizer::class);
$assignmentsNormalizer = app(AssignmentsNormalizer::class);
$scopeTagsNormalizer = app(ScopeTagsNormalizer::class);
$hasher = app(DriftHasher::class);
$hash1 = $hasher->hashNormalized([
'settings' => $settingsNormalizer->normalizeForDiff(
snapshot: $fresh1->snapshot,
policyType: (string) $fresh1->policy_type,
platform: is_string($fresh1->platform) ? (string) $fresh1->platform : null,
),
'assignments' => $assignmentsNormalizer->normalizeForDiff($fresh1->assignments ?? []),
'scope_tag_ids' => $scopeTagsNormalizer->normalizeIds($fresh1->scope_tags ?? []),
'secret_fingerprints' => $fresh1->secret_fingerprints,
'redaction_version' => $fresh1->redaction_version,
]);
$hash2 = $hasher->hashNormalized([
'settings' => $settingsNormalizer->normalizeForDiff(
snapshot: $fresh2->snapshot,
policyType: (string) $fresh2->policy_type,
platform: is_string($fresh2->platform) ? (string) $fresh2->platform : null,
),
'assignments' => $assignmentsNormalizer->normalizeForDiff($fresh2->assignments ?? []),
'scope_tag_ids' => $scopeTagsNormalizer->normalizeIds($fresh2->scope_tags ?? []),
'secret_fingerprints' => $fresh2->secret_fingerprints,
'redaction_version' => $fresh2->redaction_version,
]);
expect($hash1)->not->toBe($hash2);
});