Skip to content

Commit 789599a

Browse files
committed
Adding back security workarounds in websocket++.
1 parent fb6d26c commit 789599a

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

Release/libs/websocketpp/websocketpp/frame.hpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ static unsigned int const MAX_EXTENDED_HEADER_LENGTH = 12;
5454
/// Two byte conversion union
5555
union uint16_converter {
5656
uint16_t i;
57-
uint8_t c[2];
57+
std::array<uint8_t, 2> c;
5858
};
5959

6060
/// Four byte conversion union
6161
union uint32_converter {
6262
uint32_t i;
63-
uint8_t c[4];
63+
std::array<uint8_t, 4> c;
6464
};
6565

6666
/// Eight byte conversion union
6767
union uint64_converter {
6868
uint64_t i;
69-
uint8_t c[8];
69+
std::array<uint8_t, 8> c;
7070
};
7171

7272
/// Constants and utility functions related to WebSocket opcodes
@@ -234,28 +234,28 @@ struct basic_header {
234234
/// The variable size component of a WebSocket frame header
235235
struct extended_header {
236236
extended_header() {
237-
std::fill_n(this->bytes,MAX_EXTENDED_HEADER_LENGTH,0x00);
237+
std::fill_n(this->bytes.begin(), MAX_EXTENDED_HEADER_LENGTH, 0x00);
238238
}
239239

240240
extended_header(uint64_t payload_size) {
241-
std::fill_n(this->bytes,MAX_EXTENDED_HEADER_LENGTH,0x00);
241+
std::fill_n(this->bytes.begin(), MAX_EXTENDED_HEADER_LENGTH, 0x00);
242242

243243
copy_payload(payload_size);
244244
}
245245

246246
extended_header(uint64_t payload_size, uint32_t masking_key) {
247-
std::fill_n(this->bytes,MAX_EXTENDED_HEADER_LENGTH,0x00);
247+
std::fill_n(this->bytes.begin(), MAX_EXTENDED_HEADER_LENGTH, 0x00);
248248

249249
// Copy payload size
250250
int offset = copy_payload(payload_size);
251251

252252
// Copy Masking Key
253253
uint32_converter temp32;
254254
temp32.i = masking_key;
255-
std::copy(temp32.c,temp32.c+4,bytes+offset);
255+
std::copy(temp32.c.begin(), temp32.c.end(), bytes.begin() + offset);
256256
}
257257

258-
uint8_t bytes[MAX_EXTENDED_HEADER_LENGTH];
258+
std::array<uint8_t, MAX_EXTENDED_HEADER_LENGTH> bytes;
259259
private:
260260
int copy_payload(uint64_t payload_size) {
261261
int payload_offset = 0;
@@ -268,7 +268,7 @@ struct extended_header {
268268

269269
uint64_converter temp64;
270270
temp64.i = lib::net::_htonll(payload_size);
271-
std::copy(temp64.c+payload_offset,temp64.c+8,bytes);
271+
std::copy(temp64.c.begin() + payload_offset, temp64.c.begin() + 8, bytes.begin());
272272

273273
return 8-payload_offset;
274274
}
@@ -494,7 +494,7 @@ inline std::string prepare_header(const basic_header &h, const
494494
ret.push_back(char(h.b0));
495495
ret.push_back(char(h.b1));
496496
ret.append(
497-
reinterpret_cast<const char*>(e.bytes),
497+
reinterpret_cast<const char*>(&*e.bytes.begin()),
498498
get_header_len(h)-BASIC_HEADER_LENGTH
499499
);
500500

@@ -522,7 +522,8 @@ inline masking_key_type get_masking_key(const basic_header &h, const
522522
temp32.i = 0;
523523
} else {
524524
unsigned int offset = get_masking_key_offset(h);
525-
std::copy(e.bytes+offset,e.bytes+offset+4,temp32.c);
525+
auto ptr = e.bytes.begin() + offset;
526+
std::copy(ptr, ptr + 4, temp32.c.begin());
526527
}
527528

528529
return temp32;
@@ -539,7 +540,7 @@ inline masking_key_type get_masking_key(const basic_header &h, const
539540
*/
540541
inline uint16_t get_extended_size(const extended_header &e) {
541542
uint16_converter temp16;
542-
std::copy(e.bytes,e.bytes+2,temp16.c);
543+
std::copy(e.bytes.begin() , e.bytes.begin() + 2, temp16.c.begin());
543544
return ntohs(temp16.i);
544545
}
545546

@@ -554,7 +555,7 @@ inline uint16_t get_extended_size(const extended_header &e) {
554555
*/
555556
inline uint64_t get_jumbo_size(const extended_header &e) {
556557
uint64_converter temp64;
557-
std::copy(e.bytes,e.bytes+8,temp64.c);
558+
std::copy(e.bytes.begin(), e.bytes.begin() + 8, temp64.c.begin());
558559
return lib::net::_ntohll(temp64.i);
559560
}
560561

Release/libs/websocketpp/websocketpp/processors/hybi00.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class hybi00 : public processor<config> {
9898
lib::error_code process_handshake(request_type const & req,
9999
std::string const & subprotocol, response_type & res) const
100100
{
101-
char key_final[16];
101+
std::array<char, 16> key_final;
102102

103103
// copy key1 into final key
104104
decode_client_key(req.get_header("Sec-WebSocket-Key1"), &key_final[0]);
@@ -112,13 +112,13 @@ class hybi00 : public processor<config> {
112112
// TODO: decide if it is best to silently fail here or produce some sort
113113
// of warning or exception.
114114
std::string const & key3 = req.get_header("Sec-WebSocket-Key3");
115-
std::copy(key3.c_str(),
116-
key3.c_str()+(std::min)(static_cast<size_t>(8), key3.size()),
117-
&key_final[8]);
115+
std::copy(key3.begin(),
116+
key3.begin() + std::min(size_t(8), key3.size()),
117+
key_final.begin() + 8);
118118

119119
res.append_header(
120120
"Sec-WebSocket-Key3",
121-
md5::md5_hash_string(std::string(key_final,16))
121+
md5::md5_hash_string(std::string(key_final.begin(), key_final.end()))
122122
);
123123

124124
res.append_header("Upgrade","WebSocket");
@@ -415,9 +415,11 @@ class hybi00 : public processor<config> {
415415
num = static_cast<uint32_t>(strtoul(digits.c_str(), NULL, 10));
416416
if (spaces > 0 && num > 0) {
417417
num = htonl(num/spaces);
418-
std::copy(reinterpret_cast<char*>(&num),
419-
reinterpret_cast<char*>(&num)+4,
420-
result);
418+
#ifdef _MS_WINDOWS
419+
memcpy_s(result, 4, reinterpret_cast<char*>(&num), 4);
420+
#else
421+
memcpy(result, reinterpret_cast<char*>(&num), 4);
422+
#endif
421423
} else {
422424
std::fill(result,result+4,0);
423425
}

Release/libs/websocketpp/websocketpp/processors/hybi13.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ class hybi13 : public processor<config> {
214214

215215
// Generate handshake key
216216
frame::uint32_converter conv;
217-
unsigned char raw_key[16];
217+
std::array<unsigned char, 16> raw_key;
218218

219219
for (int i = 0; i < 4; i++) {
220220
conv.i = m_rng();
221-
std::copy(conv.c,conv.c+4,&raw_key[i*4]);
221+
std::copy(conv.c.begin(), conv.c.begin() + 4, raw_key.begin() + i * 4);
222222
}
223223

224-
req.replace_header("Sec-WebSocket-Key",base64_encode(raw_key, 16));
224+
req.replace_header("Sec-WebSocket-Key",base64_encode(&raw_key[0], 16));
225225

226226
return lib::error_code();
227227
}
@@ -461,9 +461,9 @@ class hybi13 : public processor<config> {
461461
m_basic_header.b0 = 0x00;
462462
m_basic_header.b1 = 0x00;
463463

464-
std::fill_n(
465-
m_extended_header.bytes,
466-
frame::MAX_EXTENDED_HEADER_LENGTH,
464+
std::fill(
465+
m_extended_header.bytes.begin(),
466+
m_extended_header.bytes.end(),
467467
0x00
468468
);
469469
}
@@ -686,7 +686,7 @@ class hybi13 : public processor<config> {
686686
size_t copy_extended_header_bytes(uint8_t const * buf, size_t len) {
687687
size_t bytes_to_read = (std::min)(m_bytes_needed,len);
688688

689-
std::copy(buf,buf+bytes_to_read,m_extended_header.bytes+m_cursor);
689+
std::copy(buf, buf + bytes_to_read, m_extended_header.bytes.begin() + m_cursor);
690690
m_cursor += bytes_to_read;
691691
m_bytes_needed -= bytes_to_read;
692692

0 commit comments

Comments
 (0)