Immich: inspector shows why a request was refused; server.versionCheck documented
Inspect on a real Immich got data only from /server/about and /server/storage; the other four answered with a bare message, most likely a key without the permissions. The inspector now keeps the short message of any answer with HTTP 400 and up (never for a good answer) and shows it on the page and in the copied report. The permission list now includes server.versionCheck, and the docs say which endpoints have been seen live. Tests added. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
76d6bfa85e
commit
6052595dea
7 files changed
+41
-7
No files matched your search
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
import * as http from "node:http";
|
||||
import * as https from "node:https";
|
||||
import { isInspecting, looksLike, pathForReport, recordCall, shapeOf } from "./inspector.js";
|
||||
import { isInspecting, looksLike, pathForReport, recordCall, replyOf, shapeOf } from "./inspector.js";
|
||||
|
||||
export interface JsonResponse {
|
||||
status: number;
|
||||
@@ -69,7 +69,7 @@ export function requestJson(
|
||||
// While the inspector runs a read, what was asked and the shape of the answer (never its values) are written down.
|
||||
if (isInspecting()) {
|
||||
const contentType = String(res.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase() || undefined;
|
||||
recordCall({ method, path: pathForReport(parsed), status: res.statusCode ?? 0, ms: Date.now() - started, bytes: Buffer.byteLength(text), shape: json === null ? null : shapeOf(json), ...(contentType ? { contentType } : {}), ...(json === null ? { looks: looksLike(text) } : {}) });
|
||||
recordCall({ method, path: pathForReport(parsed), status: res.statusCode ?? 0, ms: Date.now() - started, bytes: Buffer.byteLength(text), shape: json === null ? null : shapeOf(json), ...(contentType ? { contentType } : {}), ...(json === null ? { looks: looksLike(text) } : {}), ...((res.statusCode ?? 0) >= 400 && replyOf(json) ? { reply: replyOf(json) } : {}) });
|
||||
}
|
||||
resolve({ status: res.statusCode ?? 0, json, text, headers: res.headers });
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* Immich adapter. Config: url, apiKey (made in Immich under Account Settings → API Keys; read permissions are enough: server.about, server.storage, server.statistics and
|
||||
* Immich adapter. Config: url, apiKey (made in Immich under Account Settings → API Keys; read permissions are enough: server.about, server.storage, server.versionCheck, server.statistics and
|
||||
* job.read — or queue.read on newer versions); optional: insecure.
|
||||
*
|
||||
* Reading only: the version (and whether a newer one is out), how full the disk under the library is, how many photos and videos there are and how much space they take, and the
|
||||
* background job queues — above all how many jobs have **failed** (thumbnails or metadata that were never made). Nothing is sent to Immich but GET requests for these.
|
||||
*
|
||||
* Endpoints and field names are from Immich's published API (`/api/server/about`, `/server/storage`, `/server/statistics`, `/server/version-check`, `/jobs` — `/queues` on newer
|
||||
* versions) and have not been checked against a live server here; every field is read defensively, and a part that cannot be read (statistics need an administrator's key) is left out.
|
||||
* versions) and were checked against a live server only for `/server/about` and `/server/storage` (the others were refused for lack of permission); every field is read defensively, and a part that cannot be read (statistics need an administrator's key) is left out.
|
||||
*/
|
||||
import { requestJson } from "../httpJson.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
@@ -19,6 +19,8 @@ export interface InspectedCall {
|
||||
contentType?: string;
|
||||
/** For an answer that was not JSON: what kind of text it is (never its words). */
|
||||
looks?: string;
|
||||
/** For a refusal or a failure (HTTP 400 and up): the short message the system gave ("Missing required permission: …"), so the reason is plain. Never kept for a good answer. */
|
||||
reply?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
@@ -42,6 +44,20 @@ export async function inspectCalls<T>(run: () => Promise<T>): Promise<{ calls: I
|
||||
});
|
||||
}
|
||||
|
||||
const REPLY_KEYS = ["message", "error", "error_description", "detail", "title", "reason"];
|
||||
|
||||
/** Pure: the short message in an error answer ({ message: "Forbidden" }, { error: "…" }), cut to 160 characters; none when there is no such text. */
|
||||
export function replyOf(json: unknown): string | undefined {
|
||||
if (json === null || typeof json !== "object" || Array.isArray(json)) return undefined;
|
||||
const o = json as Record<string, unknown>;
|
||||
for (const key of REPLY_KEYS) {
|
||||
const v = o[key];
|
||||
if (typeof v === "string" && v.trim() !== "") return v.trim().replace(/\s+/g, " ").slice(0, 160);
|
||||
if (Array.isArray(v) && typeof v[0] === "string") return v.filter((x) => typeof x === "string").join("; ").slice(0, 160);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Pure: what an answer that is not JSON looks like — a web page, markup, plain text — from its first characters; none of its words are kept. */
|
||||
export function looksLike(text: string): string | undefined {
|
||||
const head = text.trimStart().slice(0, 200);
|
||||
|
||||
@@ -72,6 +72,18 @@ describe("where it is empty", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("the reason in a refusal", () => {
|
||||
test("a short message from an error answer is kept; nothing from a good answer, and nothing that is not text", () => {
|
||||
assert.equal(insp.replyOf({ message: "Missing required permission: server.statistics", statusCode: 403 }), "Missing required permission: server.statistics");
|
||||
assert.equal(insp.replyOf({ error: "invalid_token", error_description: "x" }), "invalid_token");
|
||||
assert.equal(insp.replyOf({ message: ["name must be a string", "age must be a number"] }), "name must be a string; age must be a number");
|
||||
assert.equal(insp.replyOf({ message: "a".repeat(500) })?.length, 160);
|
||||
assert.equal(insp.replyOf({ data: [1] }), undefined);
|
||||
assert.equal(insp.replyOf([{ message: "x" }]), undefined);
|
||||
assert.equal(insp.replyOf(null), undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what an answer that is not JSON looks like", () => {
|
||||
test("a web page, markup, metrics text, plain text or nothing — from its first characters only", () => {
|
||||
assert.equal(insp.looksLike("<!DOCTYPE html><html><body>Not found</body></html>"), "an HTML page");
|
||||
@@ -91,6 +103,7 @@ describe("the requests written down while a read runs", () => {
|
||||
res.setHeader("content-type", "application/json");
|
||||
if (req.url?.startsWith("/json")) return void res.end(JSON.stringify({ items: [{ id: 1, secret: "hidden-value" }], total: 1 }));
|
||||
if (req.url?.startsWith("/html")) return void res.end("<html>nope</html>");
|
||||
if (req.url?.startsWith("/denied")) return void ((res.statusCode = 403), res.end(JSON.stringify({ message: "Missing required permission: server.statistics" })));
|
||||
res.statusCode = 404;
|
||||
res.end("{}");
|
||||
});
|
||||
@@ -114,6 +127,9 @@ describe("the requests written down while a read runs", () => {
|
||||
assert.deepEqual([calls[0].contentType, calls[0].looks], ["application/json", undefined]);
|
||||
assert.deepEqual([calls[1].contentType, calls[1].looks], ["application/json", "an HTML page"], "what the text looks like, not what it says");
|
||||
assert.doesNotMatch(JSON.stringify(calls), /nope/);
|
||||
assert.equal(calls[0].reply, undefined, "nothing is said of a good answer");
|
||||
const denied = (await insp.inspectCalls(async () => requestJson(`${url}/denied`))).calls[0];
|
||||
assert.deepEqual([denied.status, denied.reply], [403, "Missing required permission: server.statistics"]);
|
||||
assert.ok(calls[0].bytes > 10 && calls[0].ms >= 0);
|
||||
assert.ok(calls[3].error);
|
||||
assert.doesNotMatch(JSON.stringify(calls), /hidden-value|hidden/);
|
||||
|
||||
Reference in new issue
Block a user