@@ -85,12 +85,23 @@ public class DockerRegistryEndpoint extends AbstractDescribableImpl<DockerRegist
8585 * Some regex magic to parse docker registry:port/namespace/name:tag into parts, using the same constraints as
8686 * docker push.
8787 *
88- * registry must not contain / and must contain a .
88+ * registry must not contain /, may contain dashes and must contain a .
8989 *
9090 * everything but name is optional
91+ *
92+ * See also https://github.com/docker/distribution/blob/release/2.7/reference/regexp.go
9193 */
9294 private static final Pattern DOCKER_REGISTRY_PATTERN = Pattern
93- .compile ("(([^/]+\\ .[^/]+)/)?(([a-z0-9_]+)/)?([a-zA-Z0-9-_\\ .]+)(:([a-z0-9-_\\ .]+))?" );
95+ .compile ("^" +
96+ // Domain
97+ "(?:([a-zA-Z0-9]+(?:(?:[-.][a-zA-Z0-9]+)+)?(?::([0-9]+))?)/)?" +
98+ // Repo
99+ "([a-z0-9-_.]+)(?:/([a-z0-9-_.]+))*" +
100+ // Tag
101+ "(:[a-zA-Z0-9-_.]+)?" +
102+ // Digest
103+ "(@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][A-Za-z0-9]*)?$" );
104+
94105
95106 private static final Logger LOGGER = Logger .getLogger (DockerRegistryEndpoint .class .getName ());
96107
@@ -120,13 +131,16 @@ public DockerRegistryEndpoint(String url, String credentialsId) {
120131 */
121132 public static DockerRegistryEndpoint fromImageName (String s , @ CheckForNull String credentialsId ) {
122133 Matcher matcher = DOCKER_REGISTRY_PATTERN .matcher (s );
123- if (!matcher .matches () || matcher .groupCount () < 7 ) {
134+ if (!matcher .matches () || matcher .groupCount () < 5 ) {
124135 throw new IllegalArgumentException (s + " does not match regex " + DOCKER_REGISTRY_PATTERN );
125136 }
126137 String url ;
127138 try {
128139 // docker push always uses https
129- url = matcher .group (2 ) == null ? null : new URL ("https://" + matcher .group (2 )).toString ();
140+ String domain = matcher .group (1 );
141+ url = (domain == null || !(domain .contains ("." ) || domain .contains (":" ) || "localhost" .equalsIgnoreCase (domain )))
142+ ? null
143+ : new URL ("https://" + matcher .group (1 )).toString ();
130144 } catch (MalformedURLException e ) {
131145 throw new IllegalArgumentException (s + " can not be parsed as URL: " + e .getMessage ());
132146 }
0 commit comments