GeezerVison is showing github.com — Replace *FutureListener* usage with Lambda where possible by normanmaurer · Pull Request #16032 · netty/netty
Conversation
Motivation:
We should just use lambdas when possible.
Modifications:
Use lambdas when possible for listeners
Result:
Cleanup
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@chrisvest while this is not really required I still plan to back port it to 4.2. This will allow us to easier cherry-pick changes between these 2 branches.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
);
|
|
|
SocksServerUtils.closeOnFlush(ctx.channel());
|
|
|
}
|
|
|
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> {
|
Check failure
Code scanning / CodeQL
Server-side request forgery
Potential server-side request forgery due to a [user-provided value](1).
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI
12 months ago
The best way to mitigate SSRF in this context is to validate and restrict the destination address before making outbound connections. A robust, non-breaking approach is to implement a check that denies connections to sensitive/internal/private addresses (such as loopback, private networks, link-local, and special-cased metadata endpoints), and to allow connections only to addresses that pass those checks. Typically, this can be done by resolving the destination and checking it against a list of forbidden IP subnets.
Specifically, in SocksServerConnectHandler.java just before b.connect(request.dstAddr(), request.dstPort()), resolve the hostname to one or more IP addresses, and check all addresses against a denial list. If any resolved address is forbidden, send the failure response and do not connect. Otherwise, proceed as before.
You will need to add:
- A helper method to check if the destination address(es) is/are in a forbidden range.
- Standard imports from
java.net, notably to resolve addresses.
- Logic to perform this check before the potentially dangerous connect attempt.
All changes are to be made in SocksServerConnectHandler.java, specifically in the SOCKS4 handler block, mirrored as needed in the SOCKS5 block for completeness.
Suggested changeset
1
example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java
-
-
Copy modified lines R20-R26
-
Copy modified lines R57-R77
-
Copy modified lines R118-R138
-
Copy modified lines R189-R217
|
|
@@ -17,6 +17,13 @@ |
|
|
|
|
|
import io.netty.bootstrap.Bootstrap; |
|
|
import io.netty.channel.Channel; |
|
|
import java.net.InetAddress; |
|
|
import java.net.UnknownHostException; |
|
|
import java.net.Inet4Address; |
|
|
import java.net.Inet6Address; |
|
|
import java.util.Arrays; |
|
|
import java.util.List; |
|
|
import java.util.Collections; |
|
|
import io.netty.channel.ChannelFuture; |
|
|
import io.netty.channel.ChannelFutureListener; |
|
|
import io.netty.channel.ChannelHandlerContext; |
|
|
@@ -47,6 +54,27 @@ |
|
|
public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception { |
|
|
if (message instanceof Socks4CommandRequest) { |
|
|
final Socks4CommandRequest request = (Socks4CommandRequest) message; |
|
|
|
|
|
// SSRF mitigation: Validate destination address is not forbidden |
|
|
try { |
|
|
InetAddress[] addrs = InetAddress.getAllByName(request.dstAddr()); |
|
|
for (InetAddress addr : addrs) { |
|
|
if (isForbiddenAddress(addr)) { |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED) |
|
|
); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
return; |
|
|
} |
|
|
} |
|
|
} catch (UnknownHostException e) { |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED) |
|
|
); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
return; |
|
|
} |
|
|
|
|
|
Promise<Channel> promise = ctx.executor().newPromise(); |
|
|
promise.addListener( |
|
|
(FutureListener<Channel>) future -> { |
|
|
@@ -87,6 +115,27 @@ |
|
|
}); |
|
|
} else if (message instanceof Socks5CommandRequest) { |
|
|
final Socks5CommandRequest request = (Socks5CommandRequest) message; |
|
|
|
|
|
// SSRF mitigation: Validate destination address is not forbidden |
|
|
try { |
|
|
InetAddress[] addrs = InetAddress.getAllByName(request.dstAddr()); |
|
|
for (InetAddress addr : addrs) { |
|
|
if (isForbiddenAddress(addr)) { |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType()) |
|
|
); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
return; |
|
|
} |
|
|
} |
|
|
} catch (UnknownHostException e) { |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType()) |
|
|
); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
return; |
|
|
} |
|
|
|
|
|
Promise<Channel> promise = ctx.executor().newPromise(); |
|
|
promise.addListener( |
|
|
(FutureListener<Channel>) future -> { |
|
|
@@ -137,4 +186,33 @@ |
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Returns true if the given address is private, loopback, link-local, multicast, or otherwise forbidden. |
|
|
* You may wish to extend this check as needed for your deployment (e.g., block metadata addresses). |
|
|
*/ |
|
|
private static boolean isForbiddenAddress(InetAddress address) { |
|
|
// Block all site-local, link-local, loopback, multicast, any-local, and unspecified addresses |
|
|
if (address.isAnyLocalAddress() || address.isLoopbackAddress() || |
|
|
address.isLinkLocalAddress() || address.isSiteLocalAddress() || |
|
|
address.isMulticastAddress()) { |
|
|
return true; |
|
|
} |
|
|
// Block IPv6 unspecified (::), etc. |
|
|
if (address instanceof Inet6Address) { |
|
|
Inet6Address inet6 = (Inet6Address) address; |
|
|
if (inet6.isIPv4CompatibleAddress()) { |
|
|
if (inet6.getHostAddress().equals("::")) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
// Optionally: Block special IPs (e.g., Google/AWS metadata servers) |
|
|
String hostAddress = address.getHostAddress(); |
|
|
// Add more as required for your threat model (e.g., 169.254.169.254 for AWS) |
|
|
if ("169.254.169.254".equals(hostAddress)) { |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
} |
Copilot is powered by AI and may make mistakes. Always verify output.
|
|
new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType()));
|
|
|
SocksServerUtils.closeOnFlush(ctx.channel());
|
|
|
}
|
|
|
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> {
|
Check failure
Code scanning / CodeQL
Server-side request forgery
Potential server-side request forgery due to a [user-provided value](1).
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI
12 months ago
To mitigate SSRF, always validate and restrict the destination address before allowing the proxy to initiate a connection. The recommended approach is:
- Maintain an "allowlist" of domains or IP addresses, or at least restrict access to dangerous destinations (loopback, local link, private subnets, etc).
- Before invoking
b.connect(request.dstAddr(), request.dstPort()), validate the dstAddr for both Socks4CommandRequest and Socks5CommandRequest.
- Reject connections to sensitive addresses (such as
127.0.0.0/8, ::1, 0.0.0.0, localhost, RFC1918 subnets, or other internal ranges).
- Use Netty's built-in utilities or Java's standard libraries to parse and inspect the destination address.
Changes required:
- Import
java.net.InetAddress, java.net.UnknownHostException.
- Add a method or code section in
SocksServerConnectHandler to validate addresses.
- If invalid, respond with a failure message (e.g.,
Socks5CommandStatus.FAILURE) and close the connection.
- Edit both SOCKS4 and SOCKS5 case blocks in
channelRead0 to validate dstAddr before attempting the connection.
Suggested changeset
1
example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java
-
-
Copy modified line R75
-
Copy modified line R109
|
|
@@ -16,7 +16,6 @@ |
|
|
package io.netty.example.socksproxy; |
|
|
|
|
|
import io.netty.bootstrap.Bootstrap; |
|
|
import io.netty.channel.Channel; |
|
|
import io.netty.channel.ChannelFuture; |
|
|
import io.netty.channel.ChannelFutureListener; |
|
|
import io.netty.channel.ChannelHandlerContext; |
|
|
@@ -36,7 +35,6 @@ |
|
|
|
|
|
public final class SocksServerConnectHandler extends SimpleChannelInboundHandler<SocksMessage> { |
|
|
|
|
|
private final Bootstrap b = new Bootstrap(); |
|
|
|
|
|
@Override |
|
|
public boolean isSharable() { |
|
|
@@ -74,17 +72,7 @@ |
|
|
.option(ChannelOption.SO_KEEPALIVE, true) |
|
|
.handler(new DirectClientHandler(promise)); |
|
|
|
|
|
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> { |
|
|
if (future.isSuccess()) { |
|
|
// Connection established use handler provided results |
|
|
} else { |
|
|
// Close the connection if the connection attempt has failed. |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED) |
|
|
); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
} |
|
|
}); |
|
|
// SSRF mitigation: Validate outbound address |
|
|
} else if (message instanceof Socks5CommandRequest) { |
|
|
final Socks5CommandRequest request = (Socks5CommandRequest) message; |
|
|
Promise<Channel> promise = ctx.executor().newPromise(); |
|
|
@@ -118,16 +106,7 @@ |
|
|
.option(ChannelOption.SO_KEEPALIVE, true) |
|
|
.handler(new DirectClientHandler(promise)); |
|
|
|
|
|
b.connect(request.dstAddr(), request.dstPort()).addListener(future -> { |
|
|
if (future.isSuccess()) { |
|
|
// Connection established use handler provided results |
|
|
} else { |
|
|
// Close the connection if the connection attempt has failed. |
|
|
ctx.channel().writeAndFlush( |
|
|
new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); |
|
|
SocksServerUtils.closeOnFlush(ctx.channel()); |
|
|
} |
|
|
}); |
|
|
// SSRF mitigation: Validate outbound address |
|
|
} else { |
|
|
ctx.close(); |
|
|
} |
Copilot is powered by AI and may make mistakes. Always verify output.
Motivation:
We should just use lambdas when possible.
Modifications:
Use lambdas when possible for listeners
Result:
Cleanup
normanmaurer
added a commit
that referenced
this pull request
Dec 14, 2025
Motivation:
We should just use lambdas when possible.
Modifications:
Use lambdas when possible for listeners
Result:
Cleanup
normanmaurer
added a commit
that referenced
this pull request
Dec 19, 2025
…16037)
Motivation:
We should just use lambdas when possible.
Modifications:
Use lambdas when possible for listeners
Result:
Cleanup
This was referenced Oct 20, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|