From 79edb85cde8a21edc485bee50e47ddcf43652afe Mon Sep 17 00:00:00 2001 From: Marcelo Camargo <90155206+marcelorsdd@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:16:34 -0300 Subject: [PATCH] feat: keep the pairing code out of the URL the server sees --- CHANGELOG.md | 4 ++++ core/src/pairing-code.test.ts | 4 ++-- core/src/pairing-code.ts | 2 +- web/README.md | 2 +- .../(app)/link/_hooks/use-link-flow.test.tsx | 18 +++++++++++++----- web/src/app/(app)/link/_hooks/use-link-flow.ts | 11 ++++++----- .../app/(app)/link/_lib/describe-link.test.ts | 11 ++++++++++- web/src/app/(app)/link/_lib/describe-link.ts | 4 ++++ .../app/(app)/link/_lib/scanned-code.test.ts | 2 ++ web/src/app/(app)/link/_lib/scanned-code.ts | 2 +- web/src/app/(site)/page.tsx | 2 +- 11 files changed, 45 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f5b1e..f027da8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ## [Unreleased] +### Security + +- The console's pairing QR code puts the code in the URL fragment (`/link#code=…`), which browsers never send, so the code no longer reaches the server or its logs. The website still accepts `/link?code=…` from older consoles. + ## [0.3.0] - 2026-11-15 ### Added diff --git a/core/src/pairing-code.test.ts b/core/src/pairing-code.test.ts index f0ff1a0..c2dd3aa 100644 --- a/core/src/pairing-code.test.ts +++ b/core/src/pairing-code.test.ts @@ -50,8 +50,8 @@ describe('isUserCode', () => { }); describe('pairingLink', () => { - it('appends the code without the dash to the link page', () => { - expect(pairingLink('https://openpsapp.app/link', 'ABCD-2345')).toBe('https://openpsapp.app/link?code=ABCD2345'); + it('puts the code without the dash in the fragment, which browsers never send to the server', () => { + expect(pairingLink('https://openpsapp.app/link', 'ABCD-2345')).toBe('https://openpsapp.app/link#code=ABCD2345'); }); }); diff --git a/core/src/pairing-code.ts b/core/src/pairing-code.ts index 389011c..59bf026 100644 --- a/core/src/pairing-code.ts +++ b/core/src/pairing-code.ts @@ -23,7 +23,7 @@ export function parseUserCode(input: string): string | null { } export function pairingLink(verificationUri: string, userCode: string): string { - return `${verificationUri}?code=${userCode.replace(/-/g, '')}`; + return `${verificationUri}#code=${userCode.replace(/-/g, '')}`; } export function displayUrl(uri: string): string { diff --git a/web/README.md b/web/README.md index b5974a3..c158e81 100644 --- a/web/README.md +++ b/web/README.md @@ -2,7 +2,7 @@ `@openpsapp/web` is the OpenPSApp website: sign in, see the synced profile, trophies and games from any browser, manage the linked PS5 and the account, and link a console by typing its code or scanning its QR code. It is a single-page app built with React 19, Vite, Tailwind CSS 4, shadcn (`base-nova` on Base UI), React Router, TanStack Query, react-hook-form with zod, i18next and Clerk. -The cloud server serves it. Each release attaches the build as `web.zip`, and the server downloads that file and checks it against `WEB_SHA256` (see [Releasing](../CONTRIBUTING.md#releasing)). The server answers the browser-history routes (`/`, `/trophies`, `/games/:titleId`, `/account`, `/link?code=`) with `index.html`. +The cloud server serves it. Each release attaches the build as `web.zip`, and the server downloads that file and checks it against `WEB_SHA256` (see [Releasing](../CONTRIBUTING.md#releasing)). The server answers the browser-history routes (`/`, `/trophies`, `/games/:titleId`, `/account`, `/link`) with `index.html`. The console's QR code carries the pairing code in the fragment (`/link#code=…`), which browsers never send to the server; `/link?code=…` from older consoles still works. ## Commands diff --git a/web/src/app/(app)/link/_hooks/use-link-flow.test.tsx b/web/src/app/(app)/link/_hooks/use-link-flow.test.tsx index 5b4ec31..a17fc5c 100644 --- a/web/src/app/(app)/link/_hooks/use-link-flow.test.tsx +++ b/web/src/app/(app)/link/_hooks/use-link-flow.test.tsx @@ -3,7 +3,7 @@ import type { DeviceInfo } from '@openpsapp/contract/pairing'; import { QueryClientProvider } from '@tanstack/react-query'; import { act, renderHook, waitFor } from '@testing-library/react'; import type { ReactNode } from 'react'; -import { MemoryRouter, useSearchParams } from 'react-router'; +import { MemoryRouter, useLocation } from 'react-router'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { createQueryClient } from '@/lib/query-client'; import { queryKeys } from '@/lib/query-keys'; @@ -77,8 +77,8 @@ function renderLinkPage(entry: string) { () => { const flow = useLinkFlow(); const sync = useFirstSync(flow.step.kind === 'done' ? flow.step.previousDeviceIds : null); - const [params] = useSearchParams(); - return { flow, sync, code: params.get('code') }; + const location = useLocation(); + return { flow, sync, url: `${location.pathname}${location.search}${location.hash}` }; }, { wrapper }, ); @@ -100,7 +100,7 @@ describe('useLinkFlow', () => { 'POST /api/v1/devices/inspect', 'POST /api/v1/devices/confirm', ]); - expect(result.current.code).toBeNull(); + expect(result.current.url).toBe('/link'); expect(toast.success).toHaveBeenCalledTimes(1); expect(result.current.sync).toEqual({ phase: 'waiting', step: 1, steps: 3 }); @@ -118,6 +118,14 @@ describe('useLinkFlow', () => { expect(requests).toHaveLength(polls); }); + it('takes the code from the fragment the console shows and clears it once linked', async () => { + const { result } = renderLinkPage('/link#code=abcd-2345'); + await waitFor(() => expect(result.current.flow.step.kind).toBe('confirm')); + act(() => result.current.flow.confirm()); + await waitFor(() => expect(result.current.flow.step.kind).toBe('done')); + expect(result.current.url).toBe('/link'); + }); + it('goes back to the code with the reason when the confirmation fails', async () => { confirmReply = { status: 404, body: { error: 'invalid_code' } }; const { result } = renderLinkPage('/link?code=ABCD2345'); @@ -126,7 +134,7 @@ describe('useLinkFlow', () => { act(() => result.current.flow.confirm()); await waitFor(() => expect(result.current.flow.step).toEqual({ kind: 'code' })); expect(result.current.flow.code.rootError).toBe('invalid'); - expect(result.current.code).toBe('ABCD2345'); + expect(result.current.url).toBe('/link?code=ABCD2345'); }); it('accepts only scans of a pairing code or of this site link page', async () => { diff --git a/web/src/app/(app)/link/_hooks/use-link-flow.ts b/web/src/app/(app)/link/_hooks/use-link-flow.ts index b02bff0..8408adb 100644 --- a/web/src/app/(app)/link/_hooks/use-link-flow.ts +++ b/web/src/app/(app)/link/_hooks/use-link-flow.ts @@ -2,11 +2,11 @@ import type { DeviceInfo, PairingConfirmResponse } from '@openpsapp/contract/pai import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { useSearchParams } from 'react-router'; +import { useLocation, useNavigate } from 'react-router'; import { toast } from 'sonner'; import { api, apiPost } from '@/lib/api'; import { queryKeys } from '@/lib/query-keys'; -import { codeError, initialCode, shouldInspectOnLoad } from '../_lib/describe-link'; +import { codeError, initialCode, linkCodeParam, shouldInspectOnLoad } from '../_lib/describe-link'; import { readScannedCode } from '../_lib/scanned-code'; import type { LinkStep } from '../_types/link'; import { useLinkCodeForm } from './use-link-code-form'; @@ -14,8 +14,9 @@ import { useLinkCodeForm } from './use-link-code-form'; export function useLinkFlow() { const { t } = useTranslation(); const queryClient = useQueryClient(); - const [params, setParams] = useSearchParams(); - const [start] = useState(() => initialCode(params.get('code'))); + const location = useLocation(); + const navigate = useNavigate(); + const [start] = useState(() => initialCode(linkCodeParam(location.search, location.hash))); const inspectedOnLoad = useRef(false); const [step, setStep] = useState({ kind: 'code' }); const code = useLinkCodeForm({ @@ -33,7 +34,7 @@ export function useLinkFlow() { onSuccess: ({ result, previousDeviceIds }) => { toast.success(t('link.done.toastTitle'), { description: t('link.done.toastDescription') }); setStep({ kind: 'done', profileName: result.profileName, previousDeviceIds }); - if (params.has('code')) setParams({}, { replace: true }); + if (location.search || location.hash) void navigate({ pathname: location.pathname }, { replace: true }); }, onError: (error) => { setStep({ kind: 'code' }); diff --git a/web/src/app/(app)/link/_lib/describe-link.test.ts b/web/src/app/(app)/link/_lib/describe-link.test.ts index 88c9617..1473aaa 100644 --- a/web/src/app/(app)/link/_lib/describe-link.test.ts +++ b/web/src/app/(app)/link/_lib/describe-link.test.ts @@ -1,7 +1,7 @@ import type { DeviceInfo } from '@openpsapp/contract/pairing'; import { describe, expect, it, vi } from 'vitest'; import { ApiError } from '@/lib/api'; -import { codeError, displayCode, firstSyncPhase, initialCode, shouldInspectOnLoad } from './describe-link'; +import { codeError, displayCode, firstSyncPhase, initialCode, linkCodeParam, shouldInspectOnLoad } from './describe-link'; vi.mock('@clerk/react', () => ({ getToken: () => Promise.resolve(null) })); @@ -22,6 +22,15 @@ describe('initialCode', () => { }); }); +describe('linkCodeParam', () => { + it('reads the code from the fragment, or from the query of older consoles', () => { + expect(linkCodeParam('', '#code=ABCD2345')).toBe('ABCD2345'); + expect(linkCodeParam('?code=ABCD2345', '')).toBe('ABCD2345'); + expect(linkCodeParam('?code=WXYZ2345', '#code=ABCD2345')).toBe('ABCD2345'); + expect(linkCodeParam('', '')).toBeNull(); + }); +}); + describe('codeError', () => { it('maps server answers to what the code step shows', () => { expect(codeError(new ApiError(404, 'invalid_code'))).toBe('invalid'); diff --git a/web/src/app/(app)/link/_lib/describe-link.ts b/web/src/app/(app)/link/_lib/describe-link.ts index 1263caa..5767890 100644 --- a/web/src/app/(app)/link/_lib/describe-link.ts +++ b/web/src/app/(app)/link/_lib/describe-link.ts @@ -6,6 +6,10 @@ import type { CodeError, FirstSyncPhase } from '../_types/link'; export const FIRST_SYNC_POLL_MS = 3000; export const FIRST_SYNC_STEPS: FirstSyncPhase[] = ['waiting', 'connected', 'synced']; +export function linkCodeParam(search: string, hash: string): string | null { + return new URLSearchParams(hash.replace(/^#/, '')).get('code') ?? new URLSearchParams(search).get('code'); +} + export function initialCode(param: string | null): string { return sanitizeTypedCode(param ?? ''); } diff --git a/web/src/app/(app)/link/_lib/scanned-code.test.ts b/web/src/app/(app)/link/_lib/scanned-code.test.ts index 70e0ac2..e94001d 100644 --- a/web/src/app/(app)/link/_lib/scanned-code.test.ts +++ b/web/src/app/(app)/link/_lib/scanned-code.test.ts @@ -5,6 +5,7 @@ const origin = 'https://openpsapp.app'; describe('readScannedCode', () => { it('reads the link page URL the console shows', () => { + expect(readScannedCode('https://openpsapp.app/link#code=ABCD2345', origin)).toBe('ABCD2345'); expect(readScannedCode('https://openpsapp.app/link?code=ABCD2345', origin)).toBe('ABCD2345'); expect(readScannedCode('https://openpsapp.app/link/?code=abcd-2345', origin)).toBe('ABCD2345'); expect(readScannedCode(' https://openpsapp.app/link?code=ABCD2345\n', origin)).toBe('ABCD2345'); @@ -30,6 +31,7 @@ describe('readScannedCode', () => { expect(readScannedCode('https://openpsapp.app/link', origin)).toBeNull(); expect(readScannedCode('https://openpsapp.app/link?code=ABCD', origin)).toBeNull(); expect(readScannedCode('https://openpsapp.app/link?code=ABCD2345&code=WXYZ2345', origin)).toBeNull(); + expect(readScannedCode('https://openpsapp.app/link?code=ABCD2345#code=WXYZ2345', origin)).toBeNull(); expect(readScannedCode('https://openpsapp.app/link?code=ABCD0OI1', origin)).toBeNull(); }); diff --git a/web/src/app/(app)/link/_lib/scanned-code.ts b/web/src/app/(app)/link/_lib/scanned-code.ts index adc8289..fd7fbeb 100644 --- a/web/src/app/(app)/link/_lib/scanned-code.ts +++ b/web/src/app/(app)/link/_lib/scanned-code.ts @@ -20,6 +20,6 @@ export function readScannedCode(raw: string, origin: string): string | null { return null; } if (url.origin !== origin || url.username || url.password || !LINK_PATHS.has(url.pathname)) return null; - const codes = url.searchParams.getAll('code'); + const codes = [...new URLSearchParams(url.hash.slice(1)).getAll('code'), ...url.searchParams.getAll('code')]; return codes.length === 1 ? asCode(codes[0]) : null; } diff --git a/web/src/app/(site)/page.tsx b/web/src/app/(site)/page.tsx index ebfcbe5..2d62773 100644 --- a/web/src/app/(site)/page.tsx +++ b/web/src/app/(site)/page.tsx @@ -12,7 +12,7 @@ export default function LandingPage() { const { t } = useTranslation(); const clerk = useClerk(); const location = useLocation(); - const back = `${location.pathname}${location.search}`; + const back = `${location.pathname}${location.search}${location.hash}`; const signIn = () => clerk.openSignIn({ forceRedirectUrl: back, signUpForceRedirectUrl: back }); const signUp = () => clerk.openSignUp({ forceRedirectUrl: back, signInForceRedirectUrl: back }); return (