mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-05 06:40:56 -06:00
* fix: Encode fileServerFolder to fix #25839 * fix: Encode fileServerFolder to fix #25839 add changelog * update unit tests * adjust CHANGELOG location * Update packages/server/test/unit/config_spec.js * Fix changelog entry placement * Fix some bugs to pass the test case * maintain a test case * Fix changelog entry * added integration test and updated to use encodeURI * Update cli/CHANGELOG.md --------- Co-authored-by: Jennifer Shehane <jennifer@cypress.io> Co-authored-by: Jennifer Shehane <shehane.jennifer@gmail.com> Co-authored-by: Matt Schile <mschile@cypress.io> Co-authored-by: Matt Schile <mschile@gmail.com>
82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
// TODO: move this to packages/core-file-server
|
|
|
|
const _ = require('lodash')
|
|
const debug = require('debug')('cypress:server:file_server')
|
|
const url = require('url')
|
|
const http = require('http')
|
|
const path = require('path')
|
|
const send = require('send')
|
|
const { httpUtils } = require('@packages/network')
|
|
const { allowDestroy } = require('./util/server_destroy')
|
|
const random = require('./util/random')
|
|
const networkFailures = require('./util/network_failures')
|
|
|
|
const onRequest = function (req, res, expectedToken, fileServerFolder) {
|
|
const token = req.headers['x-cypress-authorization']
|
|
|
|
if (token !== expectedToken) {
|
|
debug('authorization failed on file_server request %o', { reqUrl: req.url, expectedToken, token })
|
|
res.statusCode = 401
|
|
res.end()
|
|
|
|
return
|
|
}
|
|
|
|
const args = _.compact([
|
|
fileServerFolder,
|
|
req.url,
|
|
])
|
|
|
|
// strip off any query params from our req's url
|
|
// since we're pulling this from the file system
|
|
// it does not understand query params
|
|
// and make sure we decode the uri which swaps out
|
|
// %20 with white space
|
|
const file = decodeURI(url.parse(path.join(...args)).pathname)
|
|
|
|
res.setHeader('x-cypress-file-path', encodeURI(file))
|
|
|
|
return send(req, url.parse(req.url).pathname, {
|
|
root: path.resolve(fileServerFolder),
|
|
})
|
|
.on('error', (err) => {
|
|
res.setHeader('x-cypress-file-server-error', true)
|
|
res.setHeader('content-type', 'text/html')
|
|
res.statusCode = err.status
|
|
|
|
return res.end(networkFailures.get(file, err.status))
|
|
}).pipe(res)
|
|
}
|
|
|
|
module.exports = {
|
|
create (fileServerFolder) {
|
|
return new Promise(((resolve) => {
|
|
const token = random.id(64)
|
|
|
|
const srv = http.createServer(httpUtils.lenientOptions, (req, res) => {
|
|
return onRequest(req, res, token, fileServerFolder)
|
|
})
|
|
|
|
allowDestroy(srv)
|
|
|
|
return srv.listen(0, '127.0.0.1', () => {
|
|
return resolve({
|
|
token,
|
|
|
|
port () {
|
|
return srv.address().port
|
|
},
|
|
|
|
address () {
|
|
return `http://localhost:${this.port()}`
|
|
},
|
|
|
|
close () {
|
|
return srv.destroyAsync()
|
|
},
|
|
})
|
|
})
|
|
}))
|
|
},
|
|
}
|