mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(device-pairing): refuse to mint pairing codes from an address that cannot pair
A loopback origin in production or plain HTTP on a public address still minted a code and showed a browser QR no other device could use. - one availability rule decides untrusted, loopback and public plain-HTTP origins; POST /api/device-pairing/links checks it before minting - GET /api/device-pairing/availability reports the rule for the page origin without minting - Connect a device disables "Show QR code" and explains the fix when the address cannot pair - private-network plain HTTP keeps the browser QR; the app QR still needs the toggle
This commit is contained in:
1 parent
106e8f51fc
commit
b9d8d18124
12 files changed
+529
-64
No files matched your search
@@ -1,18 +1,26 @@
|
||||
import { Hono } from 'hono'
|
||||
import { ENV, getTrustedOrigins, isTrustedOrigin } from '@opencode-manager/shared/config/env'
|
||||
import type { MiddlewareHandler } from 'hono'
|
||||
import { ENV, getTrustedOrigins } from '@opencode-manager/shared/config/env'
|
||||
import { CreateDevicePairingLinksRequestSchema } from '@opencode-manager/shared/schemas'
|
||||
import { buildDevicePairingLinks, resolveAppLinkPolicy } from '../services/device-pairing-links'
|
||||
import {
|
||||
buildDevicePairingLinks,
|
||||
normalizeDevicePairingOrigin,
|
||||
resolveAppLinkPolicy,
|
||||
resolveDevicePairingAvailability,
|
||||
} from '../services/device-pairing-links'
|
||||
import type { RemoteAccessHeadersService } from '../services/remote-access-headers'
|
||||
import { logger } from '../utils/logger'
|
||||
import { parseJsonBody } from '../utils/route-helpers'
|
||||
|
||||
const MISSING_ORIGIN_MESSAGE =
|
||||
'The request had no Origin header. Open the Manager at an address listed in AUTH_TRUSTED_ORIGINS to pair a device.'
|
||||
const LINKS_FAILED_MESSAGE = 'Failed to create device pairing links'
|
||||
const MISSING_ORIGIN_QUERY_MESSAGE =
|
||||
'Add an origin query parameter, for example ?origin=https://manager.example.com.'
|
||||
const INVALID_ORIGIN_QUERY_MESSAGE =
|
||||
'The origin query parameter must be an absolute http or https URL.'
|
||||
|
||||
function untrustedOriginMessage(origin: string | undefined): string {
|
||||
if (!origin) return MISSING_ORIGIN_MESSAGE
|
||||
return `This address (${origin}) is not in AUTH_TRUSTED_ORIGINS. Add it and restart the Manager to pair a device.`
|
||||
const noStore: MiddlewareHandler = async (c, next) => {
|
||||
await next()
|
||||
c.header('Cache-Control', 'no-store')
|
||||
}
|
||||
|
||||
interface DevicePairingRouteDeps {
|
||||
@@ -29,9 +37,22 @@ export function createDevicePairingRoutes(deps: DevicePairingRouteDeps) {
|
||||
const resolveTrustedOrigins = deps.resolveTrustedOrigins ?? getTrustedOrigins
|
||||
const isDevelopment = deps.isDevelopment ?? (() => ENV.SERVER.NODE_ENV === 'development')
|
||||
|
||||
app.use('/links', async (c, next) => {
|
||||
await next()
|
||||
c.header('Cache-Control', 'no-store')
|
||||
app.use('/links', noStore)
|
||||
app.use('/availability', noStore)
|
||||
|
||||
app.get('/availability', (c) => {
|
||||
const requestedOrigin = c.req.query('origin')
|
||||
if (!requestedOrigin) return c.json({ error: MISSING_ORIGIN_QUERY_MESSAGE }, 400)
|
||||
|
||||
const origin = normalizeDevicePairingOrigin(requestedOrigin)
|
||||
if (!origin) return c.json({ error: INVALID_ORIGIN_QUERY_MESSAGE }, 400)
|
||||
|
||||
return c.json(
|
||||
resolveDevicePairingAvailability(origin, {
|
||||
trustedOrigins: resolveTrustedOrigins(),
|
||||
isDevelopment: isDevelopment(),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
app.post('/links', async (c) => {
|
||||
@@ -39,15 +60,18 @@ export function createDevicePairingRoutes(deps: DevicePairingRouteDeps) {
|
||||
if (!body.ok) return body.response
|
||||
|
||||
const origin = c.req.header('origin')
|
||||
if (!origin || !isTrustedOrigin(origin, { requireOrigin: true }, resolveTrustedOrigins())) {
|
||||
return c.json({ error: untrustedOriginMessage(origin) }, 400)
|
||||
const availability = resolveDevicePairingAvailability(origin, {
|
||||
trustedOrigins: resolveTrustedOrigins(),
|
||||
isDevelopment: isDevelopment(),
|
||||
})
|
||||
if (!origin || !availability.available) {
|
||||
return c.json({ error: availability.message, code: availability.reason }, 400)
|
||||
}
|
||||
|
||||
try {
|
||||
const originUrl = new URL(origin)
|
||||
const policy = resolveAppLinkPolicy(originUrl, {
|
||||
allowPrivateNetworkHttp: deps.readAllowPrivateNetworkHttp(),
|
||||
isDevelopment: isDevelopment(),
|
||||
})
|
||||
const stored = deps.remoteAccessHeaders.read()
|
||||
const minted = await deps.mintPairingCode(c.req.raw.headers)
|
||||
|
||||
@@ -1,39 +1,101 @@
|
||||
import { isTrustedOrigin } from '@opencode-manager/shared/config/env'
|
||||
import {
|
||||
buildManagerAppLink,
|
||||
buildManagerBrowserPairingLink,
|
||||
type AppLinkBlockedReason,
|
||||
type DevicePairingAvailability,
|
||||
type DevicePairingLinkWarning,
|
||||
type DevicePairingLinksResponse,
|
||||
type DevicePairingUnavailableReason,
|
||||
type RemoteAccessHeader,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import { isLoopbackHost, isPrivateNetworkHost } from '@opencode-manager/shared/utils'
|
||||
|
||||
export const MISSING_ORIGIN_MESSAGE =
|
||||
'The request had no Origin header. Open the Manager at an address listed in AUTH_TRUSTED_ORIGINS to pair a device.'
|
||||
export const LOOPBACK_ORIGIN_MESSAGE =
|
||||
'This page is open on localhost, which other devices cannot reach. Open the Manager at its public or LAN address to pair a device.'
|
||||
export const INSECURE_ORIGIN_MESSAGE =
|
||||
'Pairing needs HTTPS on a public address. Serve the Manager over HTTPS, or open it at a private network address.'
|
||||
|
||||
type AppLinkPolicy = {
|
||||
blockedReason: AppLinkBlockedReason | null
|
||||
warnings: DevicePairingLinkWarning[]
|
||||
}
|
||||
|
||||
type DevicePairingAvailabilityOptions = {
|
||||
trustedOrigins: string[]
|
||||
isDevelopment: boolean
|
||||
}
|
||||
|
||||
function untrustedOriginMessage(origin: string): string {
|
||||
return `This address (${origin}) is not in AUTH_TRUSTED_ORIGINS. Add it and restart the Manager to pair a device.`
|
||||
}
|
||||
|
||||
function unavailable(
|
||||
reason: DevicePairingUnavailableReason,
|
||||
message: string,
|
||||
): DevicePairingAvailability {
|
||||
return { available: false, reason, message }
|
||||
}
|
||||
|
||||
function parseHttpOrigin(value: string): URL | null {
|
||||
let url: URL
|
||||
try {
|
||||
url = new URL(value)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
|
||||
return url
|
||||
}
|
||||
|
||||
/** Normalises an address to its origin, or null when it is not an absolute http(s) URL. */
|
||||
export function normalizeDevicePairingOrigin(value: string): string | null {
|
||||
return parseHttpOrigin(value)?.origin ?? null
|
||||
}
|
||||
|
||||
/** Reports whether an origin can mint a pairing code, and why it cannot when blocked. */
|
||||
export function resolveDevicePairingAvailability(
|
||||
origin: string | undefined,
|
||||
options: DevicePairingAvailabilityOptions,
|
||||
): DevicePairingAvailability {
|
||||
if (!origin) return unavailable('untrusted-origin', MISSING_ORIGIN_MESSAGE)
|
||||
if (!isTrustedOrigin(origin, { requireOrigin: true }, options.trustedOrigins)) {
|
||||
return unavailable('untrusted-origin', untrustedOriginMessage(origin))
|
||||
}
|
||||
|
||||
const originUrl = parseHttpOrigin(origin)
|
||||
if (!originUrl) return unavailable('untrusted-origin', untrustedOriginMessage(origin))
|
||||
|
||||
const isLoopback = isLoopbackHost(originUrl.hostname)
|
||||
if (isLoopback && !options.isDevelopment) {
|
||||
return unavailable('loopback', LOOPBACK_ORIGIN_MESSAGE)
|
||||
}
|
||||
|
||||
if (originUrl.protocol === 'http:' && !isLoopback && !isPrivateNetworkHost(originUrl.hostname)) {
|
||||
return unavailable('insecure-origin', INSECURE_ORIGIN_MESSAGE)
|
||||
}
|
||||
|
||||
return { available: true, reason: null, message: null }
|
||||
}
|
||||
|
||||
/** Decides whether an origin can carry an app link and which warnings it raises. */
|
||||
export function resolveAppLinkPolicy(
|
||||
origin: URL,
|
||||
options: { allowPrivateNetworkHttp: boolean; isDevelopment: boolean },
|
||||
options: { allowPrivateNetworkHttp: boolean },
|
||||
): AppLinkPolicy {
|
||||
const warnings: DevicePairingLinkWarning[] = []
|
||||
const isPlainHttp = origin.protocol === 'http:'
|
||||
const isLoopback = isLoopbackHost(origin.hostname)
|
||||
|
||||
if (isPlainHttp) warnings.push('plain-http')
|
||||
if (isLoopback) warnings.push('loopback')
|
||||
if (!isPlainHttp) return { blockedReason: null, warnings }
|
||||
|
||||
if (isLoopbackHost(origin.hostname)) {
|
||||
warnings.push('loopback')
|
||||
return { blockedReason: options.isDevelopment ? null : 'loopback', warnings }
|
||||
}
|
||||
|
||||
if (isPlainHttp) {
|
||||
const isAllowed = options.allowPrivateNetworkHttp && isPrivateNetworkHost(origin.hostname)
|
||||
return { blockedReason: isAllowed ? null : 'insecure-origin', warnings }
|
||||
}
|
||||
|
||||
return { blockedReason: null, warnings }
|
||||
const isAllowed =
|
||||
isLoopback || (options.allowPrivateNetworkHttp && isPrivateNetworkHost(origin.hostname))
|
||||
return { blockedReason: isAllowed ? null : 'insecure-origin', warnings }
|
||||
}
|
||||
|
||||
/** Builds the pairing link response, dropping the app link when its origin is blocked. */
|
||||
|
||||
Reference in new issue
Block a user