mirror of
https://github.com/tiennm99/coolify.git
synced 2026-08-07 20:22:19 +00:00
refactor(validation): tokenize shell-safe command pattern
Replace the flat character-class regex for SHELL_SAFE_COMMAND_PATTERN with a token-aware alternation. The parser now recognizes explicit tokens (`&&`, `||`, balanced single/double quotes, whitespace, and an unquoted safe-char run) instead of a bag of characters, which lets us extend the accepted grammar without loosening the guarantees. New surface area, with tests: - logical OR chaining (`make build || make clean`) - shell globs and bang (`rm *.tmp`, `cp src/?.js dist/`, `! grep -q foo`) - single-quoted arguments are now treated as balanced runs rather than rejected per-character Preserved surface area: - && chaining, balanced "..." and '...' quotes, the previous safe path / argument characters, and the existing error-path contract in ApplicationDeploymentJob::validateShellSafeCommand(). Also refreshes the user-facing validation messages in General.php so the allow/deny list shown on failure matches the new grammar. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
1cf6c7d0ae
commit
817128c5af
@@ -36,15 +36,31 @@ class ValidationPatterns
|
||||
public const DOCKER_TARGET_PATTERN = '/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/';
|
||||
|
||||
/**
|
||||
* Pattern for shell-safe command strings (docker compose commands, docker run options)
|
||||
* Blocks dangerous shell metacharacters: ; | ` $ ( ) > < newlines and carriage returns
|
||||
* Allows & for command chaining (&&) which is common in multi-step build commands
|
||||
* Allows double quotes for build args with spaces (e.g. --build-arg KEY="value")
|
||||
* Blocks backslashes to prevent escape-sequence attacks
|
||||
* Allows single and double quotes for quoted arguments (e.g. --entrypoint "sh -c 'npm start'")
|
||||
* Uses [ \t] instead of \s to explicitly exclude \n and \r (which act as command separators)
|
||||
* Token-aware pattern for shell-safe command strings (docker compose commands, docker run options).
|
||||
*
|
||||
* Accepts a sequence of the following tokens only:
|
||||
* [ \t]+ — whitespace (space / tab)
|
||||
* && — logical AND (matched before bare & can match anything)
|
||||
* || — logical OR (matched before bare | can match anything)
|
||||
* "[^"$`\\\n\r]*" — balanced double-quoted string; blocks $, backtick, \, newlines inside
|
||||
* '[^'\n\r]*' — balanced single-quoted string; blocks newlines inside (all else literal)
|
||||
* [safe-chars]+ — unquoted alphanumerics + safe path/arg chars (includes glob *, ?, and !)
|
||||
*
|
||||
* Blocked everywhere (outside and inside unquoted tokens):
|
||||
* bare & (background op), bare |, ;, $, `, (, ), <, >, \, newline, CR
|
||||
*
|
||||
* Blocked inside double-quoted spans specifically:
|
||||
* $ (variable/command expansion), ` (command substitution), \ (escape)
|
||||
*
|
||||
* Legitimate use cases preserved:
|
||||
* docker compose build && docker tag x && docker push y
|
||||
* make build || make clean
|
||||
* rm *.tmp cp src/?.js dist/
|
||||
* ! grep -q foo && echo missing
|
||||
* docker compose up -d --build-arg VERSION="1.0.0"
|
||||
* --entrypoint "sh -c 'npm start'"
|
||||
*/
|
||||
public const SHELL_SAFE_COMMAND_PATTERN = '/^[a-zA-Z0-9 \t._\-\/=:@,+\[\]{}#%^~&"\']+$/';
|
||||
public const SHELL_SAFE_COMMAND_PATTERN = '/^(?:[ \t]+|&&|\|\||"[^"$`\\\\\n\r]*"|\'[^\'\n\r]*\'|[a-zA-Z0-9._\-\/=:@,+\[\]{}#%^~*?!]+)+$/';
|
||||
|
||||
/**
|
||||
* Pattern for Docker volume names
|
||||
|
||||
Reference in New Issue
Block a user