-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement source address filtering (#171)
- Loading branch information
Showing
12 changed files
with
250 additions
and
20 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/java/org/ethereum/beacon/discovery/AddressAccessPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.ethereum.beacon.discovery; | ||
|
||
import java.net.InetSocketAddress; | ||
import org.ethereum.beacon.discovery.schema.NodeRecord; | ||
|
||
@FunctionalInterface | ||
public interface AddressAccessPolicy { | ||
AddressAccessPolicy ALLOW_ALL = __ -> true; | ||
|
||
boolean allow(InetSocketAddress address); | ||
|
||
default boolean allow(NodeRecord record) { | ||
return record.getTcpAddress().map(this::allow).orElse(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/ethereum/beacon/discovery/pipeline/handler/PacketSourceFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.ethereum.beacon.discovery.pipeline.handler; | ||
|
||
import java.net.InetSocketAddress; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.ethereum.beacon.discovery.AddressAccessPolicy; | ||
import org.ethereum.beacon.discovery.pipeline.Envelope; | ||
import org.ethereum.beacon.discovery.pipeline.EnvelopeHandler; | ||
import org.ethereum.beacon.discovery.pipeline.Field; | ||
import org.ethereum.beacon.discovery.pipeline.HandlerUtil; | ||
|
||
public class PacketSourceFilter implements EnvelopeHandler { | ||
private static final Logger LOG = LogManager.getLogger(PacketSourceFilter.class); | ||
|
||
private final AddressAccessPolicy addressAccessPolicy; | ||
|
||
public PacketSourceFilter(final AddressAccessPolicy addressAccessPolicy) { | ||
this.addressAccessPolicy = addressAccessPolicy; | ||
} | ||
|
||
@Override | ||
public void handle(final Envelope envelope) { | ||
if (!HandlerUtil.requireField(Field.REMOTE_SENDER, envelope)) { | ||
return; | ||
} | ||
final InetSocketAddress sender = envelope.get(Field.REMOTE_SENDER); | ||
if (!addressAccessPolicy.allow(sender)) { | ||
envelope.remove(Field.INCOMING); | ||
LOG.debug("Ignoring message from disallowed source {}", sender); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/org/ethereum/beacon/discovery/pipeline/handler/OutgoingParcelHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.ethereum.beacon.discovery.pipeline.handler; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.ethereum.beacon.discovery.AddressAccessPolicy; | ||
import org.ethereum.beacon.discovery.network.NetworkParcel; | ||
import org.ethereum.beacon.discovery.network.NetworkParcelV5; | ||
import org.ethereum.beacon.discovery.packet.impl.RawPacketImpl; | ||
import org.ethereum.beacon.discovery.pipeline.Envelope; | ||
import org.ethereum.beacon.discovery.pipeline.Field; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.FluxSink; | ||
|
||
class OutgoingParcelHandlerTest { | ||
|
||
public static final RawPacketImpl PACKET = | ||
new RawPacketImpl(Bytes.fromHexString("0x12341234123412341234123412341234")); | ||
private static final InetSocketAddress DISALLOWED_ADDRESS = | ||
new InetSocketAddress(InetAddress.getLoopbackAddress(), 12345); | ||
private static final InetSocketAddress ALLOWED_ADDRESS = | ||
new InetSocketAddress(InetAddress.getLoopbackAddress(), 8080); | ||
|
||
@SuppressWarnings("unchecked") | ||
private final FluxSink<NetworkParcel> outgoingSink = | ||
(FluxSink<NetworkParcel>) mock(FluxSink.class); | ||
|
||
private final AddressAccessPolicy addressAccessPolicy = | ||
address -> !address.equals(DISALLOWED_ADDRESS); | ||
|
||
private final OutgoingParcelHandler handler = | ||
new OutgoingParcelHandler(outgoingSink, addressAccessPolicy); | ||
|
||
@Test | ||
void shouldNotSendPacketsToDisallowedHosts() { | ||
final Envelope envelope = new Envelope(); | ||
envelope.put(Field.INCOMING, new NetworkParcelV5(PACKET, DISALLOWED_ADDRESS)); | ||
handler.handle(envelope); | ||
|
||
verifyNoInteractions(outgoingSink); | ||
} | ||
|
||
@Test | ||
void shouldSendPacketsToAllowedHosts() { | ||
final Envelope envelope = new Envelope(); | ||
final NetworkParcelV5 parcel = new NetworkParcelV5(PACKET, ALLOWED_ADDRESS); | ||
envelope.put(Field.INCOMING, parcel); | ||
handler.handle(envelope); | ||
|
||
verify(outgoingSink).next(parcel); | ||
} | ||
} |
Oops, something went wrong.