Skip to content

Commit

Permalink
Merge pull request #39617 from wanger26
Browse files Browse the repository at this point in the history
* pr/39617:
  Polish "Prevent long parse times for images with illegal char in tag"
  Prevent long parse times for images with illegal char in tag

Closes gh-39617
  • Loading branch information
scottfrederick committed Feb 20, 2024
2 parents 27200f3 + c93acda commit e6d9fcd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class Regex implements CharSequence {
private static final Regex PATH_COMPONENT;
static {
Regex segment = Regex.of("[a-z0-9]+");
Regex separator = Regex.group("[._]|__|[-]*");
Regex separator = Regex.group("[._-]{1,2}");
Regex separatedSegment = Regex.group(separator, segment).oneOrMoreTimes();
PATH_COMPONENT = Regex.of(segment, Regex.group(separatedSegment).zeroOrOnce());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
package org.springframework.boot.docker.compose.core;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.Timeout.ThreadMode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand All @@ -39,6 +41,16 @@ void ofSimpleName() {
assertThat(reference).hasToString("docker.io/library/ubuntu");
}

@Test
void ofSimpleNameWithSingleCharacterSuffix() {
ImageReference reference = ImageReference.of("ubuntu-a");
assertThat(reference.getDomain()).isEqualTo("docker.io");
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
assertThat(reference.getTag()).isNull();
assertThat(reference.getDigest()).isNull();
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
}

@Test
void ofLibrarySlashName() {
ImageReference reference = ImageReference.of("library/ubuntu");
Expand Down Expand Up @@ -149,13 +161,21 @@ void ofCustomDomainAndPortWithTag() {
}

@Test
void ofWhenHasIllegalCharacter() {
void ofWhenHasIllegalCharacterThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ImageReference
.of("registry.example.com/example/example-app:1.6.0-dev.2.uncommitted+wip.foo.c75795d"))
.withMessageContaining("Unable to parse image reference");
}

@Test
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
void ofWhenImageNameIsVeryLongAndHasIllegalCharacterThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageReference
.of("docker.io/library/this-image-has-a-long-name-with-an-invalid-tag-which-is-at-danger-of-catastrophic-backtracking:1.0.0+1234"))
.withMessageContaining("Unable to parse image reference");
}

@Test
void equalsAndHashCode() {
ImageReference r1 = ImageReference.of("ubuntu:bionic");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,7 +50,7 @@ final class Regex implements CharSequence {
private static final Regex PATH_COMPONENT;
static {
Regex segment = Regex.of("[a-z0-9]+");
Regex separator = Regex.group("[._]|__|[-]*");
Regex separator = Regex.group("[._-]{1,2}");
Regex separatedSegment = Regex.group(separator, segment).oneOrMoreTimes();
PATH_COMPONENT = Regex.of(segment, Regex.group(separatedSegment).zeroOrOnce());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@
import java.io.File;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.Timeout.ThreadMode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand All @@ -43,6 +45,16 @@ void ofSimpleName() {
assertThat(reference).hasToString("docker.io/library/ubuntu");
}

@Test
void ofSimpleNameWithSingleCharacterSuffix() {
ImageReference reference = ImageReference.of("ubuntu-a");
assertThat(reference.getDomain()).isEqualTo("docker.io");
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
assertThat(reference.getTag()).isNull();
assertThat(reference.getDigest()).isNull();
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
}

@Test
void ofLibrarySlashName() {
ImageReference reference = ImageReference.of("library/ubuntu");
Expand Down Expand Up @@ -173,7 +185,7 @@ void ofImageNameTagAndDigest() {
}

@Test
void ofWhenHasIllegalCharacter() {
void ofWhenHasIllegalCharacterThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ImageReference
.of("registry.example.com/example/example-app:1.6.0-dev.2.uncommitted+wip.foo.c75795d"))
Expand All @@ -188,6 +200,14 @@ void ofWhenContainsUpperCaseThrowsException() {
.withMessageContaining("Unable to parse image reference");
}

@Test
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
void ofWhenIsVeryLongAndHasIllegalCharacter() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageReference
.of("docker.io/library/this-image-has-a-long-name-with-an-invalid-tag-which-is-at-danger-of-catastrophic-backtracking:1.0.0+1234"))
.withMessageContaining("Unable to parse image reference");
}

@Test
void forJarFile() {
assertForJarFile("spring-boot.2.0.0.BUILD-SNAPSHOT.jar", "library/spring-boot", "2.0.0.BUILD-SNAPSHOT");
Expand Down

0 comments on commit e6d9fcd

Please sign in to comment.