raz / registry / test.mjs

186 lines · 8.6 KiB · raw · history · last change 4c98ab55

1import assert from "node:assert/strict";
2import { createHash, createHmac } from "node:crypto";
3import { after, before, test } from "node:test";
4import { Miniflare, Log, LogLevel } from "miniflare";
5
6const origin = "https://registry.test";
7const authorization = `Basic ${Buffer.from("publisher:test-only-password").toString("base64")}`;
8const readonly = `Basic ${Buffer.from("__anonymous_pull__:public-read-only").toString("base64")}`;
9const digest = (bytes) => `sha256:${createHash("sha256").update(bytes).digest("hex")}`;
10let mf;
11
12before(async () => {
13 mf = new Miniflare({
14 modules: true,
15 scriptPath: "dist/worker.mjs",
16 compatibilityDate: "2026-07-30",
17 r2Buckets: ["REGISTRY"],
18 bindings: { REGISTRY_USERNAME: "publisher", REGISTRY_PASSWORD: "test-only-password" },
19 log: new Log(LogLevel.ERROR),
20 });
21 await mf.ready;
22});
23after(async () => mf?.dispose());
24
25async function request(path, method = "GET", auth, body, headers = {}) {
26 if (auth) headers.Authorization = auth;
27 return mf.dispatchFetch(new URL(path, origin), { method, headers, body });
28}
29
30async function upload(bytes) {
31 let response = await request("/v2/raz/blobs/uploads/", "POST", authorization);
32 assert.equal(response.status, 202, await response.text());
33 let location = response.headers.get("Location");
34 response = await request(location, "PATCH", authorization, bytes, {
35 "Content-Type": "application/octet-stream",
36 "Content-Length": String(bytes.length),
37 });
38 assert.equal(response.status, 202, await response.text());
39 location = new URL(response.headers.get("Location"), origin);
40 location.searchParams.set("digest", digest(bytes));
41 response = await request(location, "PUT", authorization, undefined, { "Content-Length": "0" });
42 assert.equal(response.status, 201, await response.text());
43 return digest(bytes);
44}
45
46test("Docker discovers Bearer authentication and gets an anonymous pull token", async () => {
47 const challenge = await request("/v2/");
48 assert.equal(challenge.status, 401);
49 assert.match(challenge.headers.get("WWW-Authenticate"), /^Bearer realm="https:\/\/registry.test\/token"/);
50 const token = (await (await request("/token?service=registry.test&scope=repository:raz:pull")).json()).token;
51 for (const method of ["GET", "HEAD"]) {
52 const response = await request("/v2/", method, `Bearer ${token}`);
53 assert.equal(response.status, 200);
54 assert.equal(response.headers.get("Docker-Distribution-Api-Version"), "registry/2.0");
55 }
56});
57
58test("supplied credentials are validated, including during docker login", async () => {
59 assert.equal((await request("/v2/", "GET", authorization)).status, 200);
60 const response = await request("/v2/", "GET", "Basic d3Jvbmc6d3Jvbmc=");
61 assert.equal(response.status, 401);
62 assert.match(response.headers.get("WWW-Authenticate"), /Bearer/);
63});
64
65test("anonymous tokens have pull permission only", async () => {
66 const response = await request("/token?scope=repository:raz:pull,push");
67 assert.equal(response.status, 200);
68 assert.equal(response.headers.get("Cache-Control"), "no-store");
69 const auth = `Bearer ${(await response.json()).token}`;
70 assert.equal((await request("/v2/", "GET", auth)).status, 200);
71 assert.equal((await request("/v2/raz/blobs/uploads/", "POST", auth)).status, 401);
72});
73
74test("publisher tokens authorize uploads, respecting requested scope", async () => {
75 for (const actions of ["pull,push", "pull"]) {
76 const token = await request(`/token?scope=repository:raz:${actions}`, "GET", authorization);
77 assert.equal(token.status, 200);
78 const response = await request("/v2/raz/blobs/uploads/", "POST", `Bearer ${(await token.json()).token}`);
79 assert.equal(response.status, actions.includes("push") ? 202 : 401);
80 }
81});
82
83test("token issuance validates credentials, service and repository scope", async () => {
84 assert.equal((await request("/token", "GET", "Basic d3Jvbmc6d3Jvbmc=")).status, 401);
85 assert.equal((await request("/token?service=other.test")).status, 400);
86 assert.equal((await request("/token?scope=repository:other:pull")).status, 400);
87 assert.equal((await request("/token?scope=repository:raz:delete")).status, 400);
88 assert.equal((await request("/token", "POST")).status, 405);
89});
90
91test("expired, tampered and foreign-audience tokens cannot authorize requests", async () => {
92 const issued = await request("/token", "GET", authorization);
93 const token = (await issued.json()).token;
94 for (const invalid of ["invalid", `${token}garbage`]) {
95 assert.equal((await request("/v2/", "GET", `Bearer ${invalid}`)).status, 401);
96 }
97 function signedToken(payload) {
98 const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
99 const body = Buffer.from(JSON.stringify(payload)).toString("base64url");
100 const message = `${header}.${body}`;
101 return `${message}.${createHmac("sha256", "test-only-password").update(message).digest("base64url")}`;
102 }
103 const payload = { iss: "raz-registry", aud: "registry.test", exp: Math.floor(Date.now() / 1000) + 300,
104 access: [{ type: "repository", name: "raz", actions: ["pull", "push"] }] };
105 for (const changed of [{ exp: 1 }, { aud: "another-registry.test" }, { iss: "someone-else" },
106 { access: [{ type: "repository", name: "other", actions: ["push"] }] }]) {
107 const auth = `Bearer ${signedToken({ ...payload, ...changed })}`;
108 assert.equal((await request("/v2/raz/blobs/uploads/", "POST", auth)).status, 401);
109 }
110});
111
112test("anonymous and public read-only credentials cannot mutate registry data", async () => {
113 for (const auth of [undefined, readonly, "Basic d3Jvbmc6d3Jvbmc="]) {
114 for (const [method, path] of [
115 ["POST", "/v2/raz/blobs/uploads/"],
116 ["PATCH", "/v2/raz/blobs/uploads/unknown"],
117 ["PUT", "/v2/raz/manifests/release"],
118 ["DELETE", "/v2/raz/manifests/release"],
119 ["DELETE", "/v2/raz/blobs/uploads/unknown"],
120 ]) {
121 const response = await request(path, method, auth);
122 assert.equal(response.status, 401, `${method} ${path}`);
123 }
124 }
125});
126
127test("unknown repositories and nested namespaces are not served", async () => {
128 for (const path of [
129 "/v2/other/manifests/release", "/v2/raz/nested/manifests/release",
130 "/v2/_catalog", "/", "/v2/raz/anything",
131 ]) {
132 assert.equal((await request(path, "GET", authorization)).status, 404, path);
133 assert.equal((await request(path, "PUT", authorization)).status, 404, path);
134 }
135});
136
137test("upload status is not an anonymous read endpoint", async () => {
138 assert.equal((await request("/v2/raz/blobs/uploads/unknown")).status, 401);
139});
140
141test("a publisher can cancel an incomplete upload", async () => {
142 const response = await request("/v2/raz/blobs/uploads/", "POST", authorization);
143 assert.equal(response.status, 202);
144 assert.equal((await request(response.headers.get("Location"), "DELETE", authorization)).status, 204);
145});
146
147test("published OCI manifests and blobs can be pulled without credentials", async () => {
148 const config = Buffer.from(JSON.stringify({
149 architecture: "amd64", os: "linux", config: {},
150 rootfs: { type: "layers", diff_ids: [] }, history: [],
151 }));
152 const configDigest = await upload(config);
153 const manifest = JSON.stringify({
154 schemaVersion: 2,
155 mediaType: "application/vnd.oci.image.manifest.v1+json",
156 config: { mediaType: "application/vnd.oci.image.config.v1+json", digest: configDigest, size: config.length },
157 layers: [],
158 });
159 const path = "/v2/raz/manifests/test-release";
160 const response = await request(path, "PUT", authorization, manifest, {
161 "Content-Type": "application/vnd.oci.image.manifest.v1+json",
162 });
163 assert.equal(response.status, 201, await response.text());
164 const manifestDigest = response.headers.get("Docker-Content-Digest");
165 assert.equal(manifestDigest, digest(manifest));
166 for (const reference of ["test-release", manifestDigest]) {
167 const get = await request(`/v2/raz/manifests/${reference}`);
168 assert.equal(get.status, 200);
169 assert.equal(await get.text(), manifest);
170 const head = await request(`/v2/raz/manifests/${reference}`, "HEAD");
171 assert.equal(head.status, 200);
172 assert.equal(head.headers.get("Docker-Content-Digest"), manifestDigest);
173 }
174 const blob = await request(`/v2/raz/blobs/${configDigest}`);
175 assert.equal(blob.status, 200);
176 assert.deepEqual(Buffer.from(await blob.arrayBuffer()), config);
177 const tags = await request("/v2/raz/tags/list");
178 assert.equal(tags.status, 200);
179 assert.ok((await tags.json()).tags.includes("test-release"));
180});
181
182test("missing manifests return a registry error rather than a login challenge", async () => {
183 const response = await request("/v2/raz/manifests/not-published");
184 assert.equal(response.status, 404);
185 assert.equal(response.headers.has("WWW-Authenticate"), false);
186});