Skip to content

Commit a9b5494

Browse files
fix: validate tag before build using OCI regex (#3191)
Sources: * https://github.com/opencontainers/distribution-spec * https://docs.docker.com/engine/reference/commandline/tag/ Closes #3153. --------- Signed-off-by: Daniel Lombardi <lombardi.daniel.o@gmail.com>
1 parent cb8f2c6 commit a9b5494

File tree

4 files changed

+157
-119
lines changed

4 files changed

+157
-119
lines changed

docker/api/build.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
129129
raise errors.DockerException(
130130
'Can not use custom encoding if gzip is enabled'
131131
)
132-
132+
if tag is not None:
133+
if not utils.match_tag(tag):
134+
raise errors.DockerException(
135+
f"invalid tag '{tag}': invalid reference format"
136+
)
133137
for key in container_limits.keys():
134138
if key not in constants.CONTAINER_LIMITS_KEYS:
135139
raise errors.DockerException(
136-
f'Invalid container_limits key {key}'
140+
f"invalid tag '{tag}': invalid reference format"
137141
)
138-
139142
if custom_context:
140143
if not fileobj:
141144
raise TypeError("You must specify fileobj with custom_context")

docker/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from .build import create_archive, exclude_paths, mkbuildcontext, tar
2+
from .build import match_tag, create_archive, exclude_paths, mkbuildcontext, tar
33
from .decorators import check_resource, minimum_version, update_headers
44
from .utils import (
55
compare_version, convert_port_bindings, convert_volume_binds,

docker/utils/build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99

1010

1111
_SEP = re.compile('/|\\\\') if IS_WINDOWS_PLATFORM else re.compile('/')
12+
_TAG = re.compile(
13+
r"^[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*" \
14+
+ "(:[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127})?$"
15+
)
16+
17+
18+
def match_tag(tag: str) -> bool:
19+
return bool(_TAG.match(tag))
1220

1321

1422
def tar(path, exclude=None, dockerfile=None, fileobj=None, gzip=False):

0 commit comments

Comments
 (0)