Skip to content

Commit c93acda

Browse files
Polish "Prevent long parse times for images with illegal char in tag"
See gh-39617
1 parent c892544 commit c93acda

File tree

2 files changed

+38
-48
lines changed
  • spring-boot-project
    • spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core
    • spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type

2 files changed

+38
-48
lines changed

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core/ImageReferenceTests.java

+19-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
2424
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25-
import static org.assertj.core.api.Assertions.fail;
2625

2726
/**
2827
* Tests for {@link ImageReference}.
@@ -42,6 +41,16 @@ void ofSimpleName() {
4241
assertThat(reference).hasToString("docker.io/library/ubuntu");
4342
}
4443

44+
@Test
45+
void ofSimpleNameWithSingleCharacterSuffix() {
46+
ImageReference reference = ImageReference.of("ubuntu-a");
47+
assertThat(reference.getDomain()).isEqualTo("docker.io");
48+
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
49+
assertThat(reference.getTag()).isNull();
50+
assertThat(reference.getDigest()).isNull();
51+
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
52+
}
53+
4554
@Test
4655
void ofLibrarySlashName() {
4756
ImageReference reference = ImageReference.of("library/ubuntu");
@@ -152,13 +161,21 @@ void ofCustomDomainAndPortWithTag() {
152161
}
153162

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

171+
@Test
172+
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
173+
void ofWhenImageNameIsVeryLongAndHasIllegalCharacterThrowsException() {
174+
assertThatIllegalArgumentException().isThrownBy(() -> ImageReference
175+
.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"))
176+
.withMessageContaining("Unable to parse image reference");
177+
}
178+
162179
@Test
163180
void equalsAndHashCode() {
164181
ImageReference r1 = ImageReference.of("ubuntu:bionic");
@@ -168,26 +185,4 @@ void equalsAndHashCode() {
168185
assertThat(r1).isEqualTo(r1).isEqualTo(r2).isNotEqualTo(r3);
169186
}
170187

171-
@Test
172-
void ofSimpleNameWithSingleCharacterSuffix() {
173-
ImageReference reference = ImageReference.of("ubuntu-a");
174-
assertThat(reference.getDomain()).isEqualTo("docker.io");
175-
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
176-
assertThat(reference.getTag()).isNull();
177-
assertThat(reference.getDigest()).isNull();
178-
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
179-
}
180-
181-
@Test
182-
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
183-
void ofWhenImageNameIsVeryLongAndHasIllegalCharacter() {
184-
try {
185-
ImageReference
186-
.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");
187-
fail("Image Reference contains an illegal character and should have thrown an IllegalArgumentException");
188-
}
189-
catch (IllegalArgumentException ignored) {
190-
}
191-
}
192-
193188
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java

+19-24
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.assertj.core.api.Assertions.assertThat;
2626
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2727
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
28-
import static org.assertj.core.api.Assertions.fail;
2928

3029
/**
3130
* Tests for {@link ImageReference}.
@@ -46,6 +45,16 @@ void ofSimpleName() {
4645
assertThat(reference).hasToString("docker.io/library/ubuntu");
4746
}
4847

48+
@Test
49+
void ofSimpleNameWithSingleCharacterSuffix() {
50+
ImageReference reference = ImageReference.of("ubuntu-a");
51+
assertThat(reference.getDomain()).isEqualTo("docker.io");
52+
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
53+
assertThat(reference.getTag()).isNull();
54+
assertThat(reference.getDigest()).isNull();
55+
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
56+
}
57+
4958
@Test
5059
void ofLibrarySlashName() {
5160
ImageReference reference = ImageReference.of("library/ubuntu");
@@ -176,7 +185,7 @@ void ofImageNameTagAndDigest() {
176185
}
177186

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

203+
@Test
204+
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
205+
void ofWhenIsVeryLongAndHasIllegalCharacter() {
206+
assertThatIllegalArgumentException().isThrownBy(() -> ImageReference
207+
.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"))
208+
.withMessageContaining("Unable to parse image reference");
209+
}
210+
194211
@Test
195212
void forJarFile() {
196213
assertForJarFile("spring-boot.2.0.0.BUILD-SNAPSHOT.jar", "library/spring-boot", "2.0.0.BUILD-SNAPSHOT");
@@ -309,26 +326,4 @@ void inTaglessForm() {
309326
assertThat(updated).hasToString("docker.io/library/ubuntu");
310327
}
311328

312-
@Test
313-
void ofSimpleNameWithSingleCharacterSuffix() {
314-
ImageReference reference = ImageReference.of("ubuntu-a");
315-
assertThat(reference.getDomain()).isEqualTo("docker.io");
316-
assertThat(reference.getName()).isEqualTo("library/ubuntu-a");
317-
assertThat(reference.getTag()).isNull();
318-
assertThat(reference.getDigest()).isNull();
319-
assertThat(reference).hasToString("docker.io/library/ubuntu-a");
320-
}
321-
322-
@Test
323-
@Timeout(value = 1, threadMode = ThreadMode.SEPARATE_THREAD)
324-
void ofWhenIsVeryLongAndHasIllegalCharacter() {
325-
try {
326-
ImageReference
327-
.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");
328-
fail("Contains an illegal character and should have thrown an IllegalArgumentException");
329-
}
330-
catch (IllegalArgumentException ignored) {
331-
}
332-
}
333-
334329
}

0 commit comments

Comments
 (0)