diff --git a/examples/shared/datafile-server.js b/examples/shared/datafile-server.js index 58ffced11..5a9215db8 100755 --- a/examples/shared/datafile-server.js +++ b/examples/shared/datafile-server.js @@ -19,6 +19,8 @@ /** * Simple HTTP server to serve the datafile for testing polling project config manager * Runs at http://localhost:PORT + * + * It listens on the loopback interface only, so it can't be reached from other machines on the network. */ const http = require('http'); @@ -26,8 +28,13 @@ const fs = require('fs'); const path = require('path'); const PORT = 8910; +// Loopback only. Without a host, Node listens on every network interface. +const HOST = 'localhost'; const datafilePath = path.join(__dirname, 'datafile.json'); +// Plain HTTP is intended here: this is a local test server for the examples. It only listens on localhost +// (see HOST) and serves the sample datafile.json, so nothing sensitive crosses a network. +// nosemgrep: problem-based-packs.insecure-transport.js-node.using-http-server.using-http-server const server = http.createServer((req, res) => { console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`); @@ -52,9 +59,9 @@ const server = http.createServer((req, res) => { }); }); -server.listen(PORT, () => { +server.listen(PORT, HOST, () => { console.log(`\n=== Datafile Server Started ===`); - console.log(`Server running at http://localhost:${PORT}`); + console.log(`Server running at http://${HOST}:${PORT}`); console.log(`Serving datafile from: ${datafilePath}`); console.log(`\nPress Ctrl+C to stop the server\n`); }); diff --git a/package-lock.json b/package-lock.json index f67e77129..396d67833 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,6 @@ "@typescript-eslint/eslint-plugin": "^5.33.0", "@typescript-eslint/parser": "^5.33.0", "chai": "^4.2.0", - "coveralls-next": "^5.0.0", "eslint": "^8.21.0", "eslint-config-prettier": "^6.10.0", "eslint-plugin-local-rules": "^3.0.2", @@ -33,7 +32,6 @@ "happy-dom": "^20.0.11", "minimatch": "^9.0.5", "mocha": "^10.2.0", - "mocha-lcov-reporter": "^1.3.0", "nise": "^6.1.1", "nock": "11.9.1", "nyc": "^15.0.1", @@ -4218,25 +4216,6 @@ "dev": true, "license": "MIT" }, - "node_modules/coveralls-next": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/coveralls-next/-/coveralls-next-5.0.0.tgz", - "integrity": "sha512-RCj6Oflf6iQtN3Q5b0SSemEbQBzeBjQlLUrc3bfNECTy83hMJA9krdNZ5GTRm7Jpbyo92yKUbQDP5FYlWcL5sA==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "js-yaml": "4.1.0", - "lcov-parse": "1.0.0", - "log-driver": "1.2.7", - "minimist": "1.2.8" - }, - "bin": { - "coveralls": "bin/coveralls.js" - }, - "engines": { - "node": ">=18" - } - }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", @@ -6225,18 +6204,6 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, - "node_modules/jiti": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/jiti/-/jiti-2.6.1.tgz", - "integrity": "sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==", - "dev": true, - "license": "MIT", - "optional": true, - "peer": true, - "bin": { - "jiti": "lib/jiti-cli.mjs" - } - }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -6349,16 +6316,6 @@ "json-buffer": "3.0.1" } }, - "node_modules/lcov-parse": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/lcov-parse/-/lcov-parse-1.0.0.tgz", - "integrity": "sha512-aprLII/vPzuQvYZnDRU78Fns9I2Ag3gi4Ipga/hxnVMCZC8DnR2nI7XBqrPoywGfxqIx/DgarGvDJZAD3YBTgQ==", - "dev": true, - "license": "BSD-3-Clause", - "bin": { - "lcov-parse": "bin/cli.js" - } - }, "node_modules/leven": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", @@ -6474,16 +6431,6 @@ "license": "MIT", "peer": true }, - "node_modules/log-driver": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/log-driver/-/log-driver-1.2.7.tgz", - "integrity": "sha512-U7KCmLdqsGHBLeWqYlFA0V0Sl6P08EE1ZrmA9cxjUE0WVqT9qnyVDPz1kzpFEP0jdJuFnasWIfSd7fsaNXkpbg==", - "dev": true, - "license": "ISC", - "engines": { - "node": ">=0.8.6" - } - }, "node_modules/log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -7210,16 +7157,6 @@ "node": ">= 14.0.0" } }, - "node_modules/mocha-lcov-reporter": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/mocha-lcov-reporter/-/mocha-lcov-reporter-1.3.0.tgz", - "integrity": "sha512-/5zI2tW4lq/ft8MGpYQ1nIH6yePPtIzdGeUEwFMKfMRdLfAQ1QW2c68eEJop32tNdN5srHa/E2TzB+erm3YMYA==", - "dev": true, - "license": "BSD-2-Clause", - "engines": { - "node": ">= 0.6.0" - } - }, "node_modules/mocha/node_modules/glob": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", diff --git a/package.json b/package.json index 57727a5f5..c17a20243 100644 --- a/package.json +++ b/package.json @@ -121,7 +121,6 @@ "@typescript-eslint/eslint-plugin": "^5.33.0", "@typescript-eslint/parser": "^5.33.0", "chai": "^4.2.0", - "coveralls-next": "^5.0.0", "eslint": "^8.21.0", "eslint-config-prettier": "^6.10.0", "eslint-plugin-local-rules": "^3.0.2", @@ -129,7 +128,6 @@ "happy-dom": "^20.0.11", "minimatch": "^9.0.5", "mocha": "^10.2.0", - "mocha-lcov-reporter": "^1.3.0", "nise": "^6.1.1", "nock": "11.9.1", "nyc": "^15.0.1", diff --git a/rolldown.config.mjs b/rolldown.config.mjs index 8ea0c7c7a..fab297371 100644 --- a/rolldown.config.mjs +++ b/rolldown.config.mjs @@ -57,6 +57,9 @@ const RN_IMPORT_SHIM_DIR = 'import.react_native'; // Shim paths relative to the shim directory, without extension. Shims are named after their package, so // a scoped package's shim is in a subdirectory, e.g. '@react-native-async-storage/async-storage'. const listShims = (subdir = '') => + // subdir only comes from readdirSync of this directory in the repo (a directory entry name never contains a + // path separator or '..'), not from user input, so it can't leave lib/import.react_native. + // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal readdirSync(join(__dirname, 'lib', RN_IMPORT_SHIM_DIR, subdir), { withFileTypes: true }).flatMap(entry => { const path = subdir ? `${subdir}/${entry.name}` : entry.name; if (entry.isDirectory()) return listShims(path); @@ -126,6 +129,8 @@ const rnImportShimCheckPlugin = { const rnImportShimsBundle = { input: Object.fromEntries( + // name comes from listShims() above (file names in lib/import.react_native), not from user input. + // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal RN_IMPORT_SHIMS.map(name => [name, join(BUILD_DIR, RN_IMPORT_SHIM_DIR, `${name}.js`)]) ), output: { diff --git a/scripts/generate-messages.js b/scripts/generate-messages.js index bd347fc01..92d8bd214 100755 --- a/scripts/generate-messages.js +++ b/scripts/generate-messages.js @@ -90,9 +90,25 @@ function buildOutputAst(exports) { return factory.createSourceFile(statements, factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None); } +// The input is a command line argument. Only a TypeScript source is accepted, and never a generated file, so the +// script only reads a .ts file and only writes .gen.ts next to it. +function assertTypeScriptSource(filePath) { + const { name, ext } = path.parse(filePath); + if (ext !== '.ts' || name.endsWith('.gen')) { + console.error(`generate-messages.js: expected a .ts source file that is not a generated .gen.ts file, got: ${filePath}`); + process.exit(1); + } +} + function generate(filePath) { + assertTypeScriptSource(filePath); + + // The path is checked above and chosen by whoever runs this script on their own machine, not by remote input. + // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal const absPath = path.resolve(filePath); const parsed = path.parse(absPath); + // parsed.dir and parsed.name come from the checked path above, and .gen.ts is written next to the input. + // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal const genPath = path.join(parsed.dir, `${parsed.name}.gen${parsed.ext}`); console.log('generating messages for:', absPath); diff --git a/scripts/platform-utils.js b/scripts/platform-utils.js index 6c8c9b827..74754637f 100644 --- a/scripts/platform-utils.js +++ b/scripts/platform-utils.js @@ -323,6 +323,9 @@ function findSourceFiles() { const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { + // entry.name is a file name from readdirSync of a folder in the repo. It never contains a path separator + // or '..', so fullPath stays inside the folder being walked. + // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { diff --git a/scripts/run-cjs-example.js b/scripts/run-cjs-example.js index 7717a9fd9..3a78c77cd 100644 --- a/scripts/run-cjs-example.js +++ b/scripts/run-cjs-example.js @@ -16,21 +16,28 @@ * limitations under the License. */ -const { execSync } = require('child_process'); +const { execFileSync } = require('child_process'); const path = require('path'); const fs = require('fs'); const rootDir = path.join(__dirname, '..'); const exampleDir = path.join(rootDir, 'examples', 'node-commonjs'); -function run(command, cwd) { +// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows, +// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the +// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg). +const isWindows = process.platform === 'win32'; +const NPM = isWindows ? 'npm.cmd' : 'npm'; + +function run(args, cwd) { + const command = `npm ${args.join(' ')}`; console.log(`\n> ${command}`); console.log(` (in ${cwd})\n`); try { - execSync(command, { + execFileSync(NPM, args, { cwd, stdio: 'inherit', - shell: true + shell: isWindows }); } catch (error) { console.error(`\nError executing: ${command}`); @@ -38,17 +45,17 @@ function run(command, cwd) { } } -function runQuiet(command, cwd) { +function runQuiet(args, cwd) { try { - const output = execSync(command, { + const output = execFileSync(NPM, args, { cwd, encoding: 'utf8', - shell: true + shell: isWindows }).trim(); const lines = output.split('\n'); return lines[lines.length - 1].trim(); } catch (error) { - console.error(`\nError executing: ${command}`); + console.error(`\nError executing: npm ${args.join(' ')}`); process.exit(1); } } @@ -84,20 +91,23 @@ function main() { tarballPath = findTarball(); } else { console.log('Installing SDK dependencies...'); - run('npm install', rootDir); + run(['install'], rootDir); console.log('\nPacking SDK tarball...'); - const packOutput = runQuiet('npm pack', rootDir); + const packOutput = runQuiet(['pack'], rootDir); tarballPath = path.join(rootDir, packOutput); console.log(`Created: ${packOutput}`); } console.log('\nInstalling SDK tarball in cjs-example...'); - run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir); + // Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe) + // checkout path is not part of the command line. + const tarballArg = path.relative(exampleDir, tarballPath); + run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir); if (!skipRun) { console.log('\nRunning cjs-example...'); - run('npm start', exampleDir); + run(['start'], exampleDir); } console.log('\nCleaning up tarball...'); diff --git a/scripts/run-ts-example.js b/scripts/run-ts-example.js index 824ac791b..81ab70b99 100755 --- a/scripts/run-ts-example.js +++ b/scripts/run-ts-example.js @@ -16,7 +16,7 @@ * limitations under the License. */ -const { execSync, spawn } = require('child_process'); +const { execFileSync, spawn } = require('child_process'); const path = require('path'); const fs = require('fs'); @@ -25,14 +25,21 @@ const exampleDir = path.join(rootDir, 'examples', 'typescript'); let datafileServer = null; -function run(command, cwd) { +// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows, +// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the +// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg). +const isWindows = process.platform === 'win32'; +const NPM = isWindows ? 'npm.cmd' : 'npm'; + +function run(args, cwd) { + const command = `npm ${args.join(' ')}`; console.log(`\n> ${command}`); console.log(` (in ${cwd})\n`); try { - execSync(command, { + execFileSync(NPM, args, { cwd, stdio: 'inherit', - shell: true + shell: isWindows }); } catch (error) { console.error(`\nError executing: ${command}`); @@ -41,18 +48,18 @@ function run(command, cwd) { } } -function runQuiet(command, cwd) { +function runQuiet(args, cwd) { try { - const output = execSync(command, { + const output = execFileSync(NPM, args, { cwd, encoding: 'utf8', - shell: true + shell: isWindows }).trim(); // npm pack outputs the tarball filename on the last line const lines = output.split('\n'); return lines[lines.length - 1].trim(); } catch (error) { - console.error(`\nError executing: ${command}`); + console.error(`\nError executing: npm ${args.join(' ')}`); cleanup(); process.exit(1); } @@ -103,26 +110,29 @@ async function main() { console.log('=== Building SDK and Running TypeScript Example ===\n'); console.log('Installing SDK dependencies...'); - run('npm install', rootDir); + run(['install'], rootDir); console.log('\nPacking SDK tarball...'); - const packOutput = runQuiet('npm pack', rootDir); + const packOutput = runQuiet(['pack'], rootDir); const tarballPath = path.join(rootDir, packOutput); console.log(`Created: ${packOutput}`); console.log('\nInstalling ts-example devDependencies...'); - run('npm ci', exampleDir); + run(['ci'], exampleDir); console.log('\nInstalling SDK tarball in ts-example...'); - run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir); + // Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe) + // checkout path is not part of the command line. + const tarballArg = path.relative(exampleDir, tarballPath); + run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir); console.log('\nBuilding ts-example...'); - run('npm run build', exampleDir); + run(['run', 'build'], exampleDir); await startDatafileServer(); console.log('\nRunning ts-example...'); - run('npm start', exampleDir); + run(['start'], exampleDir); console.log('\nCleaning up tarball...'); fs.unlinkSync(tarballPath); diff --git a/scripts/test-generate-messages.js b/scripts/test-generate-messages.js index 93f8edd60..70cb76390 100644 --- a/scripts/test-generate-messages.js +++ b/scripts/test-generate-messages.js @@ -81,6 +81,21 @@ function runTest() { } } + // Only a .ts source is accepted. A generated .gen.ts file or any other file type is rejected, and nothing is written. + const jsonFile = path.join(tmpDir, 'settings.json'); + fs.writeFileSync(jsonFile, '{}', 'utf-8'); + for (const [rejected, unexpectedOutput, label] of [ + [jsonFile, path.join(tmpDir, 'settings.gen.json'), 'a .json file'], + [genFile1, path.join(tmpDir, 'file1.gen.gen.ts'), 'a generated .gen.ts file'], + ]) { + assert.throws( + () => execFileSync(process.execPath, [SCRIPT, rejected], { stdio: 'pipe' }), + (err) => err.status === 1 && /expected a \.ts source file/.test(String(err.stderr)), + `${label} should be rejected with exit code 1`, + ); + assert.ok(!fs.existsSync(unexpectedOutput), `${label} should not produce ${path.basename(unexpectedOutput)}`); + } + console.log('All tests passed.'); } finally { fs.rmSync(tmpDir, { recursive: true, force: true });