11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This is a regression test for some scenarios in which node would pass
41cb0ef41Sopenharmony_ci// unsanitized user input to a printf-like formatting function when dlopen
51cb0ef41Sopenharmony_ci// fails, potentially crashing the process.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_citmpdir.refresh();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// This error message should not be passed to a printf-like function.
151cb0ef41Sopenharmony_ciassert.throws(() => {
161cb0ef41Sopenharmony_ci  process.dlopen({ exports: {} }, 'foo-%s.node');
171cb0ef41Sopenharmony_ci}, ({ name, code, message }) => {
181cb0ef41Sopenharmony_ci  assert.strictEqual(name, 'Error');
191cb0ef41Sopenharmony_ci  assert.strictEqual(code, 'ERR_DLOPEN_FAILED');
201cb0ef41Sopenharmony_ci  if (!common.isAIX && !common.isIBMi) {
211cb0ef41Sopenharmony_ci    assert.match(message, /foo-%s\.node/);
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci  return true;
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst notBindingDir = 'test/addons/not-a-binding';
271cb0ef41Sopenharmony_ciconst notBindingPath = `${notBindingDir}/build/Release/binding.node`;
281cb0ef41Sopenharmony_ciconst strangeBindingPath = `${tmpdir.path}/binding-%s.node`;
291cb0ef41Sopenharmony_ci// Ensure that the addon directory exists, but skip the remainder of the test if
301cb0ef41Sopenharmony_ci// the addon has not been compiled.
311cb0ef41Sopenharmony_cifs.accessSync(notBindingDir);
321cb0ef41Sopenharmony_citry {
331cb0ef41Sopenharmony_ci  fs.copyFileSync(notBindingPath, strangeBindingPath);
341cb0ef41Sopenharmony_ci} catch (err) {
351cb0ef41Sopenharmony_ci  if (err.code !== 'ENOENT') throw err;
361cb0ef41Sopenharmony_ci  common.skip(`addon not found: ${notBindingPath}`);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// This error message should also not be passed to a printf-like function.
401cb0ef41Sopenharmony_ciassert.throws(() => {
411cb0ef41Sopenharmony_ci  process.dlopen({ exports: {} }, strangeBindingPath);
421cb0ef41Sopenharmony_ci}, {
431cb0ef41Sopenharmony_ci  name: 'Error',
441cb0ef41Sopenharmony_ci  code: 'ERR_DLOPEN_FAILED',
451cb0ef41Sopenharmony_ci  message: /^Module did not self-register: '.*binding-%s\.node'\.$/
461cb0ef41Sopenharmony_ci});
47