Skip to content

Commit 6e1e9b9

Browse files
committed
fix: remove duplicate validator
1 parent 694b9a6 commit 6e1e9b9

File tree

3 files changed

+86
-103
lines changed

3 files changed

+86
-103
lines changed

lib/utils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import { PluginWarningsFact } from "./facts";
22

3-
/**
4-
* Validates a Docker image reference format using the official Docker reference regex.
5-
* @param imageReference The Docker image reference to validate
6-
* @returns true if valid, false if invalid
7-
*/
8-
export function isValidDockerImageReference(imageReference: string): boolean {
9-
// Docker image reference validation regex from the official Docker packages:
10-
// https://github.com/distribution/reference/blob/ff14fafe2236e51c2894ac07d4bdfc778e96d682/regexp.go#L9
11-
// Original regex: ^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}))?$
12-
// Note: Converted [[:xdigit:]] to [a-fA-F0-9] and escaped the forward slashes for JavaScript compatibility.
13-
const dockerImageRegex =
14-
/^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?\/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:\/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][a-fA-F0-9]{32,}))?$/;
15-
16-
return dockerImageRegex.test(imageReference);
17-
}
18-
193
// array[*] indicates to truncate each element to the indicated size
204
export const RESPONSE_SIZE_LIMITS = {
215
"containerConfig.data.user": { type: "string", limit: 1024 },

test/lib/image-reference.spec.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,89 @@ describe("image-reference", () => {
213213
});
214214
});
215215
});
216+
217+
describe("isValidImageReference", () => {
218+
describe("valid image references", () => {
219+
const validImages = [
220+
"nginx",
221+
"ubuntu",
222+
"alpine",
223+
"nginx:latest",
224+
"ubuntu:20.04",
225+
"alpine:3.14",
226+
"library/nginx",
227+
"library/ubuntu:20.04",
228+
"docker.io/nginx",
229+
"docker.io/library/nginx:latest",
230+
"gcr.io/project-id/image-name",
231+
"gcr.io/project-id/image-name:tag",
232+
"registry.hub.docker.com/library/nginx",
233+
"localhost:5000/myimage",
234+
"localhost:5000/myimage:latest",
235+
"registry.example.com/path/to/image",
236+
"registry.example.com:8080/path/to/image:v1.0",
237+
"my-registry.com/my-namespace/my-image",
238+
"my-registry.com/my-namespace/my-image:v2.1.0",
239+
"nginx@sha256:abcd1234567890abcd1234567890abcd1234567890abcd1234567890abcd1234",
240+
"ubuntu:20.04@sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12",
241+
"image_name",
242+
"image.name",
243+
"image-name",
244+
"namespace/image_name.with-dots",
245+
"registry.com/namespace/image__double_underscore",
246+
"127.0.0.1:5000/test",
247+
"[::1]:5000/test",
248+
"registry.com/a/b/c/d/e/f/image",
249+
"a.b.c/namespace/image:tag",
250+
];
251+
252+
it.each(validImages)(
253+
"should return true for valid image reference: %s",
254+
(imageName) => {
255+
expect(isValidImageReference(imageName)).toBe(true);
256+
},
257+
);
258+
});
259+
260+
describe("invalid image references", () => {
261+
const invalidImages = [
262+
"/test:unknown",
263+
"//invalid",
264+
"invalid//path",
265+
"UPPERCASE",
266+
"Invalid:Tag",
267+
"registry.com/UPPERCASE/image",
268+
"registry.com/namespace/UPPERCASE",
269+
"",
270+
"image:",
271+
":tag",
272+
"image::",
273+
"registry.com:",
274+
"registry.com:/image",
275+
"image@",
276+
"image@sha256:",
277+
"image@invalid:digest",
278+
"registry.com//namespace/image",
279+
"registry.com/namespace//image",
280+
".image",
281+
"image.",
282+
"-image",
283+
"image-",
284+
"_image",
285+
"image_",
286+
"registry-.com/image",
287+
"registry.com-/image",
288+
"image:tag@",
289+
"image:tag@sha256",
290+
"registry.com:abc/image",
291+
"registry.com:-1/image",
292+
];
293+
294+
it.each(invalidImages)(
295+
"should return false for invalid image reference: %s",
296+
(imageName) => {
297+
expect(isValidImageReference(imageName)).toBe(false);
298+
},
299+
);
300+
});
301+
});

test/lib/utils.spec.ts

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,8 @@
11
import {
2-
isValidDockerImageReference,
32
RESPONSE_SIZE_LIMITS,
43
truncateAdditionalFacts,
54
} from "../../lib/utils";
65

7-
describe("isValidDockerImageReference", () => {
8-
describe("valid image references", () => {
9-
const validImages = [
10-
"nginx",
11-
"ubuntu",
12-
"alpine",
13-
"nginx:latest",
14-
"ubuntu:20.04",
15-
"alpine:3.14",
16-
"library/nginx",
17-
"library/ubuntu:20.04",
18-
"docker.io/nginx",
19-
"docker.io/library/nginx:latest",
20-
"gcr.io/project-id/image-name",
21-
"gcr.io/project-id/image-name:tag",
22-
"registry.hub.docker.com/library/nginx",
23-
"localhost:5000/myimage",
24-
"localhost:5000/myimage:latest",
25-
"registry.example.com/path/to/image",
26-
"registry.example.com:8080/path/to/image:v1.0",
27-
"my-registry.com/my-namespace/my-image",
28-
"my-registry.com/my-namespace/my-image:v2.1.0",
29-
"nginx@sha256:abcd1234567890abcd1234567890abcd1234567890abcd1234567890abcd1234",
30-
"ubuntu:20.04@sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12",
31-
"image_name",
32-
"image.name",
33-
"image-name",
34-
"namespace/image_name.with-dots",
35-
"registry.com/namespace/image__double_underscore",
36-
"127.0.0.1:5000/test",
37-
"[::1]:5000/test",
38-
"registry.com/a/b/c/d/e/f/image",
39-
"a.b.c/namespace/image:tag",
40-
];
41-
42-
it.each(validImages)(
43-
"should return true for valid image reference: %s",
44-
(imageName) => {
45-
expect(isValidDockerImageReference(imageName)).toBe(true);
46-
},
47-
);
48-
});
49-
50-
describe("invalid image references", () => {
51-
const invalidImages = [
52-
"/test:unknown",
53-
"//invalid",
54-
"invalid//path",
55-
"UPPERCASE",
56-
"Invalid:Tag",
57-
"registry.com/UPPERCASE/image",
58-
"registry.com/namespace/UPPERCASE",
59-
"",
60-
"image:",
61-
":tag",
62-
"image::",
63-
"registry.com:",
64-
"registry.com:/image",
65-
"image@",
66-
"image@sha256:",
67-
"image@invalid:digest",
68-
"registry.com//namespace/image",
69-
"registry.com/namespace//image",
70-
".image",
71-
"image.",
72-
"-image",
73-
"image-",
74-
"_image",
75-
"image_",
76-
"registry-.com/image",
77-
"registry.com-/image",
78-
"image:tag@",
79-
"image:tag@sha256",
80-
"registry.com:abc/image",
81-
"registry.com:-1/image",
82-
];
83-
84-
it.each(invalidImages)(
85-
"should return false for invalid image reference: %s",
86-
(imageName) => {
87-
expect(isValidDockerImageReference(imageName)).toBe(false);
88-
},
89-
);
90-
});
91-
});
92-
936
describe("truncateAdditionalFacts", () => {
947
describe("should handle edge cases", () => {
958
it("should return empty array for empty input", () => {

0 commit comments

Comments
 (0)