Files
indiekit-server/scripts/patch-micropub-session-token.mjs
Sven 02f7db46e1 fix(media): fix image upload size limit and session token bug
- nginx: add client_max_body_size 20M to blog vhost (default 1MB was
  silently killing uploads, returning 413 as a JSON parse error in UI)
- patch: fix session.token → session.access_token in micropub action
  controller; caused Bearer undefined on internal /media fetch, giving
  500s for Micropub clients that upload files directly (OwnYourSwarm)
- npmrc: add sharp_from_source=true to survive future npm install on
  FreeBSD without breaking sharp's native bindings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 10:13:30 +01:00

60 lines
1.7 KiB
JavaScript

/**
* Patch: fix session.token → session.access_token in micropub action controller.
*
* The indieauth authenticate middleware stores the bearer token as
* `session.access_token`, but the micropub action controller destructures it
* as `session.token`. This causes `uploadMedia` to be called with
* `token = undefined`, resulting in `Authorization: Bearer undefined` on the
* internal /media fetch — a 500 for any Micropub client that uploads files
* directly (e.g. OwnYourSwarm).
*/
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@indiekit/endpoint-micropub/lib/controllers/action.js",
];
const oldCode = "const { scope, token } = session;";
const newCode = "const { scope, access_token: token } = session;";
async function exists(filePath) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
let checked = 0;
let patched = 0;
for (const filePath of candidates) {
if (!(await exists(filePath))) continue;
checked += 1;
const source = await readFile(filePath, "utf8");
if (source.includes(newCode)) {
continue;
}
if (!source.includes(oldCode)) {
console.warn(`[postinstall] micropub-session-token: snippet not found in ${filePath} — skipping`);
continue;
}
const updated = source.replace(oldCode, newCode);
await writeFile(filePath, updated, "utf8");
patched += 1;
}
if (checked === 0) {
console.log("[postinstall] No micropub action controller found");
} else if (patched === 0) {
console.log("[postinstall] micropub session token patch already applied");
} else {
console.log(`[postinstall] Patched micropub session token in ${patched} file(s)`);
}