fix(compose): allow required and alternative variables in network names

Accept the `?`, `:?`, `+` and `:+` interpolation operators in Compose
network names, such as `${CONTAINER_NAME?error}`. The validation added
in v4.4.0 only knew `-` and `:-`, so these valid files stopped deploying.

The text after `?` is an error message and is not used as the default of
the created environment variable. Unsafe operands are still rejected.

Fixes #12126
This commit is contained in:
Andras Bacsai
2026-10-09 08:45:03 +02:00
parent 34366a47b5
commit ea1199bf8e
4 changed files with 67 additions and 11 deletions
@@ -241,8 +241,18 @@ test('service network list entries accept the same variables as network names',
'plain variable' => ['$NET'],
'variable mixed with text' => ['${COMPOSE_PROJECT_NAME}_default'],
'text with a variable default' => ['app-${ENV:-prod}'],
'required variable' => ['${NET?error}'],
'required non-empty variable with a message' => ['${NET:?NET must be set}'],
'variable with an alternative value' => ['${NET:+proxy}'],
'text with a required variable' => ['app-${ENV?error}'],
]);
test('a network name field can be a required variable', function () {
$compose = "services:\n web:\n image: nginx\n networks:\n - example\nnetworks:\n example:\n name: '\${CONTAINER_NAME?error}'\n";
expect(fn () => validateDockerComposeForInjection($compose))->not->toThrow(Exception::class);
});
test('unsafe service network list entries with variables are rejected', function (string $network) {
$compose = "services:\n web:\n image: nginx\n networks:\n - '{$network}'\n";
@@ -254,6 +264,9 @@ test('unsafe service network list entries with variables are rejected', function
'default with command substitution' => ['${NET:-$(id)}'],
'default with a space' => ['${NET:-x y}'],
'default with backticks' => ['${NET:-`id`}'],
'error message with command substitution' => ['${NET?$(id)}'],
'error message with a separator' => ['${NET:?x;id}'],
'alternative value with a space' => ['${NET:+x y}'],
]);
test('a service network key with a variable is rejected because Compose does not interpolate keys', function () {