@hamur/devtools 0.0.0-stage → 0.0.1
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/LICENSE +22 -0
- package/README.md +24 -2
- package/dist/src/client.js +780 -0
- package/dist/src/index.js +1711 -0
- package/dist/types/src/apps/locator.d.ts +3 -0
- package/dist/types/src/bridge.d.ts +8 -0
- package/dist/types/src/client.d.ts +3 -0
- package/dist/types/src/dev/analyze.d.ts +31 -0
- package/dist/types/src/dev/assets.d.ts +33 -0
- package/dist/types/src/dev/authz.d.ts +49 -0
- package/dist/types/src/dev/bundle.d.ts +22 -0
- package/dist/types/src/dev/config.d.ts +24 -0
- package/dist/types/src/dev/handler.d.ts +20 -0
- package/dist/types/src/dev/modules.d.ts +28 -0
- package/dist/types/src/dev/overview.d.ts +20 -0
- package/dist/types/src/dev/processes.d.ts +4 -0
- package/dist/types/src/dev/server-routes.d.ts +7 -0
- package/dist/types/src/dev/tasks.d.ts +26 -0
- package/dist/types/src/dev/timeline.d.ts +20 -0
- package/dist/types/src/dev/virtual.d.ts +9 -0
- package/dist/types/src/icons.d.ts +24 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/locator.d.ts +17 -0
- package/dist/types/src/mask.d.ts +8 -0
- package/dist/types/src/module.d.ts +27 -0
- package/dist/types/src/open-in-editor.d.ts +4 -0
- package/dist/types/src/options.d.ts +14 -0
- package/dist/types/src/panel/document.d.ts +16 -0
- package/dist/types/src/panel/groups.d.ts +14 -0
- package/dist/types/src/panel/index.d.ts +1 -0
- package/dist/types/src/panel/settings.d.ts +69 -0
- package/dist/types/src/panel/style.d.ts +1 -0
- package/dist/types/src/panel/ui.d.ts +34 -0
- package/dist/types/src/server/authz.d.ts +30 -0
- package/dist/types/src/server/index.d.ts +3 -0
- package/dist/types/src/server/routes.d.ts +6 -0
- package/dist/types/src/server/storage.d.ts +56 -0
- package/dist/types/src/source-link.d.ts +2 -0
- package/dist/types/src/tabs/analyze-model.d.ts +31 -0
- package/dist/types/src/tabs/analyze.d.ts +2 -0
- package/dist/types/src/tabs/assets.d.ts +11 -0
- package/dist/types/src/tabs/audit-model.d.ts +42 -0
- package/dist/types/src/tabs/audit.d.ts +2 -0
- package/dist/types/src/tabs/authz-model.d.ts +11 -0
- package/dist/types/src/tabs/authz.d.ts +2 -0
- package/dist/types/src/tabs/config.d.ts +8 -0
- package/dist/types/src/tabs/inspect.d.ts +7 -0
- package/dist/types/src/tabs/modules.d.ts +4 -0
- package/dist/types/src/tabs/overview.d.ts +2 -0
- package/dist/types/src/tabs/pages.d.ts +2 -0
- package/dist/types/src/tabs/payload.d.ts +6 -0
- package/dist/types/src/tabs/plugins-model.d.ts +15 -0
- package/dist/types/src/tabs/plugins.d.ts +2 -0
- package/dist/types/src/tabs/seo-model.d.ts +36 -0
- package/dist/types/src/tabs/seo.d.ts +2 -0
- package/dist/types/src/tabs/server-routes.d.ts +2 -0
- package/dist/types/src/tabs/storage.d.ts +13 -0
- package/dist/types/src/tabs/tasks-model.d.ts +16 -0
- package/dist/types/src/tabs/tasks.d.ts +2 -0
- package/dist/types/src/tabs/terminals-model.d.ts +53 -0
- package/dist/types/src/tabs/terminals.d.ts +2 -0
- package/dist/types/src/tabs/timeline-model.d.ts +58 -0
- package/dist/types/src/tabs/timeline.d.ts +2 -0
- package/dist/types/src/tester.d.ts +43 -0
- package/dist/types/src/toolbar/element.d.ts +28 -0
- package/dist/types/src/toolbar/shortcut.d.ts +14 -0
- package/dist/types/src/toolbar/style.d.ts +3 -0
- package/dist/types/src/view-context.d.ts +13 -0
- package/package.json +28 -4
- package/src/apps/locator.ts +131 -0
- package/src/bridge.ts +172 -0
- package/src/client.ts +10 -0
- package/src/dev/analyze.ts +128 -0
- package/src/dev/assets.ts +208 -0
- package/src/dev/authz.ts +162 -0
- package/src/dev/bundle.ts +91 -0
- package/src/dev/config.ts +132 -0
- package/src/dev/handler.ts +161 -0
- package/src/dev/modules.ts +60 -0
- package/src/dev/overview.ts +57 -0
- package/src/dev/processes.ts +34 -0
- package/src/dev/server-routes.ts +14 -0
- package/src/dev/tasks.ts +93 -0
- package/src/dev/timeline.ts +43 -0
- package/src/dev/virtual.ts +40 -0
- package/src/env.d.ts +21 -0
- package/src/icons.ts +48 -0
- package/src/index.ts +12 -0
- package/src/locator.ts +31 -0
- package/src/mask.ts +16 -0
- package/src/module.ts +352 -0
- package/src/open-in-editor.ts +18 -0
- package/src/options.ts +36 -0
- package/src/panel/document.ts +60 -0
- package/src/panel/groups.ts +41 -0
- package/src/panel/index.ts +429 -0
- package/src/panel/settings.ts +224 -0
- package/src/panel/style.ts +321 -0
- package/src/panel/ui.ts +52 -0
- package/src/server/authz.ts +110 -0
- package/src/server/index.ts +25 -0
- package/src/server/routes.ts +51 -0
- package/src/server/storage.ts +251 -0
- package/src/source-link.ts +29 -0
- package/src/tabs/analyze-model.ts +97 -0
- package/src/tabs/analyze.ts +217 -0
- package/src/tabs/assets.ts +375 -0
- package/src/tabs/audit-model.ts +216 -0
- package/src/tabs/audit.ts +223 -0
- package/src/tabs/authz-model.ts +28 -0
- package/src/tabs/authz.ts +337 -0
- package/src/tabs/config.ts +250 -0
- package/src/tabs/inspect.ts +101 -0
- package/src/tabs/modules.ts +58 -0
- package/src/tabs/overview.ts +104 -0
- package/src/tabs/pages.ts +188 -0
- package/src/tabs/payload.ts +224 -0
- package/src/tabs/plugins-model.ts +44 -0
- package/src/tabs/plugins.ts +95 -0
- package/src/tabs/seo-model.ts +140 -0
- package/src/tabs/seo.ts +162 -0
- package/src/tabs/server-routes.ts +469 -0
- package/src/tabs/storage.ts +313 -0
- package/src/tabs/tasks-model.ts +59 -0
- package/src/tabs/tasks.ts +287 -0
- package/src/tabs/terminals-model.ts +127 -0
- package/src/tabs/terminals.ts +294 -0
- package/src/tabs/timeline-model.ts +200 -0
- package/src/tabs/timeline.ts +260 -0
- package/src/tester.ts +158 -0
- package/src/toolbar/element.ts +428 -0
- package/src/toolbar/shortcut.ts +56 -0
- package/src/toolbar/style.ts +94 -0
- package/src/view-context.ts +93 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { defineDevtoolsApp } from "hamur/app"
|
|
2
|
+
import { element, pill, UI } from "../panel/ui.ts"
|
|
3
|
+
import { sourceLink } from "../source-link.ts"
|
|
4
|
+
import {
|
|
5
|
+
auditSnapshot,
|
|
6
|
+
countBySeverity,
|
|
7
|
+
type AuditElement,
|
|
8
|
+
type AuditFinding,
|
|
9
|
+
type AuditSeverity,
|
|
10
|
+
type AuditSnapshot,
|
|
11
|
+
} from "./audit-model.ts"
|
|
12
|
+
|
|
13
|
+
const SELECTOR =
|
|
14
|
+
"html, img, a, button, [role=button], input, select, textarea, h1, h2, h3, h4, h5, h6, [id], [tabindex], video, audio"
|
|
15
|
+
const OUTLINE =
|
|
16
|
+
"position:fixed;box-sizing:border-box;pointer-events:none;z-index:2147483646;border:2px solid #ff8a4a;background:rgba(255,138,74,.14)"
|
|
17
|
+
|
|
18
|
+
type Filter = "all" | AuditSeverity
|
|
19
|
+
const FILTERS: Array<[Filter, string]> = [
|
|
20
|
+
["all", "All"],
|
|
21
|
+
["error", "Errors"],
|
|
22
|
+
["warning", "Warnings"],
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
const keyOf = (finding: AuditFinding): string => `${finding.ref}:${finding.rule}`
|
|
26
|
+
|
|
27
|
+
const textOf = (node: Element): string => (node.textContent ?? "").trim()
|
|
28
|
+
|
|
29
|
+
function referencedText(doc: Document, ids: string | null): string {
|
|
30
|
+
return (ids ?? "")
|
|
31
|
+
.split(/\s+/)
|
|
32
|
+
.map((id) => {
|
|
33
|
+
const target = id ? doc.getElementById(id) : null
|
|
34
|
+
return target ? textOf(target) : ""
|
|
35
|
+
})
|
|
36
|
+
.join(" ")
|
|
37
|
+
.trim()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function nameOf(doc: Document, node: Element): string {
|
|
41
|
+
const label = node.getAttribute("aria-label")?.trim()
|
|
42
|
+
if (label) return label
|
|
43
|
+
const referenced = referencedText(doc, node.getAttribute("aria-labelledby"))
|
|
44
|
+
if (referenced) return referenced
|
|
45
|
+
const text = textOf(node)
|
|
46
|
+
if (text) return text
|
|
47
|
+
const image = node.querySelector("img[alt]:not([alt=''])")?.getAttribute("alt")?.trim()
|
|
48
|
+
return image || node.getAttribute("title")?.trim() || ""
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isLabelled(doc: Document, node: Element): boolean {
|
|
52
|
+
if (
|
|
53
|
+
node.getAttribute("aria-label")?.trim() ||
|
|
54
|
+
node.getAttribute("title")?.trim() ||
|
|
55
|
+
referencedText(doc, node.getAttribute("aria-labelledby"))
|
|
56
|
+
)
|
|
57
|
+
return true
|
|
58
|
+
return ((node as HTMLInputElement).labels?.length ?? 0) > 0
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Reads the facts the rules need from the page's document; `nodes[ref]` is the live element of each snapshot entry. */
|
|
62
|
+
function snapshotPage(doc: Document, win: Window): { snapshot: AuditSnapshot; nodes: Element[] } {
|
|
63
|
+
const nodes = Array.from(doc.querySelectorAll(SELECTOR)).filter((node) => !node.closest("hamur-devtools"))
|
|
64
|
+
const elements = nodes.map((node, ref): AuditElement => {
|
|
65
|
+
const rect = node.getBoundingClientRect()
|
|
66
|
+
const attrs: Record<string, string> = {}
|
|
67
|
+
for (const { name, value } of Array.from(node.attributes)) attrs[name] = value
|
|
68
|
+
const image = node as HTMLImageElement
|
|
69
|
+
const source = node.closest("[data-hamur-src]")?.getAttribute("data-hamur-src")
|
|
70
|
+
return {
|
|
71
|
+
ref,
|
|
72
|
+
tag: node.localName,
|
|
73
|
+
attrs,
|
|
74
|
+
name: nameOf(doc, node),
|
|
75
|
+
labelled: isLabelled(doc, node),
|
|
76
|
+
top: rect.top + win.scrollY,
|
|
77
|
+
width: rect.width,
|
|
78
|
+
height: rect.height,
|
|
79
|
+
naturalWidth: node.localName === "img" && image.complete ? image.naturalWidth : 0,
|
|
80
|
+
naturalHeight: node.localName === "img" && image.complete ? image.naturalHeight : 0,
|
|
81
|
+
...(source ? { source } : {}),
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
return { snapshot: { viewportHeight: win.innerHeight, elements }, nodes }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function findingElement(finding: AuditFinding, selected: boolean, onHighlight: () => void): HTMLElement {
|
|
88
|
+
const dot = element("span", {
|
|
89
|
+
className: `flex-none size-2 rounded-full ${finding.severity === "error" ? "bg-red" : "bg-amber"}`,
|
|
90
|
+
})
|
|
91
|
+
const button = element("button", { type: "button", className: UI.button, onclick: onHighlight }, "Highlight")
|
|
92
|
+
button.dataset.highlight = ""
|
|
93
|
+
const actions = element("div", { className: "flex gap-1.5 pl-4" }, button)
|
|
94
|
+
const head = element(
|
|
95
|
+
"div",
|
|
96
|
+
{ className: "flex items-center gap-2" },
|
|
97
|
+
dot,
|
|
98
|
+
element("span", { className: "text-sm font-medium" }, finding.title),
|
|
99
|
+
element("span", { className: "flex-1" }),
|
|
100
|
+
)
|
|
101
|
+
if (finding.source) {
|
|
102
|
+
head.append(
|
|
103
|
+
element(
|
|
104
|
+
"span",
|
|
105
|
+
{ className: "min-w-0 truncate font-mono text-xs text-fg-3" },
|
|
106
|
+
sourceLink(finding.source.file, finding.source.line, finding.source.column),
|
|
107
|
+
),
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
const card = element(
|
|
111
|
+
"div",
|
|
112
|
+
{ className: `flex flex-col gap-2 mx-1.5 px-2.5 py-3 rounded-lg ${selected ? "bg-active" : ""}` },
|
|
113
|
+
head,
|
|
114
|
+
element("code", { className: "pl-4 text-xs font-mono text-fg-3 break-all" }, finding.snippet),
|
|
115
|
+
element("span", { className: "pl-4 text-sm text-fg-2" }, finding.hint),
|
|
116
|
+
actions,
|
|
117
|
+
)
|
|
118
|
+
card.dataset.finding = finding.rule
|
|
119
|
+
card.dataset.ref = String(finding.ref)
|
|
120
|
+
card.dataset.severity = finding.severity
|
|
121
|
+
return card
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export default defineDevtoolsApp({
|
|
125
|
+
init(canvas, ctx) {
|
|
126
|
+
if (!ctx.page) {
|
|
127
|
+
const config = JSON.parse(document.getElementById("hamur-devtools-config")!.textContent!) as {
|
|
128
|
+
shortcut: string | false
|
|
129
|
+
}
|
|
130
|
+
canvas.append(
|
|
131
|
+
element(
|
|
132
|
+
"p",
|
|
133
|
+
{ className: UI.notice },
|
|
134
|
+
`Open this tab from the app page${config.shortcut === false ? "" : ` (${config.shortcut})`}`,
|
|
135
|
+
),
|
|
136
|
+
)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const page = ctx.page
|
|
140
|
+
const doc = window.parent.document
|
|
141
|
+
const win = window.parent
|
|
142
|
+
canvas.className = "flex flex-col flex-1 min-h-0"
|
|
143
|
+
let findings: AuditFinding[] = []
|
|
144
|
+
let nodes: Element[] = []
|
|
145
|
+
let filter: Filter = "all"
|
|
146
|
+
let selected: string | undefined
|
|
147
|
+
let outline: HTMLElement | undefined
|
|
148
|
+
|
|
149
|
+
const clear = () => {
|
|
150
|
+
outline?.remove()
|
|
151
|
+
outline = undefined
|
|
152
|
+
}
|
|
153
|
+
const highlight = (finding: AuditFinding) => {
|
|
154
|
+
const node = nodes[finding.ref]
|
|
155
|
+
if (!node?.isConnected) return
|
|
156
|
+
clear()
|
|
157
|
+
node.scrollIntoView({ block: "center", inline: "nearest" })
|
|
158
|
+
const rect = node.getBoundingClientRect()
|
|
159
|
+
outline = doc.createElement("div")
|
|
160
|
+
outline.dataset.hamurAuditOutline = ""
|
|
161
|
+
outline.style.cssText = `${OUTLINE};left:${rect.left}px;top:${rect.top}px;width:${rect.width}px;height:${rect.height}px`
|
|
162
|
+
doc.body.append(outline)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const path = element("span", { className: "font-mono text-xs text-fg-3" })
|
|
166
|
+
path.dataset.path = ""
|
|
167
|
+
const run = element("button", { type: "button", className: UI.button }, "Run again")
|
|
168
|
+
run.dataset.run = ""
|
|
169
|
+
const pills = element("div", { className: "flex items-center gap-0.5" })
|
|
170
|
+
canvas.append(element("div", { className: UI.strip }, pills, element("span", { className: "flex-1" }), path, run))
|
|
171
|
+
const list = element("div", { className: "flex-1 min-h-0 overflow-auto pt-1.5 pb-2" })
|
|
172
|
+
canvas.append(list)
|
|
173
|
+
|
|
174
|
+
const paint = () => {
|
|
175
|
+
const counts = countBySeverity(findings)
|
|
176
|
+
const total: Record<Filter, number> = { all: findings.length, error: counts.error, warning: counts.warning }
|
|
177
|
+
pills.replaceChildren(
|
|
178
|
+
...FILTERS.map(([name, label]) => {
|
|
179
|
+
const button = element(
|
|
180
|
+
"button",
|
|
181
|
+
{ type: "button", className: pill(filter === name), onclick: () => ((filter = name), paint()) },
|
|
182
|
+
label,
|
|
183
|
+
element("span", { className: filter === name ? "text-fg-3" : "text-fg-4" }, String(total[name])),
|
|
184
|
+
)
|
|
185
|
+
button.dataset.filter = name
|
|
186
|
+
return button
|
|
187
|
+
}),
|
|
188
|
+
)
|
|
189
|
+
ctx.setBadge(findings.length ? String(findings.length) : undefined)
|
|
190
|
+
const shown = findings.filter(({ severity }) => filter === "all" || severity === filter)
|
|
191
|
+
if (!shown.length)
|
|
192
|
+
return void list.replaceChildren(
|
|
193
|
+
element(
|
|
194
|
+
"p",
|
|
195
|
+
{ className: UI.notice },
|
|
196
|
+
findings.length ? "No findings of this kind" : "No findings on this page",
|
|
197
|
+
),
|
|
198
|
+
)
|
|
199
|
+
list.replaceChildren(
|
|
200
|
+
...shown.map((finding) =>
|
|
201
|
+
findingElement(finding, selected === keyOf(finding), () => {
|
|
202
|
+
selected = keyOf(finding)
|
|
203
|
+
highlight(finding)
|
|
204
|
+
paint()
|
|
205
|
+
}),
|
|
206
|
+
),
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const audit = () => {
|
|
211
|
+
clear()
|
|
212
|
+
selected = undefined
|
|
213
|
+
const read = snapshotPage(doc, win)
|
|
214
|
+
nodes = read.nodes
|
|
215
|
+
findings = auditSnapshot(read.snapshot)
|
|
216
|
+
path.textContent = page.router.current().path
|
|
217
|
+
paint()
|
|
218
|
+
}
|
|
219
|
+
run.addEventListener("click", audit)
|
|
220
|
+
audit()
|
|
221
|
+
return clear
|
|
222
|
+
},
|
|
223
|
+
})
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DecisionRow } from "../server/authz.ts"
|
|
2
|
+
|
|
3
|
+
/** The tab keeps the checks the dev server keeps. */
|
|
4
|
+
export const KEPT_DECISIONS = 200
|
|
5
|
+
|
|
6
|
+
/** `incoming` (oldest first, as a push sends them) in front of `list` (newest first), the checks the tab already holds left out, cut to `KEPT_DECISIONS`. */
|
|
7
|
+
export function mergeDecisions(list: readonly DecisionRow[], incoming: readonly DecisionRow[]): DecisionRow[] {
|
|
8
|
+
const known = new Set(list.map((row) => row.seq))
|
|
9
|
+
const fresh = incoming.filter((row) => !known.has(row.seq)).reverse()
|
|
10
|
+
return [...fresh, ...list].slice(0, KEPT_DECISIONS)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const uidText = (uid: { type: string; id: string }): string => `${uid.type}::${JSON.stringify(uid.id)}`
|
|
14
|
+
|
|
15
|
+
/** The explain form's context box as the request's `context`: empty is none, anything else must be a JSON object. */
|
|
16
|
+
export function contextValue(text: string): Record<string, unknown> | undefined {
|
|
17
|
+
const trimmed = text.trim()
|
|
18
|
+
if (trimmed === "") return undefined
|
|
19
|
+
let value: unknown
|
|
20
|
+
try {
|
|
21
|
+
value = JSON.parse(trimmed)
|
|
22
|
+
} catch (error) {
|
|
23
|
+
throw new Error(`The context must be JSON: ${error instanceof Error ? error.message : String(error)}`)
|
|
24
|
+
}
|
|
25
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
26
|
+
throw new Error("The context must be a JSON object")
|
|
27
|
+
return value as Record<string, unknown>
|
|
28
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { defineDevtoolsApp } from "hamur/app"
|
|
2
|
+
import type { AuthzExplanation } from "hamur/server"
|
|
3
|
+
import type { ActionRow, EntityTypeRow, PoliciesAnswer, PolicyRow } from "../dev/authz.ts"
|
|
4
|
+
import { element, tab, UI } from "../panel/ui.ts"
|
|
5
|
+
import { sourceLink } from "../source-link.ts"
|
|
6
|
+
import type { DecisionRow } from "../server/authz.ts"
|
|
7
|
+
import { contextValue, mergeDecisions, uidText } from "./authz-model.ts"
|
|
8
|
+
|
|
9
|
+
type Part = "policies" | "schema" | "decisions" | "explain"
|
|
10
|
+
|
|
11
|
+
const PARTS: Array<[Part, string]> = [
|
|
12
|
+
["policies", "Policies"],
|
|
13
|
+
["schema", "Schema"],
|
|
14
|
+
["decisions", "Decisions"],
|
|
15
|
+
["explain", "Explain"],
|
|
16
|
+
]
|
|
17
|
+
const OUTCOME_COLORS: Record<string, string> = { allow: "text-green", deny: "text-red", missing: "text-amber" }
|
|
18
|
+
const messageOf = (error: unknown): string => (error instanceof Error ? error.message : String(error))
|
|
19
|
+
const time = (epoch: number): string => new Date(epoch).toLocaleTimeString()
|
|
20
|
+
|
|
21
|
+
async function load<T>(url: string, init?: RequestInit): Promise<T> {
|
|
22
|
+
const response = await fetch(url, init)
|
|
23
|
+
const answer = (await response.json().catch(() => ({}))) as T & { error?: string }
|
|
24
|
+
if (!response.ok) throw new Error(answer.error ?? `${url} answered ${response.status}`)
|
|
25
|
+
return answer
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const effectChip = (effect: string): HTMLElement =>
|
|
29
|
+
element("span", { className: `${UI.chip} ${effect === "forbid" ? "text-red" : "text-green"}` }, effect)
|
|
30
|
+
|
|
31
|
+
export default defineDevtoolsApp({
|
|
32
|
+
async init(canvas, ctx) {
|
|
33
|
+
canvas.className = "flex flex-col flex-1 min-h-0"
|
|
34
|
+
const strip = element("div", {
|
|
35
|
+
className: "flex-none flex gap-0.5 px-3 pt-1 border-b border-line overflow-x-auto",
|
|
36
|
+
role: "tablist",
|
|
37
|
+
})
|
|
38
|
+
strip.setAttribute("aria-label", "Authorization views")
|
|
39
|
+
const note = element("p", { className: `m-0 px-4 py-1 ${UI.fail} empty:hidden` })
|
|
40
|
+
note.dataset.error = ""
|
|
41
|
+
const body = element("div", { className: "flex-1 min-h-0 overflow-auto" })
|
|
42
|
+
canvas.append(strip, note, body)
|
|
43
|
+
|
|
44
|
+
let part: Part = "policies"
|
|
45
|
+
let set: PoliciesAnswer = { policies: [], entityTypes: [], actions: [] }
|
|
46
|
+
let decisions: DecisionRow[] = []
|
|
47
|
+
const form = { principal: "", action: "", resource: "", context: "" }
|
|
48
|
+
let result: Node | undefined
|
|
49
|
+
|
|
50
|
+
function paintStrip(): void {
|
|
51
|
+
strip.replaceChildren(
|
|
52
|
+
...PARTS.map(([name, title]) => {
|
|
53
|
+
const button = element("button", { type: "button", role: "tab", className: tab(name === part) }, title)
|
|
54
|
+
button.dataset.part = name
|
|
55
|
+
button.setAttribute("aria-selected", String(name === part))
|
|
56
|
+
button.addEventListener("click", () => {
|
|
57
|
+
part = name
|
|
58
|
+
paint()
|
|
59
|
+
})
|
|
60
|
+
return button
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function policyItem(policy: PolicyRow): HTMLElement {
|
|
66
|
+
const item = element(
|
|
67
|
+
"details",
|
|
68
|
+
{ className: "border-b border-line" },
|
|
69
|
+
element(
|
|
70
|
+
"summary",
|
|
71
|
+
{ className: "flex items-center gap-3 min-h-9 px-4 cursor-pointer hover:bg-hover text-sm" },
|
|
72
|
+
effectChip(policy.effect),
|
|
73
|
+
element("strong", { className: "flex-none font-mono text-sm font-medium text-fg" }, policy.id),
|
|
74
|
+
...(policy.template ? [element("span", { className: UI.chip }, "template")] : []),
|
|
75
|
+
element(
|
|
76
|
+
"span",
|
|
77
|
+
{ className: "flex-1 min-w-0 font-mono text-xs text-fg-3 truncate" },
|
|
78
|
+
`principal ${policy.principal} · action ${policy.action} · resource ${policy.resource}`,
|
|
79
|
+
),
|
|
80
|
+
element("code", { className: "flex-none font-mono text-xs text-fg-3" }, sourceLink(policy.file, policy.line)),
|
|
81
|
+
),
|
|
82
|
+
element(
|
|
83
|
+
"pre",
|
|
84
|
+
{ className: "m-0 mx-4 mb-2 px-3 py-2 rounded-lg bg-field font-mono text-xs overflow-auto text-fg-2" },
|
|
85
|
+
policy.text,
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
item.dataset.id = policy.id
|
|
89
|
+
return item
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function entityItem(type: EntityTypeRow): HTMLElement {
|
|
93
|
+
const item = element(
|
|
94
|
+
"li",
|
|
95
|
+
{ className: "flex flex-col gap-1 px-4 py-2.5 border-b border-line" },
|
|
96
|
+
element("strong", { className: "font-mono text-sm font-medium text-fg" }, type.name),
|
|
97
|
+
...(type.parents.length ? [element("span", { className: UI.hint }, `in ${type.parents.join(", ")}`)] : []),
|
|
98
|
+
...(type.enum ? [element("span", { className: UI.hint }, `one of ${type.enum.join(", ")}`)] : []),
|
|
99
|
+
...type.attributes.map(({ name, type: text, required }) =>
|
|
100
|
+
element("code", { className: "font-mono text-xs text-fg-2" }, `${name}${required ? "" : "?"}: ${text}`),
|
|
101
|
+
),
|
|
102
|
+
...(type.tags ? [element("code", { className: "font-mono text-xs text-fg-2" }, `tags: ${type.tags}`)] : []),
|
|
103
|
+
)
|
|
104
|
+
item.dataset.entity = type.name
|
|
105
|
+
return item
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function actionItem(action: ActionRow): HTMLElement {
|
|
109
|
+
const item = element(
|
|
110
|
+
"li",
|
|
111
|
+
{ className: "flex flex-col gap-1 px-4 py-2.5 border-b border-line" },
|
|
112
|
+
element("strong", { className: "font-mono text-sm font-medium text-fg" }, action.name),
|
|
113
|
+
...(action.groups.length ? [element("span", { className: UI.hint }, `in ${action.groups.join(", ")}`)] : []),
|
|
114
|
+
...(action.appliesTo
|
|
115
|
+
? [
|
|
116
|
+
element(
|
|
117
|
+
"code",
|
|
118
|
+
{ className: "font-mono text-xs text-fg-2" },
|
|
119
|
+
`appliesTo principal: ${action.appliesTo.principals.join(" | ")}, resource: ${action.appliesTo.resources.join(" | ")}, context: ${action.appliesTo.context}`,
|
|
120
|
+
),
|
|
121
|
+
]
|
|
122
|
+
: []),
|
|
123
|
+
)
|
|
124
|
+
item.dataset.action = action.name
|
|
125
|
+
return item
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function decisionItem(row: DecisionRow): HTMLElement {
|
|
129
|
+
const outcome = element(
|
|
130
|
+
"span",
|
|
131
|
+
{ className: `flex-none w-14 font-mono text-xs ${OUTCOME_COLORS[row.outcome] ?? ""}` },
|
|
132
|
+
row.outcome,
|
|
133
|
+
)
|
|
134
|
+
const cell = (width: string, text: string) =>
|
|
135
|
+
element("span", { className: `flex-none ${width} font-mono text-xs text-fg-2 truncate`, title: text }, text)
|
|
136
|
+
const item = element(
|
|
137
|
+
"li",
|
|
138
|
+
{ className: "flex items-center gap-3 min-h-9 px-4 border-b border-line text-sm" },
|
|
139
|
+
element("span", { className: "flex-none w-20 text-xs text-fg-3" }, time(row.time)),
|
|
140
|
+
cell("w-16", row.requestId.slice(0, 8)),
|
|
141
|
+
cell("w-36", row.principal ? uidText(row.principal) : "–"),
|
|
142
|
+
cell("w-24", row.action),
|
|
143
|
+
cell("w-40", uidText(row.resource)),
|
|
144
|
+
outcome,
|
|
145
|
+
cell("w-40", row.determining.map(({ id }) => id).join(", ") || "–"),
|
|
146
|
+
cell("w-32", row.errors.map(({ id }) => id).join(", ") || "–"),
|
|
147
|
+
element("span", { className: "flex-none w-10 text-xs text-fg-3" }, String(row.entities)),
|
|
148
|
+
element("span", { className: "flex-none w-16 text-xs text-fg-3" }, `${row.ms.toFixed(1)} ms`),
|
|
149
|
+
)
|
|
150
|
+
item.dataset.outcome = row.outcome
|
|
151
|
+
item.dataset.action = row.action
|
|
152
|
+
item.dataset.resource = uidText(row.resource)
|
|
153
|
+
return item
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function explanation(answer: AuthzExplanation): HTMLElement {
|
|
157
|
+
const policyOf = new Map(set.policies.map((policy) => [policy.id, policy]))
|
|
158
|
+
const box = element(
|
|
159
|
+
"div",
|
|
160
|
+
{ className: "flex flex-col gap-2 py-2" },
|
|
161
|
+
element("strong", { className: `font-mono text-sm ${OUTCOME_COLORS[answer.decision] ?? ""}` }, answer.decision),
|
|
162
|
+
element(
|
|
163
|
+
"span",
|
|
164
|
+
{ className: UI.hint },
|
|
165
|
+
`${answer.determining.length ? (answer.decision === "deny" ? "Forbidden by" : "Permitted by") : "No policy applies: denied by default"}; ${answer.entities} entities loaded, ${answer.ms.toFixed(1)} ms`,
|
|
166
|
+
),
|
|
167
|
+
...answer.determining.map((found) => {
|
|
168
|
+
const policy = policyOf.get(found.id)
|
|
169
|
+
const entry = element(
|
|
170
|
+
"div",
|
|
171
|
+
{ className: "flex flex-col gap-1" },
|
|
172
|
+
element(
|
|
173
|
+
"div",
|
|
174
|
+
{ className: "flex items-center gap-2" },
|
|
175
|
+
effectChip(found.effect),
|
|
176
|
+
element("strong", { className: "font-mono text-sm text-fg" }, found.id),
|
|
177
|
+
element("code", { className: "font-mono text-xs text-fg-3" }, sourceLink(found.file, found.line)),
|
|
178
|
+
),
|
|
179
|
+
...(policy
|
|
180
|
+
? [
|
|
181
|
+
element(
|
|
182
|
+
"pre",
|
|
183
|
+
{ className: "m-0 px-3 py-2 rounded-lg bg-field font-mono text-xs overflow-auto text-fg-2" },
|
|
184
|
+
policy.text,
|
|
185
|
+
),
|
|
186
|
+
]
|
|
187
|
+
: []),
|
|
188
|
+
)
|
|
189
|
+
entry.dataset.policy = found.id
|
|
190
|
+
return entry
|
|
191
|
+
}),
|
|
192
|
+
...answer.errors.map(({ id, message }) => {
|
|
193
|
+
const entry = element("span", { className: UI.fail }, `Policy ${JSON.stringify(id)} erred: ${message}`)
|
|
194
|
+
entry.dataset.erring = id
|
|
195
|
+
return entry
|
|
196
|
+
}),
|
|
197
|
+
)
|
|
198
|
+
box.dataset.decision = answer.decision
|
|
199
|
+
return box
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function explainForm(): HTMLElement {
|
|
203
|
+
const output = element("div", { className: "px-4" })
|
|
204
|
+
output.dataset.result = ""
|
|
205
|
+
if (result) output.append(result)
|
|
206
|
+
const field = (name: keyof typeof form, label: string, placeholder: string, list?: string) => {
|
|
207
|
+
const input = element("input", {
|
|
208
|
+
className: UI.field,
|
|
209
|
+
name,
|
|
210
|
+
placeholder,
|
|
211
|
+
value: form[name],
|
|
212
|
+
autocomplete: "off",
|
|
213
|
+
})
|
|
214
|
+
if (list) input.setAttribute("list", list)
|
|
215
|
+
input.addEventListener("input", () => (form[name] = input.value))
|
|
216
|
+
return element(
|
|
217
|
+
"label",
|
|
218
|
+
{ className: "flex flex-col gap-0.5" },
|
|
219
|
+
element("span", { className: UI.label }, label),
|
|
220
|
+
input,
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
const context = element("textarea", {
|
|
224
|
+
className: `${UI.textarea} min-h-12`,
|
|
225
|
+
name: "context",
|
|
226
|
+
rows: 2,
|
|
227
|
+
placeholder: '{ "ip": "127.0.0.1" }',
|
|
228
|
+
value: form.context,
|
|
229
|
+
})
|
|
230
|
+
context.addEventListener("input", () => (form.context = context.value))
|
|
231
|
+
const actions = element("datalist", { id: "authz-actions" })
|
|
232
|
+
actions.append(...set.actions.map(({ name }) => element("option", { value: name })))
|
|
233
|
+
const submit = element("button", { type: "submit", className: UI.primary }, "Explain")
|
|
234
|
+
const formNode = element(
|
|
235
|
+
"form",
|
|
236
|
+
{ className: "flex flex-col gap-2 p-4" },
|
|
237
|
+
field("principal", "Principal", 'User::"7"'),
|
|
238
|
+
field("action", "Action", "update", "authz-actions"),
|
|
239
|
+
field("resource", "Resource", 'Document::"42"'),
|
|
240
|
+
element(
|
|
241
|
+
"label",
|
|
242
|
+
{ className: "flex flex-col gap-0.5" },
|
|
243
|
+
element("span", { className: UI.label }, "Context (JSON)"),
|
|
244
|
+
context,
|
|
245
|
+
),
|
|
246
|
+
actions,
|
|
247
|
+
element("div", {}, submit),
|
|
248
|
+
)
|
|
249
|
+
formNode.dataset.explain = ""
|
|
250
|
+
formNode.addEventListener("submit", (event) => {
|
|
251
|
+
event.preventDefault()
|
|
252
|
+
void (async () => {
|
|
253
|
+
submit.disabled = true
|
|
254
|
+
try {
|
|
255
|
+
const answer = await load<AuthzExplanation>("api/authz/explain", {
|
|
256
|
+
method: "POST",
|
|
257
|
+
headers: { "content-type": "application/json" },
|
|
258
|
+
body: JSON.stringify({
|
|
259
|
+
principal: form.principal,
|
|
260
|
+
action: form.action,
|
|
261
|
+
resource: form.resource,
|
|
262
|
+
context: contextValue(form.context),
|
|
263
|
+
}),
|
|
264
|
+
})
|
|
265
|
+
note.textContent = ""
|
|
266
|
+
result = explanation(answer)
|
|
267
|
+
} catch (error) {
|
|
268
|
+
note.textContent = messageOf(error)
|
|
269
|
+
result = undefined
|
|
270
|
+
}
|
|
271
|
+
submit.disabled = false
|
|
272
|
+
output.replaceChildren(...(result ? [result] : []))
|
|
273
|
+
})()
|
|
274
|
+
})
|
|
275
|
+
return element("div", {}, formNode, output)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function paint(): void {
|
|
279
|
+
paintStrip()
|
|
280
|
+
if (part === "policies")
|
|
281
|
+
body.replaceChildren(
|
|
282
|
+
set.policies.length
|
|
283
|
+
? element("div", {}, ...set.policies.map(policyItem))
|
|
284
|
+
: element("p", { className: UI.notice }, "No policy"),
|
|
285
|
+
)
|
|
286
|
+
else if (part === "schema")
|
|
287
|
+
body.replaceChildren(
|
|
288
|
+
element("span", { className: `block mx-4 mt-2 ${UI.label}` }, "Entity types"),
|
|
289
|
+
element("ul", { className: "m-0 p-0 list-none" }, ...set.entityTypes.map(entityItem)),
|
|
290
|
+
element("span", { className: `block mx-4 mt-2 ${UI.label}` }, "Actions"),
|
|
291
|
+
element("ul", { className: "m-0 p-0 list-none" }, ...set.actions.map(actionItem)),
|
|
292
|
+
)
|
|
293
|
+
else if (part === "decisions")
|
|
294
|
+
body.replaceChildren(
|
|
295
|
+
decisions.length
|
|
296
|
+
? element("ul", { className: "m-0 p-0 list-none" }, ...decisions.map(decisionItem))
|
|
297
|
+
: element("p", { className: UI.notice }, "No check yet"),
|
|
298
|
+
)
|
|
299
|
+
else body.replaceChildren(explainForm())
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
let queued: ReturnType<typeof setTimeout> | undefined
|
|
303
|
+
const offPush = ctx.on("devtools:authz", (data) => {
|
|
304
|
+
const pushed = (data as { decisions?: DecisionRow[] }).decisions
|
|
305
|
+
if (!Array.isArray(pushed)) return
|
|
306
|
+
decisions = mergeDecisions(decisions, pushed)
|
|
307
|
+
if (part !== "decisions" || queued !== undefined) return
|
|
308
|
+
queued = setTimeout(() => {
|
|
309
|
+
queued = undefined
|
|
310
|
+
if (part === "decisions") paint()
|
|
311
|
+
}, 50)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
async function refresh(): Promise<void> {
|
|
315
|
+
try {
|
|
316
|
+
set = await load<PoliciesAnswer>("api/authz/policies")
|
|
317
|
+
decisions = (await load<{ decisions: DecisionRow[] }>("api/authz/decisions")).decisions
|
|
318
|
+
note.textContent = ""
|
|
319
|
+
} catch (error) {
|
|
320
|
+
note.textContent = messageOf(error)
|
|
321
|
+
}
|
|
322
|
+
if (part !== "explain") paint()
|
|
323
|
+
}
|
|
324
|
+
const offStatus = ctx.on("devtools:status", (data) => {
|
|
325
|
+
if ((data as { server?: string }).server === "ready") void refresh()
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
paint()
|
|
329
|
+
await refresh()
|
|
330
|
+
|
|
331
|
+
return () => {
|
|
332
|
+
offPush()
|
|
333
|
+
offStatus()
|
|
334
|
+
clearTimeout(queued)
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
})
|