Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/src/pairing-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
2 changes: 1 addition & 1 deletion core/src/pairing-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions web/src/app/(app)/link/_hooks/use-link-flow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 },
);
Expand All @@ -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 });

Expand All @@ -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');
Expand All @@ -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 () => {
Expand Down
11 changes: 6 additions & 5 deletions web/src/app/(app)/link/_hooks/use-link-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ 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';

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<LinkStep>({ kind: 'code' });
const code = useLinkCodeForm({
Expand All @@ -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' });
Expand Down
11 changes: 10 additions & 1 deletion web/src/app/(app)/link/_lib/describe-link.test.ts
Original file line number Diff line number Diff line change
@@ -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) }));

Expand All @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions web/src/app/(app)/link/_lib/describe-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '');
}
Expand Down
2 changes: 2 additions & 0 deletions web/src/app/(app)/link/_lib/scanned-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion web/src/app/(app)/link/_lib/scanned-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion web/src/app/(site)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Loading