| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 566d80f commit f0d841f
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -501,8 +501,8 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) { | |||
| 501 | 501 | max_version = TLS1_2_VERSION; | |
| 502 | 502 | method = TLS_client_method(); | |
| 503 | 503 | } else { | |
| 504 | - const std::string msg("Unknown method: "); | ||
| 505 | - THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, (msg + * sslmethod).c_str()); | ||
| 504 | + THROW_ERR_TLS_INVALID_PROTOCOL_METHOD( | ||
| 505 | + env, "Unknown method: %s", *sslmethod); | ||
| 506 | 506 | return; | |
| 507 | 507 | } | |
| 508 | 508 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,11 +68,9 @@ Maybe<bool> SecretKeyGenTraits::AdditionalConfig( | |||
| 68 | 68 | params->length = static_cast<size_t>( | |
| 69 | 69 | std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT)); | |
| 70 | 70 | if (params->length > INT_MAX) { | |
| 71 | - const std::string msg{ | ||
| 72 | - SPrintF("length must be less than or equal to %s bits", | ||
| 73 | - static_cast<uint64_t>(INT_MAX) * CHAR_BIT) | ||
| 74 | - }; | ||
| 75 | - THROW_ERR_OUT_OF_RANGE(env, msg.c_str()); | ||
| 71 | + THROW_ERR_OUT_OF_RANGE(env, | ||
| 72 | + "length must be less than or equal to %u bits", | ||
| 73 | + static_cast<uint64_t>(INT_MAX) * CHAR_BIT); | ||
| 76 | 74 | return Nothing<bool>(); | |
| 77 | 75 | } | |
| 78 | 76 | *offset += 1; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -465,7 +465,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) { | |||
| 465 | 465 | // Windows needs to add the filename into the error message | |
| 466 | 466 | errmsg += *filename; | |
| 467 | 467 | #endif // _WIN32 | |
| 468 | - THROW_ERR_DLOPEN_FAILED(env, errmsg.c_str()); | ||
| 468 | + THROW_ERR_DLOPEN_FAILED(env, "%s", errmsg.c_str()); | ||
| 469 | 469 | return false; | |
| 470 | 470 | } | |
| 471 | 471 | ||
@@ -490,12 +490,8 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) { | |||
| 490 | 490 | mp = dlib->GetSavedModuleFromGlobalHandleMap(); | |
| 491 | 491 | if (mp == nullptr || mp->nm_context_register_func == nullptr) { | |
| 492 | 492 | dlib->Close(); | |
| 493 | - char errmsg[1024]; | ||
| 494 | - snprintf(errmsg, | ||
| 495 | - sizeof(errmsg), | ||
| 496 | - "Module did not self-register: '%s'.", | ||
| 497 | - *filename); | ||
| 498 | - THROW_ERR_DLOPEN_FAILED(env, errmsg); | ||
| 493 | + THROW_ERR_DLOPEN_FAILED( | ||
| 494 | + env, "Module did not self-register: '%s'.", *filename); | ||
| 499 | 495 | return false; | |
| 500 | 496 | } | |
| 501 | 497 | } | |
@@ -510,23 +506,22 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) { | |||
| 510 | 506 | callback(exports, module, context); | |
| 511 | 507 | return true; | |
| 512 | 508 | } | |
| 513 | - char errmsg[1024]; | ||
| 514 | - snprintf(errmsg, | ||
| 515 | - sizeof(errmsg), | ||
| 516 | - "The module '%s'" | ||
| 517 | - "\nwas compiled against a different Node.js version using" | ||
| 518 | - "\nNODE_MODULE_VERSION %d. This version of Node.js requires" | ||
| 519 | - "\nNODE_MODULE_VERSION %d. Please try re-compiling or " | ||
| 520 | - "re-installing\nthe module (for instance, using `npm rebuild` " | ||
| 521 | - "or `npm install`).", | ||
| 522 | - *filename, | ||
| 523 | - mp->nm_version, | ||
| 524 | - NODE_MODULE_VERSION); | ||
| 525 | 509 | ||
| 510 | + const int actual_nm_version = mp->nm_version; | ||
| 526 | 511 | // NOTE: `mp` is allocated inside of the shared library's memory, calling | |
| 527 | 512 | // `dlclose` will deallocate it | |
| 528 | 513 | dlib->Close(); | |
| 529 | - THROW_ERR_DLOPEN_FAILED(env, errmsg); | ||
| 514 | + THROW_ERR_DLOPEN_FAILED( | ||
| 515 | + env, | ||
| 516 | + "The module '%s'" | ||
| 517 | + "\nwas compiled against a different Node.js version using" | ||
| 518 | + "\nNODE_MODULE_VERSION %d. This version of Node.js requires" | ||
| 519 | + "\nNODE_MODULE_VERSION %d. Please try re-compiling or " | ||
| 520 | + "re-installing\nthe module (for instance, using `npm rebuild` " | ||
| 521 | + "or `npm install`).", | ||
| 522 | + *filename, | ||
| 523 | + actual_nm_version, | ||
| 524 | + NODE_MODULE_VERSION); | ||
| 530 | 525 | return false; | |
| 531 | 526 | } | |
| 532 | 527 | CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0); | |
@@ -607,9 +602,7 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { | |||
| 607 | 602 | env->isolate())) | |
| 608 | 603 | .FromJust()); | |
| 609 | 604 | } else { | |
| 610 | - char errmsg[1024]; | ||
| 611 | - snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v); | ||
| 612 | - return THROW_ERR_INVALID_MODULE(env, errmsg); | ||
| 605 | + return THROW_ERR_INVALID_MODULE(env, "No such module: %s", *module_v); | ||
| 613 | 606 | } | |
| 614 | 607 | ||
| 615 | 608 | args.GetReturnValue().Set(exports); | |
@@ -639,12 +632,8 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) { | |||
| 639 | 632 | mod = FindModule(modlist_linked, name, NM_F_LINKED); | |
| 640 | 633 | ||
| 641 | 634 | if (mod == nullptr) { | |
| 642 | - char errmsg[1024]; | ||
| 643 | - snprintf(errmsg, | ||
| 644 | - sizeof(errmsg), | ||
| 645 | - "No such module was linked: %s", | ||
| 646 | - *module_name_v); | ||
| 647 | - return THROW_ERR_INVALID_MODULE(env, errmsg); | ||
| 635 | + return THROW_ERR_INVALID_MODULE( | ||
| 636 | + env, "No such module was linked: %s", *module_name_v); | ||
| 648 | 637 | } | |
| 649 | 638 | ||
| 650 | 639 | Local<Object> module = Object::New(env->isolate()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // This is a regression test for some scenarios in which node would pass | ||
| 4 | + // unsanitized user input to a printf-like formatting function when dlopen | ||
| 5 | + // fails, potentially crashing the process. | ||
| 6 | + | ||
| 7 | + const common = require('../common'); | ||
| 8 | + const tmpdir = require('../common/tmpdir'); | ||
| 9 | + tmpdir.refresh(); | ||
| 10 | + | ||
| 11 | + const assert = require('assert'); | ||
| 12 | + const fs = require('fs'); | ||
| 13 | + | ||
| 14 | + // This error message should not be passed to a printf-like function. | ||
| 15 | + assert.throws(() => { | ||
| 16 | + process.dlopen({ exports: {} }, 'foo-%s.node'); | ||
| 17 | + }, ({ name, code, message }) => { | ||
| 18 | + assert.strictEqual(name, 'Error'); | ||
| 19 | + assert.strictEqual(code, 'ERR_DLOPEN_FAILED'); | ||
| 20 | + if (!common.isAIX) { | ||
| 21 | + assert.match(message, /foo-%s\.node/); | ||
| 22 | + } | ||
| 23 | + return true; | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + const notBindingDir = 'test/addons/not-a-binding'; | ||
| 27 | + const notBindingPath = `${notBindingDir}/build/Release/binding.node`; | ||
| 28 | + const strangeBindingPath = `${tmpdir.path}/binding-%s.node`; | ||
| 29 | + // Ensure that the addon directory exists, but skip the remainder of the test if | ||
| 30 | + // the addon has not been compiled. | ||
| 31 | + fs.accessSync(notBindingDir); | ||
| 32 | + try { | ||
| 33 | + fs.copyFileSync(notBindingPath, strangeBindingPath); | ||
| 34 | + } catch (err) { | ||
| 35 | + if (err.code !== 'ENOENT') throw err; | ||
| 36 | + common.skip(`addon not found: ${notBindingPath}`); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + // This error message should also not be passed to a printf-like function. | ||
| 40 | + assert.throws(() => { | ||
| 41 | + process.dlopen({ exports: {} }, strangeBindingPath); | ||
| 42 | + }, { | ||
| 43 | + name: 'Error', | ||
| 44 | + code: 'ERR_DLOPEN_FAILED', | ||
| 45 | + message: /^Module did not self-register: '.*binding-%s\.node'\.$/ | ||
| 46 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,6 +97,11 @@ test(U, U, 'hokey-pokey', U, U, U, | |||
| 97 | 97 | test(U, U, U, U, U, 'hokey-pokey', | |
| 98 | 98 | U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); | |
| 99 | 99 | ||
| 100 | + // Regression test: this should not crash because node should not pass the error | ||
| 101 | + // message (including unsanitized user input) to a printf-like function. | ||
| 102 | + test(U, U, U, U, U, '%s_method', | ||
| 103 | + U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); | ||
| 104 | + | ||
| 100 | 105 | // Cannot use secureProtocol and min/max versions simultaneously. | |
| 101 | 106 | test(U, U, U, U, 'TLSv1.2', 'TLS1_2_method', | |
| 102 | 107 | U, U, 'ERR_TLS_PROTOCOL_VERSION_CONFLICT'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments