An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix(applications): normalize nested base64 custom labels (#11473)
Andras Bacsaie0639a8e6d88
diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.phpindex d995492e38..e343176ec2 100644--- a/app/Http/Controllers/Api/ApplicationsController.php+++ b/app/Http/Controllers/Api/ApplicationsController.php@@ -4920,8 +4920,8 @@ class ApplicationsController extends Controller],], 422);}- $customLabels = base64_decode($request->custom_labels);- if (mb_detect_encoding($customLabels, 'UTF-8', true) === false) {+ $customLabels = decodeBase64EncodedLabels($request->custom_labels);+ if ($customLabels === null) {return response()->json(['message' => 'Validation failed.','errors' => [@@ -4929,6 +4929,7 @@ class ApplicationsController extends Controller],], 422);}+ $request->offsetSet('custom_labels', base64_encode($customLabels));}if ($request->has('domains') && $server->isProxyShouldRun()) {$uuid = $request->uuid;diff --git a/app/Models/Application.php b/app/Models/Application.phpindex 59e30f23c4..a1146059d5 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -2390,18 +2390,18 @@ class Application extends BaseModelpublic function parseContainerLabels(?ApplicationPreview $preview = null){- $customLabels = data_get($this, 'custom_labels');- if (! $customLabels) {+ $storedLabels = data_get($this, 'custom_labels');+ if (! $storedLabels) {return;}- if (base64_encode(base64_decode($customLabels, true)) !== $customLabels) {- $this->custom_labels = str($customLabels)->replace(',', "\n");- $this->custom_labels = base64_encode($customLabels);- }- $customLabels = base64_decode($this->custom_labels);- if (mb_detect_encoding($customLabels, 'UTF-8', true) === false) {++ $customLabels = decodeBase64EncodedLabels($storedLabels);+ if ($customLabels === null && ! isBase64Encoded($storedLabels)) {+ $customLabels = $storedLabels;+ } elseif ($customLabels === null) {$customLabels = str(implode('|coolify|', generateLabelsApplication($this, $preview)))->replace('|coolify|', "\n");}+$this->custom_labels = base64_encode($customLabels);$this->save();diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex ae4d28b4bb..42abc2dcb0 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -2723,6 +2723,42 @@ function isBase64Encoded($strValue){return base64_encode(base64_decode($strValue, true)) === $strValue;}++function decodeBase64EncodedLabels(string $value): ?string+{+ if (! isBase64Encoded($value)) {+ return null;+ }++ $decoded = base64_decode($value, true);+ if ($decoded === false) {+ return null;+ }+ $labels = $decoded;++ while ($decoded !== '' && isBase64Encoded($decoded)) {+ $next = base64_decode($decoded, true);+ if ($next === false) {+ break;+ }+ $decoded = $next;+ if (mb_detect_encoding($decoded, 'UTF-8', true) !== false) {+ $lines = preg_split('/\r\n|\n|\r/', $decoded);+ if ($lines === false) {+ break;+ }+ $containsOnlyLabels = collect($lines)+ ->filter(fn (string $line) => $line !== '')+ ->every(fn (string $line) => str_contains($line, '=') && ! str_starts_with($line, '='));++ if ($containsOnlyLabels) {+ $labels = $decoded;+ }+ }+ }++ return mb_detect_encoding($labels, 'UTF-8', true) === false ? null : $labels;+}function customApiValidator(Collection|array $item, array $rules, array $messages = []){if (is_array($item)) {diff --git a/tests/Feature/Api/ApplicationSettingsApiTest.php b/tests/Feature/Api/ApplicationSettingsApiTest.phpindex 7742d7ddbd..71064711c5 100644--- a/tests/Feature/Api/ApplicationSettingsApiTest.php+++ b/tests/Feature/Api/ApplicationSettingsApiTest.php@@ -267,6 +267,34 @@ test('http basic auth updates preserve user-managed labels', function () {expect(base64_decode($this->application->fresh()->custom_labels))->toBe('sentinel-label=true');});+test('nested base64 custom labels are stored in canonical form', function () {+ $labels = "traefik.enable=true\ntraefik.http.routers.web.rule=Host(`example.com`)";++ $this->withHeaders(applicationSettingsApiHeaders($this->bearerToken))+ ->patchJson("/api/v1/applications/{$this->application->uuid}", [+ 'custom_labels' => base64_encode(base64_encode($labels)),+ ])+ ->assertOk();++ expect($this->application->fresh()->custom_labels)->toBe(base64_encode($labels));+});++test('deployment parsing repairs historically nested custom labels', function () {+ $labels = "traefik.enable=true\ntraefik.http.routers.web.rule=Host(`example.com`)";+ $this->application->update(['custom_labels' => base64_encode(base64_encode($labels))]);++ expect($this->application->parseContainerLabels())->toBe($labels)+ ->and($this->application->fresh()->custom_labels)->toBe(base64_encode($labels));+});++test('deployment parsing keeps commas in raw custom label values', function () {+ $labels = "traefik.enable=true\ntraefik.http.routers.web.middlewares=gzip,redirect-to-https";+ $this->application->update(['custom_labels' => $labels]);++ expect($this->application->parseContainerLabels())->toBe($labels)+ ->and($this->application->fresh()->custom_labels)->toBe(base64_encode($labels));+});+test('rejects invalid boolean application settings', function () {$this->withHeaders(applicationSettingsApiHeaders($this->bearerToken))->patchJson("/api/v1/applications/{$this->application->uuid}", [diff --git a/tests/Unit/CustomLabelEncodingTest.php b/tests/Unit/CustomLabelEncodingTest.phpnew file mode 100644index 0000000000..b871c336c1--- /dev/null+++ b/tests/Unit/CustomLabelEncodingTest.php@@ -0,0 +1,39 @@+<?php++it('decodes nested base64 container labels to label text', function () {+ $labels = "traefik.enable=true\ntraefik.http.routers.web.rule=Host(`example.com`)";++ expect(decodeBase64EncodedLabels(base64_encode(base64_encode($labels))))->toBe($labels);+});++it('decodes single encoded container labels without changing them', function () {+ $labels = "traefik.enable=true\ncom.example.version=1";++ expect(decodeBase64EncodedLabels(base64_encode($labels)))->toBe($labels);+});++it('rejects values that are not valid base64', function () {+ expect(decodeBase64EncodedLabels('traefik.enable=true'))->toBeNull();+});++it('handles an empty encoded value', function () {+ expect(decodeBase64EncodedLabels(''))->toBe('');+});++it('does not decode label text that happens to be valid base64', function () {+ expect(decodeBase64EncodedLabels(base64_encode('foo=')))->toBe('foo=');+});++it('decodes labels through an intermediate layer that is not label text', function () {+ $labels = 'traefik.enable=true';++ expect(decodeBase64EncodedLabels(base64_encode(base64_encode(base64_encode($labels)))))->toBe($labels);+});++it('preserves label text when a further decoded layer is not UTF-8', function () {+ expect(decodeBase64EncodedLabels(base64_encode(base64_encode('foo='))))->toBe('foo=');+});++it('rejects encoded non UTF-8 text', function () {+ expect(decodeBase64EncodedLabels(base64_encode("\xff")))->toBeNull();+});