diff --git a/docs/commands.md b/docs/commands.md index 48ca3c6..f033151 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -84,6 +84,9 @@ at the reviewer’s chosen width. Use local asset paths and ordinary links to ot An accepted publication writes a pending theme selection to `application.appearance.web_theme` in the working Plan. An authored preset takes precedence over the Project preset. If the Plan changed during publication, the result includes `published_web_theme` for reconciliation. Retrying a pending publication sends the exact saved request. +The CLI verifies the Project, revision and digest of the submitted Plan before saving its strong ETag. +API 0.10.3 supplies that validator in the receipt JSON even if a proxy weakens or removes the HTTP ETag. +With an older service, the CLI requires the original strong header. Record the returned review ID with the owner's feedback. After separate Compile approval, run `firstdraft plan compile --revision `, optionally with `--output `. The CLI @@ -1096,7 +1099,7 @@ publication sets the Head to its submitted Plan. The Service stores those bytes keeps the Plan's meaning still makes a new Head, although the Project's `graph_version` stays the same. A successful Plan push or screen publication returns a strong `ETag` of the form `"sha256:"`, quotes included. `` is the -64-character lowercase SHA-256 of the Head bytes. The CLI saves the complete header value in `.firstdraft/state.json` +64-character lowercase SHA-256 of the Head bytes. The CLI saves the complete service-issued validator in `.firstdraft/state.json` and replays it in `If-Match`. `plan compile` also extracts `` to check that the local Plan still matches the Head before it starts a Compilation. A saved ETag in any other form stops that check with `invalid_configuration`. diff --git a/src/commands/sketches.js b/src/commands/sketches.js index 2e42a85..6e5af4a 100644 --- a/src/commands/sketches.js +++ b/src/commands/sketches.js @@ -274,10 +274,16 @@ async function publishCurrentSketches({ ); } const sha = request.manifest.plan_sha256; - const etag = response.headers.get("etag"); + const headerEtag = response.headers.get("etag"); + const { etag = headerEtag } = body?.foundation_plan ?? {}; if ( responseMediaType(response) !== "application/json" || + typeof request.plan_source !== "string" || + digest(request.plan_source) !== sha || etag !== '"sha256:' + sha + '"' || + (headerEtag !== null && + headerEtag !== etag && + headerEtag !== "W/" + etag) || body?.project?.id !== state.project_id || body?.review?.id !== pending.id || body?.review?.plan_sha256 !== sha || diff --git a/test/sketches.test.js b/test/sketches.test.js index f590870..fcc9b7d 100644 --- a/test/sketches.test.js +++ b/test/sketches.test.js @@ -77,6 +77,85 @@ function draw(cwd) { ); } +for (const header of ["weak", "absent"]) { + test(`replays the saved Screens receipt when the HTTP validator is ${header}`, async (t) => { + const cwd = directory(t); + const opts = { cwd, apiUrl }; + const { project } = await startProject({ + ...opts, + fetchFunction: async (input) => { + const id = String(input).split("/").at(-1); + return Response.json({ project: { id, url: "/projects/" + id } }); + }, + }); + initializePlan({ + cwd, + projectId: project.id, + name: "Reading", + applicationKey: "reading", + }); + draw(cwd); + const dir = path.join(cwd, ".firstdraft"); + const sha = digest(readFileSync(path.join(dir, "foundation-plan.json"))); + const etag = '"sha256:' + sha + '"'; + const pendingPath = path.join(dir, "sketch-pending.json"); + + /** @param {string | null} httpTag @param {string | undefined} planTag */ + const publish = (httpTag, planTag) => + publishSketches({ + ...opts, + fetchFunction: async (input, init) => { + const pending = JSON.parse(readFileSync(pendingPath, "utf8")); + assert.equal(init?.body, pending.body); + assert.equal(String(input).split("/").at(-1), pending.id); + return Response.json( + { + project: { id: project.id }, + review: { id: pending.id, plan_sha256: sha }, + foundation_plan: { source_sha256: sha, etag: planTag }, + }, + { status: 200, headers: httpTag === null ? {} : { ETag: httpTag } }, + ); + }, + }); + + const weak = "W/" + etag; + await assert.rejects( + publish(weak, undefined), + /Unexpected review identity/, + ); + const pending = readFileSync(pendingPath, "utf8"); + await assert.rejects(publish(etag, weak), /Unexpected review identity/); + await assert.rejects( + publish('"other"', etag), + /Unexpected review identity/, + ); + assert.equal(readFileSync(pendingPath, "utf8"), pending); + assert.equal( + JSON.parse(readFileSync(path.join(dir, "state.json"), "utf8")) + .foundation_plan_etag, + undefined, + ); + const changed = JSON.parse(pending); + const changedBody = JSON.parse(changed.body); + changedBody.plan_source += "\n"; + changed.body = JSON.stringify(changedBody); + writeFileSync(pendingPath, JSON.stringify(changed)); + await assert.rejects(publish(weak, etag), /Unexpected review identity/); + assert.equal(readFileSync(pendingPath, "utf8"), JSON.stringify(changed)); + writeFileSync(pendingPath, pending); + + const result = await publish(header === "weak" ? weak : null, etag); + assert.equal(result.review.id, JSON.parse(pending).id); + assert.equal(existsSync(pendingPath), false); + assert.equal( + JSON.parse(readFileSync(path.join(dir, "state.json"), "utf8")) + .foundation_plan_etag, + etag, + ); + }); +} + test("a reserved Project survives a lost start, Plan init and an ambiguous review publication", async (t) => { const cwd = directory(t); const opts = { cwd, apiUrl };