GeezerVison is showing github.com — HAProxy: Reject illegal characters in UNIX socket files · lltopk/netty@409621c
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/haproxy test/java/io/netty/handler/codec/haproxy
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -100,8 +100,8 @@ public HAProxyMessage( | 100 | 100 | ObjectUtil.checkNotNull(tlvs, "tlvs"); | 101 | 101 | AddressFamily addrFamily = proxiedProtocol.addressFamily(); | 102 | 102 |
| 103 | | - checkAddress(sourceAddress, addrFamily); | 104 | | - checkAddress(destinationAddress, addrFamily); | | 103 | + checkAddress(sourceAddress, addrFamily, protocolVersion); | | 104 | + checkAddress(destinationAddress, addrFamily, protocolVersion); | 105 | 105 | checkPort(sourcePort, addrFamily); | 106 | 106 | checkPort(destinationPort, addrFamily); | 107 | 107 |
| @@ -470,11 +470,12 @@ private static int portStringToInt(String value) { | 470 | 470 | /** | 471 | 471 | * Validate an address (IPv4, IPv6, Unix Socket) | 472 | 472 | * | 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 | 476 | 477 | */ | 477 | | - private static void checkAddress(String address, AddressFamily addrFamily) { | | 478 | + private static void checkAddress(String address, AddressFamily addrFamily, HAProxyProtocolVersion version) { | 478 | 479 | ObjectUtil.checkNotNull(addrFamily, "addrFamily"); | 479 | 480 |
| 480 | 481 | switch (addrFamily) { | @@ -488,6 +489,15 @@ private static void checkAddress(String address, AddressFamily addrFamily) { | 488 | 489 | if (address.getBytes(CharsetUtil.US_ASCII).length > 108) { | 489 | 490 | throw new IllegalArgumentException("invalid AF_UNIX address: " + address); | 490 | 491 | } | | 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 | + } | 491 | 501 | return; | 492 | 502 | } | 493 | 503 |
| |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 25 | 25 | import io.netty.util.CharsetUtil; | 26 | 26 | import org.junit.jupiter.api.Test; | 27 | 27 | import org.junit.jupiter.api.function.Executable; | | 28 | +import org.junit.jupiter.params.ParameterizedTest; | | 29 | +import org.junit.jupiter.params.provider.ValueSource; | 28 | 30 |
| 29 | 31 | import java.util.ArrayList; | 30 | 32 | import java.util.Collections; | @@ -400,6 +402,44 @@ public void execute() { | 400 | 402 | }); | 401 | 403 | } | 402 | 404 |
| | 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 | + | 403 | 443 | @Test | 404 | 444 | public void testNullUnixAddress() { | 405 | 445 | assertThrows(NullPointerException.class, new Executable() { | |
|