-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: fix multiple format string bugs
The THROW_ERR_* functions interpret the first argument as a printf-like format string, which is problematic when it contains unsanitized user input. This typically happens when a printf-like function is used to produce the error message, which is then passed to a THROW_ERR_* function, which again interprets the error message as a format string. Fix such occurrences by properly formatting error messages using static format strings only, and in a single step. PR-URL: #44314 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
- Loading branch information
Showing
5 changed files
with
74 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
// This is a regression test for some scenarios in which node would pass | ||
// unsanitized user input to a printf-like formatting function when dlopen | ||
// fails, potentially crashing the process. | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
|
||
// This error message should not be passed to a printf-like function. | ||
assert.throws(() => { | ||
process.dlopen({ exports: {} }, 'foo-%s.node'); | ||
}, ({ name, code, message }) => { | ||
assert.strictEqual(name, 'Error'); | ||
assert.strictEqual(code, 'ERR_DLOPEN_FAILED'); | ||
if (!common.isAIX) { | ||
assert.match(message, /foo-%s\.node/); | ||
} | ||
return true; | ||
}); | ||
|
||
const notBindingDir = 'test/addons/not-a-binding'; | ||
const notBindingPath = `${notBindingDir}/build/Release/binding.node`; | ||
const strangeBindingPath = `${tmpdir.path}/binding-%s.node`; | ||
// Ensure that the addon directory exists, but skip the remainder of the test if | ||
// the addon has not been compiled. | ||
fs.accessSync(notBindingDir); | ||
try { | ||
fs.copyFileSync(notBindingPath, strangeBindingPath); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') throw err; | ||
common.skip(`addon not found: ${notBindingPath}`); | ||
} | ||
|
||
// This error message should also not be passed to a printf-like function. | ||
assert.throws(() => { | ||
process.dlopen({ exports: {} }, strangeBindingPath); | ||
}, { | ||
name: 'Error', | ||
code: 'ERR_DLOPEN_FAILED', | ||
message: /^Module did not self-register: '.*binding-%s\.node'\.$/ | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters