Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/ProxyParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ struct ProxyParser {
return {false, 0};
}

uint8_t command = header.ver_cmd & 0x0f;
if (command > 1) {
return {false, 0};
}

//printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4);
//printf("Command: %d\n", (header.ver_cmd & 0x0f));

Expand All @@ -157,9 +162,28 @@ struct ProxyParser {
return {false, 0};
}

/* Payload cannot be more than sizeof proxy_addr */
if (sizeof(proxy_addr) < hostLength) {
return {false, 0};
unsigned int addressLength = 0;
if (command == 1) {
switch (header.fam) {
case 0x00:
case 0x31:
case 0x32:
break;
case 0x11:
case 0x12:
addressLength = 12;
break;
case 0x21:
case 0x22:
addressLength = 36;
break;
default:
return {false, 0};
}

if (hostLength < addressLength) {
return {false, 0};
}
}

//printf("Family: %d\n", (header.fam & 0xf0) >> 4);
Expand All @@ -168,9 +192,10 @@ struct ProxyParser {
/* Copy payload (only if not already done so before) */
if (!done) {
/* We have 0 family by default, and UNSPEC is 0 as well */
family = header.fam;

memcpy(&addr, data.data() + 16, hostLength);
if (addressLength) {
family = header.fam;
memcpy(&addr, data.data() + 16, addressLength);
}

done = true;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ default:
./ExtensionsNegotiator
$(CXX) -std=c++17 -fsanitize=address HttpParser.cpp -o HttpParser
./HttpParser
$(CXX) -std=c++17 -fsanitize=address ProxyParser.cpp -o ProxyParser
./ProxyParser
$(MAKE) -C ../uSockets
$(CXX) -std=c++17 -fsanitize=address -flto=auto -I../src -I../uSockets/src HttpResponse.cpp ../uSockets/*.o -lz -pthread -o HttpResponse
./HttpResponse
Expand Down
86 changes: 86 additions & 0 deletions tests/ProxyParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <cassert>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <utility>

#define UWS_WITH_PROXY
#include "../src/ProxyParser.h"

static std::string header(uint8_t verCmd, uint8_t fam, uint16_t len) {
std::string h("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12);
h += (char) verCmd;
h += (char) fam;
h += (char) (len >> 8);
h += (char) (len & 0xff);
for (uint16_t i = 0; i < len; i++) {
h += (char) (i + 1);
}
return h;
}

int main() {

{
uWS::ProxyParser pp;
std::string h = header(0x21, 0x11, 12);
assert(pp.parse(h) == std::make_pair(true, 28u));
assert(pp.getSourceAddress() == std::string_view("\x01\x02\x03\x04", 4));
}

{
uWS::ProxyParser pp;
std::string h = header(0x21, 0x21, 36);
assert(pp.parse(h) == std::make_pair(true, 52u));
assert(pp.getSourceAddress().length() == 16);
}

{
uWS::ProxyParser pp;
std::string h = header(0x21, 0x11, 40);
assert(pp.parse(h) == std::make_pair(true, 56u));
assert(pp.getSourceAddress() == std::string_view("\x01\x02\x03\x04", 4));
}

{
uWS::ProxyParser pp;
std::string h = header(0x20, 0x11, 12);
assert(pp.parse(h) == std::make_pair(true, 28u));
assert(pp.getSourceAddress().empty());
}

{
uWS::ProxyParser pp;
assert(pp.parse(header(0x22, 0x11, 12)).first == false);
}

{
uWS::ProxyParser pp;
assert(pp.parse(header(0x21, 0x41, 12)).first == false);
assert(pp.parse(header(0x21, 0x01, 0)).first == false);
assert(pp.parse(header(0x21, 0x13, 12)).first == false);
}

{
uWS::ProxyParser pp;
assert(pp.parse(header(0x21, 0x11, 0)).first == false);
assert(pp.parse(header(0x21, 0x21, 12)).first == false);
}

{
uWS::ProxyParser pp;
std::string h = header(0x21, 0x00, 0);
assert(pp.parse(h) == std::make_pair(true, 16u));
assert(pp.getSourceAddress().empty());
}

{
uWS::ProxyParser pp;
std::string h = header(0x21, 0x31, 216);
assert(pp.parse(h) == std::make_pair(true, 232u));
assert(pp.getSourceAddress().empty());
}

return 0;
}
Loading