-
Notifications
You must be signed in to change notification settings - Fork 22
Fix ENV directive quoting, use Fmt.str #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I'm not sure how to evaluate this PR. Could I get an example of what broke, and an explanation of how this fixes it please? |
|
Sure. Currently, environment variables are quoted with https://ocaml.org/releases/4.12/api/Printf.html
Docker
However the Docker documentation is wrong: the character used to escape the value is not always a backslash, it's the character set by the escape parser directive which on Windows is usually set to My solution is to fetch the escape character, always enclose values in double-quotes, and escape inside the value the escape character and double-quotes. Without the patch applied: Dockerfile.(parser_directive (`Escape '`') @@
env [("VAL1", {|quick "fox"|}); ("VAL2", {|la`zy \dog|})] |> string_of_t) |> print_string;;
(*
# escape=`
ENV VAL1="quick \"fox\"" VAL2="la`zy \\dog"
*)
Dockerfile.(parser_directive (`Escape '\\') @@
env [("VAL1", {|quick "fox"|}); ("VAL2", {|la`zy \dog|})] |> string_of_t) |> print_string;;
(*
# escape=\
ENV VAL1="quick \"fox\"" VAL2="la`zy \\dog"
*)With the patch applied: Dockerfile.(parser_directive (`Escape '`') @@
env [("VAL1", {|quick "fox"|}); ("VAL2", {|la`zy \dog|})] |> string_of_t) |> print_string;;
(*
# escape=`
ENV VAL1="quick `"fox`"" VAL2="la``zy \dog"
*)
Dockerfile.(parser_directive (`Escape '\\') @@
env [("VAL1", {|quick "fox"|}); ("VAL2", {|la`zy \dog|})] |> string_of_t) |> print_string;;
(*
# escape=\
ENV VAL1="quick \"fox\"" VAL2="la`zy \\dog"
*)Without the patch, on Windows with Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM mcr.microsoft.com/windows/nanoserver:20H2
---> 67ae078d1460
Step 2/3 : ENV VAL1="quick \"fox\"" VAL2="la`zy \\dog"
---> Using cache
---> 3b5b781e04c9
Step 3/3 : RUN echo %VAL1% && echo %VAL2%
---> Running in d3992da5a7b7
quick \fox\
la`zy \\dog
Removing intermediate container d3992da5a7b7
---> 3d5be9509bd7
Successfully built 3d5be9509bd7With the patch, on Windows with You get the original values only with the patch. |
Docker uses the character defined by the #escape parser directive to escape characters in strings. If we surround the environment variable value with double-quotes, we only need to escape double-quotes and the escape character itself in the value. Using the OCaml quoting %S introduces wrong changes and doesn't work when Docker escape character is changed.
|
The force-push is a little simplification in the code. |
|
Thanks for the explanation! @djs55 @justincormack, just copying you about the incorrect Docker documentation referenced above in case you want to get that fixed: #53 (comment) |
|
The escape character problem can also be seen with this, when escaping a single value containing a space without double-quotes: |
|
Looks good, merging! |
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
See this for more explanations. ocurrent/ocaml-dockerfile#53 (comment)
The environment variables were not correctly quoted which introduce unnecessary and sometimes buggy escape sequences in the variable value.
Piggy-back a switch from
Fmt.strftoFmt.stras the former is a deprecated alias to the later.