Skip to content

Commit c24977b

Browse files
author
Doug Greiman
committed
Relax type check and add comments about validation
1 parent 881196a commit c24977b

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

scripts/gen_dockerfile.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import argparse
2020
import collections
21+
import collections.abc
2122
import functools
2223
import io
2324
import os
@@ -73,13 +74,25 @@ def get_app_config(raw_config, base_image, config_file, source_dir):
7374
config_file (str): Path to user's app.yaml (might be <service-name>.yaml)
7475
source_dir (str): Directory container user's source code
7576
77+
We validate the user input for security and better error messages.
78+
79+
Yaml parsing rules can lead to extremely unhelpful error messages.
80+
For example, parsing a string value where we expected a list.
81+
Python will happily use the string as a sequence of individual
82+
characters, leading to confusing results.
83+
84+
We also try to prevent Dockerfile and Bash injection attacks. For
85+
example, specifying entrypoint as "true\\nADD /etc/passwd /pwned"
86+
would allow the user to inject arbitrary directives into the
87+
Dockerfile, which is a support problem if nothing else.
88+
7689
Returns:
7790
AppConfig: valid configuration
7891
"""
7992
# Examine app.yaml
80-
if not isinstance(raw_config, dict):
93+
if not isinstance(raw_config, collections.abc.Mapping):
8194
raise ValueError(
82-
'Expected {} contents to be of type "dict", but found type "{}"'.
95+
'Expected {} contents to be a Mapping type, but found type "{}"'.
8396
format(config_file, type(raw_config)))
8497

8598
entrypoint = validation_utils.get_field_value(raw_config, 'entrypoint', str)
@@ -117,7 +130,18 @@ def get_app_config(raw_config, base_image, config_file, source_dir):
117130

118131

119132
def get_data(name):
120-
"""Return the contents of the named data resource"""
133+
"""Return the contents of the named data resource
134+
135+
Args:
136+
name (str): Name of file, without directory
137+
138+
These templates are copied from the Google Cloud SDK at
139+
google-cloud-sdk/platform/ext-runtime/python/data
140+
and the two should be kept in sync.
141+
142+
Returns:
143+
str: Contents of data file
144+
"""
121145
filename = os.path.join(os.path.dirname(__file__), 'data', name)
122146
with io.open(filename, 'r', encoding='utf8') as template_file:
123147
return template_file.read()

0 commit comments

Comments
 (0)