Authentik: reads checked live; inspector says what a non-JSON answer is

Inspect on a real Authentik showed the version, outposts, certificates, tokens and events answer as the reader expects; /admin/workers/ answered with something that is not JSON, so the worker count stays unknown (no alert on a guess). The inspector now records the content type and what a non-JSON answer looks like (a web page, markup, metrics text, plain text; never its words) and shows it with the HTTP status on the page and in the copied report. Tests and docs updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-17 19:32:32 +01:00
1 parent 730d7fb36e
commit 76d6bfa85e
7 files changed
+44 -6

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
* flows failed lately (from the events log). Nothing is sent to Authentik but GET requests for these; making a user is ./users.ts, with a token of its own.
*
* Endpoints and field names are from Authentik's published API (`/api/v3/admin/version/`, `/admin/workers/`, `/outposts/instances/` and its `/health/`, `/crypto/certificatekeypairs/`,
* `/core/tokens/`, `/events/events/`) and have not been checked against a live server here; every field is read defensively, and a part that cannot be read is left out.
* `/core/tokens/`, `/events/events/`) and were checked against a live server (Inspect), where `/admin/workers/` answered with something that is not JSON — the workers are then unknown, not none; every field is read defensively, and a part that cannot be read is left out.
*/
import { requestJson } from "../httpJson.js";
import { withDiagLogging } from "../../services/diagLog.js";
+5 -2
View File
@@ -4,7 +4,7 @@
*/
import * as http from "node:http";
import * as https from "node:https";
import { isInspecting, pathForReport, recordCall, shapeOf } from "./inspector.js";
import { isInspecting, looksLike, pathForReport, recordCall, shapeOf } from "./inspector.js";
export interface JsonResponse {
status: number;
@@ -67,7 +67,10 @@ export function requestJson(
// not JSON: the text is still there
}
// While the inspector runs a read, what was asked and the shape of the answer (never its values) are written down.
if (isInspecting()) recordCall({ method, path: pathForReport(parsed), status: res.statusCode ?? 0, ms: Date.now() - started, bytes: Buffer.byteLength(text), shape: json === null ? null : shapeOf(json) });
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) } : {}) });
}
resolve({ status: res.statusCode ?? 0, json, text, headers: res.headers });
});
},
+14
View File
@@ -15,6 +15,10 @@ export interface InspectedCall {
bytes: number;
/** What the answer looked like; null when it was not JSON or there was none. */
shape: string | null;
/** The answer's content type (without parameters), when it sent one. */
contentType?: string;
/** For an answer that was not JSON: what kind of text it is (never its words). */
looks?: string;
error?: string;
}
@@ -38,6 +42,16 @@ export async function inspectCalls<T>(run: () => Promise<T>): Promise<{ calls: I
});
}
/** 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);
if (head === "") return undefined;
if (/^<!doctype html|^<html|^<head|^<body/i.test(head)) return "an HTML page";
if (/^<\?xml|^<[a-z]/i.test(head)) return "XML or markup";
if (/^#\s*(help|type)\s/i.test(head)) return "metrics text (Prometheus format)";
return "plain text";
}
/** Pure: a path for a report — the query's parameter names, not what they were set to. */
export function pathForReport(url: URL): string {
const names = [...new Set([...url.searchParams.keys()])];