Host half (src/index.ts): /sidebar/api JSON API (session cwd, fs tree/read/ write, git status/diff/stage/commit/branch/log/show), /sidebar/file media route, /sidebar/ws/terminal WebSocket over node-pty; routes share the /api trust fence (Host-header loopback or connection.trustedHosts). Client half (src/client): portal-mounted right panel with a per-session split-pane workbench (recursive row/col splits, draggable dividers, tab drag-move), tab bar with max-width 160px + plus-menu (explorer/git/terminal multi-open rules), lazy file explorer rooted at the session cwd, editable text editor (dirty dot, Ctrl/Cmd+S), image + Markdown preview, xterm terminal with reconnect, and a VSCode-style git panel. Per-session state persists to localStorage (dsh-sidebar:v1:<sessionId>). Intercepts the chat turn-tail produced-files row (priority -1 chain entry) to open files in the sidebar. UI styled with the dsh --dsw-* theme tokens and ui-primitives. Build: tsdown emits the host lib plus the __ModuleLoader__ client bundle (externals = module table words; xterm/css inlined). Tests: 19 unit + smoke specs (pure state/tree/git parsers + real git/fs integration).
78 lines
3.1 KiB
TypeScript
78 lines
3.1 KiB
TypeScript
/**
|
|
* Browser-trust fence for the sidebar routes, behaviorally identical to the
|
|
* /api gateway's fence in @deepseek-ai/dsh-client-connection
|
|
* (src/api-request-trust.ts + src/loopback-hostname.ts, BSD-3-Clause,
|
|
* copied here because the package does not export these helpers and the
|
|
* plugin must not depend on its internals). Host-header loopback or a
|
|
* configured trusted authority passes; cross-site browser markers refuse.
|
|
* This is a DNS-rebinding / cross-site defense, not authentication.
|
|
*/
|
|
import type { IncomingHttpHeaders } from 'node:http'
|
|
|
|
/** The request facts the fence reads (structural subset of IncomingMessage). */
|
|
interface ApiTrustRequest {
|
|
headers: IncomingHttpHeaders
|
|
}
|
|
|
|
function header(headers: IncomingHttpHeaders, name: string): string | undefined {
|
|
const value = headers[name]
|
|
return typeof value === 'string' ? value : undefined
|
|
}
|
|
|
|
/** Normalized URL of a Host-header authority, or undefined when unparsable. */
|
|
function parseAuthority(authority: string): URL | undefined {
|
|
try {
|
|
return new URL(`http://${authority}`)
|
|
} catch {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
/** Whether a normalized URL hostname names the local loopback authority. */
|
|
export function isLoopbackHostname(hostname: string): boolean {
|
|
if (hostname === 'localhost' || hostname === '[::1]') return true
|
|
const parts = hostname.split('.')
|
|
return parts.length === 4
|
|
&& parts[0] === '127'
|
|
&& parts.every(part => /^\d{1,3}$/.test(part) && Number(part) <= 255)
|
|
}
|
|
|
|
/** Canonical authority form: hostname, or hostname:port when a port was written. */
|
|
function canonicalAuthority(entry: string, entryUrl: URL): string {
|
|
const port = entryUrl.port !== '' ? entryUrl.port : new URL(`https://${entry}`).port
|
|
return port === '' ? entryUrl.hostname : `${entryUrl.hostname}:${port}`
|
|
}
|
|
|
|
/** Whether the request authority matches a trustedHosts entry (exact or port-less). */
|
|
function isTrustedAuthority(hostUrl: URL, trustedHosts: readonly string[]): boolean {
|
|
return trustedHosts.some((entry) => {
|
|
const entryUrl = parseAuthority(entry)
|
|
if (entryUrl === undefined) return false
|
|
return canonicalAuthority(entry, entryUrl) === entryUrl.hostname
|
|
? entryUrl.hostname === hostUrl.hostname
|
|
: entryUrl.host === hostUrl.host
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Decide whether one sidebar request may reach the plugin routes.
|
|
* @param request - node HTTP request facts (headers).
|
|
* @param trustedHosts - non-loopback authorities this deployment serves.
|
|
* @returns true when the Host is ours (loopback or trusted) and browser markers are same-origin.
|
|
*/
|
|
export function isTrustedApiRequest(request: ApiTrustRequest, trustedHosts: readonly string[]): boolean {
|
|
const host = header(request.headers, 'host')
|
|
if (host === undefined) return false
|
|
const hostUrl = parseAuthority(host)
|
|
if (hostUrl === undefined) return false
|
|
if (!isLoopbackHostname(hostUrl.hostname) && !isTrustedAuthority(hostUrl, trustedHosts)) return false
|
|
if (header(request.headers, 'sec-fetch-site') === 'cross-site') return false
|
|
const origin = header(request.headers, 'origin')
|
|
if (origin === undefined) return true
|
|
try {
|
|
return new URL(origin).host === hostUrl.host
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|