Skip to content

Commit 4ed37d8

Browse files
committed
fix: restore 255 char limit iaw oci distribution spec
Previous image name parsing enforced a strict 255 character limit. This commit restores a check on image name length that adhears to the language in the OCI image distribution specification.
1 parent 10a8fa9 commit 4ed37d8

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
@@ -44,6 +44,11 @@ export class ParsedImageReference {
4444
/**
4545
* Rebuilds the image reference string from repository, registry, tag, and digest.
4646
* Format: [registry/]repository[:tag][@digest]
47+
*
48+
* Note: This does not normalize the reference for pulling. Specifically, it does not
49+
* add the default "library/" namespace or the "registry-1.docker.io" registry for
50+
* Docker Hub images. To get the components for pulling, use `registryForPull`,
51+
* `normalizedRepository`, and `tailReferenceForPull`.
4752
*/
4853
public toString(): string {
4954
let ref = "";
@@ -149,12 +154,21 @@ export function parseImageReference(reference: string): ParsedImageReference {
149154
repository = repository.slice(registry.length + 1);
150155
}
151156

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

160174
/**

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)