Files
cypress/packages/server/lib/util/socket_allowed.ts
Jennifer Shehane 639df99036 Rename uses of term 'whitelist' (#7782)
* Rename non-user facing instances of whitelist

* Rename server option 'whitelist' to 'ignore'

* Update use of whitelist with server to throw instead of warn

* Rename Cypress.Cookies.defaults 'whitelist' option to 'preserve'

* fix circle yml parameter parsing consistent

* compose cloning an external repo and switching to the NEXT_DEV_VERSION branch consistently

* add cypress org to repo parameter

* cd into the right dir before switching branches

* one line git checkout

* simplify passing repo

* cd into the right dir

* clone into the right dir

* oh my cd 101

* replace kitchen sink strings for 5.0.0

Co-authored-by: Brian Mann <brian.mann86@gmail.com>
2020-06-30 02:27:29 -04:00

47 lines
1.2 KiB
TypeScript

import _ from 'lodash'
import Debug from 'debug'
import net from 'net'
import { Request } from 'express'
const debug = Debug('cypress:server:util:socket_allowed')
/**
* Utility to validate incoming, local socket connections against a list of
* expected client TCP ports.
*/
export class SocketAllowed {
allowedLocalPorts: number[] = []
/**
* Add a socket to the allowed list.
*/
add = (socket: net.Socket) => {
const { localPort } = socket
debug('allowing socket %o', { localPort })
this.allowedLocalPorts.push(localPort)
socket.once('close', () => {
debug('allowed socket closed, removing %o', { localPort })
this._remove(socket)
})
}
_remove (socket: net.Socket) {
_.pull(this.allowedLocalPorts, socket.localPort)
}
/**
* Is this socket that this request originated allowed?
*/
isRequestAllowed (req: Request) {
const { remotePort, remoteAddress } = req.socket
const isAllowed = this.allowedLocalPorts.includes(remotePort!)
&& ['127.0.0.1', '::1'].includes(remoteAddress!)
debug('is incoming request allowed? %o', { isAllowed, reqUrl: req.url, remotePort, remoteAddress })
return isAllowed
}
}