hpc / charliecloud

Now hosted on GitLab.
https://gitlab.com/charliecloud/main
Apache License 2.0
312 stars 60 forks source link

color codes in Dockerfile crash build #1068

Open heasterday opened 3 years ago

heasterday commented 3 years ago

This was discovered in testing the flecsi's project's Dockefiles, specifically the CentOS 8 one. Two ARG directives in a row containing color codes cause failure:

Reproducer:

FROM centos:8
ARG EMPH="\e[38;5;36m"
ARG NEMPH="\e[0m"

Error:

building with: ch-image
  1 FROM centos:8
warning: no metadata to load; using defaults
will use --force: rhel8: CentOS/RHEL 8
  2 ARG EMPH='"\e[38;5;36m"'
Traceback (most recent call last):
  File "/usr/lib/python3.8/sre_parse.py", line 1039, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\e'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/heasterday/charliecloud/bin/ch-image", line 219, in <module>
    main()
  File "/home/heasterday/charliecloud/bin/ch-image", line 213, in main
    cli.func(cli)
  File "/home/heasterday/charliecloud/bin/../lib/charliecloud/build.py", line 166, in main
    ml.visit_topdown(tree)
  File "/home/heasterday/.local/lib/python3.8/site-packages/lark/visitors.py", line 193, in visit_topdown
    self._call_userfunc(subtree)
  File "/home/heasterday/.local/lib/python3.8/site-packages/lark/visitors.py", line 169, in _call_userfunc
    return getattr(self, tree.data, self.__default__)(tree)
  File "/home/heasterday/charliecloud/bin/../lib/charliecloud/build.py", line 197, in __default__
    inst = globals()[class_](tree)
  File "/home/heasterday/charliecloud/bin/../lib/charliecloud/build.py", line 329, in __init__
    super().__init__(*args)
  File "/home/heasterday/charliecloud/bin/../lib/charliecloud/build.py", line 304, in __init__
    self.value = variables_sub(self.value, env.env_build)
  File "/home/heasterday/charliecloud/bin/../lib/charliecloud/build.py", line 836, in variables_sub
    s = re.sub(r"(?<!\\)\${?%s}?" % k, v, s)
  File "/usr/lib/python3.8/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python3.8/re.py", line 327, in _subx
    template = _compile_repl(template, pattern)
  File "/usr/lib/python3.8/re.py", line 318, in _compile_repl
    return sre_parse.parse_template(repl, pattern)
  File "/usr/lib/python3.8/sre_parse.py", line 1042, in parse_template
    raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \e at position 1
reidpr commented 1 year ago

This still happens, but the crash is during execution of the RUN instruction, not parsing.

reidpr commented 1 year ago

I’m not sure if it’s the whole problem, but one thing that’s wrong is that the double quotes and backslash are being parsed literally, i.e., it’s interpreting it as WORD instead of PARSED_STRING:

start
  dockerfile
    from_
      image_ref centos:8
    arg
      arg_equals
        EMPH
        "\e[38;5;36m"
    arg
      arg_equals
        foo
        bar
    arg
      arg_equals
        NEMPH
        "\e[0m"