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,5 +1,6 @@
|
||||
import type {
|
||||
CreateDevicePairingLinksRequest,
|
||||
DevicePairingAvailability,
|
||||
DevicePairingLinksResponse,
|
||||
PairedDevice,
|
||||
PairedDevicesResponse,
|
||||
@@ -62,6 +63,16 @@ export async function createDevicePairingLinks(
|
||||
})
|
||||
}
|
||||
|
||||
/** Reads whether the Manager can mint pairing codes for an address without minting one. */
|
||||
export async function getDevicePairingAvailability(
|
||||
origin: string,
|
||||
): Promise<DevicePairingAvailability> {
|
||||
return fetchWrapper<DevicePairingAvailability>(
|
||||
`${API_BASE_URL}/api/device-pairing/availability`,
|
||||
{ params: { origin } },
|
||||
)
|
||||
}
|
||||
|
||||
/** Lists the devices signed in to this account. */
|
||||
export async function listPairedDevices(): Promise<PairedDevice[]> {
|
||||
const response = await fetchWrapper<PairedDevicesResponse>(
|
||||
|
||||
@@ -90,27 +90,20 @@ describe('ConnectDeviceDialog', () => {
|
||||
expect(await screen.findByAltText('Browser sign-in QR code')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('explains a loopback block without showing the mobile app QR code', async () => {
|
||||
it('warns about a localhost address while still showing the mobile app QR code', async () => {
|
||||
mocks.createDevicePairingLinks.mockResolvedValue({
|
||||
...BASE_LINKS,
|
||||
appLink: null,
|
||||
appLinkBlockedReason: 'loopback',
|
||||
warnings: ['plain-http', 'loopback'],
|
||||
})
|
||||
renderDialog()
|
||||
|
||||
expect(
|
||||
await screen.findByText(
|
||||
'This page is open on localhost, which your phone cannot reach. Open the Manager at its public or LAN address to show the app QR code.',
|
||||
),
|
||||
).toBeInTheDocument()
|
||||
expect(screen.queryByAltText('Mobile app pairing QR code')).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByText('This address uses plain HTTP, so the sign-in code travels unencrypted.'),
|
||||
).toBeInTheDocument()
|
||||
expect(await screen.findByAltText('Mobile app pairing QR code')).toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByText('This address is localhost; other devices cannot reach it.'),
|
||||
).toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByText('This address uses plain HTTP, so the sign-in code travels unencrypted.'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('explains how to unblock a plain-HTTP app link', async () => {
|
||||
|
||||
@@ -24,9 +24,6 @@ const WARNING_MESSAGES: Record<DevicePairingLinkWarning, string> = {
|
||||
'headers-unreadable': 'Stored remote access headers could not be decrypted and are not included.',
|
||||
}
|
||||
|
||||
const LOOPBACK_EXPLANATION =
|
||||
'This page is open on localhost, which your phone cannot reach. Open the Manager at its public or LAN address to show the app QR code.'
|
||||
|
||||
const INSECURE_ORIGIN_EXPLANATION =
|
||||
"The mobile app QR code needs HTTPS. Use an HTTPS address, or turn on 'Allow plain-HTTP app links on private networks' if this Manager uses a private IP address."
|
||||
|
||||
@@ -187,11 +184,7 @@ export function ConnectDeviceDialog({ open, onOpenChange }: ConnectDeviceDialogP
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{links.appLinkBlockedReason === 'loopback'
|
||||
? LOOPBACK_EXPLANATION
|
||||
: INSECURE_ORIGIN_EXPLANATION}
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground">{INSECURE_ORIGIN_EXPLANATION}</p>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { DEFAULT_USER_PREFERENCES } from '@opencode-manager/shared/schemas'
|
||||
import { createUseSettingsMock } from '@/test/test-utils'
|
||||
import { ConnectDeviceSettings } from './ConnectDeviceSettings'
|
||||
|
||||
const UNAVAILABLE_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.'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
useSettings: vi.fn(),
|
||||
updateSettings: vi.fn(),
|
||||
createDevicePairingLinks: vi.fn(),
|
||||
getDevicePairingAvailability: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSettings', () => ({
|
||||
@@ -18,6 +22,7 @@ vi.mock('@/hooks/useSettings', () => ({
|
||||
|
||||
vi.mock('@/api/devicePairing', () => ({
|
||||
createDevicePairingLinks: mocks.createDevicePairingLinks,
|
||||
getDevicePairingAvailability: mocks.getDevicePairingAvailability,
|
||||
}))
|
||||
|
||||
function renderSettings() {
|
||||
@@ -56,6 +61,11 @@ describe('ConnectDeviceSettings', () => {
|
||||
headerNames: [],
|
||||
warnings: [],
|
||||
})
|
||||
mocks.getDevicePairingAvailability.mockResolvedValue({
|
||||
available: true,
|
||||
reason: null,
|
||||
message: null,
|
||||
})
|
||||
})
|
||||
|
||||
it('persists the private-network plain-HTTP preference', async () => {
|
||||
@@ -75,8 +85,45 @@ describe('ConnectDeviceSettings', () => {
|
||||
const user = userEvent.setup()
|
||||
renderSettings()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Show QR code' }))
|
||||
const button = screen.getByRole('button', { name: 'Show QR code' })
|
||||
await waitFor(() => expect(button).toBeEnabled())
|
||||
await user.click(button)
|
||||
|
||||
expect(await screen.findByAltText('Mobile app pairing QR code')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('disables the QR code button while availability loads', async () => {
|
||||
mocks.getDevicePairingAvailability.mockReturnValue(new Promise(() => undefined))
|
||||
renderSettings()
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Show QR code' })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('disables the QR code button and shows the reason when pairing is unavailable', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getDevicePairingAvailability.mockResolvedValue({
|
||||
available: false,
|
||||
reason: 'loopback',
|
||||
message: UNAVAILABLE_MESSAGE,
|
||||
})
|
||||
renderSettings()
|
||||
|
||||
const button = screen.getByRole('button', { name: 'Show QR code' })
|
||||
await screen.findByText(UNAVAILABLE_MESSAGE)
|
||||
expect(button).toBeDisabled()
|
||||
|
||||
await user.click(button)
|
||||
|
||||
expect(screen.queryByAltText('Mobile app pairing QR code')).not.toBeInTheDocument()
|
||||
expect(mocks.createDevicePairingLinks).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps the QR code button enabled when the availability check fails', async () => {
|
||||
mocks.getDevicePairingAvailability.mockRejectedValue(new Error('availability failed'))
|
||||
renderSettings()
|
||||
|
||||
const button = screen.getByRole('button', { name: 'Show QR code' })
|
||||
await waitFor(() => expect(button).toBeEnabled())
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useState } from 'react'
|
||||
import { QrCode } from 'lucide-react'
|
||||
import { AlertCircle, QrCode } from 'lucide-react'
|
||||
import { useDevicePairingAvailability } from '@/hooks/useDevicePairing'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Label } from '@/components/ui/label'
|
||||
@@ -12,7 +14,11 @@ const ALLOW_PRIVATE_NETWORK_HTTP_LABEL = 'Allow plain-HTTP app links on private
|
||||
export function ConnectDeviceSettings() {
|
||||
const { preferences, updateSettings } = useSettings()
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const availability = useDevicePairingAvailability()
|
||||
const allowPrivateNetworkHttp = preferences?.devicePairing?.allowPrivateNetworkHttp ?? false
|
||||
const isPairingAvailable = availability.data?.available !== false
|
||||
const unavailableMessage =
|
||||
availability.data?.available === false ? availability.data.message : null
|
||||
|
||||
return (
|
||||
<Card className="border-0 shadow-none">
|
||||
@@ -26,10 +32,22 @@ export function ConnectDeviceSettings() {
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<Button variant="outline" size="sm" onClick={() => setDialogOpen(true)}>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setDialogOpen(true)}
|
||||
disabled={availability.isLoading || !isPairingAvailable}
|
||||
>
|
||||
Show QR code
|
||||
</Button>
|
||||
|
||||
{unavailableMessage && (
|
||||
<Alert variant="destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription>{unavailableMessage}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<Label htmlFor="allow-private-network-http" className="text-xs sm:text-sm">
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
import {
|
||||
clearRemoteAccessHeaders,
|
||||
createDevicePairingLinks,
|
||||
getDevicePairingAvailability,
|
||||
getRemoteAccessHeaders,
|
||||
listPairedDevices,
|
||||
replaceRemoteAccessHeaders,
|
||||
@@ -16,6 +17,7 @@ import {
|
||||
|
||||
const REMOTE_ACCESS_HEADERS_QUERY_KEY = ['remote-access-headers']
|
||||
const PAIRED_DEVICES_QUERY_KEY = ['paired-devices']
|
||||
const DEVICE_PAIRING_AVAILABILITY_QUERY_KEY = ['device-pairing-availability']
|
||||
|
||||
export function useRemoteAccessHeaders() {
|
||||
const queryClient = useQueryClient()
|
||||
@@ -55,6 +57,15 @@ export function useCreateDevicePairingLinks() {
|
||||
})
|
||||
}
|
||||
|
||||
export function useDevicePairingAvailability() {
|
||||
const origin = window.location.origin
|
||||
|
||||
return useQuery({
|
||||
queryKey: [...DEVICE_PAIRING_AVAILABILITY_QUERY_KEY, origin],
|
||||
queryFn: () => getDevicePairingAvailability(origin),
|
||||
})
|
||||
}
|
||||
|
||||
export function usePairedDevices() {
|
||||
return useQuery({
|
||||
queryKey: PAIRED_DEVICES_QUERY_KEY,
|
||||
|
||||
Reference in new issue
Block a user