Skip to content

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.

fix: improve -f flag detection to prevent false positives

Andras Bacsaid753d49ce6d4
diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php
index 37e705518e..256a2cb66d 100644
--- a/bootstrap/helpers/docker.php
+++ b/bootstrap/helpers/docker.php
@@ -1280,14 +1280,19 @@ function generateDockerEnvFlags($variables): string
* @param string $composeFilePath The path to the compose file
* @param string $envFilePath The path to the .env file
* @return string The modified command with injected flags
+ *
+ * @example
+ * Input: "docker compose build"
+ * Output: "docker compose -f ./docker-compose.yml --env-file .env build"
*/
function injectDockerComposeFlags(string $command, string $composeFilePath, string $envFilePath): string
{
$dockerComposeReplacement = 'docker compose';
// Add -f flag if not present (checks for both -f and --file with various formats)
- // Detects: -f path, -f=path, -fpath (concatenated), --file path, --file=path with any whitespace (space, tab, newline)
- if (! preg_match('/(?:^|\s)(?:-f(?:[=\s]|\S)|--file(?:=|\s))/', $command)) {
+ // Detects: -f path, -f=path, -fpath (concatenated with path chars: . / ~), --file path, --file=path
+ // Note: Uses [.~/]|$ instead of \S to prevent false positives with flags like -foo, -from, -feature
+ if (! preg_match('/(?:^|\s)(?:-f(?:[=\s]|[.\/~]|$)|--file(?:=|\s))/', $command)) {
$dockerComposeReplacement .= " -f {$composeFilePath}";
}
diff --git a/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php b/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php
index c5b11dfce3..fc29f19c39 100644
--- a/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php
+++ b/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php
@@ -489,3 +489,129 @@ it('does not modify docker compose string in bash comments', function () {
expect(substr_count($customCommand, 'docker compose', 0))->toBe(2); // Two total occurrences
expect(substr_count($customCommand, '-f /artifacts/uuid/docker-compose.yaml'))->toBe(1); // Only first has flags
});
+
+// False positive prevention tests: Flags like -foo, -from, -feature should NOT be detected as -f
+
+it('injects -f flag when command contains -foo flag (not -f)', function () {
+ $customCommand = 'docker compose build --foo bar';
+
+ // Use the helper function
+ $customCommand = injectDockerComposeFlags($customCommand, '/artifacts/uuid/docker-compose.yaml', '/artifacts/build-time.env');
+
+ // SHOULD inject -f flag because -foo is NOT the -f flag
+ expect($customCommand)->toBe('docker compose -f /artifacts/uuid/docker-compose.yaml --env-file /artifacts/build-time.env build --foo bar');
+ expect($customCommand)->toContain('-f /artifacts/uuid/docker-compose.yaml');
+});
+
+it('injects -f flag when command contains --from flag (not -f)', function () {
+ $customCommand = 'docker compose build --from cache-image';
+
+ // Use the helper function
+ $customCommand = injectDockerComposeFlags($customCommand, '/artifacts/uuid/docker-compose.yaml', '/artifacts/build-time.env');
+
+ // SHOULD inject -f flag because --from is NOT the -f flag
+ expect($customCommand)->toBe('docker compose -f /artifacts/uuid/docker-compose.yaml --env-file /artifacts/build-time.env build --from cache-image');
+ expect($customCommand)->toContain('-f /artifacts/uuid/docker-compose.yaml');
+});
+
+it('injects -f flag when command contains -feature flag (not -f)', function () {
+ $customCommand = 'docker compose build -feature test';
+
+ // Use the helper function
+ $customCommand = injectDockerComposeFlags($customCommand, '/artifacts/uuid/docker-compose.yaml', '/artifacts/build-time.env');
+
+ // SHOULD inject -f flag because -feature is NOT the -f flag
+ expect($customCommand)->toBe('docker compose -f /artifacts/uuid/docker-compose.yaml --env-file /artifacts/build-time.env build -feature test');
+ expect($customCommand)->toContain('-f /artifacts/uuid/docker-compose.yaml');
+});
+
+it('injects -f flag when command contains -fast flag (not -f)', function () {
+ $customCommand = 'docker compose build -fast';
+
+ // Use the helper function
+ $customCommand = injectDockerComposeFlags($customCommand, '/artifacts/uuid/docker-compose.yaml', '/artifacts/build-time.env');
+
+ // SHOULD inject -f flag because -fast is NOT the -f flag
+ expect($customCommand)->toBe('docker compose -f /artifacts/uuid/docker-compose.yaml --env-file /artifacts/build-time.env build -fast');
+ expect($customCommand)->toContain('-f /artifacts/uuid/docker-compose.yaml');
+});
+
+// Path normalization tests for preview methods
+
+it('normalizes path when baseDirectory is root slash', function () {
+ $baseDirectory = '/';
+ $composeLocation = '/docker-compose.yaml';
+
+ // Normalize baseDirectory to prevent double slashes
+ $normalizedBase = $baseDirectory === '/' ? '' : rtrim($baseDirectory, '/');
+ $path = ".{$normalizedBase}{$composeLocation}";
+
+ expect($path)->toBe('./docker-compose.yaml');
+ expect($path)->not->toContain('//');
+});
+
+it('normalizes path when baseDirectory has trailing slash', function () {
+ $baseDirectory = '/backend/';
+ $composeLocation = '/docker-compose.yaml';
+
+ // Normalize baseDirectory to prevent double slashes
+ $normalizedBase = $baseDirectory === '/' ? '' : rtrim($baseDirectory, '/');
+ $path = ".{$normalizedBase}{$composeLocation}";
+
+ expect($path)->toBe('./backend/docker-compose.yaml');
+ expect($path)->not->toContain('//');
+});
+
+it('handles empty baseDirectory correctly', function () {
+ $baseDirectory = '';
+ $composeLocation = '/docker-compose.yaml';
+
+ // Normalize baseDirectory to prevent double slashes
+ $normalizedBase = $baseDirectory === '/' ? '' : rtrim($baseDirectory, '/');
+ $path = ".{$normalizedBase}{$composeLocation}";
+
+ expect($path)->toBe('./docker-compose.yaml');
+ expect($path)->not->toContain('//');
+});
+
+it('handles normal baseDirectory without trailing slash', function () {
+ $baseDirectory = '/backend';
+ $composeLocation = '/docker-compose.yaml';
+
+ // Normalize baseDirectory to prevent double slashes
+ $normalizedBase = $baseDirectory === '/' ? '' : rtrim($baseDirectory, '/');
+ $path = ".{$normalizedBase}{$composeLocation}";
+
+ expect($path)->toBe('./backend/docker-compose.yaml');
+ expect($path)->not->toContain('//');
+});
+
+it('handles nested baseDirectory with trailing slash', function () {
+ $baseDirectory = '/app/backend/';
+ $composeLocation = '/docker-compose.prod.yaml';
+
+ // Normalize baseDirectory to prevent double slashes
+ $normalizedBase = $baseDirectory === '/' ? '' : rtrim($baseDirectory, '/');
+ $path = ".{$normalizedBase}{$composeLocation}";
+
+ expect($path)->toBe('./app/backend/docker-compose.prod.yaml');
+ expect($path)->not->toContain('//');
+});
+
+it('produces correct preview path with normalized baseDirectory', function () {
+ $testCases = [
+ ['baseDir' => '/', 'compose' => '/docker-compose.yaml', 'expected' => './docker-compose.yaml'],
+ ['baseDir' => '', 'compose' => '/docker-compose.yaml', 'expected' => './docker-compose.yaml'],
+ ['baseDir' => '/backend', 'compose' => '/docker-compose.yaml', 'expected' => './backend/docker-compose.yaml'],
+ ['baseDir' => '/backend/', 'compose' => '/docker-compose.yaml', 'expected' => './backend/docker-compose.yaml'],
+ ['baseDir' => '/app/src/', 'compose' => '/docker-compose.prod.yaml', 'expected' => './app/src/docker-compose.prod.yaml'],
+ ];
+
+ foreach ($testCases as $case) {
+ $normalizedBase = $case['baseDir'] === '/' ? '' : rtrim($case['baseDir'], '/');
+ $path = ".{$normalizedBase}{$case['compose']}";
+
+ expect($path)->toBe($case['expected'], "Failed for baseDir: {$case['baseDir']}");
+ expect($path)->not->toContain('//', "Double slash found for baseDir: {$case['baseDir']}");
+ }
+});