AdminServer should not change working directory when extracting sources
@@ -134,15 +134,16 @@ def apply_mask_to_version(given_version, desired_fields):
return masked_version
-def canonical_path(path):
+def canonical_path(path, base=os.curdir):
""" Resolves a path, following symlinks.
Args:
path: A string specifying a file system location.
+ base: The path against which to resolve relative paths.
Returns:
A string specifying a file system location.
"""
- return os.path.realpath(os.path.abspath(path))
+ return os.path.realpath(os.path.abspath(os.path.join(base, path)))
def valid_link(link_name, link_target, base):
@@ -176,7 +177,7 @@ def ensure_path(path):
def extract_source(revision_key, location, runtime):
- """ Unpacks an archive to a given location.
+ """ Unpacks an archive from a given location.
Args:
revision_key: A string specifying the revision key.
@@ -191,9 +192,6 @@ def extract_source(revision_key, location, runtime):
app_path = os.path.join(revision_base, 'app')
ensure_path(app_path)
- # The working directory must be the target in order to validate paths.
- original_cwd = os.getcwd()
- os.chdir(app_path)
if runtime == JAVA:
config_file_name = 'appengine-web.xml'
@@ -204,15 +202,14 @@ def extract_source(revision_key, location, runtime):
config_file_name = 'app.yaml'
def is_version_config(path):
- return canonical_path(path) == os.path.join(app_path, config_file_name)
+ return canonical_path(path, app_path) == os.path.join(app_path, config_file_name)
- try:
with tarfile.open(location, 'r:gz') as archive:
# Check if the archive is valid before extracting it.
has_config = False
for file_info in archive:
file_name = file_info.name
- if not canonical_path(file_name).startswith(app_path):
+ if not canonical_path(file_name, app_path).startswith(app_path):
raise constants.InvalidSource(
'Invalid location in archive: {}'.format(file_name))
@@ -240,8 +237,6 @@ def extract_source(revision_key, location, runtime):
if runtime == JAVA:
remove_conflicting_jars(app_path)
copy_modified_jars(app_path)
- finally:
- os.chdir(original_cwd)
def port_is_open(host, port):
