Skip to content

src: make Sec-WebSocket-Key check case-insensitive #7248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2016
Merged
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
9 changes: 6 additions & 3 deletions src/inspector_socket.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "inspector_socket.h"
#include "util.h"
#include "util-inl.h"

#define NODE_WANT_INTERNALS 1
#include "base64.h"
Expand Down Expand Up @@ -445,9 +447,10 @@ static int header_value_cb(http_parser* parser, const char* at, size_t length) {
struct http_parsing_state_s* state = (struct http_parsing_state_s*)
(reinterpret_cast<inspector_socket_t*>(parser->data))->http_parsing_state;
state->parsing_value = true;
if (state->current_header && strncmp(state->current_header,
SEC_WEBSOCKET_KEY_HEADER,
sizeof(SEC_WEBSOCKET_KEY_HEADER)) == 0) {
if (state->current_header &&
node::StringEqualNoCaseN(state->current_header,
SEC_WEBSOCKET_KEY_HEADER,
sizeof(SEC_WEBSOCKET_KEY_HEADER))) {
append(&state->ws_key, at, length);
}
return 0;
Expand Down
10 changes: 10 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ bool StringEqualNoCase(const char* a, const char* b) {
return false;
}

bool StringEqualNoCaseN(const char* a, const char* b, size_t length) {
for (size_t i = 0; i < length; i++) {
if (ToLower(a[i]) != ToLower(b[i]))
return false;
if (a[i] == '\0')
return true;
}
return true;
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ inline char ToLower(char c);
// strcasecmp() is locale-sensitive. Use StringEqualNoCase() instead.
inline bool StringEqualNoCase(const char* a, const char* b);

// strncasecmp() is locale-sensitive. Use StringEqualNoCaseN() instead.
inline bool StringEqualNoCaseN(const char* a, const char* b, size_t length);

// Allocates an array of member type T. For up to kStackStorageSize items,
// the stack is used, otherwise malloc().
template <typename T, size_t kStackStorageSize = 1024>
Expand Down
15 changes: 15 additions & 0 deletions test/cctest/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ TEST(UtilTest, StringEqualNoCase) {
EXPECT_FALSE(StringEqualNoCase("equals", "equal"));
}

TEST(UtilTest, StringEqualNoCaseN) {
using node::StringEqualNoCaseN;
EXPECT_FALSE(StringEqualNoCaseN("a", "b", strlen("a")));
EXPECT_TRUE(StringEqualNoCaseN("", "", strlen("")));
EXPECT_TRUE(StringEqualNoCaseN("equal", "equal", strlen("equal")));
EXPECT_TRUE(StringEqualNoCaseN("equal", "EQUAL", strlen("equal")));
EXPECT_TRUE(StringEqualNoCaseN("EQUAL", "EQUAL", strlen("equal")));
EXPECT_TRUE(StringEqualNoCaseN("equal", "equals", strlen("equal")));
EXPECT_FALSE(StringEqualNoCaseN("equal", "equals", strlen("equals")));
EXPECT_TRUE(StringEqualNoCaseN("equals", "equal", strlen("equal")));
EXPECT_FALSE(StringEqualNoCaseN("equals", "equal", strlen("equals")));
EXPECT_TRUE(StringEqualNoCaseN("abc\0abc", "abc\0efg", strlen("abcdefgh")));
EXPECT_FALSE(StringEqualNoCaseN("abc\0abc", "abcd\0efg", strlen("abcdefgh")));
}

TEST(UtilTest, ToLower) {
using node::ToLower;
EXPECT_EQ('0', ToLower('0'));
Expand Down