Skip to content

Commit 7fcb342

Browse files
committed
Add default CORS origins
1 parent e57a940 commit 7fcb342

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Diff for: src/index.ts

+8
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+
/^https?:\/\/127\.0\.0\.1(:\d+)?$/, // Artisan serve (SCHEME://127.0.0.1:PORT)
159+
/^https?:\/\/.*\.test(:\d+)?$/, // Valet / Herd (SCHEME://*.test:PORT)
160+
/^https?:\/\/localhost(:\d+)?$/, // 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),

Diff for: tests/index.test.ts

+76
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,80 @@ 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+
'https://my-app.test:8',
489+
// APP_URL
490+
'http://example.com',
491+
].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(true)
492+
// Disallowed origins...
493+
expect([
494+
'http://laravel.com',
495+
'https://laravel.com',
496+
'http://laravel.com:8000',
497+
'https://laravel.com:8000',
498+
'http://128.0.0.1',
499+
'https://128.0.0.1',
500+
'http://128.0.0.1:8000',
501+
'https://128.0.0.1:8000',
502+
'https://example.com',
503+
'http://example.com:8000',
504+
'https://example.com:8000',
505+
'http://exampletest',
506+
'http://example.test:',
507+
].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(false)
508+
509+
fs.rmSync(path.join(__dirname, '.env'))
510+
})
511+
512+
it("respects the user's server.cors config", () => {
513+
const plugins = laravel({
514+
input: 'resources/js/app.js',
515+
})
516+
const resolvedConfig = plugins[0].config({
517+
envDir: __dirname,
518+
server: {
519+
cors: true,
520+
}
521+
}, {
522+
mode: '',
523+
command: 'serve'
524+
})
525+
526+
expect(resolvedConfig.server.cors)
527+
})
452528
})
453529

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

0 commit comments

Comments
 (0)