Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cors): fixup #4985, allow same-origin ws requests of any domain #5142

Merged
merged 2 commits into from Feb 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/@vue/cli/lib/ui.js
Expand Up @@ -2,6 +2,18 @@ const { log, error, openBrowser } = require('@vue/cli-shared-utils')
const { portfinder, server } = require('@vue/cli-ui/server')
const shortid = require('shortid')

function simpleCorsValidation (allowedHost) {
return function (req, socket) {
const { host, origin } = req.headers
// maybe we should just use strict string equal?
const hostRegExp = new RegExp(`^(${host}|${allowedHost}|localhost)(:\\d+)?$`)

if (!origin || !hostRegExp.test(origin)) {
socket.destroy()
}
}
}

async function ui (options = {}, context = process.cwd()) {
const host = options.host || 'localhost'

Expand Down Expand Up @@ -69,12 +81,7 @@ async function ui (options = {}, context = process.cwd()) {
}
})

httpServer.on('upgrade', (req, socket) => {
const { origin } = req.headers
if (!origin || !(new RegExp(host)).test(origin)) {
socket.destroy()
}
})
httpServer.on('upgrade', simpleCorsValidation(host))
}

module.exports = (...args) => {
Expand Down