Skip to content

Commit 076c6cd

Browse files
committed
Add default CORS origins
1 parent e57a940 commit 076c6cd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
152152
},
153153
server: {
154154
origin: userConfig.server?.origin ?? '__laravel_vite_placeholder__',
155+
cors: userConfig.server?.cors ?? {
156+
origin: userConfig.server?.origin ?? [
157+
...(env.APP_URL ? [env.APP_URL] : []), // * (APP_URL="http://my-app.tld")
158+
new RegExp('^https?://127.0.0.1(:[0-9]+)?$'), // Artisan serve (SCHEME://127.0.0.1:PORT)
159+
new RegExp('^https?://.*.test(:[0-9]+)?$'), // Valet / Herd (SCHEME://*.test:PORT)
160+
new RegExp('^https?://localhost(:[0-9]+)?$'), // Docker (SCHEME://localhost:PORT)
161+
],
162+
},
155163
...(process.env.LARAVEL_SAIL ? {
156164
host: userConfig.server?.host ?? '0.0.0.0',
157165
port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),

tests/index.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest'
2+
import fs from 'fs'
23
import laravel from '../src'
34
import { resolvePageComponent } from '../src/inertia-helpers';
5+
import path from 'path';
46

57
vi.mock('fs', async () => {
68
const actual = await vi.importActual<typeof import('fs')>('fs')
@@ -449,6 +451,77 @@ describe('laravel-vite-plugin', () => {
449451
config: { delay: 123 }
450452
})
451453
})
454+
455+
it('configures default cors.origin values', () => {
456+
const test = (pattern: RegExp|string, value: string) => pattern instanceof RegExp ? pattern.test(value) : pattern === value
457+
fs.writeFileSync(path.join(__dirname, '.env'), 'APP_URL=http://example.com')
458+
459+
const plugins = laravel({
460+
input: 'resources/js/app.js',
461+
})
462+
const resolvedConfig = plugins[0].config({ envDir: __dirname }, {
463+
mode: '',
464+
command: 'serve'
465+
})
466+
467+
// Allowed origins...
468+
expect([
469+
// localhost
470+
'http://localhost',
471+
'https://localhost',
472+
'http://localhost:8080',
473+
'https://localhost:8080',
474+
// 127.0.0.1
475+
'http://127.0.0.1',
476+
'https://127.0.0.1',
477+
'http://127.0.0.1:8000',
478+
'https://127.0.0.1:8000',
479+
// *.test
480+
'http://laravel.test',
481+
'https://laravel.test',
482+
'http://laravel.test:8000',
483+
'https://laravel.test:8000',
484+
'http://my-app.test',
485+
'https://my-app.test',
486+
'http://my-app.test:8000',
487+
'https://my-app.test:8000',
488+
// APP_URL
489+
'http://example.com',
490+
].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(true)
491+
// Disallowed origins...
492+
expect([
493+
'http://laravel.com',
494+
'https://laravel.com',
495+
'http://laravel.com:8000',
496+
'https://laravel.com:8000',
497+
'http://128.0.0.1',
498+
'https://128.0.0.1',
499+
'http://128.0.0.1:8000',
500+
'https://128.0.0.1:8000',
501+
'https://example.com',
502+
'http://example.com:8000',
503+
'https://example.com:8000',
504+
].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(false)
505+
506+
fs.rmSync(path.join(__dirname, '.env'))
507+
})
508+
509+
it("respects the user's server.cors config", () => {
510+
const plugins = laravel({
511+
input: 'resources/js/app.js',
512+
})
513+
const resolvedConfig = plugins[0].config({
514+
envDir: __dirname,
515+
server: {
516+
cors: true,
517+
}
518+
}, {
519+
mode: '',
520+
command: 'serve'
521+
})
522+
523+
expect(resolvedConfig.server.cors)
524+
})
452525
})
453526

454527
describe('inertia-helpers', () => {

0 commit comments

Comments
 (0)