|
| 1 | +/** |
| 2 | + * XSS Smoke Tests |
| 3 | + * |
| 4 | + * Verifies that user-input rendering paths properly escape XSS payloads. |
| 5 | + * Tests the three primary attack surfaces: |
| 6 | + * - Comment text (stored XSS) |
| 7 | + * - Board title (stored XSS) |
| 8 | + * - Project info link URL (javascript: scheme) |
| 9 | + * |
| 10 | + * @see https://github.com/laststance/gitbox/issues/127 |
| 11 | + */ |
| 12 | + |
| 13 | +import { test, expect } from '../fixtures/coverage' |
| 14 | +import { |
| 15 | + BOARD_IDS, |
| 16 | + CARD_IDS, |
| 17 | + resetProjectInfoComments, |
| 18 | +} from '../helpers/db-query' |
| 19 | + |
| 20 | +test.describe('XSS Prevention Smoke Tests (Authenticated)', () => { |
| 21 | + test.use({ storageState: 'e2e/.auth/user.json' }) |
| 22 | + |
| 23 | + const BOARD_URL = `/board/${BOARD_IDS.testBoard}` |
| 24 | + const XSS_SCRIPT_PAYLOAD = '<script>alert("xss")</script>' |
| 25 | + const XSS_IMG_PAYLOAD = '<img src=x onerror=alert("xss")>' |
| 26 | + const XSS_JS_URL = 'javascript:alert(1)' |
| 27 | + |
| 28 | + test.beforeEach(async ({ page }) => { |
| 29 | + await resetProjectInfoComments() |
| 30 | + await page.goto(BOARD_URL) |
| 31 | + await page.waitForLoadState('networkidle') |
| 32 | + await expect( |
| 33 | + page.locator('[data-testid^="repo-card-"]').first(), |
| 34 | + ).toBeVisible({ timeout: 10000 }) |
| 35 | + }) |
| 36 | + |
| 37 | + test('should escape script tags in comment text', async ({ page }) => { |
| 38 | + // Track any dialog/alert events — should never fire |
| 39 | + let alertFired = false |
| 40 | + page.on('dialog', async (dialog) => { |
| 41 | + alertFired = true |
| 42 | + await dialog.dismiss() |
| 43 | + }) |
| 44 | + |
| 45 | + // Click on card-1 comment to enter edit mode |
| 46 | + const commentDisplay = page.locator( |
| 47 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-display"]`, |
| 48 | + ) |
| 49 | + await expect(commentDisplay).toBeVisible({ timeout: 10000 }) |
| 50 | + await commentDisplay.click() |
| 51 | + |
| 52 | + // Type XSS payload into textarea |
| 53 | + const textarea = page.locator( |
| 54 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-inline-edit"] textarea`, |
| 55 | + ) |
| 56 | + await expect(textarea).toBeVisible({ timeout: 5000 }) |
| 57 | + await textarea.fill(XSS_SCRIPT_PAYLOAD) |
| 58 | + await textarea.press('Enter') |
| 59 | + |
| 60 | + // Verify the XSS payload is rendered as escaped text, not executed |
| 61 | + // (toContainText auto-waits for the save to complete) |
| 62 | + const commentText = page.locator( |
| 63 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-text"]`, |
| 64 | + ) |
| 65 | + await expect(commentText).toContainText('<script>') |
| 66 | + |
| 67 | + // Verify no alert fired after DOM settled |
| 68 | + expect(alertFired).toBe(false) |
| 69 | + }) |
| 70 | + |
| 71 | + test('should escape img onerror payload in comment text', async ({ |
| 72 | + page, |
| 73 | + }) => { |
| 74 | + let alertFired = false |
| 75 | + page.on('dialog', async (dialog) => { |
| 76 | + alertFired = true |
| 77 | + await dialog.dismiss() |
| 78 | + }) |
| 79 | + |
| 80 | + const commentDisplay = page.locator( |
| 81 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-display"]`, |
| 82 | + ) |
| 83 | + await expect(commentDisplay).toBeVisible({ timeout: 10000 }) |
| 84 | + await commentDisplay.click() |
| 85 | + |
| 86 | + const textarea = page.locator( |
| 87 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-inline-edit"] textarea`, |
| 88 | + ) |
| 89 | + await expect(textarea).toBeVisible({ timeout: 5000 }) |
| 90 | + await textarea.fill(XSS_IMG_PAYLOAD) |
| 91 | + await textarea.press('Enter') |
| 92 | + |
| 93 | + // Verify the XSS payload is rendered as escaped text, not executed |
| 94 | + // (toContainText auto-waits for the save to complete) |
| 95 | + const commentText = page.locator( |
| 96 | + `[data-testid="repo-card-${CARD_IDS.card1}"] [data-testid="comment-text"]`, |
| 97 | + ) |
| 98 | + await expect(commentText).toContainText('<img') |
| 99 | + |
| 100 | + // Verify no alert fired after DOM settled |
| 101 | + expect(alertFired).toBe(false) |
| 102 | + }) |
| 103 | + |
| 104 | + test('should reject javascript: URL in project info link', async ({ |
| 105 | + page, |
| 106 | + }) => { |
| 107 | + let alertFired = false |
| 108 | + page.on('dialog', async (dialog) => { |
| 109 | + alertFired = true |
| 110 | + await dialog.dismiss() |
| 111 | + }) |
| 112 | + |
| 113 | + // Open the first card's note/project info modal |
| 114 | + const card = page.locator(`[data-testid="repo-card-${CARD_IDS.card1}"]`) |
| 115 | + await card.click() |
| 116 | + |
| 117 | + // Wait for note modal to appear |
| 118 | + const modal = page.locator('[role="dialog"]') |
| 119 | + await expect(modal).toBeVisible({ timeout: 5000 }) |
| 120 | + |
| 121 | + // Find a link edit button and click it |
| 122 | + let enteredEditMode = false |
| 123 | + const editButton = modal.locator('[data-testid^="url-edit-"]').first() |
| 124 | + if (await editButton.isVisible({ timeout: 3000 }).catch(() => false)) { |
| 125 | + await editButton.click() |
| 126 | + enteredEditMode = true |
| 127 | + } else { |
| 128 | + // If no existing link, look for "add link" button |
| 129 | + const addLink = modal.locator('button:has-text("Add")').first() |
| 130 | + if (await addLink.isVisible({ timeout: 3000 }).catch(() => false)) { |
| 131 | + await addLink.click() |
| 132 | + enteredEditMode = true |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Ensure we actually found an edit/add button — test is meaningless otherwise |
| 137 | + expect(enteredEditMode).toBe(true) |
| 138 | + |
| 139 | + // Try to enter javascript: URL |
| 140 | + const urlInput = modal.locator('[data-testid^="url-input-"]').first() |
| 141 | + await expect(urlInput).toBeVisible({ timeout: 3000 }) |
| 142 | + await urlInput.fill(XSS_JS_URL) |
| 143 | + await urlInput.press('Tab') |
| 144 | + |
| 145 | + // Wait for validation |
| 146 | + await page.waitForTimeout(500) |
| 147 | + |
| 148 | + // Should show error — javascript: is not allowed |
| 149 | + const error = modal.locator('[role="alert"]').first() |
| 150 | + await expect(error).toBeVisible({ timeout: 3000 }) |
| 151 | + await expect(error).toContainText('http') |
| 152 | + |
| 153 | + expect(alertFired).toBe(false) |
| 154 | + }) |
| 155 | +}) |
0 commit comments