Skip to content

Commit c29906d

Browse files
[FSSDK-13472] Fix Arnica findings: example runners, loopback server, unused dev dependencies (#1195)
* [FSSDK-13472] Fix Arnica findings: example runners, loopback server, unused dev dependencies - Run npm with execFileSync and an argument list, with no shell, in the CommonJS and TypeScript example runners (fixes the detect-child-process findings and the unquoted tarball path). - Bind the example datafile server to localhost only, instead of every network interface. - Accept only a .ts source in generate-messages.js, and test it. - Remove the unused dev dependencies mocha-lcov-reporter and coveralls-next. - Document why the remaining path-traversal and http-server findings are safe. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * [FSSDK-13472] Pass tarball path relative to example dir so Windows shell cannot split it Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 7ab324a commit c29906d

9 files changed

Lines changed: 94 additions & 93 deletions

‎examples/shared/datafile-server.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@
1919
/**
2020
* Simple HTTP server to serve the datafile for testing polling project config manager
2121
* Runs at http://localhost:PORT
22+
*
23+
* It listens on the loopback interface only, so it can't be reached from other machines on the network.
2224
*/
2325

2426
const http = require('http');
2527
const fs = require('fs');
2628
const path = require('path');
2729

2830
const PORT = 8910;
31+
// Loopback only. Without a host, Node listens on every network interface.
32+
const HOST = 'localhost';
2933
const datafilePath = path.join(__dirname, 'datafile.json');
3034

35+
// Plain HTTP is intended here: this is a local test server for the examples. It only listens on localhost
36+
// (see HOST) and serves the sample datafile.json, so nothing sensitive crosses a network.
37+
// nosemgrep: problem-based-packs.insecure-transport.js-node.using-http-server.using-http-server
3138
const server = http.createServer((req, res) => {
3239
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
3340

@@ -52,9 +59,9 @@ const server = http.createServer((req, res) => {
5259
});
5360
});
5461

55-
server.listen(PORT, () => {
62+
server.listen(PORT, HOST, () => {
5663
console.log(`\n=== Datafile Server Started ===`);
57-
console.log(`Server running at http://localhost:${PORT}`);
64+
console.log(`Server running at http://${HOST}:${PORT}`);
5865
console.log(`Serving datafile from: ${datafilePath}`);
5966
console.log(`\nPress Ctrl+C to stop the server\n`);
6067
});

‎package-lock.json‎

Lines changed: 0 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@
121121
"@typescript-eslint/eslint-plugin": "^5.33.0",
122122
"@typescript-eslint/parser": "^5.33.0",
123123
"chai": "^4.2.0",
124-
"coveralls-next": "^5.0.0",
125124
"eslint": "^8.21.0",
126125
"eslint-config-prettier": "^6.10.0",
127126
"eslint-plugin-local-rules": "^3.0.2",
128127
"eslint-plugin-prettier": "^3.1.2",
129128
"happy-dom": "^20.0.11",
130129
"minimatch": "^9.0.5",
131130
"mocha": "^10.2.0",
132-
"mocha-lcov-reporter": "^1.3.0",
133131
"nise": "^6.1.1",
134132
"nock": "11.9.1",
135133
"nyc": "^15.0.1",

‎rolldown.config.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const RN_IMPORT_SHIM_DIR = 'import.react_native';
5757
// Shim paths relative to the shim directory, without extension. Shims are named after their package, so
5858
// a scoped package's shim is in a subdirectory, e.g. '@react-native-async-storage/async-storage'.
5959
const listShims = (subdir = '') =>
60+
// subdir only comes from readdirSync of this directory in the repo (a directory entry name never contains a
61+
// path separator or '..'), not from user input, so it can't leave lib/import.react_native.
62+
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
6063
readdirSync(join(__dirname, 'lib', RN_IMPORT_SHIM_DIR, subdir), { withFileTypes: true }).flatMap(entry => {
6164
const path = subdir ? `${subdir}/${entry.name}` : entry.name;
6265
if (entry.isDirectory()) return listShims(path);
@@ -126,6 +129,8 @@ const rnImportShimCheckPlugin = {
126129

127130
const rnImportShimsBundle = {
128131
input: Object.fromEntries(
132+
// name comes from listShims() above (file names in lib/import.react_native), not from user input.
133+
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
129134
RN_IMPORT_SHIMS.map(name => [name, join(BUILD_DIR, RN_IMPORT_SHIM_DIR, `${name}.js`)])
130135
),
131136
output: {

‎scripts/generate-messages.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,25 @@ function buildOutputAst(exports) {
9090
return factory.createSourceFile(statements, factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None);
9191
}
9292

93+
// The input is a command line argument. Only a TypeScript source is accepted, and never a generated file, so the
94+
// script only reads a .ts file and only writes <name>.gen.ts next to it.
95+
function assertTypeScriptSource(filePath) {
96+
const { name, ext } = path.parse(filePath);
97+
if (ext !== '.ts' || name.endsWith('.gen')) {
98+
console.error(`generate-messages.js: expected a .ts source file that is not a generated .gen.ts file, got: ${filePath}`);
99+
process.exit(1);
100+
}
101+
}
102+
93103
function generate(filePath) {
104+
assertTypeScriptSource(filePath);
105+
106+
// The path is checked above and chosen by whoever runs this script on their own machine, not by remote input.
107+
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
94108
const absPath = path.resolve(filePath);
95109
const parsed = path.parse(absPath);
110+
// parsed.dir and parsed.name come from the checked path above, and <name>.gen.ts is written next to the input.
111+
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
96112
const genPath = path.join(parsed.dir, `${parsed.name}.gen${parsed.ext}`);
97113

98114
console.log('generating messages for:', absPath);

‎scripts/platform-utils.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ function findSourceFiles() {
323323
const entries = fs.readdirSync(dir, { withFileTypes: true });
324324

325325
for (const entry of entries) {
326+
// entry.name is a file name from readdirSync of a folder in the repo. It never contains a path separator
327+
// or '..', so fullPath stays inside the folder being walked.
328+
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
326329
const fullPath = path.join(dir, entry.name);
327330

328331
if (entry.isDirectory()) {

‎scripts/run-cjs-example.js‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,46 @@
1616
* limitations under the License.
1717
*/
1818

19-
const { execSync } = require('child_process');
19+
const { execFileSync } = require('child_process');
2020
const path = require('path');
2121
const fs = require('fs');
2222

2323
const rootDir = path.join(__dirname, '..');
2424
const exampleDir = path.join(rootDir, 'examples', 'node-commonjs');
2525

26-
function run(command, cwd) {
26+
// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows,
27+
// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the
28+
// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg).
29+
const isWindows = process.platform === 'win32';
30+
const NPM = isWindows ? 'npm.cmd' : 'npm';
31+
32+
function run(args, cwd) {
33+
const command = `npm ${args.join(' ')}`;
2734
console.log(`\n> ${command}`);
2835
console.log(` (in ${cwd})\n`);
2936
try {
30-
execSync(command, {
37+
execFileSync(NPM, args, {
3138
cwd,
3239
stdio: 'inherit',
33-
shell: true
40+
shell: isWindows
3441
});
3542
} catch (error) {
3643
console.error(`\nError executing: ${command}`);
3744
process.exit(1);
3845
}
3946
}
4047

41-
function runQuiet(command, cwd) {
48+
function runQuiet(args, cwd) {
4249
try {
43-
const output = execSync(command, {
50+
const output = execFileSync(NPM, args, {
4451
cwd,
4552
encoding: 'utf8',
46-
shell: true
53+
shell: isWindows
4754
}).trim();
4855
const lines = output.split('\n');
4956
return lines[lines.length - 1].trim();
5057
} catch (error) {
51-
console.error(`\nError executing: ${command}`);
58+
console.error(`\nError executing: npm ${args.join(' ')}`);
5259
process.exit(1);
5360
}
5461
}
@@ -84,20 +91,23 @@ function main() {
8491
tarballPath = findTarball();
8592
} else {
8693
console.log('Installing SDK dependencies...');
87-
run('npm install', rootDir);
94+
run(['install'], rootDir);
8895

8996
console.log('\nPacking SDK tarball...');
90-
const packOutput = runQuiet('npm pack', rootDir);
97+
const packOutput = runQuiet(['pack'], rootDir);
9198
tarballPath = path.join(rootDir, packOutput);
9299
console.log(`Created: ${packOutput}`);
93100
}
94101

95102
console.log('\nInstalling SDK tarball in cjs-example...');
96-
run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir);
103+
// Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe)
104+
// checkout path is not part of the command line.
105+
const tarballArg = path.relative(exampleDir, tarballPath);
106+
run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir);
97107

98108
if (!skipRun) {
99109
console.log('\nRunning cjs-example...');
100-
run('npm start', exampleDir);
110+
run(['start'], exampleDir);
101111
}
102112

103113
console.log('\nCleaning up tarball...');

‎scripts/run-ts-example.js‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
const { execSync, spawn } = require('child_process');
19+
const { execFileSync, spawn } = require('child_process');
2020
const path = require('path');
2121
const fs = require('fs');
2222

@@ -25,14 +25,21 @@ const exampleDir = path.join(rootDir, 'examples', 'typescript');
2525

2626
let datafileServer = null;
2727

28-
function run(command, cwd) {
28+
// npm runs with an argument list and no shell, so each argument stays one argument. npm is npm.cmd on Windows,
29+
// which Node only runs through a shell, and a shell joins the arguments without escaping them. So no part of the
30+
// checkout path may be an argument: the tarball is passed relative to the example folder (see tarballArg).
31+
const isWindows = process.platform === 'win32';
32+
const NPM = isWindows ? 'npm.cmd' : 'npm';
33+
34+
function run(args, cwd) {
35+
const command = `npm ${args.join(' ')}`;
2936
console.log(`\n> ${command}`);
3037
console.log(` (in ${cwd})\n`);
3138
try {
32-
execSync(command, {
39+
execFileSync(NPM, args, {
3340
cwd,
3441
stdio: 'inherit',
35-
shell: true
42+
shell: isWindows
3643
});
3744
} catch (error) {
3845
console.error(`\nError executing: ${command}`);
@@ -41,18 +48,18 @@ function run(command, cwd) {
4148
}
4249
}
4350

44-
function runQuiet(command, cwd) {
51+
function runQuiet(args, cwd) {
4552
try {
46-
const output = execSync(command, {
53+
const output = execFileSync(NPM, args, {
4754
cwd,
4855
encoding: 'utf8',
49-
shell: true
56+
shell: isWindows
5057
}).trim();
5158
// npm pack outputs the tarball filename on the last line
5259
const lines = output.split('\n');
5360
return lines[lines.length - 1].trim();
5461
} catch (error) {
55-
console.error(`\nError executing: ${command}`);
62+
console.error(`\nError executing: npm ${args.join(' ')}`);
5663
cleanup();
5764
process.exit(1);
5865
}
@@ -103,26 +110,29 @@ async function main() {
103110
console.log('=== Building SDK and Running TypeScript Example ===\n');
104111

105112
console.log('Installing SDK dependencies...');
106-
run('npm install', rootDir);
113+
run(['install'], rootDir);
107114

108115
console.log('\nPacking SDK tarball...');
109-
const packOutput = runQuiet('npm pack', rootDir);
116+
const packOutput = runQuiet(['pack'], rootDir);
110117
const tarballPath = path.join(rootDir, packOutput);
111118
console.log(`Created: ${packOutput}`);
112119

113120
console.log('\nInstalling ts-example devDependencies...');
114-
run('npm ci', exampleDir);
121+
run(['ci'], exampleDir);
115122

116123
console.log('\nInstalling SDK tarball in ts-example...');
117-
run(`npm install --no-save --no-package-lock ${tarballPath}`, exampleDir);
124+
// Relative to the example folder, e.g. ../../optimizely-optimizely-sdk-6.7.1.tgz, so the (possibly unsafe)
125+
// checkout path is not part of the command line.
126+
const tarballArg = path.relative(exampleDir, tarballPath);
127+
run(['install', '--no-save', '--no-package-lock', tarballArg], exampleDir);
118128

119129
console.log('\nBuilding ts-example...');
120-
run('npm run build', exampleDir);
130+
run(['run', 'build'], exampleDir);
121131

122132
await startDatafileServer();
123133

124134
console.log('\nRunning ts-example...');
125-
run('npm start', exampleDir);
135+
run(['start'], exampleDir);
126136

127137
console.log('\nCleaning up tarball...');
128138
fs.unlinkSync(tarballPath);

0 commit comments

Comments
 (0)