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

client - Add functionality to specify cors for rpc server #1762

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ const args = yargs(hideBin(process.argv))
describe: 'Additionally log complete RPC calls on log level debug (i.e. --loglevel=debug)',
boolean: true,
})
.option('rpcCors', {
describe: 'Configure the Access-Control-Allow-Origin CORS header for RPC server',
string: true,
default: '*',
})
.option('maxPerRequest', {
describe: 'Max items per block or header request',
number: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/client/bin/startRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RPCArgs = {
helprpc: boolean
'jwt-secret'?: string
rpcEngineAuth: boolean
rpcCors: string
}

/**
Expand Down Expand Up @@ -104,6 +105,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
rpcEnginePort,
'jwt-secret': jwtSecretPath,
rpcEngineAuth,
rpcCors,
} = args
const manager = new RPCManager(client, config)
const jwtSecret = parseJwtSecret(config, jwtSecretPath)
Expand All @@ -121,6 +123,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {

if (rpc) {
rpcHttpServer = createRPCServerListener({
rpcCors,
server,
withEngineMiddleware:
withEngineMethods && rpcEngineAuth
Expand All @@ -140,6 +143,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
}
if (ws) {
const opts: any = {
rpcCors,
server,
withEngineMiddleware: withEngineMethods && rpcEngineAuth ? { jwtSecret } : undefined,
}
Expand All @@ -166,6 +170,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) {
server.on('response', onBatchResponse)

createRPCServerListener({
rpcCors,
server,
withEngineMiddleware: rpcEngineAuth
? {
Expand Down
19 changes: 16 additions & 3 deletions packages/client/lib/util/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Server as RPCServer, HttpServer } from 'jayson/promise'
import { json as jsonParser } from 'body-parser'
import { decode, TAlgorithm } from 'jwt-simple'
import Connect, { IncomingMessage } from 'connect'
import cors from 'cors'

const algorithm: TAlgorithm = 'HS256'

type CreateRPCServerListenerOpts = {
rpcCors?: string
server: RPCServer
withEngineMiddleware?: WithEngineMiddleware
}
Expand All @@ -24,8 +26,10 @@ function checkHeaderAuth(req: any, jwtSecret: Buffer): void {
}

export function createRPCServerListener(opts: CreateRPCServerListenerOpts): HttpServer {
const { server, withEngineMiddleware } = opts
const { server, withEngineMiddleware, rpcCors } = opts

const app = Connect()
if (rpcCors) app.use(cors({ origin: rpcCors }))
app.use(jsonParser())

if (withEngineMiddleware) {
Expand Down Expand Up @@ -54,9 +58,18 @@ export function createRPCServerListener(opts: CreateRPCServerListenerOpts): Http
export function createWsRPCServerListener(
opts: CreateRPCServerListenerOpts & { httpServer?: HttpServer }
): HttpServer | undefined {
const { server, withEngineMiddleware } = opts
const { server, withEngineMiddleware, rpcCors } = opts

// Get the server to hookup upgrade request on
const httpServer = opts.httpServer ?? createServer()
let httpServer = opts.httpServer
if (!httpServer) {
const app = Connect()
// In case browser pre-flights the upgrade request with an options request
// more likely in case of wss connection
if (rpcCors) app.use(cors({ origin: rpcCors }))
httpServer = createServer(app)
}

const wss = server.websocket({ noServer: true })

httpServer.on('upgrade', (req, socket, head) => {
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"body-parser": "^1.19.2",
"chalk": "^4.1.2",
"connect": "^3.7.0",
"cors": "^2.8.5",
"debug": "^4.3.3",
"ethereumjs-util": "^7.1.4",
"fs-extra": "^10.0.0",
Expand Down