GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — HAProxy: Reject illegal characters in UNIX socket files · lltopk/netty@409621c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-haproxy/src
    • main/java/io/netty/handler/codec/haproxy
      • HAProxyMessage.java
    • test/java/io/netty/handler/codec/haproxy
      • HaProxyMessageEncoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessage.java‎

Copy file name to clipboardExpand all lines: codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyMessage.java
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public HAProxyMessage(
100100
ObjectUtil.checkNotNull(tlvs, "tlvs");
101101
AddressFamily addrFamily = proxiedProtocol.addressFamily();
102102

103-
checkAddress(sourceAddress, addrFamily);
104-
checkAddress(destinationAddress, addrFamily);
103+
checkAddress(sourceAddress, addrFamily, protocolVersion);
104+
checkAddress(destinationAddress, addrFamily, protocolVersion);
105105
checkPort(sourcePort, addrFamily);
106106
checkPort(destinationPort, addrFamily);
107107

@@ -470,11 +470,12 @@ private static int portStringToInt(String value) {
470470
/**
471471
* Validate an address (IPv4, IPv6, Unix Socket)
472472
*
473-
* @param address human-readable address
474-
* @param addrFamily the {@link AddressFamily} to check the address against
475-
* @throws IllegalArgumentException if the address is invalid
473+
* @param address human-readable address
474+
* @param addrFamily the {@link AddressFamily} to check the address against
475+
* @param version the protocol version
476+
* @throws IllegalArgumentException if the address is invalid
476477
*/
477-
private static void checkAddress(String address, AddressFamily addrFamily) {
478+
private static void checkAddress(String address, AddressFamily addrFamily, HAProxyProtocolVersion version) {
478479
ObjectUtil.checkNotNull(addrFamily, "addrFamily");
479480

480481
switch (addrFamily) {
@@ -488,6 +489,15 @@ private static void checkAddress(String address, AddressFamily addrFamily) {
488489
if (address.getBytes(CharsetUtil.US_ASCII).length > 108) {
489490
throw new IllegalArgumentException("invalid AF_UNIX address: " + address);
490491
}
492+
if (version == HAProxyProtocolVersion.V1) {
493+
// V1 is text-based and uses CR LF as header delimiters, and space as field delimiter.
494+
for (int i = 0, len = address.length(); i < len; i++) {
495+
char c = address.charAt(i);
496+
if (c == '\r' || c == '\n' || c == ' ') {
497+
throw new IllegalArgumentException("invalid AF_UNIX address: " + address);
498+
}
499+
}
500+
}
491501
return;
492502
}
493503

Collapse file

‎codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec-haproxy/src/test/java/io/netty/handler/codec/haproxy/HaProxyMessageEncoderTest.java
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import io.netty.util.CharsetUtil;
2626
import org.junit.jupiter.api.Test;
2727
import org.junit.jupiter.api.function.Executable;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.ValueSource;
2830

2931
import java.util.ArrayList;
3032
import java.util.Collections;
@@ -400,6 +402,44 @@ public void execute() {
400402
});
401403
}
402404

405+
@ParameterizedTest
406+
@ValueSource(chars = {'\r', '\n', ' '})
407+
public void testIllegalCharacterInV1UnixAddress(char illegal) {
408+
final String invalidUnixAddress = "/var/run/dst" + illegal + ".sock";
409+
final String fineUnixAddress = "/var/run/dst.sock";
410+
assertThrows(IllegalArgumentException.class, new Executable() {
411+
@Override
412+
public void execute() {
413+
new HAProxyMessage(
414+
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
415+
invalidUnixAddress, fineUnixAddress, 0, 0);
416+
}
417+
});
418+
assertThrows(IllegalArgumentException.class, new Executable() {
419+
@Override
420+
public void execute() {
421+
new HAProxyMessage(
422+
HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
423+
fineUnixAddress, invalidUnixAddress, 0, 0);
424+
}
425+
});
426+
}
427+
428+
@ParameterizedTest
429+
@ValueSource(chars = {'\r', '\n', ' '})
430+
public void testIllegalV1UnixCharactersAreFineInV2(char illegal) {
431+
final String suspectUnixAddress = "/var/run/dst" + illegal + ".sock";
432+
final String fineUnixAddress = "/var/run/dst.sock";
433+
new HAProxyMessage(
434+
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
435+
suspectUnixAddress, fineUnixAddress, 0, 0)
436+
.release();
437+
new HAProxyMessage(
438+
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.UNIX_STREAM,
439+
fineUnixAddress, suspectUnixAddress, 0, 0)
440+
.release();
441+
}
442+
403443
@Test
404444
public void testNullUnixAddress() {
405445
assertThrows(NullPointerException.class, new Executable() {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.