Skip to content

Commit c841730

Browse files
mentatpsiclaude
andcommitted
fix(server): reject cross-site requests to /__open-in-editor
Hardening (not a reported vulnerability): the launch-editor middleware at /__open-in-editor opens the developer's configured editor on a caller- supplied file path with no origin check, so any page the developer visits while the dev server runs can trigger it with a cross-site GET. Impact is low (blind; opens existing files only), but the endpoint is easy to align with how Vite already guards other dev-only endpoints. Reject requests whose Sec-Fetch-Site is `cross-site`. Same-origin (Vite's own client), same-site (e.g. another local dev port, since the port is not part of the site, so cross-port setups keep working) and direct user navigations are allowed. For clients that do not send Sec-Fetch-Site, fall back to comparing the Origin hostname, ignoring the port; a request with no Origin is a non-browser client and is allowed. No new dependencies. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 942f6d7 commit c841730

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎packages/vite/src/node/server/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { disableCache, ignoreInput } from '@voidzero-dev/vite-task-client'
1111
import chokidar from 'chokidar'
1212
import connect from 'connect'
1313
import corsMiddleware from 'cors'
14-
import launchEditorMiddleware from 'launch-editor-middleware'
1514
import colors from 'picocolors'
1615
import type { SourceMap } from 'rolldown'
1716
import { detectAgent } from 'std-env'
@@ -87,6 +86,7 @@ import {
8786
} from './middlewares/indexHtml'
8887
import { memoryFilesMiddleware } from './middlewares/memoryFiles'
8988
import { notFoundMiddleware } from './middlewares/notFound'
89+
import { openInEditorMiddleware } from './middlewares/openInEditor'
9090
import { proxyMiddleware } from './middlewares/proxy'
9191
import { rejectInvalidRequestMiddleware } from './middlewares/rejectInvalidRequest'
9292
import {
@@ -1140,7 +1140,7 @@ async function applyMiddlewares(
11401140
}
11411141

11421142
// open in editor support
1143-
middlewares.use('/__open-in-editor', launchEditorMiddleware())
1143+
middlewares.use('/__open-in-editor', openInEditorMiddleware())
11441144

11451145
// ping request handler
11461146
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type * as http from 'node:http'
2+
import { describe, expect, test } from 'vitest'
3+
import { isCrossSiteRequest } from '../openInEditor'
4+
5+
const HOST = 'localhost:5173'
6+
7+
function req(headers: Record<string, string>): http.IncomingMessage {
8+
return {
9+
headers: { host: HOST, ...headers },
10+
} as unknown as http.IncomingMessage
11+
}
12+
13+
describe('isCrossSiteRequest (the /__open-in-editor guard)', () => {
14+
test.each([
15+
['same-origin request', { 'sec-fetch-site': 'same-origin' }],
16+
['direct navigation (Sec-Fetch-Site: none)', { 'sec-fetch-site': 'none' }],
17+
['same-site / cross-port request', { 'sec-fetch-site': 'same-site' }],
18+
['non-browser request (no fetch metadata, no Origin)', {}],
19+
['request whose Origin matches the host', { origin: `http://${HOST}` }],
20+
['request from another local port', { origin: 'http://localhost:3000' }],
21+
])('treats %s as not cross-site', (_name, headers) => {
22+
expect(isCrossSiteRequest(req(headers))).toBe(false)
23+
})
24+
25+
test.each([
26+
['cross-site request', { 'sec-fetch-site': 'cross-site' }],
27+
['request with a cross-origin Origin', { origin: 'http://evil.example' }],
28+
['request with an opaque Origin (null)', { origin: 'null' }],
29+
])('treats %s as cross-site', (_name, headers) => {
30+
expect(isCrossSiteRequest(req(headers))).toBe(true)
31+
})
32+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type * as http from 'node:http'
2+
import launchEditorMiddleware from 'launch-editor-middleware'
3+
import type { Connect } from '#dep-types/connect'
4+
5+
export function isCrossSiteRequest(req: http.IncomingMessage): boolean {
6+
const site = req.headers['sec-fetch-site']
7+
if (site !== undefined) {
8+
// Reject only genuinely cross-site requests (a different site, e.g. a page
9+
// the developer happens to be visiting). `same-origin` (Vite's own client),
10+
// `same-site` (e.g. another local dev port — the port is not part of the
11+
// site) and `none` (a direct user navigation) are allowed, so cross-port
12+
// local dev setups keep working.
13+
return site === 'cross-site'
14+
}
15+
// Fallback for clients that do not send `Sec-Fetch-Site`: compare the `Origin`
16+
// hostname, ignoring the port so cross-port local dev still works. A request
17+
// with no `Origin` is a non-browser client and cannot be a CSRF vector.
18+
const origin = req.headers.origin
19+
if (origin === undefined) return false
20+
if (origin === 'null') return true
21+
const host = req.headers.host
22+
if (host === undefined) return true
23+
try {
24+
return new URL(origin).hostname !== new URL(`http://${host}`).hostname
25+
} catch {
26+
return true
27+
}
28+
}
29+
30+
// The editor-launch endpoint (`/__open-in-editor`) opens the developer's
31+
// configured editor on a caller-supplied file path. Guard it against cross-site
32+
// requests so a page the developer visits cannot trigger it, while leaving
33+
// same-origin and cross-port local dev requests working.
34+
export function openInEditorMiddleware(): Connect.NextHandleFunction {
35+
const launchEditor = launchEditorMiddleware()
36+
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
37+
return function viteOpenInEditorMiddleware(req, res) {
38+
if (isCrossSiteRequest(req)) {
39+
res.statusCode = 403
40+
res.end('Forbidden: cross-site request to /__open-in-editor')
41+
return
42+
}
43+
launchEditor(req, res)
44+
}
45+
}

0 commit comments

Comments
 (0)