diff --git a/CHANGELOG.md b/CHANGELOG.md index 1321ba3..31489ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- **Test credentials are generated at run time.** Secrets and tokens in the tests come from + `TestCredentials` instead of literals, which secret scanning reported as leaks. A guard test + fails on any literal left behind. The `laranail/package-tools` floor is now `^0.1.10`, which + adds a second check: every purpose passed to `TestCredentials` must be a single plain lowercase + word, because secret scanning reads a compound purpose as the password itself. - **The editor prompts through `laranail/console` instead of `laravel/prompts`.** `laranail::env-kit.edit` imported `select()`, `text()`, `confirm()` and `SelectPrompt` from `laravel/prompts` without declaring the package; it arrived only because `laranail/console` diff --git a/composer.json b/composer.json index e62f789..74a555a 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "php": "^8.4.1 || ^8.5", "ext-mbstring": "*", "laranail/console": "^0.1.7", - "laranail/package-tools": "^0.1", + "laranail/package-tools": "^0.1.10", "illuminate/auth": "^13.0", "illuminate/bus": "^13.0", "illuminate/console": "^13.0", diff --git a/tests/Feature/AuditTest.php b/tests/Feature/AuditTest.php index 3cfa353..3cfc67d 100644 --- a/tests/Feature/AuditTest.php +++ b/tests/Feature/AuditTest.php @@ -10,6 +10,7 @@ use Simtabi\Laranail\EnvKit\Headless\Audit\FileAuditSink; use Simtabi\Laranail\EnvKit\Headless\Document\EnvDocument; use Simtabi\Laranail\EnvKit\Headless\Pipeline\Pipes\Audit; +use Simtabi\Laranail\Package\Tools\Testing\TestCredentials; use Simtabi\Laranail\EnvKit\Headless\Pipeline\CommitContext; use Simtabi\Laranail\EnvKit\Headless\Security\SecretRedactor; use Simtabi\Laranail\EnvKit\Headless\Contracts\AuditSinkInterface; @@ -43,11 +44,12 @@ Event::fake(); $this->bindEnv("A=1\n", ['laranail.env-kit.auto_backup' => false]); - EnvKit::set('API_TOKEN', 'abc123'); + $value = TestCredentials::secret('token'); + EnvKit::set('API_TOKEN', $value); - Event::assertDispatched(AfterWrite::class, function (AfterWrite $event): bool { + Event::assertDispatched(AfterWrite::class, function (AfterWrite $event) use ($value): bool { return $event->changes[0]['key'] === 'API_TOKEN' - && $event->changes[0]['new'] !== 'abc123'; // masked (API_TOKEN matches *_TOKEN) + && $event->changes[0]['new'] !== $value; // masked (API_TOKEN matches *_TOKEN) }); }); diff --git a/tests/Feature/ConfigContractTest.php b/tests/Feature/ConfigContractTest.php index 165eb7f..7848e65 100644 --- a/tests/Feature/ConfigContractTest.php +++ b/tests/Feature/ConfigContractTest.php @@ -4,6 +4,7 @@ use Simtabi\Laranail\EnvKit\Headless\EnvKitManager; use Simtabi\Laranail\EnvKit\Headless\Tests\TestCase; +use Simtabi\Laranail\Package\Tools\Testing\TestCredentials; use Simtabi\Laranail\EnvKit\Headless\Contracts\ValueCipherInterface; use Simtabi\Laranail\EnvKit\Headless\Exceptions\ProtectedKeyException; @@ -104,7 +105,7 @@ function shippedEnvKitConfig(): array // The concrete class, not Throwable: Pest's toThrow() falls back to matching the *message* // for anything class_exists() rejects, and Throwable is an interface -- so the loose spelling // silently asserts that the message contains the word 'Throwable'. - expect(fn () => EnvKit::set('DB_PASSWORD', 'should-be-refused'))->toThrow(ProtectedKeyException::class); + expect(fn () => EnvKit::set('DB_PASSWORD', TestCredentials::secret('refused')))->toThrow(ProtectedKeyException::class); }); it('can build a cipher for every driver its config can name', function (): void { diff --git a/tests/Feature/LifecycleEventsTest.php b/tests/Feature/LifecycleEventsTest.php index 11c85bc..b0f443b 100644 --- a/tests/Feature/LifecycleEventsTest.php +++ b/tests/Feature/LifecycleEventsTest.php @@ -11,6 +11,7 @@ use Simtabi\Laranail\EnvKit\Headless\Events\BackupCreated; use Simtabi\Laranail\EnvKit\Headless\Events\BeforeRestore; use Simtabi\Laranail\EnvKit\Headless\Events\WriteRejected; +use Simtabi\Laranail\Package\Tools\Testing\TestCredentials; use Simtabi\Laranail\EnvKit\Headless\Events\WriteRolledBack; use Simtabi\Laranail\EnvKit\Headless\Events\ConflictDetected; use Simtabi\Laranail\EnvKit\Headless\Contracts\WriterInterface; @@ -26,7 +27,7 @@ app(EnvKitConfigurator::class)->resolveActorUsing(fn () => 'alice'); Event::fake([BeforeWrite::class, AfterWrite::class]); - EnvKit::set('WIDGET_PASSWORD', 'newsecret'); + EnvKit::set('WIDGET_PASSWORD', TestCredentials::secret('widget')); Event::assertDispatched(BeforeWrite::class, fn (BeforeWrite $e) => $e->actor === 'alice' && $e->operation === 'write' diff --git a/tests/Feature/NotificationsTest.php b/tests/Feature/NotificationsTest.php index e46e6db..2d92079 100644 --- a/tests/Feature/NotificationsTest.php +++ b/tests/Feature/NotificationsTest.php @@ -6,6 +6,7 @@ use Illuminate\Notifications\AnonymousNotifiable; use Simtabi\Laranail\EnvKit\Headless\Facades\EnvKit; use Simtabi\Laranail\EnvKit\Headless\Tests\TestCase; +use Simtabi\Laranail\Package\Tools\Testing\TestCredentials; use Simtabi\Laranail\EnvKit\Headless\Extension\EnvKitConfigurator; use Simtabi\Laranail\EnvKit\Headless\Notifications\EnvKitEventNotification; @@ -67,7 +68,7 @@ function enableEnvKitNotifications(): array app(EnvKitConfigurator::class)->resolveActorUsing(fn () => 'alice'); Notification::fake(); - EnvKit::set('WIDGET_PASSWORD', 'newsecret'); + EnvKit::set('WIDGET_PASSWORD', TestCredentials::secret('widget')); Notification::assertSentOnDemand( EnvKitEventNotification::class, diff --git a/tests/Unit/Architecture/NoLiteralCredentialsTest.php b/tests/Unit/Architecture/NoLiteralCredentialsTest.php new file mode 100644 index 0000000..b9b8a26 --- /dev/null +++ b/tests/Unit/Architecture/NoLiteralCredentialsTest.php @@ -0,0 +1,19 @@ +assertNoLiteralCredentialsIn([dirname(__DIR__, 2)], minFiles: 60); + + expect(true)->toBeTrue(); +}); + +it('names every generated credential with a single word', function (): void { + $this->assertCredentialPurposesAreWordsIn([dirname(__DIR__, 2)], minFiles: 60); + + expect(true)->toBeTrue(); +}); diff --git a/tests/Unit/SecretLeakTest.php b/tests/Unit/SecretLeakTest.php index cfd2abb..efc5eeb 100644 --- a/tests/Unit/SecretLeakTest.php +++ b/tests/Unit/SecretLeakTest.php @@ -2,12 +2,13 @@ declare(strict_types=1); +use Simtabi\Laranail\Package\Tools\Testing\TestCredentials; use Simtabi\Laranail\EnvKit\Headless\Security\SecretRedactor; use Simtabi\Laranail\EnvKit\Headless\Security\ValueSanitizer; use Simtabi\Laranail\EnvKit\Headless\Exceptions\InvalidValueException; it('never puts a raw secret value in an exception message', function () { - $secret = 'super-secret-token-ABC123'; + $secret = TestCredentials::secret('leak'); $thrown = null; try { @@ -22,7 +23,7 @@ }); it('scrubs a secret out of any message via the redactor', function () { - $secret = 'test-secret-not-real'; + $secret = TestCredentials::secret('scrub'); $scrubbed = (new SecretRedactor)->scrub('dsn=postgres://u:' . $secret . '@h/db', [$secret]);