Skip to content

Commit 72c36b0

Browse files
committed
fix: restore 255 char image name limit
1 parent 79a0338 commit 72c36b0

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/image-reference.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export class ParsedImageReference {
4242
/**
4343
* Rebuilds the image reference string from repository, registry, tag, and digest.
4444
* Format: [registry/]repository[:tag][@digest]
45+
*
46+
* Note: This does not normalize the reference for pulling. Specifically, it does not
47+
* add the default "library/" namespace or the "registry-1.docker.io" registry for
48+
* Docker Hub images. To get the components for pulling, use `registryForPull`,
49+
* `normalizedRepository`, and `tailReferenceForPull`.
4550
*/
4651
public toString(): string {
4752
let ref = "";
@@ -147,12 +152,21 @@ export function parseImageReference(reference: string): ParsedImageReference {
147152
repository = repository.slice(registry.length + 1);
148153
}
149154

150-
return new ParsedImageReference({
155+
const parsed = new ParsedImageReference({
151156
repository,
152157
registry,
153158
tag: tag ?? undefined,
154159
digest: digest ?? undefined,
155160
});
161+
162+
// According to the OCI distribution specification: "Many clients impose a limit of
163+
// 255 characters on the length of the concatenation of the registry hostname
164+
// (and optional port), `/`, and `<name>` value."
165+
if (parsed.fullName.length > 255) {
166+
throw new Error("image repository name is more than 255 characters");
167+
}
168+
169+
return parsed;
156170
}
157171

158172
/**

test/lib/image-reference.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,33 @@ describe("image-reference", () => {
184184
);
185185
});
186186

187+
it("throws for registry and repository name exceeding 255 characters", () => {
188+
const registry = "example.com/";
189+
const longName = "a".repeat(256 - registry.length);
190+
expect(() => parseImageReference(registry + longName)).toThrow(
191+
"image repository name is more than 255 characters",
192+
);
193+
});
194+
195+
it("allows registry and repository name of exactly 255 characters", () => {
196+
const registry = "example.com/";
197+
const name = "a".repeat(255 - registry.length);
198+
expect(parseImageReference(registry + name)).toMatchObject({
199+
registry: "example.com",
200+
repository: name,
201+
});
202+
});
203+
204+
it("allows registry and repository name of exactly 255 characters with a digest", () => {
205+
const registry = "example.com/";
206+
const name = "a".repeat(255 - registry.length);
207+
const digest = "@" + validSha256;
208+
expect(parseImageReference(registry + name + digest)).toMatchObject({
209+
registry: "example.com",
210+
repository: name,
211+
});
212+
});
213+
187214
it("throws for invalid digest (too short)", () => {
188215
expect(() => parseImageReference("nginx@sha256:abc")).toThrow(
189216
"invalid image reference format",

0 commit comments

Comments
 (0)