Merge pull request #773 from xiaohui-zhangxh/docker-env-file-keep-non-ascii

don't escape non-ascii characters in docker env file
This commit is contained in:
Donal McBreen
2024-04-25 11:57:14 +01:00
committed by GitHub
5 changed files with 39 additions and 5 deletions

View File

@@ -24,6 +24,13 @@ class Kamal::EnvFile
# Escape a value to make it safe to dump in a docker file.
def escape_docker_env_file_value(value)
# keep non-ascii(UTF-8) characters as it is
value.to_s.scan(/[\x00-\x7F]+|[^\x00-\x7F]+/).map do |part|
part.ascii_only? ? escape_docker_env_file_ascii_value(part) : part
end.join
end
def escape_docker_env_file_ascii_value(value)
# Doublequotes are treated literally in docker env files
# so remove leading and trailing ones and unescape any others
value.to_s.dump[1..-2].gsub(/\\"/, "\"")

View File

@@ -11,6 +11,33 @@ class EnvFileTest < ActiveSupport::TestCase
Kamal::EnvFile.new(env).to_s
end
test "to_str won't escape chinese characters" do
env = {
"foo" => '你好 means hello, "欢迎" means welcome, that\'s simple! 😃 {smile}'
}
assert_equal "foo=你好 means hello, \"欢迎\" means welcome, that's simple! 😃 {smile}\n",
Kamal::EnvFile.new(env).to_s
end
test "to_s won't escape japanese characters" do
env = {
"foo" => 'こんにちは means hello, "ようこそ" means welcome, that\'s simple! 😃 {smile}'
}
assert_equal "foo=こんにちは means hello, \"ようこそ\" means welcome, that's simple! 😃 {smile}\n", \
Kamal::EnvFile.new(env).to_s
end
test "to_s won't escape korean characters" do
env = {
"foo" => '안녕하세요 means hello, "어서 오십시오" means welcome, that\'s simple! 😃 {smile}'
}
assert_equal "foo=안녕하세요 means hello, \"어서 오십시오\" means welcome, that's simple! 😃 {smile}\n", \
Kamal::EnvFile.new(env).to_s
end
test "to_s empty" do
assert_equal "\n", Kamal::EnvFile.new({}).to_s
end

View File

@@ -1 +1 @@
SECRET_TOKEN=1234
SECRET_TOKEN='1234 with "中文"'

View File

@@ -1 +1 @@
SECRET_TOKEN=1234
SECRET_TOKEN='1234 with "中文"'

View File

@@ -3,8 +3,8 @@ require_relative "integration_test"
class MainTest < IntegrationTest
test "envify, deploy, redeploy, rollback, details and audit" do
kamal :envify
assert_local_env_file "SECRET_TOKEN=1234"
assert_remote_env_file "SECRET_TOKEN=1234"
assert_local_env_file "SECRET_TOKEN='1234 with \"中文\"'"
assert_remote_env_file "SECRET_TOKEN=1234 with \"中文\""
remove_local_env_file
first_version = latest_app_version
@@ -16,7 +16,7 @@ class MainTest < IntegrationTest
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "post-deploy"
assert_env :CLEAR_TOKEN, "4321", version: first_version
assert_env :HOST_TOKEN, "abcd", version: first_version
assert_env :SECRET_TOKEN, "1234", version: first_version
assert_env :SECRET_TOKEN, "1234 with \"中文\"", version: first_version
second_version = update_app_rev