@app-vitals/agent-lib 1.139.0 → 1.140.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/route-auth-sweep.ts +126 -0
- package/route-auth-sweep.unit.test.ts +142 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-lib",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.140.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"./pricing": "./pricing.ts",
|
|
28
28
|
"./progress-phases": "./progress-phases.ts",
|
|
29
29
|
"./redact": "./redact.ts",
|
|
30
|
+
"./route-auth-sweep": "./route-auth-sweep.ts",
|
|
30
31
|
"./request-context": "./request-context.ts",
|
|
31
32
|
"./secret-env-vars": "./secret-env-vars.ts",
|
|
32
33
|
"./default-agent-env": "./default-agent-env.ts",
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route authentication sweep: sends every route of an app an unauthenticated
|
|
3
|
+
* request and reports the ones that answer with anything other than 401, 403
|
|
4
|
+
* or a redirect to the login page.
|
|
5
|
+
*
|
|
6
|
+
* The app is described structurally so this module needs no hono dependency;
|
|
7
|
+
* a hono app satisfies `SweepApp` as-is.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface SweepApp {
|
|
11
|
+
routes: ReadonlyArray<{ method: string; path: string }>;
|
|
12
|
+
request(
|
|
13
|
+
path: string,
|
|
14
|
+
init?: { method?: string; redirect?: "manual" },
|
|
15
|
+
): Response | Promise<Response>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PublicRoute {
|
|
19
|
+
method: string;
|
|
20
|
+
path: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SweepOptions {
|
|
25
|
+
/** Routes that are intentionally reachable without authentication. */
|
|
26
|
+
publicRoutes?: ReadonlyArray<PublicRoute>;
|
|
27
|
+
/** Path prefix a login redirect points at. Defaults to `/login`. */
|
|
28
|
+
loginPath?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface OpenRoute {
|
|
32
|
+
method: string;
|
|
33
|
+
path: string;
|
|
34
|
+
status: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SweepResult {
|
|
38
|
+
/** Routes that answered an unauthenticated request without 401/403/login redirect. */
|
|
39
|
+
open: OpenRoute[];
|
|
40
|
+
/** Public-list entries that match no route. */
|
|
41
|
+
stale: PublicRoute[];
|
|
42
|
+
/** Number of distinct routes requested. */
|
|
43
|
+
checked: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Replaces path parameters and wildcards with concrete placeholder values. */
|
|
47
|
+
export function fillPathParams(path: string): string {
|
|
48
|
+
return path
|
|
49
|
+
.replace(/:[A-Za-z0-9_]+(?:\{[^}]*\})?\??/g, "1")
|
|
50
|
+
.replace(/\*/g, "x");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const key = (method: string, path: string) => `${method.toUpperCase()} ${path}`;
|
|
54
|
+
|
|
55
|
+
function isLoginRedirect(res: Response, loginPath: string): boolean {
|
|
56
|
+
if (res.status < 300 || res.status > 399) return false;
|
|
57
|
+
const location = res.headers.get("location");
|
|
58
|
+
if (!location) return false;
|
|
59
|
+
let pathname: string;
|
|
60
|
+
try {
|
|
61
|
+
pathname = new URL(location, "http://localhost").pathname;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return pathname === loginPath || pathname.startsWith(`${loginPath}/`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function sweepRouteAuth(
|
|
69
|
+
app: SweepApp,
|
|
70
|
+
options: SweepOptions = {},
|
|
71
|
+
): Promise<SweepResult> {
|
|
72
|
+
const publicRoutes = options.publicRoutes ?? [];
|
|
73
|
+
const loginPath = options.loginPath ?? "/login";
|
|
74
|
+
const publicKeys = new Set(publicRoutes.map((p) => key(p.method, p.path)));
|
|
75
|
+
const seen = new Set<string>();
|
|
76
|
+
const open: OpenRoute[] = [];
|
|
77
|
+
|
|
78
|
+
for (const route of app.routes) {
|
|
79
|
+
// Middleware registrations (app.use) are listed with method ALL and a
|
|
80
|
+
// wildcard path; an ALL entry on a concrete path is a real handler.
|
|
81
|
+
const method = route.method.toUpperCase();
|
|
82
|
+
if (method === "ALL" && route.path.includes("*")) continue;
|
|
83
|
+
const k = key(route.method, route.path);
|
|
84
|
+
if (seen.has(k)) continue;
|
|
85
|
+
seen.add(k);
|
|
86
|
+
if (publicKeys.has(k)) continue;
|
|
87
|
+
|
|
88
|
+
const res = await app.request(fillPathParams(route.path), {
|
|
89
|
+
method: method === "ALL" ? "GET" : method,
|
|
90
|
+
redirect: "manual",
|
|
91
|
+
});
|
|
92
|
+
if (res.status === 401 || res.status === 403) continue;
|
|
93
|
+
if (isLoginRedirect(res, loginPath)) continue;
|
|
94
|
+
open.push({
|
|
95
|
+
method,
|
|
96
|
+
path: route.path,
|
|
97
|
+
status: res.status,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const stale = publicRoutes.filter((p) => !seen.has(key(p.method, p.path)));
|
|
102
|
+
return { open, stale, checked: seen.size };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Runs the sweep and throws with an actionable message when anything is open or stale. */
|
|
106
|
+
export async function assertRoutesAuthenticated(
|
|
107
|
+
app: SweepApp,
|
|
108
|
+
options: SweepOptions = {},
|
|
109
|
+
): Promise<void> {
|
|
110
|
+
const { open, stale } = await sweepRouteAuth(app, options);
|
|
111
|
+
const lines: string[] = [];
|
|
112
|
+
if (open.length > 0) {
|
|
113
|
+
lines.push(
|
|
114
|
+
"Routes answered an unauthenticated request without 401, 403 or a login redirect:",
|
|
115
|
+
...open.map((r) => ` ${r.method} ${r.path} -> ${r.status}`),
|
|
116
|
+
"Add authentication to each route, or declare it public (method, path and reason) in the public list.",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (stale.length > 0) {
|
|
120
|
+
lines.push(
|
|
121
|
+
"Public-list entries match no route (stale, remove them):",
|
|
122
|
+
...stale.map((r) => ` ${r.method} ${r.path} (${r.reason})`),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
if (lines.length > 0) throw new Error(lines.join("\n"));
|
|
126
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
assertRoutesAuthenticated,
|
|
4
|
+
fillPathParams,
|
|
5
|
+
type SweepApp,
|
|
6
|
+
sweepRouteAuth,
|
|
7
|
+
} from "./route-auth-sweep.ts";
|
|
8
|
+
|
|
9
|
+
type Handler = (path: string) => Response;
|
|
10
|
+
|
|
11
|
+
function fakeApp(
|
|
12
|
+
routes: Array<{ method: string; path: string; respond?: Handler }>,
|
|
13
|
+
): SweepApp & { requested: string[] } {
|
|
14
|
+
const requested: string[] = [];
|
|
15
|
+
return {
|
|
16
|
+
requested,
|
|
17
|
+
routes: routes.map(({ method, path }) => ({ method, path })),
|
|
18
|
+
request(path, init) {
|
|
19
|
+
requested.push(`${init?.method} ${path}`);
|
|
20
|
+
const r = routes.find(
|
|
21
|
+
(x) =>
|
|
22
|
+
x.method.toUpperCase() === init?.method &&
|
|
23
|
+
fillPathParams(x.path) === path,
|
|
24
|
+
);
|
|
25
|
+
return (r?.respond ?? (() => new Response("", { status: 404 })))(path);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const deny: Handler = () => new Response("", { status: 401 });
|
|
31
|
+
const ok: Handler = () => new Response("ok", { status: 200 });
|
|
32
|
+
|
|
33
|
+
describe("sweepRouteAuth", () => {
|
|
34
|
+
test("passes when every route is authenticated", async () => {
|
|
35
|
+
const app = fakeApp([
|
|
36
|
+
{ method: "GET", path: "/a", respond: deny },
|
|
37
|
+
{
|
|
38
|
+
method: "POST",
|
|
39
|
+
path: "/b",
|
|
40
|
+
respond: () => new Response("", { status: 403 }),
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
method: "GET",
|
|
44
|
+
path: "/c",
|
|
45
|
+
respond: () =>
|
|
46
|
+
new Response(null, {
|
|
47
|
+
status: 302,
|
|
48
|
+
headers: { location: "/login?next=/c" },
|
|
49
|
+
}),
|
|
50
|
+
},
|
|
51
|
+
]);
|
|
52
|
+
const res = await sweepRouteAuth(app);
|
|
53
|
+
expect(res).toEqual({ open: [], stale: [], checked: 3 });
|
|
54
|
+
await assertRoutesAuthenticated(app);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("reports an open route and the assertion tells how to fix it", async () => {
|
|
58
|
+
const app = fakeApp([
|
|
59
|
+
{ method: "GET", path: "/a", respond: deny },
|
|
60
|
+
{ method: "GET", path: "/open", respond: ok },
|
|
61
|
+
]);
|
|
62
|
+
const res = await sweepRouteAuth(app);
|
|
63
|
+
expect(res.open).toEqual([{ method: "GET", path: "/open", status: 200 }]);
|
|
64
|
+
await expect(assertRoutesAuthenticated(app)).rejects.toThrow(
|
|
65
|
+
/GET \/open -> 200[\s\S]*add authentication[\s\S]*declare it public/i,
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("redirect elsewhere than login is open", async () => {
|
|
70
|
+
const app = fakeApp([
|
|
71
|
+
{
|
|
72
|
+
method: "GET",
|
|
73
|
+
path: "/r",
|
|
74
|
+
respond: () =>
|
|
75
|
+
new Response(null, { status: 302, headers: { location: "/home" } }),
|
|
76
|
+
},
|
|
77
|
+
]);
|
|
78
|
+
expect((await sweepRouteAuth(app)).open).toHaveLength(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("public routes are ignored and not requested", async () => {
|
|
82
|
+
const app = fakeApp([{ method: "GET", path: "/health", respond: ok }]);
|
|
83
|
+
const res = await sweepRouteAuth(app, {
|
|
84
|
+
publicRoutes: [{ method: "get", path: "/health", reason: "probe" }],
|
|
85
|
+
});
|
|
86
|
+
expect(res.open).toEqual([]);
|
|
87
|
+
expect(app.requested).toEqual([]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("stale public entries are reported", async () => {
|
|
91
|
+
const app = fakeApp([{ method: "GET", path: "/a", respond: deny }]);
|
|
92
|
+
const publicRoutes = [{ method: "GET", path: "/gone", reason: "old" }];
|
|
93
|
+
const res = await sweepRouteAuth(app, { publicRoutes });
|
|
94
|
+
expect(res.stale).toEqual(publicRoutes);
|
|
95
|
+
await expect(
|
|
96
|
+
assertRoutesAuthenticated(app, { publicRoutes }),
|
|
97
|
+
).rejects.toThrow(/stale[\s\S]*GET \/gone \(old\)/);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("fills path parameters and skips middleware entries", async () => {
|
|
101
|
+
const app = fakeApp([
|
|
102
|
+
{ method: "ALL", path: "/*", respond: ok },
|
|
103
|
+
{ method: "GET", path: "/u/:id/p/:pid{[0-9]+}", respond: deny },
|
|
104
|
+
{ method: "GET", path: "/f/*", respond: deny },
|
|
105
|
+
]);
|
|
106
|
+
const res = await sweepRouteAuth(app);
|
|
107
|
+
expect(res).toEqual({ open: [], stale: [], checked: 2 });
|
|
108
|
+
expect(app.requested).toEqual(["GET /u/1/p/1", "GET /f/x"]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("an ALL entry on a concrete path is a real handler and is checked", async () => {
|
|
112
|
+
const app = fakeApp([{ method: "GET", path: "/any", respond: ok }]);
|
|
113
|
+
app.routes = [{ method: "ALL", path: "/any" }];
|
|
114
|
+
const res = await sweepRouteAuth(app);
|
|
115
|
+
expect(res.open).toEqual([{ method: "ALL", path: "/any", status: 200 }]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("duplicate method+path entries are requested once", async () => {
|
|
119
|
+
const app = fakeApp([
|
|
120
|
+
{ method: "GET", path: "/a", respond: deny },
|
|
121
|
+
{ method: "GET", path: "/a", respond: deny },
|
|
122
|
+
]);
|
|
123
|
+
expect((await sweepRouteAuth(app)).checked).toBe(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("custom login path", async () => {
|
|
127
|
+
const app = fakeApp([
|
|
128
|
+
{
|
|
129
|
+
method: "GET",
|
|
130
|
+
path: "/a",
|
|
131
|
+
respond: () =>
|
|
132
|
+
new Response(null, {
|
|
133
|
+
status: 302,
|
|
134
|
+
headers: { location: "https://x.test/signin" },
|
|
135
|
+
}),
|
|
136
|
+
},
|
|
137
|
+
]);
|
|
138
|
+
expect((await sweepRouteAuth(app, { loginPath: "/signin" })).open).toEqual(
|
|
139
|
+
[],
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
});
|