diff --git a/.env.example b/.env.example index 1b305db..2a08852 100644 --- a/.env.example +++ b/.env.example @@ -70,6 +70,7 @@ CLOUDINARY_API_KEY=my-api-key CLOUDINARY_API_SECRET=my-api-secret LARAVEL_DEFAULT_FATHOM_SITE_ID=WECFOADW +CODEBAR_ANALYTICS_URL=https://analytics.codebar.ch LITELLM_URL=https://llm.codebar.net LITELLM_MASTER_KEY= GITHUB_TOKEN= diff --git a/app/Security/Presets/MyCspPreset.php b/app/Security/Presets/MyCspPreset.php index 2e80913..146cbee 100644 --- a/app/Security/Presets/MyCspPreset.php +++ b/app/Security/Presets/MyCspPreset.php @@ -20,6 +20,7 @@ public function configure(Policy $policy): void Keyword::SELF, 'cdn.usefathom.com', 'cdn-eu.usefathom.com', + 'analytics.codebar.ch', $cdnHost ?: null, ]); @@ -78,6 +79,7 @@ public function configure(Policy $policy): void Keyword::SELF, 'cdn.usefathom.com', 'cdn-eu.usefathom.com', + 'analytics.codebar.ch', ]); } } diff --git a/config/default.php b/config/default.php index e52f2df..0f024e8 100644 --- a/config/default.php +++ b/config/default.php @@ -14,6 +14,12 @@ EnvironmentEnum::PRODUCTION->value, ], ], + 'codebar_analytics' => [ + 'url' => env('CODEBAR_ANALYTICS_URL'), + 'environments' => [ + EnvironmentEnum::PRODUCTION->value, + ], + ], ], 'feature_policy' => [ diff --git a/resources/views/layouts/_partials/_codebar_analytics.blade.php b/resources/views/layouts/_partials/_codebar_analytics.blade.php new file mode 100644 index 0000000..4c38286 --- /dev/null +++ b/resources/views/layouts/_partials/_codebar_analytics.blade.php @@ -0,0 +1,8 @@ +@php + $environments = config('default.services.codebar_analytics.environments', []); + $url = config('default.services.codebar_analytics.url'); +@endphp + +@if (in_array(app()->environment(), $environments) && $url) + +@endif diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 606e325..887bfff 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -79,6 +79,7 @@ class="sr-only focus:not-sr-only focus:absolute focus:top-4 focus:left-4 focus:z @include('layouts._partials._fathom') +@include('layouts._partials._codebar_analytics') diff --git a/tests/Feature/Views/CodebarAnalyticsPartialTest.php b/tests/Feature/Views/CodebarAnalyticsPartialTest.php new file mode 100644 index 0000000..c463077 --- /dev/null +++ b/tests/Feature/Views/CodebarAnalyticsPartialTest.php @@ -0,0 +1,26 @@ +render()) + ->toContain(''); +})->group('analytics'); + +it('does not embed the codebar analytics script outside production', function () { + Config::set('default.services.codebar_analytics.url', 'https://analytics.codebar.ch'); + + expect(view('layouts._partials._codebar_analytics')->render())->not->toContain('analytics.codebar.ch'); +})->group('analytics'); + +it('does not embed the codebar analytics script without a url', function () { + Config::set('default.services.codebar_analytics.url', null); + app()['env'] = 'production'; + + expect(view('layouts._partials._codebar_analytics')->render())->not->toContain('group('analytics'); diff --git a/tests/Unit/Security/MyCspPresetTest.php b/tests/Unit/Security/MyCspPresetTest.php index a4db781..b2b2277 100644 --- a/tests/Unit/Security/MyCspPresetTest.php +++ b/tests/Unit/Security/MyCspPresetTest.php @@ -21,3 +21,13 @@ expect($contents)->toContain('res.cloudinary.com'); expect($contents)->toContain('www.gravatar.com'); })->group('unit', 'security'); + +it('allows the codebar analytics script and its event endpoint', function () { + $policy = new Policy; + + (new MyCspPreset)->configure($policy); + + expect($policy->getContents()) + ->toMatch('/script-src [^;]*analytics\.codebar\.ch/') + ->toMatch('/connect-src [^;]*analytics\.codebar\.ch/'); +})->group('unit', 'security');