Fix the argv builder for a deploy helper
Your task
Agent-written deploy scripts keep pasting their inputs into a remote shell command, like ssh deploy@$HOST "cd /srv/$SERVICE && git checkout $VERSION". The replacement checks every input and builds an argument list (argv) for ssh instead, so no value can become a second command.
An assistant wrote build_deploy_command(service, version, host, allowed_services). It is in the editor. It has a bug. Find it and fix it.
Input and output:
service,version,host: the values the deploy was asked for.allowed_services: the services anyone may deploy.- Return
["ssh", "deploy@<host>", "/usr/local/bin/deploy", service, version], or a list holding one error string.
Rules the tests check, in this order:
- The host must be non-empty and made only of ASCII letters, digits,
.and-. Otherwise return["error:invalid_host"]. - The service must equal one of
allowed_servicesexactly, case included. Otherwise return["error:service_not_allowed"]. - The version must be 1 to 12 characters, made only of ASCII letters, digits,
.,-and_, and must not contain... Otherwise return["error:invalid_version"].
Examples
build_deploy_command(service: str, version: str, host: str, allowed_services: list[str]) -> list[str]Example 1
Input
build_deploy_command("web", "v1.2.3", "host1.internal", ["web", "api"])Output
["ssh", "deploy@host1.internal", "/usr/local/bin/deploy", "web", "v1.2.3"]Example 2
Input
build_deploy_command("web; rm -rf /", "v1", "host1.internal", ["web", "api"])Output
["error:service_not_allowed"]Example 3
Input
build_deploy_command("web", "../../etc", "host1.internal", ["web", "api"])Output
["error:invalid_version"]