Skip to content

Commit ce64455

Browse files
authored
fix(cors): fixup #4985, allow same-origin ws requests of any domain (#5142)
* fix: followup of #4985, allow same-site ws requests of any domain * fix: match whole string
1 parent 3ee096e commit ce64455

File tree

1 file changed

+13
-6
lines changed
  • packages/@vue/cli/lib

1 file changed

+13
-6
lines changed

packages/@vue/cli/lib/ui.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ const { log, error, openBrowser } = require('@vue/cli-shared-utils')
22
const { portfinder, server } = require('@vue/cli-ui/server')
33
const shortid = require('shortid')
44

5+
function simpleCorsValidation (allowedHost) {
6+
return function (req, socket) {
7+
const { host, origin } = req.headers
8+
// maybe we should just use strict string equal?
9+
const hostRegExp = new RegExp(`^(${host}|${allowedHost}|localhost)(:\\d+)?$`)
10+
11+
if (!origin || !hostRegExp.test(origin)) {
12+
socket.destroy()
13+
}
14+
}
15+
}
16+
517
async function ui (options = {}, context = process.cwd()) {
618
const host = options.host || 'localhost'
719

@@ -69,12 +81,7 @@ async function ui (options = {}, context = process.cwd()) {
6981
}
7082
})
7183

72-
httpServer.on('upgrade', (req, socket) => {
73-
const { origin } = req.headers
74-
if (!origin || !(new RegExp(host)).test(origin)) {
75-
socket.destroy()
76-
}
77-
})
84+
httpServer.on('upgrade', simpleCorsValidation(host))
7885
}
7986

8087
module.exports = (...args) => {

0 commit comments

Comments
 (0)