Skip to content

Commit 535c777

Browse files
committed
src: replace usage of String::Utf8Value
v8::String::Utf8Value previously could allow invalid surrogates when interpreting values.
1 parent 0da4c67 commit 535c777

10 files changed

Lines changed: 158 additions & 68 deletions

src/cares_wrap.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "tree.h"
3232
#include "uv.h"
3333

34+
#include "util.h"
35+
3436
#if defined(__OpenBSD__) || defined(__MINGW32__) || defined(_MSC_VER)
3537
# include <nameser.h>
3638
#else
@@ -739,7 +741,7 @@ static Handle<Value> Query(const Arguments& args) {
739741
// object reference, causing wrap->GetObject() to return undefined.
740742
Local<Object> object = Local<Object>::New(wrap->GetObject());
741743

742-
String::Utf8Value name(args[0]);
744+
node::Utf8Value name(args[0]);
743745

744746
int r = wrap->Send(*name);
745747
if (r) {
@@ -768,7 +770,7 @@ static Handle<Value> QueryWithFamily(const Arguments& args) {
768770
// object reference, causing wrap->GetObject() to return undefined.
769771
Local<Object> object = Local<Object>::New(wrap->GetObject());
770772

771-
String::Utf8Value name(args[0]);
773+
node::Utf8Value name(args[0]);
772774
int family = args[1]->Int32Value();
773775

774776
int r = wrap->Send(*name, family);
@@ -898,7 +900,7 @@ static Handle<Value> IsIP(const Arguments& args) {
898900
static Handle<Value> GetAddrInfo(const Arguments& args) {
899901
HandleScope scope;
900902

901-
String::Utf8Value hostname(args[0]);
903+
node::Utf8Value hostname(args[0]);
902904

903905
int fam = AF_UNSPEC;
904906
if (args[1]->IsInt32()) {

src/fs_event_wrap.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "node.h"
2323
#include "handle_wrap.h"
24+
#include "util.h"
2425

2526
#include <stdlib.h>
2627

@@ -97,7 +98,7 @@ Handle<Value> FSEventWrap::Start(const Arguments& args) {
9798
return ThrowException(Exception::TypeError(String::New("Bad arguments")));
9899
}
99100

100-
String::Utf8Value path(args[0]);
101+
node::Utf8Value path(args[0]);
101102

102103
int r = uv_fs_event_init(uv_default_loop(), &wrap->handle_, *path, OnEvent, 0);
103104
if (r == 0) {

src/node.cc

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ typedef int mode_t;
8282
#include "node_script.h"
8383
#include "v8_typed_array.h"
8484

85+
#include "util.h"
86+
8587
using namespace v8;
8688

8789
# ifdef __APPLE__
@@ -1102,7 +1104,7 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
11021104

11031105
if (!encoding_v->IsString()) return _default;
11041106

1105-
String::Utf8Value encoding(encoding_v);
1107+
node::Utf8Value encoding(encoding_v);
11061108

11071109
if (strcasecmp(*encoding, "utf8") == 0) {
11081110
return UTF8;
@@ -1202,12 +1204,12 @@ void DisplayExceptionLine (TryCatch &try_catch) {
12021204

12031205
if (!message.IsEmpty()) {
12041206
// Print (filename):(line number): (message).
1205-
String::Utf8Value filename(message->GetScriptResourceName());
1207+
node::Utf8Value filename(message->GetScriptResourceName());
12061208
const char* filename_string = *filename;
12071209
int linenum = message->GetLineNumber();
12081210
fprintf(stderr, "%s:%i\n", filename_string, linenum);
12091211
// Print line of source code.
1210-
String::Utf8Value sourceline(message->GetSourceLine());
1212+
node::Utf8Value sourceline(message->GetSourceLine());
12111213
const char* sourceline_string = *sourceline;
12121214

12131215
// Because of how node modules work, all scripts are wrapped with a
@@ -1254,7 +1256,7 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
12541256

12551257
if (show_line) DisplayExceptionLine(try_catch);
12561258

1257-
String::Utf8Value trace(try_catch.StackTrace());
1259+
node::Utf8Value trace(try_catch.StackTrace());
12581260

12591261
// range errors have a trace member set to undefined
12601262
if (trace.length() > 0 && !try_catch.StackTrace()->IsUndefined()) {
@@ -1269,11 +1271,11 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
12691271
!(er->ToObject()->Get(String::New("name"))->IsUndefined());
12701272

12711273
if (isErrorObject) {
1272-
String::Utf8Value name(er->ToObject()->Get(String::New("name")));
1274+
node::Utf8Value name(er->ToObject()->Get(String::New("name")));
12731275
fprintf(stderr, "%s: ", *name);
12741276
}
12751277

1276-
String::Utf8Value msg(!isErrorObject ? er
1278+
node::Utf8Value msg(!isErrorObject ? er
12771279
: er->ToObject()->Get(String::New("message")));
12781280
fprintf(stderr, "%s\n", *msg);
12791281
}
@@ -1355,7 +1357,7 @@ static Handle<Value> Chdir(const Arguments& args) {
13551357
return ThrowException(Exception::Error(String::New("Bad argument.")));
13561358
}
13571359

1358-
String::Utf8Value path(args[0]);
1360+
node::Utf8Value path(args[0]);
13591361

13601362
uv_err_t r = uv_chdir(*path);
13611363

@@ -1406,7 +1408,7 @@ static Handle<Value> Umask(const Arguments& args) {
14061408
oct = args[0]->Uint32Value();
14071409
} else {
14081410
oct = 0;
1409-
String::Utf8Value str(args[0]);
1411+
node::Utf8Value str(args[0]);
14101412

14111413
// Parse the octal string.
14121414
for (int i = 0; i < str.length(); i++) {
@@ -1512,7 +1514,7 @@ static uid_t uid_by_name(Handle<Value> value) {
15121514
if (value->IsUint32()) {
15131515
return static_cast<uid_t>(value->Uint32Value());
15141516
} else {
1515-
String::Utf8Value name(value);
1517+
node::Utf8Value name(value);
15161518
return uid_by_name(*name);
15171519
}
15181520
}
@@ -1522,7 +1524,7 @@ static gid_t gid_by_name(Handle<Value> value) {
15221524
if (value->IsUint32()) {
15231525
return static_cast<gid_t>(value->Uint32Value());
15241526
} else {
1525-
String::Utf8Value name(value);
1527+
node::Utf8Value name(value);
15261528
return gid_by_name(*name);
15271529
}
15281530
}
@@ -1665,7 +1667,7 @@ static Handle<Value> InitGroups(const Arguments& args) {
16651667
return ThrowTypeError("argument 2 must be a number or a string");
16661668
}
16671669

1668-
String::Utf8Value arg0(args[0]);
1670+
node::Utf8Value arg0(args[0]);
16691671
gid_t extra_group;
16701672
bool must_free;
16711673
char* user;
@@ -1826,7 +1828,7 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
18261828
}
18271829

18281830
Local<Object> module = args[0]->ToObject(); // Cast
1829-
String::Utf8Value filename(args[1]); // Cast
1831+
node::Utf8Value filename(args[1]); // Cast
18301832

18311833
if (exports_symbol.IsEmpty()) {
18321834
exports_symbol = NODE_PSYMBOL("exports");
@@ -1842,7 +1844,7 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
18421844
return ThrowException(Exception::Error(errmsg));
18431845
}
18441846

1845-
String::Utf8Value path(args[1]);
1847+
node::Utf8Value path(args[1]);
18461848
base = *path;
18471849

18481850
/* Find the shared library filename within the full path. */
@@ -1961,7 +1963,7 @@ static Handle<Value> Binding(const Arguments& args) {
19611963
HandleScope scope;
19621964

19631965
Local<String> module = args[0]->ToString();
1964-
String::Utf8Value module_v(module);
1966+
node::Utf8Value module_v(module);
19651967
node_module_struct* modp;
19661968

19671969
if (binding_cache.IsEmpty()) {
@@ -2020,7 +2022,7 @@ static void ProcessTitleSetter(Local<String> property,
20202022
Local<Value> value,
20212023
const AccessorInfo& info) {
20222024
HandleScope scope;
2023-
String::Utf8Value title(value);
2025+
node::Utf8Value title(value);
20242026
// TODO: protect with a lock
20252027
uv_set_process_title(*title);
20262028
}
@@ -2030,7 +2032,7 @@ static Handle<Value> EnvGetter(Local<String> property,
20302032
const AccessorInfo& info) {
20312033
HandleScope scope;
20322034
#ifdef __POSIX__
2033-
String::Utf8Value key(property);
2035+
node::Utf8Value key(property);
20342036
const char* val = getenv(*key);
20352037
if (val) {
20362038
return scope.Close(String::New(val));
@@ -2059,8 +2061,8 @@ static Handle<Value> EnvSetter(Local<String> property,
20592061
const AccessorInfo& info) {
20602062
HandleScope scope;
20612063
#ifdef __POSIX__
2062-
String::Utf8Value key(property);
2063-
String::Utf8Value val(value);
2064+
node::Utf8Value key(property);
2065+
node::Utf8Value val(value);
20642066
setenv(*key, *val, 1);
20652067
#else // _WIN32
20662068
String::Value key(property);
@@ -2080,7 +2082,7 @@ static Handle<Integer> EnvQuery(Local<String> property,
20802082
const AccessorInfo& info) {
20812083
HandleScope scope;
20822084
#ifdef __POSIX__
2083-
String::Utf8Value key(property);
2085+
node::Utf8Value key(property);
20842086
if (getenv(*key)) {
20852087
return scope.Close(Integer::New(0));
20862088
}
@@ -2108,7 +2110,7 @@ static Handle<Boolean> EnvDeleter(Local<String> property,
21082110
const AccessorInfo& info) {
21092111
HandleScope scope;
21102112
#ifdef __POSIX__
2111-
String::Utf8Value key(property);
2113+
node::Utf8Value key(property);
21122114
if (!getenv(*key)) return False();
21132115
unsetenv(*key); // can't check return value, it's void on some platforms
21142116
return True();

src/node_crypto.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "node.h"
2727
#include "node_buffer.h"
2828
#include "string_bytes.h"
29+
#include "util.h"
2930

3031
#include <string.h>
3132
#ifdef _MSC_VER
@@ -241,7 +242,7 @@ Handle<Value> SecureContext::Init(const Arguments& args) {
241242
OPENSSL_CONST SSL_METHOD *method = SSLv23_method();
242243

243244
if (args.Length() == 1 && args[0]->IsString()) {
244-
String::Utf8Value sslmethod(args[0]);
245+
node::Utf8Value sslmethod(args[0]);
245246

246247
if (strcmp(*sslmethod, "SSLv2_method") == 0) {
247248
#ifndef OPENSSL_NO_SSL2
@@ -361,7 +362,7 @@ static BIO* LoadBIO (Handle<Value> v) {
361362
int r = -1;
362363

363364
if (v->IsString()) {
364-
String::Utf8Value s(v);
365+
node::Utf8Value s(v);
365366
r = BIO_write(bio, *s, s.length());
366367
} else if (Buffer::HasInstance(v)) {
367368
char* buffer_data = Buffer::Data(v);
@@ -413,7 +414,7 @@ Handle<Value> SecureContext::SetKey(const Arguments& args) {
413414
BIO *bio = LoadBIO(args[0]);
414415
if (!bio) return False();
415416

416-
String::Utf8Value passphrase(args[1]);
417+
node::Utf8Value passphrase(args[1]);
417418

418419
EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, NULL, NULL,
419420
len == 1 ? NULL : *passphrase);
@@ -643,7 +644,7 @@ Handle<Value> SecureContext::SetCiphers(const Arguments& args) {
643644
return ThrowException(Exception::TypeError(String::New("Bad parameter")));
644645
}
645646

646-
String::Utf8Value ciphers(args[0]);
647+
node::Utf8Value ciphers(args[0]);
647648
SSL_CTX_set_cipher_list(sc->ctx_, *ciphers);
648649

649650
return True();
@@ -672,7 +673,7 @@ Handle<Value> SecureContext::SetSessionIdContext(const Arguments& args) {
672673
return ThrowException(Exception::TypeError(String::New("Bad parameter")));
673674
}
674675

675-
String::Utf8Value sessionIdContext(args[0]);
676+
node::Utf8Value sessionIdContext(args[0]);
676677
const unsigned char* sid_ctx = (const unsigned char*) *sessionIdContext;
677678
unsigned int sid_ctx_len = sessionIdContext.length();
678679

@@ -1280,7 +1281,7 @@ Handle<Value> Connection::New(const Arguments& args) {
12801281
if (is_server) {
12811282
SSL_CTX_set_tlsext_servername_callback(sc->ctx_, SelectSNIContextCallback_);
12821283
} else {
1283-
String::Utf8Value servername(args[2]);
1284+
node::Utf8Value servername(args[2]);
12841285
SSL_set_tlsext_host_name(p->ssl_, *servername);
12851286
}
12861287
#endif
@@ -2233,7 +2234,7 @@ class Cipher : public ObjectWrap {
22332234
ssize_t key_written = DecodeWrite(key_buf, key_buf_len, args[1], BINARY);
22342235
assert(key_written == key_buf_len);
22352236

2236-
String::Utf8Value cipherType(args[0]);
2237+
node::Utf8Value cipherType(args[0]);
22372238

22382239
bool r = cipher->CipherInit(*cipherType, key_buf, key_buf_len);
22392240

@@ -2284,7 +2285,7 @@ class Cipher : public ObjectWrap {
22842285
ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
22852286
assert(iv_written == iv_len);
22862287

2287-
String::Utf8Value cipherType(args[0]);
2288+
node::Utf8Value cipherType(args[0]);
22882289

22892290
bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
22902291

@@ -2543,7 +2544,7 @@ class Decipher : public ObjectWrap {
25432544
ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
25442545
assert(key_written == key_len);
25452546

2546-
String::Utf8Value cipherType(args[0]);
2547+
node::Utf8Value cipherType(args[0]);
25472548

25482549
bool r = cipher->DecipherInit(*cipherType, key_buf,key_len);
25492550

@@ -2594,7 +2595,7 @@ class Decipher : public ObjectWrap {
25942595
ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
25952596
assert(iv_written == iv_len);
25962597

2597-
String::Utf8Value cipherType(args[0]);
2598+
node::Utf8Value cipherType(args[0]);
25982599

25992600
bool r = cipher->DecipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
26002601

@@ -2775,7 +2776,7 @@ class Hmac : public ObjectWrap {
27752776
return ThrowException(exception);
27762777
}
27772778

2778-
String::Utf8Value hashType(args[0]);
2779+
node::Utf8Value hashType(args[0]);
27792780

27802781
bool r;
27812782

@@ -2920,7 +2921,7 @@ class Hash : public ObjectWrap {
29202921
"Must give hashtype string as argument")));
29212922
}
29222923

2923-
String::Utf8Value hashType(args[0]);
2924+
node::Utf8Value hashType(args[0]);
29242925

29252926
Hash *hash = new Hash();
29262927
if (!hash->HashInit(*hashType)) {
@@ -3094,7 +3095,7 @@ class Sign : public ObjectWrap {
30943095
"Must give signtype string as argument")));
30953096
}
30963097

3097-
String::Utf8Value signType(args[0]);
3098+
node::Utf8Value signType(args[0]);
30983099

30993100
bool r = sign->SignInit(*signType);
31003101

@@ -3327,7 +3328,7 @@ class Verify : public ObjectWrap {
33273328
"Must give verifytype string as argument")));
33283329
}
33293330

3330-
String::Utf8Value verifyType(args[0]);
3331+
node::Utf8Value verifyType(args[0]);
33313332

33323333
bool r = verify->VerifyInit(*verifyType);
33333334

@@ -3510,7 +3511,7 @@ class DiffieHellman : public ObjectWrap {
35103511
String::New("No group name given")));
35113512
}
35123513

3513-
String::Utf8Value group_name(args[0]);
3514+
node::Utf8Value group_name(args[0]);
35143515

35153516
modp_group* it = modp_groups;
35163517

src/node_dtrace.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222

23+
#include "util.h"
24+
2325
#ifdef HAVE_DTRACE
2426
#include "node_dtrace.h"
2527
#include <string.h>
@@ -66,7 +68,7 @@ using namespace v8;
6668
return (ThrowException(Exception::Error(String::New("expected " \
6769
"object for " #obj " to contain string member " #member)))); \
6870
} \
69-
String::Utf8Value _##member(obj->Get(String::New(#member))); \
71+
node::Utf8Value _##member(obj->Get(String::New(#member))); \
7072
if ((*(const char **)valp = *_##member) == NULL) \
7173
*(const char **)valp = "<unknown>";
7274

@@ -241,7 +243,7 @@ Handle<Value> DTRACE_HTTP_SERVER_REQUEST(const Arguments& args) {
241243
"object for request to contain string member headers"))));
242244

243245
Local<Value> strfwdfor = headers->Get(String::New("x-forwarded-for"));
244-
String::Utf8Value fwdfor(strfwdfor);
246+
node::Utf8Value fwdfor(strfwdfor);
245247

246248
if (!strfwdfor->IsString() || (req.forwardedFor = *fwdfor) == NULL)
247249
req.forwardedFor = const_cast<char*>("");

0 commit comments

Comments
 (0)