Add ability to configure CORS header (#1345)

## Issue Addressed

https://github.com/sigp/lighthouse/issues/1177

## Proposed Changes

Add a command line option (`--http-allow-origin`) and a config item for configuring the `Access-Control-Allow-Origin` response header.  This should unblock making XMLHttpRequests.
This commit is contained in:
Adam Szkoda
2020-07-16 07:23:14 +00:00
parent 4a01f44206
commit fc5e6cbbb0
9 changed files with 210 additions and 152 deletions

View File

@@ -4,6 +4,7 @@ use clap_utils::BAD_TESTNET_DIR_MESSAGE;
use client::{config::DEFAULT_DATADIR, ClientConfig, ClientGenesis};
use eth2_libp2p::{Enr, Multiaddr};
use eth2_testnet_config::Eth2TestnetConfig;
use hyper;
use slog::{crit, info, Logger};
use ssz::Encode;
use std::fs;
@@ -220,6 +221,15 @@ pub fn get_config<E: EthSpec>(
.map_err(|_| "http-port is not a valid u16.")?;
}
if let Some(allow_origin) = cli_args.value_of("http-allow-origin") {
// Pre-validate the config value to give feedback to the user on node startup, instead of
// as late as when the first API response is produced.
hyper::header::HeaderValue::from_str(allow_origin)
.map_err(|_| "Invalid allow-origin value")?;
client_config.rest_api.allow_origin = allow_origin.to_string();
}
/*
* Websocket server
*/