diff --git a/buildman/component/buildcomponent.py b/buildman/component/buildcomponent.py index 52ac7abfb..e1fa37cf2 100644 --- a/buildman/component/buildcomponent.py +++ b/buildman/component/buildcomponent.py @@ -18,6 +18,7 @@ from buildman.jobutil.workererror import WorkerError from data import model from data.database import BUILD_PHASE from data.model import InvalidRepositoryBuildException +from util import slash_join HEARTBEAT_DELTA = datetime.timedelta(seconds=60) BUILD_HEARTBEAT_DELAY = datetime.timedelta(seconds=30) @@ -180,9 +181,10 @@ class BuildComponent(BaseComponent): context = build_config.get('context', '') if not (dockerfile_path == '' or context == ''): # This should not happen and can be removed when we centralize validating build_config - if ".." in os.path.relpath(dockerfile_path, context): + dockerfile_abspath = slash_join('', dockerfile_path) + if ".." in os.path.relpath(dockerfile_abspath, context): return os.path.split(dockerfile_path) - dockerfile_path = os.path.relpath(dockerfile_path, context) + dockerfile_path = os.path.relpath(dockerfile_abspath, context) return context, dockerfile_path diff --git a/buildman/component/test/test_buildcomponent.py b/buildman/component/test/test_buildcomponent.py index a434dd092..c4e026916 100644 --- a/buildman/component/test/test_buildcomponent.py +++ b/buildman/component/test/test_buildcomponent.py @@ -23,11 +23,12 @@ def test_path_is_dockerfile(input, expected_path, expected_file): assert actual_file == expected_file @pytest.mark.parametrize('build_config,context,dockerfile_path', [ - ({}, "", ""), - ({'build_subdir': "/builddir/Dockerfile"}, "", "/builddir/Dockerfile"), - ({'context': "/builddir"}, "/builddir", ""), - ({'context': "/builddir", 'build_subdir': "/builddir/Dockerfile"}, "/builddir", "Dockerfile"), - ({'context': "/some_other_dir/Dockerfile", 'build_subdir': "/builddir/Dockerfile"}, "/builddir", "Dockerfile"), + ({}, '', ''), + ({'build_subdir': '/builddir/Dockerfile'}, '', '/builddir/Dockerfile'), + ({'context': '/builddir'}, '/builddir', ''), + ({'context': '/builddir', 'build_subdir': '/builddir/Dockerfile'}, '/builddir', 'Dockerfile'), + ({'context': '/some_other_dir/Dockerfile', 'build_subdir': '/builddir/Dockerfile'}, '/builddir', 'Dockerfile'), + ({'context': '/', 'build_subdir':'Dockerfile'}, '/', 'Dockerfile') ]) def test_extract_dockerfile_args(build_config, context, dockerfile_path): actual_context, actual_dockerfile_path = BuildComponent.extract_dockerfile_args(build_config) diff --git a/util/__init__.py b/util/__init__.py index c45aad78f..f4744897a 100644 --- a/util/__init__.py +++ b/util/__init__.py @@ -10,8 +10,8 @@ def slash_join(*args): are not deduplicated. """ def rmslash(path): - path = path[1:] if path[0] == '/' else path - path = path[:-1] if path[-1] == '/' else path + path = path[1:] if len(path) > 0 and path[0] == '/' else path + path = path[:-1] if len(path) > 0 and path[-1] == '/' else path return path args = [rmslash(path) for path in args]