Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions app/Security/Presets/MyCspPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function configure(Policy $policy): void
Keyword::SELF,
'cdn.usefathom.com',
'cdn-eu.usefathom.com',
'analytics.codebar.ch',
$cdnHost ?: null,
]);

Expand Down Expand Up @@ -78,6 +79,7 @@ public function configure(Policy $policy): void
Keyword::SELF,
'cdn.usefathom.com',
'cdn-eu.usefathom.com',
'analytics.codebar.ch',
]);
}
}
6 changes: 6 additions & 0 deletions config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
EnvironmentEnum::PRODUCTION->value,
],
],
'codebar_analytics' => [
'url' => env('CODEBAR_ANALYTICS_URL'),
'environments' => [
EnvironmentEnum::PRODUCTION->value,
],
],
],

'feature_policy' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
<script src="{{ rtrim($url, '/') }}/js/script.js" defer></script>
@endif
1 change: 1 addition & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class="sr-only focus:not-sr-only focus:absolute focus:top-4 focus:left-4 focus:z
</div>

@include('layouts._partials._fathom')
@include('layouts._partials._codebar_analytics')

</body>
</html>
26 changes: 26 additions & 0 deletions tests/Feature/Views/CodebarAnalyticsPartialTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Config;

it('embeds the codebar analytics script in production', function () {
Config::set('default.services.codebar_analytics.url', 'https://analytics.codebar.ch');
app()['env'] = 'production';

expect(view('layouts._partials._codebar_analytics')->render())
->toContain('<script src="https://analytics.codebar.ch/js/script.js" defer></script>');
})->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('<script');
})->group('analytics');
10 changes: 10 additions & 0 deletions tests/Unit/Security/MyCspPresetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Loading