|
1 | 1 | import fetch from 'node-fetch'
|
2 |
| -import { beforeAll, describe, expect, test } from 'vitest' |
| 2 | +import { |
| 3 | + afterEach, |
| 4 | + beforeAll, |
| 5 | + beforeEach, |
| 6 | + describe, |
| 7 | + expect, |
| 8 | + test, |
| 9 | +} from 'vitest' |
| 10 | +import type { Page } from 'playwright-chromium' |
3 | 11 | import testJSON from '../safe.json'
|
4 |
| -import { isServe, page, viteTestUrl } from '~utils' |
| 12 | +import { browser, isServe, page, viteTestUrl } from '~utils' |
| 13 | + |
| 14 | +const getViteTestIndexHtmlUrl = () => { |
| 15 | + const srcPrefix = viteTestUrl.endsWith('/') ? '' : '/' |
| 16 | + // NOTE: viteTestUrl is set lazily |
| 17 | + return viteTestUrl + srcPrefix + 'src/' |
| 18 | +} |
5 | 19 |
|
6 | 20 | const stringified = JSON.stringify(testJSON)
|
7 | 21 |
|
8 | 22 | describe.runIf(isServe)('main', () => {
|
9 | 23 | beforeAll(async () => {
|
10 |
| - const srcPrefix = viteTestUrl.endsWith('/') ? '' : '/' |
11 |
| - await page.goto(viteTestUrl + srcPrefix + 'src/') |
| 24 | + await page.goto(getViteTestIndexHtmlUrl()) |
12 | 25 | })
|
13 | 26 |
|
14 | 27 | test('default import', async () => {
|
@@ -113,3 +126,59 @@ describe('fetch', () => {
|
113 | 126 | expect(res.headers.get('x-served-by')).toBe('vite')
|
114 | 127 | })
|
115 | 128 | })
|
| 129 | + |
| 130 | +describe('cross origin', () => { |
| 131 | + const fetchStatusFromPage = async (page: Page, url: string) => { |
| 132 | + return await page.evaluate(async (url: string) => { |
| 133 | + try { |
| 134 | + const res = await globalThis.fetch(url) |
| 135 | + return res.status |
| 136 | + } catch { |
| 137 | + return -1 |
| 138 | + } |
| 139 | + }, url) |
| 140 | + } |
| 141 | + |
| 142 | + describe('allowed for same origin', () => { |
| 143 | + beforeEach(async () => { |
| 144 | + await page.goto(getViteTestIndexHtmlUrl()) |
| 145 | + }) |
| 146 | + |
| 147 | + test('fetch HTML file', async () => { |
| 148 | + const status = await fetchStatusFromPage(page, viteTestUrl + '/src/') |
| 149 | + expect(status).toBe(200) |
| 150 | + }) |
| 151 | + |
| 152 | + test.runIf(isServe)('fetch JS file', async () => { |
| 153 | + const status = await fetchStatusFromPage( |
| 154 | + page, |
| 155 | + viteTestUrl + '/src/code.js', |
| 156 | + ) |
| 157 | + expect(status).toBe(200) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + describe('denied for different origin', async () => { |
| 162 | + let page2: Page |
| 163 | + beforeEach(async () => { |
| 164 | + page2 = await browser.newPage() |
| 165 | + await page2.goto('http://vite.dev/404') |
| 166 | + }) |
| 167 | + afterEach(async () => { |
| 168 | + await page2.close() |
| 169 | + }) |
| 170 | + |
| 171 | + test('fetch HTML file', async () => { |
| 172 | + const status = await fetchStatusFromPage(page2, viteTestUrl + '/src/') |
| 173 | + expect(status).not.toBe(200) |
| 174 | + }) |
| 175 | + |
| 176 | + test.runIf(isServe)('fetch JS file', async () => { |
| 177 | + const status = await fetchStatusFromPage( |
| 178 | + page2, |
| 179 | + viteTestUrl + '/src/code.js', |
| 180 | + ) |
| 181 | + expect(status).not.toBe(200) |
| 182 | + }) |
| 183 | + }) |
| 184 | +}) |
3 commit comments
timvandam commentedon Feb 18, 2025
Hi @sapphi-red, do you know why this behavior was changed in a patch bump (4.5.5 -> 4.5.6)? Does vite ship breaking changes and should we pin its version? Or was this an accident
sapphi-red commentedon Feb 18, 2025
@timvandam Please read GHSA-vg6x-rcgg-rjx6. We don't ship breaking changes in minor and patch, but we ship breaking changes when it's a vuln fix.
timvandam commentedon Feb 18, 2025
@sapphi-red I see, thanks for the link!