Skip to content

Commit

Permalink
Prevent printing binary in RPC errors (#1604)
Browse files Browse the repository at this point in the history
## Issue Addressed

#1566 

## Proposed Changes

Prevents printing binary characters in the RPC error response from peers.
  • Loading branch information
AgeManning committed Sep 10, 2020
1 parent b19cf02 commit d79366c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions beacon_node/eth2_libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tiny-keccak = "2.0.2"
environment = { path = "../../lighthouse/environment" }
# TODO: Remove rand crate for mainnet
rand = "0.7.3"
regex = "1.3.9"

[dependencies.libp2p]
#version = "0.23.0"
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/eth2_libp2p/src/rpc/methods.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Available RPC methods types and ids.
use crate::types::EnrBitfield;
use regex::bytes::Regex;
use serde::Serialize;
use ssz_derive::{Decode, Encode};
use ssz_types::{
Expand Down Expand Up @@ -42,10 +43,9 @@ impl Deref for ErrorType {

impl ToString for ErrorType {
fn to_string(&self) -> String {
match std::str::from_utf8(self.0.deref()) {
Ok(s) => s.to_string(),
Err(_) => format!("{:?}", self.0.deref()), // Display raw bytes if not a UTF-8 string
}
#[allow(clippy::invalid_regex)]
let re = Regex::new("\\p{C}").expect("Regex is valid");
String::from_utf8_lossy(&re.replace_all(self.0.deref(), &b""[..])).to_string()
}
}

Expand Down

0 comments on commit d79366c

Please sign in to comment.