test(e2e): address self-review follow-ups (dedup, XSS render guard, SSE hardening)

Follow-ups from the code review of the test-quality batches:

- Consolidate duplicated helpers into e2e/helpers/: seed.ts (seedUpload,
  seedComment, listComments, findFeedRow) and sse.ts (mintSseTicket, openStream,
  trackStreamOpens). Refactor authorization-deep, xss-injection, like-comment,
  sse-ticket-abuse, ddos, sse-realtime, multi-tab, and SseListener to use them —
  the upload/comment/ticket-flow contracts now live in one place each instead of
  being re-inlined across 3–7 specs.
- xss-injection display-name loop: it navigated to /feed (which renders uploader
  names, not the viewer's) so "nothing fired" passed vacuously — the payload was
  never rendered. Now navigate to /account (the actual sink) and add a render
  guard asserting the payload reached the DOM as escaped text before checking
  __xssFired.
- sse-realtime reconnect: snapshot the stream-open count AFTER backgrounding, so
  the "new connection" assertion is attributable to the foreground event and can't
  be satisfied by a spurious native/error reconnect before the toggle.
- recover-page: correct the comment (auto-submit is the onPinInput handler, not an
  $effect).

44 affected specs verified green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
fabi
2026-07-01 19:49:44 +02:00
parent ec64fc361b
commit b1e2e66305
11 changed files with 156 additions and 158 deletions

View File

@@ -12,22 +12,10 @@
* / a navigation to a `javascript:` URL.
*/
import { test, expect } from '../../fixtures/test';
import { uploadRaw, JPEG_MAGIC } from '../../helpers/upload-client';
import { seedUpload, seedComment } from '../../helpers/seed';
const BASE = process.env.E2E_FRONTEND_URL ?? 'http://localhost:3101';
/** Seed a real, feed-visible upload owned by `jwt`; returns its id. */
async function seedVisibleUpload(jwt: string, caption: string, db: any): Promise<string> {
const body = new Uint8Array(1024);
body.set(JPEG_MAGIC, 0);
const res = await uploadRaw(jwt, body, { filename: 'x.jpg', contentType: 'image/jpeg', caption });
if (res.status !== 201) throw new Error(`seed upload failed: ${res.status} ${await res.text()}`);
const { id } = await res.json();
// Mark compression done so the card is fully rendered in the feed.
await db.setUploadCompressionStatus(id, 'done');
return id;
}
const XSS_PAYLOADS = [
`<script>window.__xssFired=true</script>`,
`<img src=x onerror="window.__xssFired=true">`,
@@ -76,9 +64,16 @@ test.describe('Adversarial — input injection (display name)', () => {
d.dismiss().catch(() => {});
});
await page.goto('/feed');
// /account is where the viewer's own display name is rendered (the sink). /feed
// shows uploader names, not the viewer's — navigating there rendered nothing, so
// the non-firing check passed vacuously. Go where the payload actually lands.
await page.goto('/account');
await page.waitForLoadState('domcontentloaded');
// Render guard: confirm the payload actually reached the DOM as escaped text,
// so a "nothing fired" pass can't be because the name was never rendered.
await expect(page.getByText(payload, { exact: false }).first()).toBeVisible({ timeout: 10_000 });
const fired = await page.evaluate(() => (window as any).__xssFired === true);
expect(fired, 'window.__xssFired should never be set').toBe(false);
expect(dialogs, 'no dialogs should appear').toHaveLength(0);
@@ -92,11 +87,11 @@ test.describe('Adversarial — input injection (display name)', () => {
test.describe('Adversarial — stored XSS (caption)', () => {
for (const payload of XSS_PAYLOADS) {
test(`caption with XSS payload ${JSON.stringify(payload).slice(0, 40)} renders inert`, async ({ guest, db, page, signIn }) => {
test(`caption with XSS payload ${JSON.stringify(payload).slice(0, 40)} renders inert`, async ({ guest, page, signIn }) => {
const g = await guest('CapXss');
// A trailing marker lets us wait until the caption has actually rendered before
// asserting nothing fired — otherwise a caption that never rendered would pass vacuously.
const id = await seedVisibleUpload(g.jwt, `${payload} CAPMARK`, db);
const id = await seedUpload(g.jwt, { caption: `${payload} CAPMARK` });
expect(id).toMatch(/^[0-9a-f-]{36}$/);
const dialogs: string[] = [];
@@ -123,16 +118,11 @@ test.describe('Adversarial — stored XSS (comment)', () => {
`"><svg onload="window.__xssFired=true">`,
];
for (const payload of COMMENT_PAYLOADS) {
test(`comment with XSS payload ${JSON.stringify(payload).slice(0, 40)} renders inert`, async ({ guest, db, page, signIn }) => {
test(`comment with XSS payload ${JSON.stringify(payload).slice(0, 40)} renders inert`, async ({ guest, page, signIn }) => {
const author = await guest('CmtXss');
const id = await seedVisibleUpload(author.jwt, 'pic CAPMARK', db);
const id = await seedUpload(author.jwt, { caption: 'pic CAPMARK' });
// Post the XSS comment via the API (verbatim storage).
const cRes = await fetch(`${BASE}/api/v1/upload/${id}/comments`, {
method: 'POST',
headers: { Authorization: `Bearer ${author.jwt}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ body: `${payload} CMTMARK` }),
});
expect(cRes.status).toBe(201);
await seedComment(author.jwt, id, `${payload} CMTMARK`);
const dialogs: string[] = [];
page.on('dialog', (d) => { dialogs.push(d.message()); d.dismiss().catch(() => {}); });