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()])];
+14
View File
@@ -72,6 +72,17 @@ describe("where it is empty", () => {
});
});
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");
assert.equal(insp.looksLike(" \n<html lang=en>"), "an HTML page");
assert.equal(insp.looksLike('<?xml version="1.0"?><a/>'), "XML or markup");
assert.equal(insp.looksLike("# HELP up whether it is up\nup 1"), "metrics text (Prometheus format)");
assert.equal(insp.looksLike("Not Found"), "plain text");
assert.equal(insp.looksLike(" "), undefined);
});
});
describe("the requests written down while a read runs", () => {
let http: Server;
let url = "";
@@ -100,6 +111,9 @@ describe("the requests written down while a read runs", () => {
assert.deepEqual(calls.map((c) => [c.method, c.path, c.status]), [["GET", "/json?token&page", 200], ["GET", "/html", 200], ["GET", "/missing", 404], ["GET", "/refused", 0]]);
assert.equal(calls[0].shape, "{ items: array(1) of { id: number, secret: string }, total: number }");
assert.equal(calls[1].shape, null, "not JSON");
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.ok(calls[0].bytes > 10 && calls[0].ms >= 0);
assert.ok(calls[3].error);
assert.doesNotMatch(JSON.stringify(calls), /hidden-value|hidden/);