@warlock.js/devtools 5.54.0 → 5.56.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/CHANGELOG.md +36 -0
- package/README.md +26 -13
- package/cjs/{ai-source-CSPh1HqJ.cjs → ai-source-DTpxumMA.cjs} +54 -3
- package/cjs/{ai-source-CSPh1HqJ.cjs.map → ai-source-DTpxumMA.cjs.map} +1 -1
- package/cjs/auth-DzU0gHDG.cjs +1655 -0
- package/cjs/auth-DzU0gHDG.cjs.map +1 -0
- package/cjs/boot-BQi0Etor.cjs +138 -0
- package/cjs/boot-BQi0Etor.cjs.map +1 -0
- package/cjs/capture-CqIiaznN.cjs +1342 -0
- package/cjs/capture-CqIiaznN.cjs.map +1 -0
- package/cjs/cascade-store-pHjKeSFm.cjs +863 -0
- package/cjs/cascade-store-pHjKeSFm.cjs.map +1 -0
- package/cjs/collector-CJbQbzzK.cjs +271 -0
- package/cjs/collector-CJbQbzzK.cjs.map +1 -0
- package/cjs/contracts-CVmplkNb.cjs +112 -0
- package/cjs/contracts-CVmplkNb.cjs.map +1 -0
- package/cjs/current-request-id-BoJJb3y7.cjs +37 -0
- package/cjs/current-request-id-BoJJb3y7.cjs.map +1 -0
- package/cjs/errors-CyZp5yS-.cjs +177 -0
- package/cjs/errors-CyZp5yS-.cjs.map +1 -0
- package/cjs/histogram-BaI-PnCv.cjs +102 -0
- package/cjs/histogram-BaI-PnCv.cjs.map +1 -0
- package/cjs/http-CabcUwBx.cjs +870 -0
- package/cjs/http-CabcUwBx.cjs.map +1 -0
- package/cjs/index.cjs +1 -1
- package/cjs/insights-BKtqxdnP.cjs +811 -0
- package/cjs/insights-BKtqxdnP.cjs.map +1 -0
- package/cjs/ip-DbNhPOzi.cjs +153 -0
- package/cjs/ip-DbNhPOzi.cjs.map +1 -0
- package/cjs/migrations-w7GMsrQw.cjs +297 -0
- package/cjs/migrations-w7GMsrQw.cjs.map +1 -0
- package/cjs/openapi-CC7OAUc6.cjs +52 -0
- package/cjs/openapi-CC7OAUc6.cjs.map +1 -0
- package/cjs/options-CWqFC5__.cjs +181 -0
- package/cjs/options-CWqFC5__.cjs.map +1 -0
- package/cjs/panels-B0dAfGbJ.cjs +298 -0
- package/cjs/panels-B0dAfGbJ.cjs.map +1 -0
- package/cjs/process-panel-DEOnLxoT.cjs +48 -0
- package/cjs/process-panel-DEOnLxoT.cjs.map +1 -0
- package/cjs/production/index.cjs +238 -0
- package/cjs/production/index.cjs.map +1 -0
- package/cjs/rolldown-runtime-Zuuoy_Rh.cjs +52 -0
- package/cjs/server-B6xdeKS8.cjs +184 -0
- package/cjs/server-B6xdeKS8.cjs.map +1 -0
- package/cjs/{src-BzuntLcd.cjs → src-Ce-3Lniv.cjs} +171 -308
- package/cjs/src-Ce-3Lniv.cjs.map +1 -0
- package/cjs/store-aq207T8v.cjs +9 -0
- package/cjs/stream-route-BGJa-zQu.cjs +875 -0
- package/cjs/stream-route-BGJa-zQu.cjs.map +1 -0
- package/esm/devtools-connector.d.mts +16 -12
- package/esm/devtools-connector.d.mts.map +1 -1
- package/esm/devtools-connector.mjs +137 -18
- package/esm/devtools-connector.mjs.map +1 -1
- package/esm/index.d.mts +3 -2
- package/esm/options.mjs +3 -1
- package/esm/options.mjs.map +1 -1
- package/esm/panels.mjs +267 -0
- package/esm/panels.mjs.map +1 -0
- package/esm/process-panel.mjs +20 -1
- package/esm/process-panel.mjs.map +1 -1
- package/esm/production/auth/common-passwords.mjs +303 -0
- package/esm/production/auth/common-passwords.mjs.map +1 -0
- package/esm/production/auth/default-crypto.mjs +19 -0
- package/esm/production/auth/default-crypto.mjs.map +1 -0
- package/esm/production/auth/helpers.mjs +77 -0
- package/esm/production/auth/helpers.mjs.map +1 -0
- package/esm/production/auth/index.mjs +6 -0
- package/esm/production/auth/passkeys.mjs +96 -0
- package/esm/production/auth/passkeys.mjs.map +1 -0
- package/esm/production/auth/service.mjs +1166 -0
- package/esm/production/auth/service.mjs.map +1 -0
- package/esm/production/boot.mjs +138 -0
- package/esm/production/boot.mjs.map +1 -0
- package/esm/production/capture/dev-stats.mjs +83 -0
- package/esm/production/capture/dev-stats.mjs.map +1 -0
- package/esm/production/capture/engine.mjs +442 -0
- package/esm/production/capture/engine.mjs.map +1 -0
- package/esm/production/capture/explain.mjs +271 -0
- package/esm/production/capture/explain.mjs.map +1 -0
- package/esm/production/capture/filter.mjs +24 -0
- package/esm/production/capture/filter.mjs.map +1 -0
- package/esm/production/capture/index.mjs +7 -0
- package/esm/production/capture/production-capture.mjs +134 -0
- package/esm/production/capture/production-capture.mjs.map +1 -0
- package/esm/production/capture/redact.mjs +109 -0
- package/esm/production/capture/redact.mjs.map +1 -0
- package/esm/production/capture/stats-stores.mjs +300 -0
- package/esm/production/capture/stats-stores.mjs.map +1 -0
- package/esm/production/cli/actions.mjs +151 -0
- package/esm/production/cli/actions.mjs.map +1 -0
- package/esm/production/cli/index.d.mts +17 -0
- package/esm/production/cli/index.d.mts.map +1 -0
- package/esm/production/cli/index.mjs +86 -0
- package/esm/production/cli/index.mjs.map +1 -0
- package/esm/production/contracts.d.mts +592 -0
- package/esm/production/contracts.d.mts.map +1 -0
- package/esm/production/contracts.mjs +95 -0
- package/esm/production/contracts.mjs.map +1 -0
- package/esm/production/errors.d.mts +31 -0
- package/esm/production/errors.d.mts.map +1 -0
- package/esm/production/errors.mjs +142 -0
- package/esm/production/errors.mjs.map +1 -0
- package/esm/production/http/access.mjs +124 -0
- package/esm/production/http/access.mjs.map +1 -0
- package/esm/production/http/auth-routes.mjs +280 -0
- package/esm/production/http/auth-routes.mjs.map +1 -0
- package/esm/production/http/client.mjs +57 -0
- package/esm/production/http/client.mjs.map +1 -0
- package/esm/production/http/constants.mjs +11 -0
- package/esm/production/http/constants.mjs.map +1 -0
- package/esm/production/http/context.mjs +14 -0
- package/esm/production/http/context.mjs.map +1 -0
- package/esm/production/http/cookie.mjs +39 -0
- package/esm/production/http/cookie.mjs.map +1 -0
- package/esm/production/http/headers.mjs +25 -0
- package/esm/production/http/headers.mjs.map +1 -0
- package/esm/production/http/index.mjs +10 -0
- package/esm/production/http/ip.mjs +130 -0
- package/esm/production/http/ip.mjs.map +1 -0
- package/esm/production/http/mount.mjs +300 -0
- package/esm/production/http/mount.mjs.map +1 -0
- package/esm/production/http/state.mjs +54 -0
- package/esm/production/http/state.mjs.map +1 -0
- package/esm/production/index.d.mts +6 -0
- package/esm/production/index.mjs +5 -0
- package/esm/production/insights/cap-report.mjs +67 -0
- package/esm/production/insights/cap-report.mjs.map +1 -0
- package/esm/production/insights/health.mjs +88 -0
- package/esm/production/insights/health.mjs.map +1 -0
- package/esm/production/insights/index.mjs +10 -0
- package/esm/production/insights/insights.mjs +167 -0
- package/esm/production/insights/insights.mjs.map +1 -0
- package/esm/production/insights/instance.mjs +24 -0
- package/esm/production/insights/instance.mjs.map +1 -0
- package/esm/production/insights/jobs.mjs +100 -0
- package/esm/production/insights/jobs.mjs.map +1 -0
- package/esm/production/insights/reporter.mjs +59 -0
- package/esm/production/insights/reporter.mjs.map +1 -0
- package/esm/production/insights/resources.mjs +139 -0
- package/esm/production/insights/resources.mjs.map +1 -0
- package/esm/production/insights/writer.mjs +72 -0
- package/esm/production/insights/writer.mjs.map +1 -0
- package/esm/production/openapi.mjs +52 -0
- package/esm/production/openapi.mjs.map +1 -0
- package/esm/production/options.d.mts +60 -0
- package/esm/production/options.d.mts.map +1 -0
- package/esm/production/options.mjs +170 -0
- package/esm/production/options.mjs.map +1 -0
- package/esm/production/stats/histogram.mjs +67 -0
- package/esm/production/stats/histogram.mjs.map +1 -0
- package/esm/production/stats/merge.mjs +106 -0
- package/esm/production/stats/merge.mjs.map +1 -0
- package/esm/production/store/cascade-store.mjs +322 -0
- package/esm/production/store/cascade-store.mjs.map +1 -0
- package/esm/production/store/index.mjs +5 -0
- package/esm/production/store/mapping.mjs +258 -0
- package/esm/production/store/mapping.mjs.map +1 -0
- package/esm/production/store/memory-store.mjs +291 -0
- package/esm/production/store/memory-store.mjs.map +1 -0
- package/esm/production/store/migrations.d.mts +11 -0
- package/esm/production/store/migrations.d.mts.map +1 -0
- package/esm/production/store/migrations.mjs +29 -0
- package/esm/production/store/migrations.mjs.map +1 -0
- package/esm/production/store/models/account.migration.mjs +34 -0
- package/esm/production/store/models/account.migration.mjs.map +1 -0
- package/esm/production/store/models/account.model.mjs +13 -0
- package/esm/production/store/models/account.model.mjs.map +1 -0
- package/esm/production/store/models/audit.migration.mjs +30 -0
- package/esm/production/store/models/audit.migration.mjs.map +1 -0
- package/esm/production/store/models/audit.model.mjs +13 -0
- package/esm/production/store/models/audit.model.mjs.map +1 -0
- package/esm/production/store/models/base.mjs +27 -0
- package/esm/production/store/models/base.mjs.map +1 -0
- package/esm/production/store/models/index.mjs +17 -0
- package/esm/production/store/models/invite.migration.mjs +26 -0
- package/esm/production/store/models/invite.migration.mjs.map +1 -0
- package/esm/production/store/models/invite.model.mjs +13 -0
- package/esm/production/store/models/invite.model.mjs.map +1 -0
- package/esm/production/store/models/report.migration.mjs +20 -0
- package/esm/production/store/models/report.migration.mjs.map +1 -0
- package/esm/production/store/models/report.model.mjs +13 -0
- package/esm/production/store/models/report.model.mjs.map +1 -0
- package/esm/production/store/models/sample.migration.mjs +25 -0
- package/esm/production/store/models/sample.migration.mjs.map +1 -0
- package/esm/production/store/models/sample.model.mjs +13 -0
- package/esm/production/store/models/sample.model.mjs.map +1 -0
- package/esm/production/store/models/session.migration.mjs +31 -0
- package/esm/production/store/models/session.migration.mjs.map +1 -0
- package/esm/production/store/models/session.model.mjs +13 -0
- package/esm/production/store/models/session.model.mjs.map +1 -0
- package/esm/production/store/models/state.migration.mjs +26 -0
- package/esm/production/store/models/state.migration.mjs.map +1 -0
- package/esm/production/store/models/state.model.mjs +13 -0
- package/esm/production/store/models/state.model.mjs.map +1 -0
- package/esm/query-shape.mjs +14 -1
- package/esm/query-shape.mjs.map +1 -1
- package/esm/server/explain-route.mjs +26 -1
- package/esm/server/explain-route.mjs.map +1 -1
- package/esm/server/index.mjs +16 -6
- package/esm/server/index.mjs.map +1 -1
- package/esm/server/insights-routes.mjs +62 -0
- package/esm/server/insights-routes.mjs.map +1 -0
- package/esm/server/log-routes.mjs +32 -5
- package/esm/server/log-routes.mjs.map +1 -1
- package/esm/server/openapi-routes.mjs +3 -2
- package/esm/server/openapi-routes.mjs.map +1 -1
- package/esm/server/panel-routes.mjs +44 -0
- package/esm/server/panel-routes.mjs.map +1 -0
- package/esm/server/requests-routes.mjs +125 -0
- package/esm/server/requests-routes.mjs.map +1 -1
- package/esm/server/session-routes.mjs +42 -0
- package/esm/server/session-routes.mjs.map +1 -0
- package/esm/server/stream-route.mjs +81 -11
- package/esm/server/stream-route.mjs.map +1 -1
- package/esm/server/ui-assets.mjs +34 -12
- package/esm/server/ui-assets.mjs.map +1 -1
- package/esm/sources/log-source.mjs +14 -1
- package/esm/sources/log-source.mjs.map +1 -1
- package/esm/types.d.mts +271 -1
- package/esm/types.d.mts.map +1 -1
- package/esm/ui-root.mjs +11 -6
- package/esm/ui-root.mjs.map +1 -1
- package/llms-full.txt +208 -19
- package/llms.txt +2 -2
- package/package.json +32 -8
- package/skills/SKILL.md +208 -19
- package/ui/dashboard/assets/app.css +3 -0
- package/ui/dashboard/assets/app.js +50 -0
- package/ui/dashboard/build-info.json +21 -0
- package/ui/dashboard/index.html +15 -0
- package/ui/panels/processes.js +15 -0
- package/cjs/server-BlQshJLE.cjs +0 -658
- package/cjs/server-BlQshJLE.cjs.map +0 -1
- package/cjs/src-BzuntLcd.cjs.map +0 -1
- package/esm/server/process-routes.mjs +0 -11
- package/esm/server/process-routes.mjs.map +0 -1
- package/ui/app.css +0 -376
- package/ui/app.js +0 -1054
- package/ui/index.html +0 -28
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
const require_panels = require("./panels-B0dAfGbJ.cjs");
|
|
2
|
+
const require_errors = require("./errors-CyZp5yS-.cjs");
|
|
3
|
+
const require_ip = require("./ip-DbNhPOzi.cjs");
|
|
4
|
+
const require_stream_route = require("./stream-route-BGJa-zQu.cjs");
|
|
5
|
+
let node_crypto = require("node:crypto");
|
|
6
|
+
let node_net = require("node:net");
|
|
7
|
+
|
|
8
|
+
//#region ../../packages/devtools/src/production/http/access.ts
|
|
9
|
+
const PUBLIC = { type: "public" };
|
|
10
|
+
const VIEWER = { type: "viewer" };
|
|
11
|
+
const ADMIN = { type: "admin" };
|
|
12
|
+
const pending = (...stages) => ({
|
|
13
|
+
type: "pending",
|
|
14
|
+
stages
|
|
15
|
+
});
|
|
16
|
+
const ALL_STEPS = [
|
|
17
|
+
"password-change",
|
|
18
|
+
"mfa-enrol",
|
|
19
|
+
"mfa-verify"
|
|
20
|
+
];
|
|
21
|
+
/**
|
|
22
|
+
* The access table: every production route, its method and who may call it.
|
|
23
|
+
* A route with no entry is `admin` (fail closed). A package panel `<id>`
|
|
24
|
+
* gets `GET /api/<id>` and `GET /api/<id>/*` for viewers and every other
|
|
25
|
+
* method there for admins, audited as `panel.action`; a url ending in `/*`
|
|
26
|
+
* matches every pattern below it.
|
|
27
|
+
*/
|
|
28
|
+
function productionRouteAccess(panelIds) {
|
|
29
|
+
const rule = (method, url, access, audit) => ({
|
|
30
|
+
method,
|
|
31
|
+
url,
|
|
32
|
+
access,
|
|
33
|
+
...audit ? { audit } : {}
|
|
34
|
+
});
|
|
35
|
+
const rules = [
|
|
36
|
+
rule("GET", "/", PUBLIC),
|
|
37
|
+
rule("GET", "/assets/:name", PUBLIC),
|
|
38
|
+
rule("GET", "/api/auth/session", PUBLIC),
|
|
39
|
+
rule("POST", "/api/auth/login", PUBLIC),
|
|
40
|
+
rule("POST", "/api/auth/setup", PUBLIC),
|
|
41
|
+
rule("POST", "/api/auth/invite/inspect", PUBLIC),
|
|
42
|
+
rule("POST", "/api/auth/invite/accept", PUBLIC),
|
|
43
|
+
rule("POST", "/api/auth/password", pending("password-change")),
|
|
44
|
+
rule("POST", "/api/auth/totp/start", pending("mfa-enrol")),
|
|
45
|
+
rule("POST", "/api/auth/totp/confirm", pending("mfa-enrol")),
|
|
46
|
+
rule("POST", "/api/auth/passkey/options", pending("mfa-enrol", "mfa-verify")),
|
|
47
|
+
rule("POST", "/api/auth/passkey/verify", pending("mfa-enrol", "mfa-verify")),
|
|
48
|
+
rule("POST", "/api/auth/mfa", pending("mfa-verify")),
|
|
49
|
+
rule("POST", "/api/auth/logout", pending(...ALL_STEPS)),
|
|
50
|
+
rule("GET", "/api/panels", VIEWER),
|
|
51
|
+
rule("GET", "/api/account", VIEWER),
|
|
52
|
+
rule("GET", "/api/account/sessions", VIEWER),
|
|
53
|
+
rule("DELETE", "/api/account/sessions/:id", VIEWER),
|
|
54
|
+
rule("POST", "/api/account/password", VIEWER),
|
|
55
|
+
rule("POST", "/api/account/totp/start", VIEWER),
|
|
56
|
+
rule("POST", "/api/account/totp/confirm", VIEWER),
|
|
57
|
+
rule("DELETE", "/api/account/totp", VIEWER),
|
|
58
|
+
rule("POST", "/api/account/recovery-codes", VIEWER),
|
|
59
|
+
rule("POST", "/api/account/passkey/options", VIEWER),
|
|
60
|
+
rule("POST", "/api/account/passkey/verify", VIEWER),
|
|
61
|
+
rule("DELETE", "/api/account/passkeys/:id", VIEWER),
|
|
62
|
+
rule("GET", "/api/admin/users", ADMIN),
|
|
63
|
+
rule("POST", "/api/admin/invites", ADMIN),
|
|
64
|
+
rule("DELETE", "/api/admin/invites/:id", ADMIN),
|
|
65
|
+
rule("PATCH", "/api/admin/users/:id", ADMIN),
|
|
66
|
+
rule("DELETE", "/api/admin/users/:id", ADMIN),
|
|
67
|
+
rule("POST", "/api/admin/users/:id/reset-mfa", ADMIN),
|
|
68
|
+
rule("POST", "/api/admin/users/:id/sign-out", ADMIN),
|
|
69
|
+
rule("GET", "/api/admin/audit", ADMIN),
|
|
70
|
+
rule("POST", "/api/admin/disable", ADMIN),
|
|
71
|
+
rule("GET", "/api/stream", VIEWER),
|
|
72
|
+
rule("GET", "/api/instances", VIEWER),
|
|
73
|
+
rule("GET", "/api/resources", VIEWER),
|
|
74
|
+
rule("GET", "/api/queries/stats", VIEWER),
|
|
75
|
+
rule("GET", "/api/routes/stats", VIEWER),
|
|
76
|
+
rule("GET", "/api/health", VIEWER),
|
|
77
|
+
rule("GET", "/api/errors", VIEWER),
|
|
78
|
+
rule("GET", "/api/jobs", VIEWER),
|
|
79
|
+
rule("GET", "/api/logs", VIEWER),
|
|
80
|
+
rule("GET", "/api/requests", VIEWER),
|
|
81
|
+
rule("GET", "/api/requests/:id", VIEWER),
|
|
82
|
+
rule("GET", "/api/requests/:id/curl", VIEWER),
|
|
83
|
+
rule("DELETE", "/api/requests", ADMIN, "requests.cleared"),
|
|
84
|
+
rule("POST", "/api/queries/:shapeId/explain", ADMIN, "explain.run"),
|
|
85
|
+
rule("GET", "/api/routes", VIEWER),
|
|
86
|
+
rule("GET", "/panels/:id/:name", VIEWER),
|
|
87
|
+
rule("GET", "/api/mails", VIEWER),
|
|
88
|
+
rule("GET", "/api/mails/:id", VIEWER),
|
|
89
|
+
rule("GET", "/api/mails/:id/html", VIEWER),
|
|
90
|
+
rule("GET", "/api/ai", VIEWER),
|
|
91
|
+
rule("GET", "/api/ai/agent-runs", VIEWER),
|
|
92
|
+
rule("GET", "/api/ai/agent-runs/:id", VIEWER),
|
|
93
|
+
rule("GET", "/api/ai/runs/:id", VIEWER),
|
|
94
|
+
rule("GET", "/api/openapi.json", VIEWER),
|
|
95
|
+
rule("GET", "/api/postman.json", VIEWER),
|
|
96
|
+
rule("GET", "/docs", VIEWER),
|
|
97
|
+
rule("GET", "/docs/swagger", VIEWER)
|
|
98
|
+
];
|
|
99
|
+
for (const id of panelIds) {
|
|
100
|
+
rules.push(rule("GET", `/api/${id}`, VIEWER), rule("GET", `/api/${id}/*`, VIEWER));
|
|
101
|
+
for (const method of [
|
|
102
|
+
"POST",
|
|
103
|
+
"PUT",
|
|
104
|
+
"PATCH",
|
|
105
|
+
"DELETE"
|
|
106
|
+
]) rules.push(rule(method, `/api/${id}`, ADMIN, "panel.action"), rule(method, `/api/${id}/*`, ADMIN, "panel.action"));
|
|
107
|
+
}
|
|
108
|
+
return rules;
|
|
109
|
+
}
|
|
110
|
+
/** Looks routes up by method and URL pattern: exact first, then the longest `/*` prefix. */
|
|
111
|
+
function createAccessLookup(rules) {
|
|
112
|
+
const exact = /* @__PURE__ */ new Map();
|
|
113
|
+
const wildcards = [];
|
|
114
|
+
for (const rule of rules) if (rule.url.endsWith("/*")) wildcards.push({
|
|
115
|
+
method: rule.method,
|
|
116
|
+
prefix: rule.url.slice(0, -1),
|
|
117
|
+
rule
|
|
118
|
+
});
|
|
119
|
+
else exact.set(`${rule.method} ${rule.url}`, rule);
|
|
120
|
+
wildcards.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
121
|
+
return (method, pattern) => {
|
|
122
|
+
const verb = method === "HEAD" ? "GET" : method;
|
|
123
|
+
const found = exact.get(`${verb} ${pattern}`);
|
|
124
|
+
if (found) return found;
|
|
125
|
+
return wildcards.find((item) => item.method === verb && pattern.startsWith(item.prefix))?.rule;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region ../../packages/devtools/src/production/http/client.ts
|
|
131
|
+
/** The first value of a header that may repeat or hold a comma list. */
|
|
132
|
+
function firstValue(value) {
|
|
133
|
+
const first = (Array.isArray(value) ? value[0] : value)?.split(",")[0]?.trim();
|
|
134
|
+
return first === void 0 || first === "" ? void 0 : first;
|
|
135
|
+
}
|
|
136
|
+
/** The host name of a `Host` value: no port, lower case, brackets of an IPv6 literal kept. */
|
|
137
|
+
function hostnameOf(host) {
|
|
138
|
+
const value = host.trim().toLowerCase();
|
|
139
|
+
if (value.startsWith("[")) {
|
|
140
|
+
const end = value.indexOf("]");
|
|
141
|
+
return end === -1 ? value : value.slice(0, end + 1);
|
|
142
|
+
}
|
|
143
|
+
const colon = value.indexOf(":");
|
|
144
|
+
return colon === -1 ? value : value.slice(0, colon);
|
|
145
|
+
}
|
|
146
|
+
/** The client address out of an `X-Forwarded-For` value, walking right to left past trusted hops. */
|
|
147
|
+
function forwardedClient(header, trusted) {
|
|
148
|
+
const hops = ((Array.isArray(header) ? header.join(",") : header) ?? "").split(",").map((hop) => require_ip.normalizeIp(hop.trim().replace(/^\[|\]$/g, ""))).filter((hop) => (0, node_net.isIP)(hop) !== 0);
|
|
149
|
+
for (let index = hops.length - 1; index >= 0; index -= 1) {
|
|
150
|
+
const hop = hops[index];
|
|
151
|
+
if (hop !== void 0 && !require_ip.ipMatches(hop, trusted)) return hop;
|
|
152
|
+
}
|
|
153
|
+
return hops[0];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The client IP, protocol and host. Forwarding headers are read only when the
|
|
157
|
+
* socket peer is a trusted proxy; `X-Forwarded-For` is walked right to left,
|
|
158
|
+
* skipping trusted addresses, and the first untrusted one is the client.
|
|
159
|
+
*/
|
|
160
|
+
function resolveClient(request, trustedProxies) {
|
|
161
|
+
const peer = require_ip.normalizeIp(request.socket.remoteAddress ?? "");
|
|
162
|
+
const headers = request.headers;
|
|
163
|
+
let ip = peer;
|
|
164
|
+
let proto = request.socket.encrypted === true ? "https" : "http";
|
|
165
|
+
let host = firstValue(headers.host) ?? "";
|
|
166
|
+
if (peer !== "" && require_ip.ipMatches(peer, trustedProxies)) {
|
|
167
|
+
ip = forwardedClient(headers["x-forwarded-for"], trustedProxies) ?? peer;
|
|
168
|
+
const forwardedProto = firstValue(headers["x-forwarded-proto"])?.toLowerCase();
|
|
169
|
+
if (forwardedProto === "http" || forwardedProto === "https") proto = forwardedProto;
|
|
170
|
+
host = firstValue(headers["x-forwarded-host"]) ?? host;
|
|
171
|
+
}
|
|
172
|
+
host = host.toLowerCase();
|
|
173
|
+
return {
|
|
174
|
+
ip,
|
|
175
|
+
proto,
|
|
176
|
+
hostname: hostnameOf(host),
|
|
177
|
+
host
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region ../../packages/devtools/src/production/http/constants.ts
|
|
183
|
+
/** The header the dashboard sends the CSRF token in. */
|
|
184
|
+
const DEVTOOLS_CSRF_HEADER = "x-devtools-csrf";
|
|
185
|
+
/** The session cookie name over HTTPS (`__Secure-` makes browsers refuse it over plain HTTP). */
|
|
186
|
+
const DEVTOOLS_SESSION_COOKIE = "__Secure-warlock-devtools";
|
|
187
|
+
/** The session cookie name when `requireHttps` is off (local trials only). */
|
|
188
|
+
const DEVTOOLS_SESSION_COOKIE_INSECURE = "warlock-devtools";
|
|
189
|
+
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region ../../packages/devtools/src/production/http/context.ts
|
|
192
|
+
const contexts = /* @__PURE__ */ new WeakMap();
|
|
193
|
+
/** Stores the context of `request` (the gate calls this). */
|
|
194
|
+
function setDevtoolsRequestContext(request, context) {
|
|
195
|
+
contexts.set(request, context);
|
|
196
|
+
}
|
|
197
|
+
/** The context the gate stored for `request`, or `undefined` outside the production dashboard. */
|
|
198
|
+
function devtoolsRequestContext(request) {
|
|
199
|
+
return contexts.get(request);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region ../../packages/devtools/src/production/http/cookie.ts
|
|
204
|
+
/** The cookie name for the HTTPS mode. */
|
|
205
|
+
function sessionCookieName(secure) {
|
|
206
|
+
return secure ? DEVTOOLS_SESSION_COOKIE : DEVTOOLS_SESSION_COOKIE_INSECURE;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* `Set-Cookie` value for a session token (or a clearing cookie when `token`
|
|
210
|
+
* is `null`). The cookie is a browser-session cookie scoped to the dashboard
|
|
211
|
+
* path, `HttpOnly` and `SameSite=Strict`, with no `Domain`.
|
|
212
|
+
*/
|
|
213
|
+
function sessionCookie(token, options) {
|
|
214
|
+
const parts = [
|
|
215
|
+
`${sessionCookieName(options.secure)}=${token ?? ""}`,
|
|
216
|
+
`Path=${options.path}`,
|
|
217
|
+
"HttpOnly",
|
|
218
|
+
"SameSite=Strict"
|
|
219
|
+
];
|
|
220
|
+
if (options.secure) parts.push("Secure");
|
|
221
|
+
if (token === null) parts.push("Max-Age=0", "Expires=Thu, 01 Jan 1970 00:00:00 GMT");
|
|
222
|
+
return parts.join("; ");
|
|
223
|
+
}
|
|
224
|
+
/** The value of cookie `name` in a `Cookie` header, or `undefined`. */
|
|
225
|
+
function readCookie(header, name) {
|
|
226
|
+
if (!header) return void 0;
|
|
227
|
+
for (const pair of header.split(";")) {
|
|
228
|
+
const equals = pair.indexOf("=");
|
|
229
|
+
if (equals === -1) continue;
|
|
230
|
+
if (pair.slice(0, equals).trim() === name) {
|
|
231
|
+
const value = pair.slice(equals + 1).trim();
|
|
232
|
+
return value === "" ? void 0 : value;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region ../../packages/devtools/src/production/http/auth-routes.ts
|
|
239
|
+
const MAX_TEXT = 1024;
|
|
240
|
+
const MAX_EMAIL = 320;
|
|
241
|
+
const MAX_CODE = 128;
|
|
242
|
+
const MAX_TOKEN = 512;
|
|
243
|
+
const ROLES = ["admin", "viewer"];
|
|
244
|
+
/** A request body that is a plain JSON object, or `input.invalid`. */
|
|
245
|
+
function bodyOf(request) {
|
|
246
|
+
const body = request.body;
|
|
247
|
+
if (body === null || typeof body !== "object" || Array.isArray(body)) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
248
|
+
return body;
|
|
249
|
+
}
|
|
250
|
+
/** A non-empty string field no longer than `max`, or `input.invalid`. */
|
|
251
|
+
function text(body, key, max = MAX_TEXT) {
|
|
252
|
+
const value = body[key];
|
|
253
|
+
if (typeof value !== "string" || value === "" || value.length > max) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
254
|
+
return value;
|
|
255
|
+
}
|
|
256
|
+
/** An optional string field; `undefined` when absent, `input.invalid` when present but wrong. */
|
|
257
|
+
function optionalText(body, key, max = MAX_TEXT) {
|
|
258
|
+
return body[key] === void 0 ? void 0 : text(body, key, max);
|
|
259
|
+
}
|
|
260
|
+
function email(body) {
|
|
261
|
+
const value = text(body, "email", MAX_EMAIL).trim().toLowerCase();
|
|
262
|
+
if (!/^[^\s@]+@[^\s@]+$/.test(value)) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
265
|
+
function role(body) {
|
|
266
|
+
const value = body.role;
|
|
267
|
+
if (typeof value !== "string" || !ROLES.includes(value)) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
268
|
+
return value;
|
|
269
|
+
}
|
|
270
|
+
function purpose(body) {
|
|
271
|
+
const value = body.purpose;
|
|
272
|
+
if (value !== "register" && value !== "login") throw new require_errors.DevtoolsApiError("input.invalid");
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
function idParam(request) {
|
|
276
|
+
const { id } = request.params ?? {};
|
|
277
|
+
if (typeof id !== "string" || id === "" || id.length > MAX_CODE) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
278
|
+
return id;
|
|
279
|
+
}
|
|
280
|
+
/** The `current` password of a request that may have no body (a DELETE). */
|
|
281
|
+
function currentOf(request) {
|
|
282
|
+
return request.body === void 0 || request.body === null ? void 0 : optionalText(bodyOf(request), "current");
|
|
283
|
+
}
|
|
284
|
+
/** What the gate stored, for a route that is never reached without it. */
|
|
285
|
+
function contextOf(request) {
|
|
286
|
+
const context = devtoolsRequestContext(request);
|
|
287
|
+
if (!context?.authenticated) throw new Error("A dashboard route ran without its session.");
|
|
288
|
+
return {
|
|
289
|
+
meta: context.meta,
|
|
290
|
+
auth: context.authenticated
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function metaOf(request) {
|
|
294
|
+
const context = devtoolsRequestContext(request);
|
|
295
|
+
if (!context) throw new Error("A dashboard route ran without its request context.");
|
|
296
|
+
return context.meta;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Registers the sign-in, account and admin routes. All decisions live in the
|
|
300
|
+
* auth service; the handlers read the body, call it and answer. The gate
|
|
301
|
+
* (`mountProductionDevtools`) has already checked the session, role, origin
|
|
302
|
+
* and CSRF token by the time a handler runs.
|
|
303
|
+
*/
|
|
304
|
+
function registerAuthRoutes(server, deps) {
|
|
305
|
+
const { auth, store, state, production } = deps;
|
|
306
|
+
const cookieOptions = {
|
|
307
|
+
path: production.path,
|
|
308
|
+
secure: production.requireHttps
|
|
309
|
+
};
|
|
310
|
+
/** Sets the new session cookie when the step minted one; answers the step. */
|
|
311
|
+
const answerStep = (reply, step, recoveryCodes) => {
|
|
312
|
+
if (step.token !== null) reply.header("set-cookie", sessionCookie(step.token, cookieOptions));
|
|
313
|
+
return {
|
|
314
|
+
stage: step.stage,
|
|
315
|
+
csrf: step.csrf,
|
|
316
|
+
...recoveryCodes ? { recoveryCodes } : {}
|
|
317
|
+
};
|
|
318
|
+
};
|
|
319
|
+
/** The stage and CSRF token of the session in force, for answers that do not change it. */
|
|
320
|
+
const currentStep = (authenticated) => ({
|
|
321
|
+
stage: authenticated.stage,
|
|
322
|
+
csrf: auth.csrfToken(authenticated.session)
|
|
323
|
+
});
|
|
324
|
+
const clearCookie = (reply) => {
|
|
325
|
+
reply.header("set-cookie", sessionCookie(null, cookieOptions));
|
|
326
|
+
};
|
|
327
|
+
const requireAdmin = (authenticated) => {
|
|
328
|
+
if (authenticated.account.role !== "admin" || authenticated.stage !== "active") throw new require_errors.DevtoolsApiError("role.forbidden");
|
|
329
|
+
};
|
|
330
|
+
server.get("/api/auth/session", async (request, reply) => {
|
|
331
|
+
const token = readCookie(request.headers.cookie, sessionCookieName(production.requireHttps));
|
|
332
|
+
const authenticated = token === void 0 ? null : await auth.authenticate(token);
|
|
333
|
+
if (token !== void 0 && authenticated === null) clearCookie(reply);
|
|
334
|
+
return auth.sessionView(authenticated);
|
|
335
|
+
});
|
|
336
|
+
server.post("/api/auth/login", async (request, reply) => {
|
|
337
|
+
const body = bodyOf(request);
|
|
338
|
+
const step = await auth.login({
|
|
339
|
+
email: email(body),
|
|
340
|
+
password: text(body, "password")
|
|
341
|
+
}, metaOf(request));
|
|
342
|
+
return answerStep(reply, step);
|
|
343
|
+
});
|
|
344
|
+
server.post("/api/auth/setup", async (request, reply) => {
|
|
345
|
+
const body = bodyOf(request);
|
|
346
|
+
const step = await auth.setup({
|
|
347
|
+
token: text(body, "token", MAX_TOKEN),
|
|
348
|
+
password: text(body, "password")
|
|
349
|
+
}, metaOf(request));
|
|
350
|
+
return answerStep(reply, step);
|
|
351
|
+
});
|
|
352
|
+
server.post("/api/auth/invite/inspect", async (request) => {
|
|
353
|
+
const body = bodyOf(request);
|
|
354
|
+
return auth.inspectInvite(text(body, "token", MAX_TOKEN));
|
|
355
|
+
});
|
|
356
|
+
server.post("/api/auth/invite/accept", async (request, reply) => {
|
|
357
|
+
const body = bodyOf(request);
|
|
358
|
+
const step = await auth.acceptInvite({
|
|
359
|
+
token: text(body, "token", MAX_TOKEN),
|
|
360
|
+
password: text(body, "password")
|
|
361
|
+
}, metaOf(request));
|
|
362
|
+
return answerStep(reply, step);
|
|
363
|
+
});
|
|
364
|
+
const changePassword = async (request, reply) => {
|
|
365
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
366
|
+
const body = bodyOf(request);
|
|
367
|
+
const current = optionalText(body, "current");
|
|
368
|
+
const step = await auth.changePassword(authenticated, {
|
|
369
|
+
...current === void 0 ? {} : { current },
|
|
370
|
+
password: text(body, "password")
|
|
371
|
+
}, meta);
|
|
372
|
+
return answerStep(reply, step);
|
|
373
|
+
};
|
|
374
|
+
const startTotp = async (request) => {
|
|
375
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
376
|
+
return auth.startTotp(authenticated, meta);
|
|
377
|
+
};
|
|
378
|
+
const confirmTotp = async (request, reply) => {
|
|
379
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
380
|
+
const body = bodyOf(request);
|
|
381
|
+
const result = await auth.confirmTotp(authenticated, text(body, "code", MAX_CODE), meta, optionalText(body, "current"));
|
|
382
|
+
return answerStep(reply, result.step, result.recoveryCodes);
|
|
383
|
+
};
|
|
384
|
+
const passkeyOptions = async (request) => {
|
|
385
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
386
|
+
return auth.passkeyOptions(authenticated, purpose(bodyOf(request)), meta);
|
|
387
|
+
};
|
|
388
|
+
const passkeyVerify = async (request, reply) => {
|
|
389
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
390
|
+
const body = bodyOf(request);
|
|
391
|
+
if (body.response === null || typeof body.response !== "object") throw new require_errors.DevtoolsApiError("input.invalid");
|
|
392
|
+
const result = await auth.verifyPasskey(authenticated, purpose(body), body.response, meta, optionalText(body, "current"));
|
|
393
|
+
if (result.step === null) return {
|
|
394
|
+
...currentStep(authenticated),
|
|
395
|
+
...result.recoveryCodes ? { recoveryCodes: result.recoveryCodes } : {}
|
|
396
|
+
};
|
|
397
|
+
return answerStep(reply, result.step, result.recoveryCodes);
|
|
398
|
+
};
|
|
399
|
+
server.post("/api/auth/password", changePassword);
|
|
400
|
+
server.post("/api/auth/totp/start", startTotp);
|
|
401
|
+
server.post("/api/auth/totp/confirm", confirmTotp);
|
|
402
|
+
server.post("/api/auth/passkey/options", passkeyOptions);
|
|
403
|
+
server.post("/api/auth/passkey/verify", passkeyVerify);
|
|
404
|
+
server.post("/api/auth/mfa", async (request, reply) => {
|
|
405
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
406
|
+
const body = bodyOf(request);
|
|
407
|
+
const code = optionalText(body, "code", MAX_CODE);
|
|
408
|
+
const recoveryCode = optionalText(body, "recoveryCode", MAX_CODE);
|
|
409
|
+
if (code === void 0 === (recoveryCode === void 0)) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
410
|
+
const step = await auth.verifySecondFactor(authenticated, code === void 0 ? { recoveryCode } : { code }, meta);
|
|
411
|
+
return answerStep(reply, step);
|
|
412
|
+
});
|
|
413
|
+
server.post("/api/auth/logout", async (request, reply) => {
|
|
414
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
415
|
+
await auth.logout(authenticated, meta);
|
|
416
|
+
clearCookie(reply);
|
|
417
|
+
return reply.code(204).send();
|
|
418
|
+
});
|
|
419
|
+
server.get("/api/account", async (request) => {
|
|
420
|
+
const { auth: authenticated } = contextOf(request);
|
|
421
|
+
return auth.accountView(authenticated.account);
|
|
422
|
+
});
|
|
423
|
+
server.get("/api/account/sessions", async (request) => {
|
|
424
|
+
const { auth: authenticated } = contextOf(request);
|
|
425
|
+
return auth.listSessions(authenticated);
|
|
426
|
+
});
|
|
427
|
+
server.delete("/api/account/sessions/:id", async (request, reply) => {
|
|
428
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
429
|
+
await auth.revokeSession(authenticated, idParam(request), meta);
|
|
430
|
+
return reply.code(204).send();
|
|
431
|
+
});
|
|
432
|
+
server.post("/api/account/password", changePassword);
|
|
433
|
+
server.post("/api/account/totp/start", startTotp);
|
|
434
|
+
server.post("/api/account/totp/confirm", confirmTotp);
|
|
435
|
+
server.delete("/api/account/totp", async (request, reply) => {
|
|
436
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
437
|
+
await auth.removeTotp(authenticated, meta, currentOf(request));
|
|
438
|
+
return reply.code(204).send();
|
|
439
|
+
});
|
|
440
|
+
server.post("/api/account/recovery-codes", async (request) => {
|
|
441
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
442
|
+
return { codes: await auth.regenerateRecoveryCodes(authenticated, optionalText(bodyOf(request), "code", MAX_CODE) ?? "", meta) };
|
|
443
|
+
});
|
|
444
|
+
server.post("/api/account/passkey/options", passkeyOptions);
|
|
445
|
+
server.post("/api/account/passkey/verify", passkeyVerify);
|
|
446
|
+
server.delete("/api/account/passkeys/:id", async (request, reply) => {
|
|
447
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
448
|
+
await auth.removePasskey(authenticated, idParam(request), meta, currentOf(request));
|
|
449
|
+
return reply.code(204).send();
|
|
450
|
+
});
|
|
451
|
+
server.get("/api/admin/users", async (request) => {
|
|
452
|
+
const { auth: authenticated } = contextOf(request);
|
|
453
|
+
return auth.listUsers(authenticated);
|
|
454
|
+
});
|
|
455
|
+
server.post("/api/admin/invites", async (request) => {
|
|
456
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
457
|
+
const body = bodyOf(request);
|
|
458
|
+
return auth.createInvite(authenticated, {
|
|
459
|
+
email: email(body),
|
|
460
|
+
role: role(body)
|
|
461
|
+
}, meta);
|
|
462
|
+
});
|
|
463
|
+
server.delete("/api/admin/invites/:id", async (request, reply) => {
|
|
464
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
465
|
+
await auth.revokeInvite(authenticated, idParam(request), meta);
|
|
466
|
+
return reply.code(204).send();
|
|
467
|
+
});
|
|
468
|
+
server.patch("/api/admin/users/:id", async (request) => {
|
|
469
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
470
|
+
return auth.setRole(authenticated, idParam(request), role(bodyOf(request)), meta);
|
|
471
|
+
});
|
|
472
|
+
server.delete("/api/admin/users/:id", async (request, reply) => {
|
|
473
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
474
|
+
await auth.removeAccount(authenticated, idParam(request), meta);
|
|
475
|
+
return reply.code(204).send();
|
|
476
|
+
});
|
|
477
|
+
server.post("/api/admin/users/:id/reset-mfa", async (request, reply) => {
|
|
478
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
479
|
+
await auth.resetSecondFactor(authenticated, idParam(request), meta);
|
|
480
|
+
return reply.code(204).send();
|
|
481
|
+
});
|
|
482
|
+
server.post("/api/admin/users/:id/sign-out", async (request, reply) => {
|
|
483
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
484
|
+
await auth.signOutAccount(authenticated, idParam(request), meta);
|
|
485
|
+
return reply.code(204).send();
|
|
486
|
+
});
|
|
487
|
+
server.get("/api/admin/audit", async (request) => {
|
|
488
|
+
const { auth: authenticated } = contextOf(request);
|
|
489
|
+
requireAdmin(authenticated);
|
|
490
|
+
const query = request.query;
|
|
491
|
+
const types = query.type?.split(",").map((type) => type.trim()).filter((type) => type !== "");
|
|
492
|
+
const before = query.before === void 0 ? void 0 : Number(query.before);
|
|
493
|
+
const limit = query.limit === void 0 ? 100 : Number(query.limit);
|
|
494
|
+
if (before !== void 0 && !Number.isFinite(before) || !Number.isInteger(limit) || limit < 1) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
495
|
+
return store.audit.list({
|
|
496
|
+
...types && types.length > 0 ? { types } : {},
|
|
497
|
+
...query.account ? { accountId: query.account } : {},
|
|
498
|
+
...before === void 0 ? {} : { before },
|
|
499
|
+
limit: Math.min(limit, 500)
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
server.post("/api/admin/disable", async (request, reply) => {
|
|
503
|
+
const { auth: authenticated, meta } = contextOf(request);
|
|
504
|
+
if (bodyOf(request).confirm !== "disable") throw new require_errors.DevtoolsApiError("input.invalid");
|
|
505
|
+
await auth.disableDashboard(authenticated, meta);
|
|
506
|
+
state.invalidate();
|
|
507
|
+
return reply.code(204).send();
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
//#endregion
|
|
512
|
+
//#region ../../packages/devtools/src/production/http/headers.ts
|
|
513
|
+
/** Headers on every dashboard answer that is not the uniform 404. */
|
|
514
|
+
const DASHBOARD_HEADERS = {
|
|
515
|
+
"Cache-Control": "no-store",
|
|
516
|
+
"X-Robots-Tag": "noindex, nofollow, noarchive",
|
|
517
|
+
"Referrer-Policy": "no-referrer",
|
|
518
|
+
"X-Content-Type-Options": "nosniff",
|
|
519
|
+
"X-Frame-Options": "DENY",
|
|
520
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
521
|
+
"Cross-Origin-Resource-Policy": "same-origin",
|
|
522
|
+
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), publickey-credentials-get=(self), publickey-credentials-create=(self)"
|
|
523
|
+
};
|
|
524
|
+
/** The policy of the dashboard's own HTML: scripts and styles from the bundle only. */
|
|
525
|
+
const DASHBOARD_CSP = "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; object-src 'none'";
|
|
526
|
+
/**
|
|
527
|
+
* The policy of the opt-in API documentation pages: scripts from the dashboard
|
|
528
|
+
* only (Scalar and Swagger UI probe `Function` in a try/catch and work without
|
|
529
|
+
* `'unsafe-eval'`), inline styles, and framing by the dashboard itself, whose
|
|
530
|
+
* API tab shows them in a frame.
|
|
531
|
+
*/
|
|
532
|
+
const DOCS_CSP = "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'; object-src 'none'";
|
|
533
|
+
|
|
534
|
+
//#endregion
|
|
535
|
+
//#region ../../packages/devtools/src/production/http/mount.ts
|
|
536
|
+
/** Open live streams a session may hold, and an instance may serve. */
|
|
537
|
+
const MAX_STREAMS_PER_SESSION = 3;
|
|
538
|
+
const MAX_STREAMS_PER_INSTANCE = 50;
|
|
539
|
+
const MAX_USER_AGENT = 200;
|
|
540
|
+
/** Methods that never change state. */
|
|
541
|
+
const SAFE_METHODS = /* @__PURE__ */ new Set([
|
|
542
|
+
"GET",
|
|
543
|
+
"HEAD",
|
|
544
|
+
"OPTIONS"
|
|
545
|
+
]);
|
|
546
|
+
/** The routes of the opt-in API documentation pages (they keep a CSP of their own). */
|
|
547
|
+
const DOCS_PATTERNS = /* @__PURE__ */ new Set(["/docs", "/docs/swagger"]);
|
|
548
|
+
/** The mail preview's HTML: framed by the dashboard, with a policy of its own. */
|
|
549
|
+
const MAIL_HTML_PATTERN = "/api/mails/:id/html";
|
|
550
|
+
/** Which tab a route belongs to: a route of a tab that is off answers the uniform 404. */
|
|
551
|
+
const TAB_OF_ROUTE = [
|
|
552
|
+
["/api/resources", "resources"],
|
|
553
|
+
["/api/queries", "queries"],
|
|
554
|
+
["/api/routes/stats", "routes"],
|
|
555
|
+
["/api/routes", "routes"],
|
|
556
|
+
["/api/health", "health"],
|
|
557
|
+
["/api/errors", "errors"],
|
|
558
|
+
["/api/jobs", "jobs"],
|
|
559
|
+
["/api/logs", "logs"],
|
|
560
|
+
["/api/requests", "requests"],
|
|
561
|
+
["/api/mails", "mail"],
|
|
562
|
+
["/api/ai/agent-runs", "agents"],
|
|
563
|
+
["/api/ai", "ai"],
|
|
564
|
+
["/api/openapi.json", "api"],
|
|
565
|
+
["/api/postman.json", "api"],
|
|
566
|
+
["/docs", "api"]
|
|
567
|
+
];
|
|
568
|
+
function tabOf(pattern) {
|
|
569
|
+
return TAB_OF_ROUTE.find(([prefix]) => pattern === prefix || pattern.startsWith(`${prefix}/`))?.[1];
|
|
570
|
+
}
|
|
571
|
+
function tokenHash(token) {
|
|
572
|
+
return (0, node_crypto.createHash)("sha256").update(token).digest("hex");
|
|
573
|
+
}
|
|
574
|
+
/** Requests that were answered with the uniform 404: they get none of the dashboard's headers. */
|
|
575
|
+
const uniformNotFound = /* @__PURE__ */ new WeakSet();
|
|
576
|
+
/**
|
|
577
|
+
* The origin the browser must send for an unsafe request: `production.origin`,
|
|
578
|
+
* else the scheme and host the request was addressed to.
|
|
579
|
+
*/
|
|
580
|
+
function originAllowed(request, expected) {
|
|
581
|
+
const origin = request.headers.origin;
|
|
582
|
+
const site = request.headers["sec-fetch-site"];
|
|
583
|
+
if (site !== void 0 && site !== "same-origin") return false;
|
|
584
|
+
if (origin !== void 0) return origin.toLowerCase() === expected.toLowerCase();
|
|
585
|
+
return site === "same-origin";
|
|
586
|
+
}
|
|
587
|
+
function hasBody(request) {
|
|
588
|
+
const length = request.headers["content-length"];
|
|
589
|
+
return length !== void 0 && length !== "0" || request.headers["transfer-encoding"] !== void 0;
|
|
590
|
+
}
|
|
591
|
+
function isJson(request) {
|
|
592
|
+
return /^application\/json\s*(;|$)/i.test(request.headers["content-type"] ?? "");
|
|
593
|
+
}
|
|
594
|
+
function sessionAccessOk(access, stage) {
|
|
595
|
+
if (stage === "active") return true;
|
|
596
|
+
return access.type === "pending" && access.stages.includes(stage);
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Mounts the production monitor under `production.path` in one encapsulated
|
|
600
|
+
* Fastify context: the gate (uniform 404, HTTPS, allowed IPs, open state),
|
|
601
|
+
* session and role checks from the access table, CSRF and origin checks,
|
|
602
|
+
* security headers and CORS stripping, the auth/account/admin routes, and
|
|
603
|
+
* the shared data routes of the enabled tabs.
|
|
604
|
+
*/
|
|
605
|
+
function mountProductionDevtools(server, deps) {
|
|
606
|
+
const { production, auth, state, store } = deps;
|
|
607
|
+
const prefix = production.path;
|
|
608
|
+
const cookieName = sessionCookieName(production.requireHttps);
|
|
609
|
+
const enabledTabs = new Set(deps.builtInPanels);
|
|
610
|
+
const lookup = createAccessLookup(productionRouteAccess(deps.panels.map((panel) => panel.id)));
|
|
611
|
+
const streamsPerSession = /* @__PURE__ */ new Map();
|
|
612
|
+
let streamsInstance = 0;
|
|
613
|
+
let lastWarningAt = 0;
|
|
614
|
+
const warn = (message) => {
|
|
615
|
+
const now = Date.now();
|
|
616
|
+
if (now - lastWarningAt < 6e4) return;
|
|
617
|
+
lastWarningAt = now;
|
|
618
|
+
deps.warn?.(message);
|
|
619
|
+
};
|
|
620
|
+
const notFound = (request, reply) => {
|
|
621
|
+
uniformNotFound.add(request);
|
|
622
|
+
reply.callNotFound();
|
|
623
|
+
return reply;
|
|
624
|
+
};
|
|
625
|
+
const streamSession = async (request) => {
|
|
626
|
+
const authenticated = devtoolsRequestContext(request)?.authenticated;
|
|
627
|
+
if (!authenticated) throw new Error("A dashboard stream was opened without a session.");
|
|
628
|
+
const sessionId = authenticated.session.id;
|
|
629
|
+
const token = readCookie(request.headers.cookie, cookieName);
|
|
630
|
+
const open = streamsPerSession.get(sessionId) ?? 0;
|
|
631
|
+
if (open >= MAX_STREAMS_PER_SESSION || streamsInstance >= MAX_STREAMS_PER_INSTANCE) throw new require_errors.DevtoolsApiError("stream.too-many");
|
|
632
|
+
streamsPerSession.set(sessionId, open + 1);
|
|
633
|
+
streamsInstance += 1;
|
|
634
|
+
let released = false;
|
|
635
|
+
return {
|
|
636
|
+
role: authenticated.account.role,
|
|
637
|
+
logLevels: production.logLevels,
|
|
638
|
+
headers: DASHBOARD_HEADERS,
|
|
639
|
+
async revalidate() {
|
|
640
|
+
if (token === void 0 || !await state.isOpen()) return false;
|
|
641
|
+
const session = await store.sessions.findByTokenHash(tokenHash(token));
|
|
642
|
+
const now = Date.now();
|
|
643
|
+
if (session === null || session.stage !== "active" || now >= session.expiresAt || now - session.lastSeenAt >= production.session.idleMinutes * 6e4) return false;
|
|
644
|
+
return await store.accounts.findById(session.accountId) !== null;
|
|
645
|
+
},
|
|
646
|
+
release() {
|
|
647
|
+
if (released) return;
|
|
648
|
+
released = true;
|
|
649
|
+
streamsInstance -= 1;
|
|
650
|
+
const left = (streamsPerSession.get(sessionId) ?? 1) - 1;
|
|
651
|
+
if (left <= 0) streamsPerSession.delete(sessionId);
|
|
652
|
+
else streamsPerSession.set(sessionId, left);
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
};
|
|
656
|
+
const routes = {
|
|
657
|
+
...deps.routes,
|
|
658
|
+
mode: "production",
|
|
659
|
+
isDevelopment: () => false,
|
|
660
|
+
instance: deps.instance,
|
|
661
|
+
insights: deps.insights,
|
|
662
|
+
collector: deps.routes.collector ?? deps.capture.collector,
|
|
663
|
+
requests: deps.routes.requests ?? deps.capture.requests,
|
|
664
|
+
explainShape: deps.routes.explainShape ?? ((shapeId) => deps.capture.explain(shapeId)),
|
|
665
|
+
builtInPanels: deps.builtInPanels,
|
|
666
|
+
streamSession
|
|
667
|
+
};
|
|
668
|
+
server.register(async (instance) => {
|
|
669
|
+
instance.addHook("onRequest", async (request, reply) => {
|
|
670
|
+
let open;
|
|
671
|
+
try {
|
|
672
|
+
open = await state.isOpen();
|
|
673
|
+
} catch (error) {
|
|
674
|
+
warn(`The devtools dashboard state could not be read and the dashboard is closed: ${error instanceof Error ? error.message : String(error)}`);
|
|
675
|
+
open = false;
|
|
676
|
+
}
|
|
677
|
+
if (!open) return notFound(request, reply);
|
|
678
|
+
const client = resolveClient(request, production.trustedProxies);
|
|
679
|
+
if (production.requireHttps && client.proto !== "https") return notFound(request, reply);
|
|
680
|
+
if (production.allowedIps.length > 0 && !require_ip.ipMatches(client.ip, production.allowedIps)) return notFound(request, reply);
|
|
681
|
+
const userAgent = request.headers["user-agent"];
|
|
682
|
+
const expectedOrigin = production.origin ?? `${client.proto}://${client.host}`;
|
|
683
|
+
const meta = {
|
|
684
|
+
ip: client.ip,
|
|
685
|
+
userAgent: typeof userAgent === "string" ? userAgent.slice(0, MAX_USER_AGENT) : null,
|
|
686
|
+
origin: expectedOrigin,
|
|
687
|
+
hostname: client.hostname
|
|
688
|
+
};
|
|
689
|
+
const routeUrl = request.routeOptions.url ?? "";
|
|
690
|
+
const relative = routeUrl.startsWith(prefix) ? routeUrl.slice(prefix.length) : routeUrl;
|
|
691
|
+
const pattern = relative === "" ? "/" : relative;
|
|
692
|
+
const method = request.method;
|
|
693
|
+
const sent = request.originalUrl ?? request.raw.url ?? "";
|
|
694
|
+
if (sent.split("?")[0] === prefix) {
|
|
695
|
+
setDevtoolsRequestContext(request, {
|
|
696
|
+
client,
|
|
697
|
+
meta,
|
|
698
|
+
rule: void 0,
|
|
699
|
+
pattern,
|
|
700
|
+
authenticated: null
|
|
701
|
+
});
|
|
702
|
+
if (method !== "GET" && method !== "HEAD") return notFound(request, reply);
|
|
703
|
+
const search = sent.includes("?") ? `?${sent.split("?").slice(1).join("?")}` : "";
|
|
704
|
+
return reply.redirect(`${prefix}/${search}`, 308);
|
|
705
|
+
}
|
|
706
|
+
const tab = tabOf(pattern);
|
|
707
|
+
if (tab !== void 0 && !enabledTabs.has(tab)) return notFound(request, reply);
|
|
708
|
+
const rule = lookup(method, pattern);
|
|
709
|
+
const access = rule?.access ?? { type: "admin" };
|
|
710
|
+
let authenticated = null;
|
|
711
|
+
if (access.type !== "public") {
|
|
712
|
+
try {
|
|
713
|
+
authenticated = await auth.authenticate(readCookie(request.headers.cookie, cookieName));
|
|
714
|
+
} catch (error) {
|
|
715
|
+
warn(`The devtools session could not be checked and the request was refused: ${error instanceof Error ? error.message : String(error)}`);
|
|
716
|
+
return notFound(request, reply);
|
|
717
|
+
}
|
|
718
|
+
if (authenticated === null || !sessionAccessOk(access, authenticated.stage)) return notFound(request, reply);
|
|
719
|
+
if (access.type === "admin" && authenticated.account.role !== "admin") {
|
|
720
|
+
setDevtoolsRequestContext(request, {
|
|
721
|
+
client,
|
|
722
|
+
meta,
|
|
723
|
+
rule,
|
|
724
|
+
pattern,
|
|
725
|
+
authenticated
|
|
726
|
+
});
|
|
727
|
+
throw new require_errors.DevtoolsApiError("role.forbidden");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
setDevtoolsRequestContext(request, {
|
|
731
|
+
client,
|
|
732
|
+
meta,
|
|
733
|
+
rule,
|
|
734
|
+
pattern,
|
|
735
|
+
authenticated
|
|
736
|
+
});
|
|
737
|
+
if (SAFE_METHODS.has(method)) return;
|
|
738
|
+
if (!originAllowed(request, expectedOrigin)) {
|
|
739
|
+
if (authenticated === null) return notFound(request, reply);
|
|
740
|
+
throw new require_errors.DevtoolsApiError("csrf.invalid");
|
|
741
|
+
}
|
|
742
|
+
if (hasBody(request) && !isJson(request)) throw new require_errors.DevtoolsApiError("input.invalid");
|
|
743
|
+
if (authenticated !== null) {
|
|
744
|
+
const header = request.headers[DEVTOOLS_CSRF_HEADER];
|
|
745
|
+
if (!auth.verifyCsrf(authenticated, Array.isArray(header) ? header[0] : header)) throw new require_errors.DevtoolsApiError("csrf.invalid");
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
instance.addHook("onSend", async (request, reply, payload) => {
|
|
749
|
+
if (uniformNotFound.has(request)) return payload;
|
|
750
|
+
for (const name of Object.keys(reply.getHeaders())) if (name.startsWith("access-control-")) reply.removeHeader(name);
|
|
751
|
+
reply.headers(DASHBOARD_HEADERS);
|
|
752
|
+
const pattern = devtoolsRequestContext(request)?.pattern ?? "";
|
|
753
|
+
if (pattern === MAIL_HTML_PATTERN) {
|
|
754
|
+
reply.header("X-Frame-Options", "SAMEORIGIN");
|
|
755
|
+
return payload;
|
|
756
|
+
}
|
|
757
|
+
if (DOCS_PATTERNS.has(pattern)) reply.header("X-Frame-Options", "SAMEORIGIN");
|
|
758
|
+
if (String(reply.getHeader("content-type") ?? "").startsWith("text/html")) reply.header("Content-Security-Policy", DOCS_PATTERNS.has(pattern) ? DOCS_CSP : DASHBOARD_CSP);
|
|
759
|
+
return payload;
|
|
760
|
+
});
|
|
761
|
+
instance.addHook("onResponse", async (request, reply) => {
|
|
762
|
+
const context = devtoolsRequestContext(request);
|
|
763
|
+
const type = context?.rule?.audit;
|
|
764
|
+
if (!context || !type || reply.statusCode < 200 || reply.statusCode > 299) return;
|
|
765
|
+
const params = request.params ?? {};
|
|
766
|
+
const detail = type === "explain.run" ? { shapeId: params.shapeId ?? null } : type === "panel.action" ? {
|
|
767
|
+
panel: context.pattern.split("/")[2] ?? null,
|
|
768
|
+
method: request.method,
|
|
769
|
+
route: context.pattern
|
|
770
|
+
} : {};
|
|
771
|
+
await auth.audit(type, {
|
|
772
|
+
auth: context.authenticated,
|
|
773
|
+
meta: context.meta,
|
|
774
|
+
detail
|
|
775
|
+
});
|
|
776
|
+
});
|
|
777
|
+
instance.setErrorHandler((error, request, reply) => {
|
|
778
|
+
if (error instanceof require_errors.DevtoolsApiError) return reply.code(error.statusCode).send(error.toBody());
|
|
779
|
+
const status = error.statusCode;
|
|
780
|
+
if (status !== void 0 && status >= 400 && status < 500) return reply.code(status).send(error.code?.startsWith("FST_ERR_") ? new require_errors.DevtoolsApiError("input.invalid").toBody() : { error: error.message });
|
|
781
|
+
const ref = require_errors.createErrorRef();
|
|
782
|
+
deps.warn?.(`Devtools dashboard error ${ref} on ${request.method} ${devtoolsRequestContext(request)?.pattern ?? "(dashboard route)"}: ${error.message}`);
|
|
783
|
+
request.log.error({
|
|
784
|
+
ref,
|
|
785
|
+
err: error
|
|
786
|
+
}, "devtools dashboard error");
|
|
787
|
+
return reply.code(500).send({
|
|
788
|
+
error: require_errors.DEVTOOLS_ERROR_MESSAGES["server.error"],
|
|
789
|
+
code: "server.error",
|
|
790
|
+
ref
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
require_stream_route.registerUiAssets(instance, { notFound });
|
|
794
|
+
registerAuthRoutes(instance, deps);
|
|
795
|
+
instance.get("/api/panels", async () => require_panels.listPanelInfos(deps.panels, deps.builtInPanels));
|
|
796
|
+
require_stream_route.registerRequestsRoutes(instance, routes);
|
|
797
|
+
require_stream_route.registerRouteRoutes(instance, routes);
|
|
798
|
+
require_stream_route.registerInsightsRoutes(instance, routes);
|
|
799
|
+
require_stream_route.registerLogRoutes(instance, routes);
|
|
800
|
+
require_stream_route.registerShapeExplainRoute(instance, routes);
|
|
801
|
+
require_stream_route.registerPanelRoutes(instance, deps.panels);
|
|
802
|
+
if (enabledTabs.has("mail")) require_stream_route.registerMailRoutes(instance, routes);
|
|
803
|
+
if (enabledTabs.has("ai") || enabledTabs.has("agents")) require_stream_route.registerAiRoutes(instance, routes);
|
|
804
|
+
if (enabledTabs.has("api")) require_stream_route.registerOpenApiRoutes(instance, routes);
|
|
805
|
+
require_stream_route.registerStreamRoute(instance, routes);
|
|
806
|
+
}, { prefix });
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
//#endregion
|
|
810
|
+
//#region ../../packages/devtools/src/production/http/state.ts
|
|
811
|
+
/** SHA-256 hex of a dashboard path: what `disabledPathHash` holds. */
|
|
812
|
+
function hashDashboardPath(path) {
|
|
813
|
+
return (0, node_crypto.createHash)("sha256").update(path).digest("hex");
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* The open/closed state with a `ttlMs` cache (default 5000). Open means the
|
|
817
|
+
* dashboard is not disabled for this path and not locked. A failed read is
|
|
818
|
+
* not cached and rejects, so the caller can fail closed.
|
|
819
|
+
*/
|
|
820
|
+
function createDashboardState(store, path, options = {}) {
|
|
821
|
+
const ttlMs = options.ttlMs ?? 5e3;
|
|
822
|
+
const now = options.now ?? Date.now;
|
|
823
|
+
const pathHash = hashDashboardPath(path);
|
|
824
|
+
let cached = null;
|
|
825
|
+
let pending = null;
|
|
826
|
+
let generation = 0;
|
|
827
|
+
const read = async () => {
|
|
828
|
+
const started = generation;
|
|
829
|
+
const state = await store.state.read();
|
|
830
|
+
const at = now();
|
|
831
|
+
const disabled = state.disabled && state.disabledPathHash === pathHash;
|
|
832
|
+
const locked = state.lockedUntil !== null && state.lockedUntil > at;
|
|
833
|
+
const open = !disabled && !locked;
|
|
834
|
+
if (started === generation) cached = {
|
|
835
|
+
open,
|
|
836
|
+
at
|
|
837
|
+
};
|
|
838
|
+
return open;
|
|
839
|
+
};
|
|
840
|
+
return {
|
|
841
|
+
async isOpen() {
|
|
842
|
+
if (cached !== null && now() - cached.at < ttlMs) return cached.open;
|
|
843
|
+
if (pending === null) {
|
|
844
|
+
const current = read().finally(() => {
|
|
845
|
+
if (pending === current) pending = null;
|
|
846
|
+
});
|
|
847
|
+
pending = current;
|
|
848
|
+
}
|
|
849
|
+
return pending;
|
|
850
|
+
},
|
|
851
|
+
invalidate() {
|
|
852
|
+
generation += 1;
|
|
853
|
+
cached = null;
|
|
854
|
+
pending = null;
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
//#endregion
|
|
860
|
+
exports.DEVTOOLS_CSRF_HEADER = DEVTOOLS_CSRF_HEADER;
|
|
861
|
+
exports.DEVTOOLS_SESSION_COOKIE = DEVTOOLS_SESSION_COOKIE;
|
|
862
|
+
exports.DEVTOOLS_SESSION_COOKIE_INSECURE = DEVTOOLS_SESSION_COOKIE_INSECURE;
|
|
863
|
+
exports.createDashboardState = createDashboardState;
|
|
864
|
+
exports.devtoolsRequestContext = devtoolsRequestContext;
|
|
865
|
+
exports.ipMatches = require_ip.ipMatches;
|
|
866
|
+
exports.mountProductionDevtools = mountProductionDevtools;
|
|
867
|
+
exports.productionRouteAccess = productionRouteAccess;
|
|
868
|
+
exports.resolveClient = resolveClient;
|
|
869
|
+
exports.sessionCookie = sessionCookie;
|
|
870
|
+
//# sourceMappingURL=http-CabcUwBx.cjs.map
|