Prevent connecting to self (#1150)

When receiving an AddPeer, or when loading the static-nodes.json file, pantheon
should not accept its own NodeID as a valid connection (however, this should not invalidate the static-nodes file).
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Trent Mohay 6 years ago committed by GitHub
parent 892fe87032
commit 5b41b56d8e
  1. 3
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeer.java
  2. 23
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/ConnectingToLocalNodeException.java
  3. 6
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/NettyP2PNetwork.java
  4. 2
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/peers/StaticNodesParser.java
  5. 6
      pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java
  6. 2
      pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java

@ -17,6 +17,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.PeerNotPermittedException;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer;
@ -50,6 +51,8 @@ public class AdminAddPeer extends AdminModifyPeer {
} catch (final PeerNotPermittedException e) {
return new JsonRpcErrorResponse(
id, JsonRpcError.NON_PERMITTED_NODE_CANNOT_BE_ADDED_AS_A_PEER);
} catch (final ConnectingToLocalNodeException e) {
return new JsonRpcErrorResponse(id, JsonRpcError.INVALID_PARAMS);
}
}
}

@ -0,0 +1,23 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.p2p;
public class ConnectingToLocalNodeException extends RuntimeException {
public ConnectingToLocalNodeException(final String message) {
super(message);
}
public ConnectingToLocalNodeException() {
super("Cannot add the local node as a peer connection");
}
}

@ -18,6 +18,7 @@ import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.chain.BlockAddedEvent;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.PeerNotPermittedException;
import tech.pegasys.pantheon.ethereum.p2p.api.DisconnectCallback;
import tech.pegasys.pantheon.ethereum.p2p.api.Message;
@ -369,6 +370,11 @@ public class NettyP2PNetwork implements P2PNetwork {
if (!isPeerAllowed(peer)) {
throw new PeerNotPermittedException();
}
if (peer.getId().equals(ourPeerInfo.getNodeId())) {
throw new ConnectingToLocalNodeException();
}
final boolean added = peerMaintainConnectionList.add(peer);
if (added) {
connect(peer);

@ -40,7 +40,7 @@ public class StaticNodesParser {
try {
return readEnodesFromPath(path);
} catch (FileNotFoundException | NoSuchFileException ex) {
LOG.info("No StaticNodes file ({}) exists, creating empty cache.", path);
LOG.info("StaticNodes file {} does not exist, no static connections will be created.", path);
return emptySet();
} catch (IOException ex) {
LOG.info("Unable to parse static nodes file ({})", path);

@ -40,6 +40,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.logs.LogsSu
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.pending.PendingTransactionSubscriptionService;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscriptionService;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.p2p.ConnectingToLocalNodeException;
import tech.pegasys.pantheon.ethereum.p2p.NetworkRunner;
import tech.pegasys.pantheon.ethereum.p2p.NoopP2PNetwork;
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork;
@ -304,7 +305,10 @@ public class RunnerBuilder {
staticNodes.forEach(
enodeURL -> {
final Peer peer = DefaultPeer.fromEnodeURL(enodeURL);
peerNetwork.addMaintainConnectionPeer(peer);
try {
peerNetwork.addMaintainConnectionPeer(peer);
} catch (ConnectingToLocalNodeException ex) {
}
});
Optional<JsonRpcHttpService> jsonRpcHttpService = Optional.empty();

@ -622,6 +622,8 @@ public class PantheonCommand implements DefaultCommandValues, Runnable {
permissioningConfiguration();
final Collection<EnodeURL> staticNodes = loadStaticNodes();
logger.info("Connecting to {} static nodes.", staticNodes.size());
logger.trace("Static Nodes = {}", staticNodes);
permissioningConfiguration
.flatMap(PermissioningConfiguration::getLocalConfig)

Loading…
Cancel
Save