dsh-builtin-browser 0.5.6 → 0.5.8
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 +148 -0
- package/README.en.md +75 -81
- package/README.md +75 -82
- package/client.js +831 -384
- package/desktop-bridge/install-and-start.cmd +4 -5
- package/desktop-bridge/install.mjs +322 -141
- package/desktop-bridge/maintenance-progress.ps1 +166 -0
- package/desktop-bridge/plugin-browser-bridge.js +157 -19
- package/desktop-bridge/request-desktop-quit.ps1 +170 -0
- package/docs/tool-reference.md +20 -1
- package/docs/user-guide.md +28 -4
- package/lib/browser/runtime.d.ts +3 -1
- package/lib/browser/runtime.js +9 -0
- package/lib/browser/types.d.ts +42 -0
- package/lib/browser-electron/authenticated-route.d.ts +27 -0
- package/lib/browser-electron/authenticated-route.js +39 -0
- package/lib/browser-electron/automatic-maintenance.d.ts +41 -0
- package/lib/browser-electron/automatic-maintenance.js +426 -0
- package/lib/browser-electron/bridge-auto.d.ts +93 -0
- package/lib/browser-electron/bridge-auto.js +113 -0
- package/lib/browser-electron/bridge-connection.d.ts +0 -2
- package/lib/browser-electron/bridge-connection.js +2 -4
- package/lib/browser-electron/bridge-endpoint.js +8 -19
- package/lib/browser-electron/bridge-install.d.ts +85 -0
- package/lib/browser-electron/bridge-install.js +211 -0
- package/lib/browser-electron/bridge-maintenance.d.ts +24 -0
- package/lib/browser-electron/bridge-maintenance.js +125 -0
- package/lib/browser-electron/bridge-status.d.ts +10 -3
- package/lib/browser-electron/bridge-status.js +74 -24
- package/lib/browser-electron/desktop-bridge-host.d.ts +11 -5
- package/lib/browser-electron/desktop-bridge-host.js +67 -25
- package/lib/browser-electron/desktop-identity.js +21 -9
- package/lib/browser-electron/electron-provision.d.ts +1 -1
- package/lib/browser-electron/electron-provision.js +36 -13
- package/lib/browser-electron/entry.js +173 -67
- package/lib/browser-electron/ephemeral-profile.js +67 -26
- package/lib/browser-electron/history-store.js +2 -2
- package/lib/browser-electron/host-main.js +48 -38
- package/lib/browser-electron/http-body.d.ts +24 -0
- package/lib/browser-electron/http-body.js +33 -0
- package/lib/browser-electron/json-reply.d.ts +13 -1
- package/lib/browser-electron/json-reply.js +16 -2
- package/lib/browser-electron/maintenance-broker.d.ts +35 -0
- package/lib/browser-electron/maintenance-broker.js +114 -0
- package/lib/browser-electron/maintenance-lifecycle.d.ts +32 -0
- package/lib/browser-electron/maintenance-lifecycle.js +67 -0
- package/lib/browser-electron/maintenance-route.d.ts +18 -0
- package/lib/browser-electron/maintenance-route.js +75 -0
- package/lib/browser-electron/paths.d.ts +4 -0
- package/lib/browser-electron/paths.js +19 -0
- package/lib/browser-electron/process.d.ts +14 -0
- package/lib/browser-electron/process.js +24 -0
- package/lib/browser-electron/provider.d.ts +16 -9
- package/lib/browser-electron/provider.js +87 -18
- package/lib/browser-electron/remote-host.js +22 -20
- package/lib/browser-electron/runtime-route.d.ts +3 -5
- package/lib/browser-electron/runtime-status.d.ts +27 -13
- package/lib/browser-electron/runtime-status.js +28 -6
- package/lib/browser-electron/settings-route.d.ts +5 -18
- package/lib/browser-electron/settings-route.js +4 -14
- package/lib/browser-electron/settings-store.d.ts +28 -0
- package/lib/browser-electron/settings-store.js +32 -2
- package/lib/browser-electron/sidebar-only-host.d.ts +36 -0
- package/lib/browser-electron/sidebar-only-host.js +192 -0
- package/lib/browser-electron/system-browser.d.ts +6 -11
- package/lib/browser-electron/system-browser.js +29 -24
- package/lib/browser-electron/timing.d.ts +8 -0
- package/lib/browser-electron/timing.js +13 -0
- package/lib/browser-electron/windows-system.d.ts +3 -0
- package/lib/browser-electron/windows-system.js +19 -0
- package/lib/tool-browser/index.js +1045 -7
- package/lib/tool-browser/page-probe.d.ts +86 -0
- package/lib/tool-browser/page-probe.js +460 -0
- package/package.json +5 -3
- package/scripts/run-tests.mjs +32 -0
- package/scripts/verify-preview-route.mjs +19 -1
- package/scripts/verify-runtime-status.mjs +14 -0
- package/skills/dsh-builtin-browser/SKILL.md +64 -0
- package/skills/dsh-builtin-browser/references/batch.md +23 -0
- package/skills/dsh-builtin-browser/references/deliver.md +31 -0
- package/skills/dsh-builtin-browser/references/locate.md +11 -0
- package/skills/dsh-builtin-browser/references/recover.md +11 -0
- package/skills/dsh-builtin-browser/references/verify.md +13 -0
- package/src/browser/runtime.ts +11 -0
- package/src/browser/types.ts +19 -0
- package/src/browser-electron/authenticated-route.ts +56 -0
- package/src/browser-electron/automatic-maintenance.ts +414 -0
- package/src/browser-electron/bridge-auto.ts +158 -0
- package/src/browser-electron/bridge-connection.ts +2 -4
- package/src/browser-electron/bridge-endpoint.ts +8 -18
- package/src/browser-electron/bridge-install.ts +253 -0
- package/src/browser-electron/bridge-maintenance.ts +161 -0
- package/src/browser-electron/bridge-status.ts +76 -26
- package/src/browser-electron/desktop-bridge-host.ts +60 -26
- package/src/browser-electron/desktop-identity.ts +18 -9
- package/src/browser-electron/electron-provision.ts +35 -14
- package/src/browser-electron/entry.ts +152 -62
- package/src/browser-electron/ephemeral-profile.ts +65 -28
- package/src/browser-electron/history-store.ts +2 -2
- package/src/browser-electron/host-main.ts +40 -34
- package/src/browser-electron/http-body.ts +42 -0
- package/src/browser-electron/json-reply.ts +18 -3
- package/src/browser-electron/maintenance-broker.ts +142 -0
- package/src/browser-electron/maintenance-lifecycle.ts +86 -0
- package/src/browser-electron/maintenance-route.ts +64 -0
- package/src/browser-electron/paths.ts +21 -0
- package/src/browser-electron/process.ts +23 -0
- package/src/browser-electron/provider.ts +106 -22
- package/src/browser-electron/remote-host.ts +23 -23
- package/src/browser-electron/runtime-route.ts +3 -5
- package/src/browser-electron/runtime-status.ts +56 -19
- package/src/browser-electron/settings-route.ts +9 -32
- package/src/browser-electron/settings-store.ts +59 -3
- package/src/browser-electron/sidebar-only-host.ts +199 -0
- package/src/browser-electron/system-browser.ts +26 -23
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/windows-system.ts +23 -0
- package/src/tool-browser/index.ts +995 -10
- package/src/tool-browser/page-probe.ts +474 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dsh-builtin-browser
|
|
3
|
+
description: Use when a task needs a real web page — opening a URL, reading or extracting from it, filling and submitting a form, uploading an attachment, checking whether a result actually happened, or recovering after a page or carrier goes away. Covers which dsh_browser_* tool to reach for, how to batch steps, and how to verify instead of guessing.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Driving the browser
|
|
7
|
+
|
|
8
|
+
The browser is a real Chromium the human can watch and take over. Your tools drive it over the
|
|
9
|
+
page itself, so everything here works the same on the desktop sidebar, on an installed Chrome and
|
|
10
|
+
on the plugin's own window.
|
|
11
|
+
|
|
12
|
+
## Before acting
|
|
13
|
+
|
|
14
|
+
1. `dsh_browser_state` — where am I? URL, load state, requests in flight, what is on the page.
|
|
15
|
+
2. `dsh_browser_snapshot` (numbered interactive elements) or `dsh_browser_a11y` (roles, names,
|
|
16
|
+
states) — pick the structure map, not a screenshot, unless the page is canvas-only.
|
|
17
|
+
3. Then act with a semantic target: `{by: "text"|"css"|"xpath", value}`. Elements carry a stable
|
|
18
|
+
CSS selector, so a click re-resolves the element; never trust an index from an older snapshot.
|
|
19
|
+
|
|
20
|
+
## Batch, do not drip
|
|
21
|
+
|
|
22
|
+
`dsh_browser_do` runs a list of steps in ONE call and stops at the first that cannot proceed.
|
|
23
|
+
"Click search → type the query → press Enter → wait for results" is one call, not five. Steps:
|
|
24
|
+
`open`, `tap`, `field`, `type`, `key`, `scroll`, `sleep_ms`, `wait_text`, `wait_gone`, `wait_url`,
|
|
25
|
+
`wait_idle`, `eval`, `check`. Waits are condition-based; a `tap` whose target is covered by a
|
|
26
|
+
cookie banner dismisses the banner first.
|
|
27
|
+
|
|
28
|
+
## Wait on facts, not on time
|
|
29
|
+
|
|
30
|
+
- `dsh_browser_wait` — load complete, an expected URL or a selector.
|
|
31
|
+
- `dsh_browser_wait_for_response` — a server answer arrived **after** this call started. This is
|
|
32
|
+
the "did the submit reach the server" wait; a screenshot cannot answer it.
|
|
33
|
+
- `dsh_browser_console` / `dsh_browser_network` — what the page actually did when the screen does
|
|
34
|
+
not explain the failure.
|
|
35
|
+
|
|
36
|
+
## Verify, then retry
|
|
37
|
+
|
|
38
|
+
`dsh_browser_assert` checks a selector, text, URL or JS predicate and returns evidence per check.
|
|
39
|
+
"Clicked" and "the form accepted it" are different claims. Before retrying anything that changes
|
|
40
|
+
server state, assert that the previous attempt did not already take effect.
|
|
41
|
+
|
|
42
|
+
## Files
|
|
43
|
+
|
|
44
|
+
- `dsh_browser_download` — save a URL through the directory admission gate (never overwrites).
|
|
45
|
+
- `dsh_browser_upload` — put a local file (inside the workspace) into a file input.
|
|
46
|
+
- `dsh_browser_screenshot` — for the human, or when the page has no usable DOM.
|
|
47
|
+
|
|
48
|
+
## When the page fights you
|
|
49
|
+
|
|
50
|
+
- Dialogs: native page handling by default. Explicit accept/dismiss enables automatic answers; native restores page handling. `dsh_browser_dialog` reports them and can
|
|
51
|
+
switch the policy to accept.
|
|
52
|
+
- Menus that appear on hover: use `dsh_browser_hover` before snapshotting. It moves the browser
|
|
53
|
+
pointer and activates CSS hover styles and mouse handlers.
|
|
54
|
+
- Sliders, sortable lists, drop targets, and canvas strokes: use `dsh_browser_drag` with viewport
|
|
55
|
+
CSS coordinates, then verify the resulting value, order, dropped data, or canvas state.
|
|
56
|
+
- Runs of canvas clicks: use `dsh_browser_click_sequence`.
|
|
57
|
+
- Something is odd and nothing explains it: `dsh_browser_doctor`, then `dsh_browser_help`.
|
|
58
|
+
|
|
59
|
+
## Never
|
|
60
|
+
|
|
61
|
+
- Do not re-submit a form whose result you have not verified — a lost reply is not a failed submit.
|
|
62
|
+
- Do not paste cookies, tokens or exported login state into the conversation.
|
|
63
|
+
- Do not assume another task's tabs or navigation are visible to you: each task has its own
|
|
64
|
+
session, and `dsh_browser_reset_session` ends yours without touching anyone else's.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Batching with dsh_browser_do
|
|
2
|
+
|
|
3
|
+
One call, one result, and it stops at the first step that cannot proceed:
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{"steps": [
|
|
7
|
+
{"open": "https://example.com/search"},
|
|
8
|
+
{"field": {"target": {"by": "css", "value": "#q"}, "text": "invoice"}},
|
|
9
|
+
{"key": "Enter"},
|
|
10
|
+
{"wait_idle": {}},
|
|
11
|
+
{"check": {"text": "3 results"}}
|
|
12
|
+
]}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Rules that keep a batch honest:
|
|
16
|
+
|
|
17
|
+
- Give each step exactly one key; anything else is refused before the first action runs.
|
|
18
|
+
- Put a `check` after the steps that change server state — the batch then fails at the step where
|
|
19
|
+
the outcome was already wrong, not three steps later.
|
|
20
|
+
- `wait_idle` waits for in-flight requests to drain and the page to go quiet for 300 ms; use it
|
|
21
|
+
instead of `sleep_ms` unless the page genuinely needs wall-clock time.
|
|
22
|
+
- A failed batch reports `stoppedAt`, `completed` and a per-step log, so re-running only the
|
|
23
|
+
remainder is a deliberate choice, and it starts with a fresh `state`.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Delivering a file somewhere else
|
|
2
|
+
|
|
3
|
+
The browser's job ends with a file on disk. Sending it to Feishu (Lark) drive, Baidu Netdisk or any
|
|
4
|
+
other storage is a separate capability the user connects as an MCP server — both platforms ship an
|
|
5
|
+
official one (`lark_drive_upload`, `baidu-netdisk/mcp`'s upload tool). The plugin does not hold
|
|
6
|
+
their credentials and should not.
|
|
7
|
+
|
|
8
|
+
## The path
|
|
9
|
+
|
|
10
|
+
1. `dsh_browser_download` saves the URL through the directory admission gate and returns an
|
|
11
|
+
**absolute path**. It never overwrites an existing file; if the name is taken, ask for another.
|
|
12
|
+
2. Hand that exact path to the upload tool the user connected, e.g.
|
|
13
|
+
`lark_drive_upload({ file_path: "<returned path>", folder_token: "<the user's folder>" })`.
|
|
14
|
+
3. Report both facts: where the file now sits locally, and what the upload tool answered.
|
|
15
|
+
|
|
16
|
+
## Rules
|
|
17
|
+
|
|
18
|
+
- **Never** report "saved to Feishu" from the download alone. The download proves a local file; only
|
|
19
|
+
the upload tool's reply proves the remote copy.
|
|
20
|
+
- If the upload tool fails or is not connected, say so plainly and leave the local path — the file
|
|
21
|
+
is still usable and the human can move it by hand. Do not silently retry somewhere else.
|
|
22
|
+
- A folder id / token belongs to the user's workspace: ask for it, or read it from the tool's own
|
|
23
|
+
documentation, instead of guessing a default.
|
|
24
|
+
- Before uploading something sensitive (an invoice, a screenshot with an account name), say what is
|
|
25
|
+
about to leave the machine.
|
|
26
|
+
|
|
27
|
+
## When the user wants this to happen automatically
|
|
28
|
+
|
|
29
|
+
A per-call handoff (`dsh_browser_download` with a `handoff` argument) and a settings field for the
|
|
30
|
+
default tool are planned; until they exist, the two-step form above is the supported path and it
|
|
31
|
+
keeps the human in control of where their files go.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Locating things
|
|
2
|
+
|
|
3
|
+
- Prefer roles and names (`dsh_browser_a11y`) over CSS when the page is a normal app: a label
|
|
4
|
+
survives a re-render, a generated class does not.
|
|
5
|
+
- `dsh_browser_snapshot` numbers every interactive element and appends `{#id}` / `{[name=x]}`
|
|
6
|
+
when it can derive one — copy that straight into `{by:"css", value:"#id"}`.
|
|
7
|
+
- Text targets match the element's own visible text or aria-label; if two match, pass `index`.
|
|
8
|
+
- `dsh_browser_find` searches the page text and returns viewport points — use it to find a table
|
|
9
|
+
row or a sentence, then click at the point or scroll the element into view.
|
|
10
|
+
- An element inside an iframe or shadow root is reachable, but only when same-origin; a
|
|
11
|
+
cross-origin frame is a wall — report it instead of guessing coordinates at random.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Recovering
|
|
2
|
+
|
|
3
|
+
- Page stuck or a script half-applied: `dsh_browser_refresh`.
|
|
4
|
+
- Session wedged after a carrier loss: `dsh_browser_reset_session` (this task only), then re-open
|
|
5
|
+
and re-read the page. A lost carrier means the page context is gone — nothing you knew about it
|
|
6
|
+
still holds.
|
|
7
|
+
- A call fails with a reason you cannot place: `dsh_browser_doctor` (session, page state, probe
|
|
8
|
+
buffers, which tools this task restricted away), then `dsh_browser_help` for the topic.
|
|
9
|
+
- Everything is refused with "restricted": the task restricted its own tools. `dsh_browser_restrict`
|
|
10
|
+
with the needed names lifts it; observation tools are never blocked.
|
|
11
|
+
- Do not retry a state-changing call blindly after an error. Read first, decide second.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Verifying
|
|
2
|
+
|
|
3
|
+
| Question | Tool |
|
|
4
|
+
| --- | --- |
|
|
5
|
+
| Is the element there / the text there / the URL changed? | `dsh_browser_assert` |
|
|
6
|
+
| Did a request reach the server? | `dsh_browser_wait_for_response` |
|
|
7
|
+
| What did the page log or fetch? | `dsh_browser_console`, `dsh_browser_network` |
|
|
8
|
+
| Did a field take the value? | `dsh_browser_get_value` |
|
|
9
|
+
|
|
10
|
+
A submit is the dangerous case. After clicking submit: wait for the response, assert the visible
|
|
11
|
+
outcome, and if the call died before either answer arrived, **read the site's own record** (the
|
|
12
|
+
list page, the order history) before acting again. "Result unknown" is an acceptable answer to
|
|
13
|
+
report; a second submit is not.
|
package/src/browser/runtime.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* @module dsh-browser/browser
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { pageOperationScript } from '../tool-browser/page-probe.js'
|
|
10
11
|
import { Context, Service } from '@deepseek-ai/cordis'
|
|
11
12
|
import z from '@deepseek-ai/schemastery'
|
|
12
13
|
import type {
|
|
@@ -22,6 +23,7 @@ import type {
|
|
|
22
23
|
BrowserDownloadRequest,
|
|
23
24
|
BrowserElementTarget,
|
|
24
25
|
BrowserExecuteRequest,
|
|
26
|
+
BrowserPageOperationRequest,
|
|
25
27
|
BrowserExecuteResult,
|
|
26
28
|
BrowserFillRequest,
|
|
27
29
|
BrowserFillResult,
|
|
@@ -247,6 +249,15 @@ export class BrowserRuntime extends Service {
|
|
|
247
249
|
return this.resolveProvider().execute(session, request, signal)
|
|
248
250
|
}
|
|
249
251
|
|
|
252
|
+
/** Execute one provider-owned page operation with validated operation names. */
|
|
253
|
+
async pageOperation(session: BrowserSessionId, request: BrowserPageOperationRequest, signal?: AbortSignal): Promise<BrowserExecuteResult> {
|
|
254
|
+
const provider = this.resolveProvider()
|
|
255
|
+
// Legacy providers retain their execute permission checks.
|
|
256
|
+
return provider.pageOperation
|
|
257
|
+
? provider.pageOperation(session, request, signal)
|
|
258
|
+
: provider.execute(session, { script: pageOperationScript(request) }, signal)
|
|
259
|
+
}
|
|
260
|
+
|
|
250
261
|
/** Wait until the session's page is ready (and optional URL/selector match). */
|
|
251
262
|
async waitFor(session: BrowserSessionId, request: BrowserWaitRequest, signal?: AbortSignal): Promise<BrowserWaitResult> {
|
|
252
263
|
return this.resolveProvider().waitFor(session, request, signal)
|
package/src/browser/types.ts
CHANGED
|
@@ -359,6 +359,22 @@ export interface BrowserExecuteRequest {
|
|
|
359
359
|
readonly timeoutMs?: number
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
+
/**
|
|
363
|
+
* Fixed page operations implemented by the provider. Strings are serialized as data.
|
|
364
|
+
* Arbitrary JavaScript is handled only by BrowserExecuteRequest.
|
|
365
|
+
*/
|
|
366
|
+
export type BrowserPageOperationRequest =
|
|
367
|
+
| { readonly operation: 'install' }
|
|
368
|
+
| { readonly operation: 'probe'; readonly method: BrowserProbeMethod; readonly args?: readonly unknown[] }
|
|
369
|
+
| { readonly operation: 'text'; readonly text: string }
|
|
370
|
+
| { readonly operation: 'url' }
|
|
371
|
+
| { readonly operation: 'drag'; readonly from: { x: number; y: number }; readonly to: { x: number; y: number }; readonly steps: number }
|
|
372
|
+
| { readonly operation: 'upload'; readonly target: { by: 'css' | 'text' | 'xpath'; value: string }; readonly file: { name: string; mime: string; base64: string } }
|
|
373
|
+
|
|
374
|
+
/** Provider-owned probe operations; JavaScript properties such as constructor are excluded. */
|
|
375
|
+
export type BrowserProbeMethod = 'read' | 'clear' | 'settle' | 'find' | 'overlayAt' | 'summary'
|
|
376
|
+
| 'findText' | 'hoverAt' | 'setDialogPolicy' | 'dialogStatus' | 'networkCursor'
|
|
377
|
+
|
|
362
378
|
/**
|
|
363
379
|
* Result of an execute. Mirrors CDP `Runtime.evaluate` semantics: either a
|
|
364
380
|
* value (by value, not by reference) or the exception thrown.
|
|
@@ -632,6 +648,9 @@ export interface BrowserProvider {
|
|
|
632
648
|
navigate(session: BrowserSessionId, request: BrowserNavigateRequest, signal?: AbortSignal): Promise<void>
|
|
633
649
|
/** Execute JS in the active tab's page context. */
|
|
634
650
|
execute(session: BrowserSessionId, request: BrowserExecuteRequest, signal?: AbortSignal): Promise<BrowserExecuteResult>
|
|
651
|
+
|
|
652
|
+
/** Execute one fixed browser operation without granting arbitrary page scripting. */
|
|
653
|
+
pageOperation?(session: BrowserSessionId, request: BrowserPageOperationRequest, signal?: AbortSignal): Promise<BrowserExecuteResult>
|
|
635
654
|
/** Wait until the page is ready (and optional URL/selector match). Honor `signal` for cancellation. */
|
|
636
655
|
waitFor(session: BrowserSessionId, request: BrowserWaitRequest, signal?: AbortSignal): Promise<BrowserWaitResult>
|
|
637
656
|
/** Produce an AI-friendly snapshot of the active tab. */
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authenticate plugin HTTP routes with the browser session owned by DSH.
|
|
3
|
+
* The connection service verifies its signed cookie and completes denied responses.
|
|
4
|
+
* @module dsh-browser/browser-electron/authenticated-route
|
|
5
|
+
*/
|
|
6
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
7
|
+
import { replyJson } from './json-reply.js'
|
|
8
|
+
|
|
9
|
+
/** The host authentication method shared with its application and inspector routes. */
|
|
10
|
+
export interface BrowserConnection {
|
|
11
|
+
authorizeIndex(request: IncomingMessage, response: ServerResponse): boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** An exact HTTP route registered with the DSH web server. */
|
|
15
|
+
export type BrowserHttpHandler = (request: IncomingMessage, response: ServerResponse) => void | Promise<void>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check the current host credential on every request before reading settings, capturing a frame,
|
|
19
|
+
* or starting a write. Origin checks remain inside the handler as an additional browser boundary.
|
|
20
|
+
* @param connection - resolves the host's current connection service.
|
|
21
|
+
* @param handler - the operation to run after authentication.
|
|
22
|
+
* @returns an authenticated route handler.
|
|
23
|
+
*/
|
|
24
|
+
export function authenticatedRoute(
|
|
25
|
+
connection: () => BrowserConnection | undefined,
|
|
26
|
+
handler: BrowserHttpHandler,
|
|
27
|
+
): BrowserHttpHandler {
|
|
28
|
+
return (request, response) => {
|
|
29
|
+
let host: BrowserConnection | undefined
|
|
30
|
+
try {
|
|
31
|
+
host = connection()
|
|
32
|
+
} catch {
|
|
33
|
+
// A disposed host cannot authorize new work.
|
|
34
|
+
}
|
|
35
|
+
if (typeof host?.authorizeIndex !== 'function') {
|
|
36
|
+
replyJson(response, 503, { error: 'browser-auth-unavailable' })
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
// A false result owns the response. Preserving it also preserves host-specific login behavior.
|
|
40
|
+
if (!host.authorizeIndex(request, response)) return
|
|
41
|
+
return handler(request, response)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Admit machine-wide installation changes from a direct loopback connection.
|
|
47
|
+
* Forwarded requests belong to their remote operator even when the last hop is loopback.
|
|
48
|
+
* @param request - request received by the host.
|
|
49
|
+
* @returns true for direct loopback requests.
|
|
50
|
+
*/
|
|
51
|
+
export function isDirectLoopback(request: IncomingMessage): boolean {
|
|
52
|
+
const address = request.socket.remoteAddress
|
|
53
|
+
const local = address === '::1' || address === '127.0.0.1' || address === '::ffff:127.0.0.1'
|
|
54
|
+
return local && !['forwarded', 'x-forwarded-for', 'x-forwarded-host', 'x-real-ip']
|
|
55
|
+
.some(name => request.headers[name] !== undefined)
|
|
56
|
+
}
|