forked from cflmy/deepseek-harness
Implement the downstream product in `packages/marqdo`: - `@marqdo/mdh-harness`: application home layout, atomic writes, locks, bounded fixed-argv processes, safe tar extraction, Observation 1 and ValueWire validation; - trusted Gitea release discovery, SHA-256/platform/Bundle verification, transactional environment install, rollback and cleanup, CLI Tool changes; - private knowledge collections with strict import policy, the official `marqdo-knowledge` compile/query adapter and a disclosed locator fallback; - Analysis 3 parsing, approved run/call execution, evidence and staleness; - the six model operations (find/read/check/program/ecosystem/knowledge_search); - the `/mdh` workbench over the existing web server with a same-origin fence; - `mdh` launcher, CI/package/upstream scripts and a Woodpecker pipeline. Tests: 28 node:test cases (27 PASS, one network-gated install test also PASS with MDH_TEST_INSTALL=1). Product install/workbench evidence is recorded in docs/mdh/evidence. Part of marqdo#25 (MDH-03..MDH-08).
63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
/**
|
|
* Same-origin fence for the `/mdh` API.
|
|
*
|
|
* The DSH web server already applies its own browser-trust policy to `/api`.
|
|
* This module applies the equivalent Host/Origin policy to the product routes:
|
|
* a request must address loopback, the configured bind literal or an explicit
|
|
* trusted authority, and a browser Origin must equal that Host.
|
|
*
|
|
* @module @marqdo/mdh-harness/web/trust
|
|
*/
|
|
|
|
/** Parse `host[:port]` (including bracketed IPv6) into host and port. */
|
|
function parseAuthority(value: string): { host: string; port: string } | undefined {
|
|
const trimmed = value.trim()
|
|
if (trimmed === '') return undefined
|
|
if (trimmed.startsWith('[')) {
|
|
const close = trimmed.indexOf(']')
|
|
if (close === -1) return undefined
|
|
const host = trimmed.slice(1, close)
|
|
const rest = trimmed.slice(close + 1)
|
|
if (rest !== '' && !rest.startsWith(':')) return undefined
|
|
return { host: host.toLowerCase(), port: rest.slice(1) }
|
|
}
|
|
const lastColon = trimmed.lastIndexOf(':')
|
|
if (lastColon !== -1 && trimmed.indexOf(':') === lastColon) {
|
|
return { host: trimmed.slice(0, lastColon).toLowerCase(), port: trimmed.slice(lastColon + 1) }
|
|
}
|
|
return { host: trimmed.toLowerCase(), port: '' }
|
|
}
|
|
|
|
/** Whether a Host header names a loopback authority. */
|
|
export function isLoopbackAuthority(value: string): boolean {
|
|
const parsed = parseAuthority(value)
|
|
if (!parsed) return false
|
|
if (parsed.host === 'localhost') return true
|
|
if (parsed.host === '::1') return true
|
|
if (parsed.host === '0.0.0.0' || parsed.host === '::') return false
|
|
const match = /^127\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(parsed.host)
|
|
if (!match) return false
|
|
return match.slice(1).every(part => Number(part) <= 255)
|
|
}
|
|
|
|
/**
|
|
* Whether a Host header is one of the explicit non-loopback authorities.
|
|
* Entries may be `host:port` or a port-less `host` matching any port.
|
|
*/
|
|
export function isTrustedAuthority(value: string, trustedHosts: readonly string[], bindHost?: string): boolean {
|
|
const parsed = parseAuthority(value)
|
|
if (!parsed) return false
|
|
for (const entry of trustedHosts) {
|
|
const trusted = parseAuthority(entry)
|
|
if (!trusted) continue
|
|
if (trusted.host !== parsed.host) continue
|
|
if (trusted.port === '' || trusted.port === parsed.port) return true
|
|
}
|
|
if (bindHost) {
|
|
const zoneAt = bindHost.indexOf('%')
|
|
const address = (zoneAt === -1 ? bindHost : bindHost.slice(0, zoneAt)).toLowerCase()
|
|
if (address === parsed.host) return true
|
|
}
|
|
return false
|
|
}
|